3. Code
Tạo đối tượng ball.cs
Mã:
using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media; namespace Zstar_Ball_Game{ public class Ball { private GraphicsDeviceManager graphics; private Texture2D texture;//sprite bóng private Vector2 location;//vị trí của bóng private Point size = new Point(48, 48);//kích thước của bóng private SpriteFont font;//font để vẽ chữ private Vector2 direction;//hướng chuyển động của bóng private float h = 0.5f;//bước chuyển động public Ball(GraphicsDeviceManager g, Random rand) { this.graphics = g; this.location = new Vector2(rand.Next(200), rand.Next(200));//random ngẫu nhiên vị trí bóng this.direction = new Vector2(rand.Next(1, 3), rand.Next(1, 3));//random ngẫu nhiên hướng chuyển động this.h = rand.Next(1, 5) * rand.Next(1, 3) * 0.5f;//random ngẫu nhiên bước chuyển động } public void LoadContent(ContentManager content, string path) { this.texture = content.Load<Texture2D>(path); this.font = content.Load<SpriteFont>("Font/SpriteFont1"); } public void Update() { MouseState mouse = Mouse.GetState(); if (mouse.LeftButton == ButtonState.Pressed) { this.Press = true; } else this.Press = false; if (this.Press) { if (mouse.X > 0 && mouse.Y > 0 && mouse.X + size.X <= this.graphics.PreferredBackBufferWidth && mouse.Y + size.Y <= this.graphics.PreferredBackBufferHeight) this.Location = new Vector2(mouse.X, mouse.Y); } //di chuyển this.location += this.direction * h; if (!outside()) { if (location.X * location.Y < 0) this.location = new Vector2(); } } public bool outside() { if (this.Location.X < 0) { this.direction = new Vector2(-this.direction.X, this.direction.Y); return true; } if (this.Location.Y < 0) { this.direction = new Vector2(this.direction.X, -this.direction.Y); return true; } if (this.Location.X + this.size.X > this.graphics.PreferredBackBufferWidth) { this.direction = new Vector2(-this.direction.X, this.direction.Y); return true; } if (this.Location.Y + this.size.Y > this.graphics.PreferredBackBufferHeight) { this.direction = new Vector2(this.direction.X, -this.direction.Y); return true; } return false; } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(this.texture, this.location, Color.White); //spriteBatch.DrawString(this.font, this.location.ToString(), this.location + new Vector2(0, -20), Color.White); } public Vector2 Location { get { return this.location; } set { this.location = value; } } public bool Press { get; set; } }}
tạo đôi tượng BallManager.cs
mục đích của lớp này là quản lí bóng cho dễ,có thể thêm nhiều bóng rất dễ dàng
Mã:
using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media; namespace Zstar_Ball_Game{ public class BallManager { private Random rand = new Random(); private Ball[] balls; private int num = 6; private string[] paths = new string[] { "Images/ball4", "Images/ball2", "Images/ball3" }; public BallManager(GraphicsDeviceManager graphics, Random rand) { balls = new Ball[num]; for (int i = 0; i < balls.Length; i++) { balls[i] = new Ball(graphics, rand); } } public void LoadContent(ContentManager content) { for (int i = 0; i < balls.Length; i++) { balls[i].LoadContent(content, paths[rand.Next(paths.Length)]); } } public void Draw(SpriteBatch spriteBatch) { for (int i = 0; i < balls.Length; i++) { balls[i].Draw(spriteBatch); } } public void Update() { for (int i = 0; i < balls.Length; i++) { balls[i].Update(); } } }}
code ở Game1.cs
Mã:
using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media; namespace Zstar_Ball_Game{ /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager graphics; private SpriteBatch spriteBatch; private BallManager ballManager; private SpriteFont font; private string s = "Hello Zstar !
Welcome to Window phone OS Emulator"; private Random rand = new Random(); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; this.ballManager = new BallManager(graphics, rand); s += "
Screen : (" + this.graphics.PreferredBackBufferWidth + "," + this.graphics.PreferredBackBufferHeight + ")"; TargetElapsedTime = TimeSpan.FromTicks(333333); } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); this.ballManager.LoadContent(Content); this.font = Content.Load<SpriteFont>("Font/SpriteFont1"); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); this.ballManager.Update(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.DarkCyan); // TODO: Add your drawing code here spriteBatch.Begin(); this.ballManager.Draw(spriteBatch); spriteBatch.DrawString(font, s, new Vector2(), Color.Orange); spriteBatch.End(); base.Draw(gameTime); } }}
sau đó F5, chạy thử ta được kết quả :
video demo : http://www.youtube.com/watch?v=Ji5sOrayM_M
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ