// Updates the logic of the game state each frame, checking for collision, gathering input, etc. protected override void Update(GameTime gameTime) { float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; // Get time elapsed since last Update iteration KeyboardHandler(); // Handle keyboard input // Stop all movement when the game ends if (gameOver) { dino.dX = 0; dino.dY = 0; broccoli.dX = 0; broccoli.dY = 0; broccoli.dA = 0; } // Update animated SpriteClass objects based on their current rates of change dino.Update(elapsedTime); broccoli.Update(elapsedTime); // Accelerate the dino downward each frame to simulate gravity. dino.dY += gravitySpeed; // Set game floor if (dino.y > screenHeight * SKYRATIO) { dino.dY = 0; dino.y = screenHeight * SKYRATIO; } // Set right edge if (dino.x > screenWidth - dino.texture.Width / 2) { dino.x = screenWidth - dino.texture.Width / 2; dino.dX = 0; } // Set left edge if (dino.x < 0 + dino.texture.Width / 2) { dino.x = 0 + dino.texture.Width / 2; dino.dX = 0; } // If the broccoli goes offscreen, spawn a new one and iterate the score if (broccoli.y > screenHeight + 100 || broccoli.y < -100 || broccoli.x > screenWidth + 100 || broccoli.x < -100) { SpawnBroccoli(); score++; } if (dino.RectangleCollision(broccoli)) { gameOver = true; // End game if the dino collides with the broccoli } base.Update(gameTime); }
/// <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) { // TODO: Add your update logic here float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardHandler(); // Handle keyboard input if (gameOver) { dino.dX = 0; dino.dY = 0; broccoli.dX = 0; broccoli.dY = 0; broccoli.dA = 0; } // Update animated SpriteClass objects based on their current rates of change dino.Update(elapsedTime); broccoli.Update(elapsedTime); // Accelerate the dino downward each frame to simulate gravity. dino.dY += gravitySpeed; // Set game floor so the player does not fall through it if (dino.y > screenHeight * SKYRATIO) { dino.dY = 0; dino.y = screenHeight * SKYRATIO; } // Set game edges to prevent the player from moving offscreen if (dino.x > screenWidth - dino.texture.Width / 2) { dino.x = screenWidth - dino.texture.Width / 2; dino.dX = 0; } if (dino.x < 0 + dino.texture.Width / 2) { dino.x = 0 + dino.texture.Width / 2; dino.dX = 0; } // If the broccoli goes offscreen, spawn a new one and iterate the score if (broccoli.y > screenHeight + 100 || broccoli.y < -100 || broccoli.x > screenWidth + 100 || broccoli.x < -100) { SpawnBroccoli(); score++; } if (dino.RectangleCollision(broccoli)) { gameOver = true; } base.Update(gameTime); }