Har fastnat lite på Asteroidprojektet... har lyckats slumpa ut - TopicsExpress



          

Har fastnat lite på Asteroidprojektet... har lyckats slumpa ut ett antal asteroids med hjälp av en for loop. Men hur gör jag så varje slumpad asteroid får sin egna speed i X och Y? namespace Ö1A { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D asteroid_texture; Vector2 pos1; Vector2 pos2; List astroidList; Astroid astroid_move; Random rnd; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = Content; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); asteroid_texture = Content.Load(@astroid); rnd = new Random(); astroid_move = new Astroid(pos1, 900, 900); astroidList = new List(); pos1 = Vector2.Zero; int X = Window.ClientBounds.Width / 2 - asteroid_texture.Width / 2; int Y = Window.ClientBounds.Height / 2 - asteroid_texture.Height / 2; pos2 = new Vector2(X, Y); int stopX = Window.ClientBounds.Width - asteroid_texture.Width; int stopY = Window.ClientBounds.Height - asteroid_texture.Height; astroid_move = new Astroid(pos1, stopX, stopY); for (int i = 0; i < 10; i++) { int randX = rnd.Next(300, 500); int randY = rnd.Next(150, 300); Vector2 pos = new Vector2(randX, randY); Astroid tempastroid = new Astroid(pos, 200, 200); astroidList.Add(tempastroid); } } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); IsMouseVisible = true; astroid_move.Update(); pos2.X = pos2.X + 1; pos2.Y = pos2.Y + 1; pos1.X = pos1.X + 1; pos1.Y = pos1.Y + 1; for (int i = 0; i < astroidList.Count; i++) { astroidList[i].move(); } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); foreach (Astroid astroid in astroidList) { spriteBatch.Draw(asteroid_texture, astroid.pos, Color.White); } spriteBatch.Draw(asteroid_texture, pos1, Color.White); spriteBatch.Draw(asteroid_texture, pos2, Color.White); spriteBatch.End(); base.Draw(gameTime); } } } (AsteroidClass) namespace Ö1A { class Astroid { public Vector2 pos; public int stopX; public int stopY; public Random rnd; public Astroid(Vector2 pos, int stopX, int stopY) { this.pos = pos; this.stopX = stopX; this.stopY = stopY; } public void Update() { if (pos.X < stopX && pos.Y < stopY) { /* pos.X = pos.X + 8; pos.Y = pos.Y + 8; */ } } public void move() { rnd = new Random(); pos.X = pos.X + (rnd.Next(-5, 5)); pos.Y = pos.Y + (rnd.Next(-5, 5)); } } }
Posted on: Fri, 12 Sep 2014 11:26:43 +0000

Trending Topics



Recently Viewed Topics




© 2015