/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; if (gameOver == false) { totalTime += elapsedTime; aliveTime += elapsedTime; if (totalTime > totalLaunchpad * 5) { Launchpad temp = new Launchpad(texture_blank, circle_center, circle_radius, 10, prepareShow, rand.Next()); temp.showHandler += prepareShow; ObjectCollect.Add(temp); totalLaunchpad++; } for (int ii = 0; ii < ObjectCollect.Count; ii++) { ObjectCollect[ii].Update(elapsedTime); } if (player.OnCheck() == true) { gameOver = true; } } KeyboardState keyboard = Keyboard.GetState(); if (keyboard.IsKeyDown(Keys.A) || keyboard.IsKeyDown(Keys.Left)) { player.MoveLeft(); } else if (keyboard.IsKeyDown(Keys.D) || keyboard.IsKeyDown(Keys.Right)) { player.MoveRight(); } if (keyboard.IsKeyDown(Keys.W) || keyboard.IsKeyDown(Keys.Up)) { player.MoveUP(); } else if (keyboard.IsKeyDown(Keys.S) || keyboard.IsKeyDown(Keys.Down)) { player.MoveDown(); } if (keyboard.IsKeyDown(Keys.R) && gameOver) { gameOver = false; StartGame(); } // TODO: Add your update logic here base.Update(gameTime); }
public void StartGame() { player.X = window_width / 2; player.Y = window_height / 2; int count = ObjectCollect.Count; for (int ii = 0; ii < count; ii++) { bool clear = false; foreach (var item in ObjectCollect) { Bullet temp = item as Bullet; if (temp != null) { ObjectCollect.Remove(item); clear = false; break; } clear = true; } if (clear == true) { break; } } count = ObjectCollect.Count; for (int ii = 0; ii < count; ii++) { foreach (var item in ObjectCollect) { Launchpad temp = item as Launchpad; if (temp != null) { ObjectCollect.Remove(item); totalLaunchpad--; break; } } if (totalLaunchpad == 1) { break; } } totalTime = 0; aliveTime = 0; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); texture_blank = Content.Load <Texture2D>("blank"); stateFont = Content.Load <SpriteFont>("GameState"); ObjectCollect = new MyObjList(); player = new Player(texture_blank, window_width / 2, window_height / 2, 10, 10, Color.Yellow, 5); player.showHandler += prepareShow; ObjectCollect.Add(player); Launchpad temp = new Launchpad(texture_blank, circle_center, circle_radius, 10, prepareShow, rand.Next()); temp.showHandler += prepareShow; ObjectCollect.Add(temp); totalLaunchpad++; // TODO: use this.Content to load your game content here }