// The student will complete this function as part of an activity private void moveSnowball() { if (snowball.IsAlive) { snowball.Accelerate(0.0f, 0.1f); snowball.MoveAndVanish(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); if (snowball.IsAlive == false) { changePlayer(); } } }
void onAddSkill(SkillData sd) { switch (sd.stype) { case SkillType.DoubleJump: m_sprite.UpdateJumpCountLimit(2); break; case SkillType.Accelerate: m_sprite.Accelerate(5); break; case SkillType.Trajectory: m_sprite.ShowTrajectory(); break; } }
void UpdateGame(GameTime gameTime) { if (toGame) { playerSprite.UpperLeft = new Vector2(0, 0); pointerSprite.UpperLeft = new Vector2(0, 200); toGame = false; } if (playerSprite.IsCollided(pointerSprite) == true) { playerSprite.SetVelocity(0, 0); } else { playerSprite.Accelerate(0.0f, 0.05f); playerSprite.Move(); } }
/// <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) { currentKeyState = Keyboard.GetState(); currentMouseState = Mouse.GetState(); if (oldKeyState == null) { oldKeyState = currentKeyState; } if (oldMouseState == null) { oldMouseState = currentMouseState; } foreach (Sprite floor in floors) { if (playerSprite.IsCollided(floor)) { if (playerSprite.GetVelocity().Y > 0) { if (playerSprite.AccelerateY(0.1f, 0 * -1) < 0 == false) { playerSprite.UpperLeft = new Vector2(playerSprite.UpperLeft.X, floor.UpperLeft.Y - playerSprite.GetHeight() + 1); } playerSprite.SetVelocity(playerSprite.GetVelocity().X, 0); jumped = false; jumping = false; jumpSpeed = -1f; if (!firing) { fired = false; verticalCompensation = 0; } } floored = true; } } walking = false; if (currentKeyState.IsKeyDown(Keys.A)) { playerSprite.Accelerate(-0.2f, 0); walking = true; if (playerSprite.GetVelocity().X > 0) { playerSprite.Accelerate(-playerSprite.windResistance, 0); } } if (currentKeyState.IsKeyDown(Keys.D)) { playerSprite.Accelerate(0.2f, 0); walking = true; if (playerSprite.GetVelocity().X < 0) { playerSprite.Accelerate(playerSprite.windResistance, 0); } } if ((currentMouseState.LeftButton == ButtonState.Pressed) && (mana > 0)) { double direction = Sprite.CalculateDirectionAngle(new Vector2(currentMouseState.X - ((playerSprite.UpperLeft.X - camera.UpperLeft.X) + playerSprite.GetWidth() / 2), (currentMouseState.Y - ((playerSprite.UpperLeft.Y - camera.UpperLeft.Y) - playerSprite.GetHeight() / 2)))); projectileSprite = new Sprite(); projectileSprite.SetTexture(projectileTexture); projectileSprite.RotationAngle = direction; projectileSprite.SetSpeedAndDirection(1, direction); projectileSprite.UpperLeft = new Vector2(playerSprite.UpperLeft.X + playerSprite.GetWidth() / 2, playerSprite.UpperLeft.Y + playerSprite.GetHeight() / 2); activeProjectiles.AddFirst(projectileSprite); activeSprites.AddFirst(projectileSprite); direction = Sprite.CalculateDirectionAngle(new Vector2(((playerSprite.UpperLeft.X - camera.UpperLeft.X) + playerSprite.GetWidth() / 2) - currentMouseState.X, (((playerSprite.UpperLeft.Y - camera.UpperLeft.Y) - playerSprite.GetHeight() / 2) - currentMouseState.Y) * -1)); //playerSprite.AccelerateX(10, direction * -1); if (floored) { playerSprite.AccelerateX(0.01f, direction * -1); horizontalCompensation = 0; horizontalAddition = 0.1f; } else { playerSprite.AccelerateX(horizontalCompensation, direction * -1); if (horizontalAddition < 0.2) { horizontalCompensation += horizontalAddition; horizontalAddition += 0.01f; } } if ((direction > 90) && (direction <= 270)) { if (playerSprite.GetVelocity().X > 0) { playerSprite.Accelerate(-playerSprite.windResistance, 0); } } if (((direction > 270) && (direction <= 360)) || ((direction > 0) && (direction <= 90))) { if (playerSprite.GetVelocity().X < 0) { playerSprite.Accelerate(playerSprite.windResistance, 0); } } if (playerSprite.AccelerateY(0.1f, direction * -1) < 0) { playerSprite.AccelerateY(verticalCompensation, direction * -1); if (verticalAddition > 0) { verticalCompensation += verticalAddition; verticalAddition -= 0.01f; } } else { verticalCompensation = 0; verticalAddition = 0.1f; } mana -= 1; fired = true; firing = true; } else if ((currentMouseState.LeftButton == ButtonState.Released)) { mana = 100; firing = false; verticalCompensation = 0; verticalAddition = 0.1f; } else { firing = false; verticalCompensation = 0; verticalAddition = 0.1f; } if (currentKeyState.IsKeyDown(Keys.W) && oldKeyState.IsKeyUp(Keys.W)) { if (jumped == false) { playerSprite.SetVelocity(playerSprite.GetVelocity().X, -10); playerSprite.UpperLeft.Y -= 10; jumped = true; jumping = true; jumpSpeed = -0.6f; } } else if (currentKeyState.IsKeyDown(Keys.W) && jumped == true && jumping == true) { if (jumpSpeed < 0) { playerSprite.Accelerate(0, jumpSpeed); jumpSpeed += 0.015f; } } else { jumping = false; } if (!floored) { playerSprite.Accelerate(0, playerSprite.gravity); } if ((!walking) && (!firing)) { if (playerSprite.GetVelocity().X > 0) { playerSprite.Accelerate(-playerSprite.windResistance, 0); } if (playerSprite.GetVelocity().X < 0) { playerSprite.Accelerate(playerSprite.windResistance, 0); } if ((playerSprite.GetVelocity().X < 0.5) && (playerSprite.GetVelocity().X > -0.5) && (playerSprite.GetVelocity().X != 0)) { playerSprite.SetVelocity(0, playerSprite.GetVelocity().Y); } } //playerSprite.Move(); foreach (Sprite sprite in activeProjectiles) { double X = sprite.GetVelocity().X; double Y = sprite.GetVelocity().Y; if (X < 0) { X *= -1; } if (Y < 0) { Y *= -1; } //if ((sprite.GetVelocity().X <= 0.01) && (sprite.GetVelocity().Y <= 0.01)) if ((X <= 0.01) && (Y <= 0.01)) { sprite.SetVelocity(0, 0); sprite.IsAlive = false; } if (sprite.lifeSpan >= 60) { sprite.AccelerateDirection(-0.01, sprite.GetDirectionAngle()); } } foreach (Sprite sprite in activeSprites) { sprite.Move(); sprite.lifeSpan += 1; } camera.UpperLeft = new Vector2((playerSprite.UpperLeft.X - playerSprite.GetWidth() / 2) - (camera.ViewWidth / 2), (playerSprite.UpperLeft.Y + playerSprite.GetHeight()) - ((camera.ViewHeight / 4) * 3)); //camera.LockCamera(); oldKeyState = currentKeyState; oldMouseState = currentMouseState; floored = false; base.Update(gameTime); }
// This method is provided fully complete as part of the activity starter. private void updatePlaying(GameTime gameTime, KeyboardState currentKeyboard, GamePadState currentGamePad1, GamePadState currentGamePad2) { // Escape key (or gamepad Start button)pauses the game when playing if (wasKeyPressed(Keys.Escape, currentKeyboard) || wasButtonStartPressed(currentGamePad1, currentGamePad2)) { currentScreen = GameScreen.PAUSED; return; // don't do anything else if we're now paused! } // check player 1 keys if (currentKeyboard.IsKeyDown(Keys.W) || (currentGamePad1.ThumbSticks.Left.Y > 0)) { // 'W' (or gamepad thumbstick up) means accelerate up StarShip1.Accelerate(0, -ACCELERATION_FACTOR); } if (currentKeyboard.IsKeyDown(Keys.A) || (currentGamePad1.ThumbSticks.Left.X < 0)) { // 'A' (or gamepad thumbstick left) means move a bit to the left StarShip1.UpperLeft.X -= X_STEER_FACTOR; if (StarShip1.UpperLeft.X < 0) { StarShip1.UpperLeft.X = 0; } } if (currentKeyboard.IsKeyDown(Keys.D) || (currentGamePad1.ThumbSticks.Left.X > 0)) { // 'D' (or gamepad thumbstick right) means move a bit to the right StarShip1.UpperLeft.X += X_STEER_FACTOR; if (StarShip1.UpperLeft.X + StarShip1.GetWidth() > VIEWPORT_WIDTH) { StarShip1.UpperLeft.X = VIEWPORT_WIDTH - StarShip1.GetWidth(); } } if (currentKeyboard.IsKeyDown(Keys.X) || (currentGamePad1.ThumbSticks.Left.Y < 0)) { // 'X' (or gamepad thumbstick down) means slow down if (StarShip1.GetVelocity().Y < 0) { StarShip1.Accelerate(0, ACCELERATION_FACTOR); } } // now check player2 input unless it's an AI-controlled player if (!isGameAIEnabled) { // check player 2 controls if (currentKeyboard.IsKeyDown(Keys.Up) || (currentGamePad2.ThumbSticks.Left.Y > 0)) { // up arrow means accelerate up StarShip2.Accelerate(0, -ACCELERATION_FACTOR); } if (currentKeyboard.IsKeyDown(Keys.Down) || (currentGamePad2.ThumbSticks.Left.Y < 0)) { // down arrow means slow down if (StarShip2.GetVelocity().Y < 0) { StarShip2.Accelerate(0, ACCELERATION_FACTOR); } } if (currentKeyboard.IsKeyDown(Keys.Left) || (currentGamePad2.ThumbSticks.Left.X < 0)) { // left arrow means move a bit to the left StarShip2.UpperLeft.X -= X_STEER_FACTOR; if (StarShip2.UpperLeft.X < 0) { StarShip2.UpperLeft.X = 0; } } if (currentKeyboard.IsKeyDown(Keys.Right) || (currentGamePad2.ThumbSticks.Left.X > 0)) { // right arrow means move a bit to the right StarShip2.UpperLeft.X += X_STEER_FACTOR; if (StarShip2.UpperLeft.X + StarShip2.GetWidth() > VIEWPORT_WIDTH) { StarShip2.UpperLeft.X = VIEWPORT_WIDTH - StarShip2.GetWidth(); } } } else { // call the AI routine to control the second space ship DoSimpleAI(); } // move both ships in the Y direction // (X velocity we leave at zero so we can move strictly according to left/right keys above) StarShip1.Move(); StarShip2.Move(); // move the cameras along with the space ship, keeping ship 4/5 of the way down the screen if (player1Camera != null) { player1Camera.UpperLeft.Y = StarShip1.UpperLeft.Y - VIEWPORT_HEIGHT * 4 / 5; } if (player2Camera != null) { player2Camera.UpperLeft.Y = StarShip2.UpperLeft.Y - VIEWPORT_HEIGHT * 4 / 5; } // make sure camera remains within valid world coordinates near the finish line! if (player1Camera != null) { player1Camera.LockCamera(); } if (player2Camera != null) { player2Camera.LockCamera(); } // move all the asteroids, allowing them to wrap around the screen. // not realistic, but to the player it might as well be a new asteroid! foreach (Sprite asteroid in Asteroids) { asteroid.MoveAndWrap(WORLD_WIDTH, WORLD_HEIGHT); } // see if any asteroids have hit a ship checkCollisions(); // check to see if anyone has won if (StarShip1.UpperLeft.Y <= (finishLine.UpperLeft.Y + finishLine.GetHeight())) { currentScreen = GameScreen.GAMEOVER; gameOverMessage = "Player 1 has won!"; } if (StarShip2.UpperLeft.Y <= finishLine.GetHeight()) { currentScreen = GameScreen.GAMEOVER; gameOverMessage = "Player 2 has won!"; } }