public override void Update(GameTime gameTime) { prevKeyState = keyState; keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.Space) && !prevKeyState.IsKeyDown(Keys.Space)) { gameStateManager.setLevel(1); intro.Stop(); } startSub.setPosX(startSub.getPosX() + 5); if (startSub.getPosX() > Dir.rightBoundary) { startSub.setPosX(-265); } intro.Play(); }
public override void Update(GameTime gameTime) { ticks++; //load animation //anmiation1.animationTick(); dragonList.animationTick(); if (scrolling1.rectangle.X + scrolling1.texture.Width <= 0) { scrolling1.rectangle.X = scrolling2.rectangle.X + scrolling2.texture.Width; } if (scrolling2.rectangle.X + scrolling2.texture.Width <= 0) { scrolling2.rectangle.X = scrolling1.rectangle.X + scrolling1.texture.Width; } scrolling1.Update(); scrolling2.Update(); for (int i = 0; i < dragonList.count(); i++) { Sprite3 s = dragonList.getSprite(i); if (s == null) { continue; } if (!s.active) { continue; } if (!s.visible) { continue; } if (ball.getBoundingBoxAA().Intersects(s.getBoundingBoxAA())) { baz.Play(); s.setActive(false); scoure++; ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(1, -1)); setSys5(); p.Update(gameTime); } } if (k.IsKeyDown(Keys.B) && prevK.IsKeyUp(Keys.B)) // *** { showbb = !showbb; } k = Keyboard.GetState(); if (k.IsKeyDown(Keys.Right)) { if (paddle.getPosX() < rhs - texpaddle.Width) { paddle.setPosX(paddle.getPosX() + paddleSpeed); } } if (k.IsKeyDown(Keys.Left)) { if (paddle.getPosX() > lhs) { paddle.setPosX(paddle.getPosX() - paddleSpeed); } } if (ballStuck) { ball.setPos(paddle.getPos() + ballOffset); } //if (ballStuck) //{ // ball.setPos(paddle.getPos() + ballOffset); // if (k.IsKeyDown(Keys.Space) && prevK.IsKeyUp(Keys.Space)) // { // ballStuck = false; // ball.setDeltaSpeed(new Vector2(1, -3)); // } //} else { // move ball ball.savePosition(); ball.moveByDeltaXY(); } if (ballStuck) { ball.setPos(paddle.getPos() + ballOffset); if (k.IsKeyDown(Keys.Space) && prevK.IsKeyUp(Keys.Space)) { ballStuck = false; ball.setDeltaSpeed(new Vector2(2, -3)); } } else { // move ball ball.savePosition(); ball.moveByDeltaXY(); Rectangle ballbb = ball.getBoundingBoxAA(); if (ballbb.X + ballbb.Width > rhs) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(-1, 1)); } if (ballbb.X < lhs) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(-1, 1)); } if (ballbb.Y < top) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(1, -1)); } //pedal intersects if (ballbb.Intersects(paddle.getBoundingBoxAA())) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(1, -1)); } } if (k.IsKeyDown(Keys.P)) { gameStateManager.setLevel(0); } if (k.IsKeyDown(Keys.M)) { gameStateManager.setLevel(0); ball.setPos(xx, yy); paddle.setPos(xx, bot - texpaddle.Height); scoure = 0; //ball.setDeltaSpeed(new Vector2(0,0)); for (int i = 0; i < dragonList.count(); i++) { Sprite3 s = dragonList.getSprite(i); s.setActive(true); } } if (ball.getPosY() > bot) { gameStateManager.setLevel(5); ball.setPos(xx, yy); paddle.setPos(xx, bot - texpaddle.Height); scoure = 0; //ball.setDeltaSpeed(new Vector2(0,0)); for (int i = 0; i < dragonList.count(); i++) { Sprite3 s = dragonList.getSprite(i); s.setActive(true); // p.Update(gameTime); } } // next leve condition if (scoure >= 15) { gameStateManager.setLevel(0); limSound.playSound(); } ballPos = new Vector2(ball.getPosX(), ball.getPosY()); if (p != null) { p.Update(gameTime); } base.Update(gameTime); }
public void UpdateL2(GameTime gameTime) { // the crazy dog will be running towards the right and will // appear randomly on the screen crazyDog.visible = true; crazyDog.moveByDeltaXY(); crazyDog.animationTick(gameTime); crazyDog.active = true; doggyVec.X++; crazyDog.setPosX(doggyVec.X); if (crazyDog.getPosX() > 800) { doggyVec.X = 0; } if (doggyVec.X == 20 || doggyVec.X == 400) { barkoSound.playSoundIfOk(); } // check whether the player sprite has clashed with the crazy dog colWBB = playerSprite.collision(crazyDog); // play the sound if this is true if (colWBB == true && doggyVec.X == playerVec.X) { yowlinSound.playSoundIfOk(); lives--; } // play the kaching sound if the player sprite passes over // the dog and also does not colide if (colWBB == false && doggyVec.X == playerVec.X) { kachingSound.playSoundIfOk(); points++; } ks = Keyboard.GetState(); // this is really bad practice to repeat code from another method // but have no time right now to implement something better if (ks.IsKeyDown(Keys.Left)) { playerSprite.moveByDeltaXY(); playerSprite.animationTick(gameTime); successSound.playSoundIfOk(); playerSprite.active = true; background.Update(gameTime); } if (ks.IsKeyDown(Keys.Right)) { // NOTE: the move right functionality does not work currently background.Update(gameTime); } if (ks.IsKeyDown(Keys.B)) { showBB = !showBB; } /// increases the player's jump speed - this is a secret function if (ks.IsKeyDown(Keys.Z)) { playerJumpSpeed++; } // decreases the player's jump speed - this is another secret function if (ks.IsKeyDown(Keys.X)) { playerJumpSpeed--; } // this gives the small kitty cat the ability to jump around the screen if (playerJump) { playerVec.Y += playerJumpSpeed; playerJumpSpeed += 1; playerSprite.setPosY(playerVec.Y); if (playerVec.Y >= prevPos.Y) { playerSprite.setPosY(prevPos.Y); playerJump = false; } } else { if (ks.IsKeyDown(Keys.Up)) { playerJump = true; playerJumpSpeed = -14; jumpoSound.playSoundIfOk(); playerSprite.setPosY(playerVec.Y); } } // transition to end of game if user reaches 5 pts if (points == 5) { state = 6; } if (lives == 0) { state = 7; } }
/// <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> /// This is a mess, please forgive me protected override void Update(GameTime gameTime) { //Console.WriteLine(enemies.count()); curPos = horse.getPos(); bool keyDown = false; switch (level) { case 1: horse.setWidthHeight(1568 / 8 * 0.3f, texHorseRun.Height * 0.3f); k = Keyboard.GetState(); if (!started) { started = true; StartMovement(8); } if (k.IsKeyDown(Keys.Down)) { keyDown = true; horse.setPosY(horse.getPosY() + movementSpeed - 2); } if (k.IsKeyDown(Keys.Up)) { keyDown = true; horse.setPosY(horse.getPosY() - movementSpeed + 2); } if (k.IsKeyDown(Keys.Left)) { keyDown = true; horse.setFlip(SpriteEffects.FlipHorizontally); horse.setPosX(horse.getPosX() - movementSpeed + 2); } if (k.IsKeyDown(Keys.Right)) { keyDown = true; horse.setFlip(SpriteEffects.None); horse.setPosX(horse.getPosX() + movementSpeed - 2); } if (!keyDown) { horse.setAnimationSequence(anim, 0, 7, 0); } else { horse.setAnimationSequence(anim, 0, 7, 8); } //387 330 if (curPos.X >= -155 && curPos.X <= -75 && curPos.Y >= 330 && curPos.Y <= 387) { horse.setPos(xx, yy); started = false; horse.setFlip(SpriteEffects.None); level = 0; } horseRun.animationTick(gameTime); mainCamera.Follow(horse); break; default: if (!started) { if (k.IsKeyDown(Keys.D1) || k.IsKeyDown(Keys.NumPad1)) { difficulty = 1; } else if (k.IsKeyDown(Keys.D2) || k.IsKeyDown(Keys.NumPad2)) { difficulty = 2; } else if (k.IsKeyDown(Keys.D3) || k.IsKeyDown(Keys.NumPad3)) { difficulty = 3; } } switch (difficulty) { case 1: difficultyOffset = 1; break; case 2: difficultyOffset = 0.5f; break; case 3: difficultyOffset = 0.1f; break; default: break; } horse.setWidthHeight(1568 / 8 * 0.5f, texHorseRun.Height * 0.5f); //This timer makes basic instructions disappear after 3 seconds if (textFadeTimer < 3 && started) { textFadeTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (started) { enemySpawnTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (enemySpawnTimer > difficultyOffset) { enemySpawnTimer = 0; LoadEnemies(); } //Escape key exits game if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } //Keyboard current and previous state prevK = k; k = Keyboard.GetState(); //Begin all game functionality essentially if (k.IsKeyDown(Keys.Enter) && !started) { started = true; StartMovement(8); } if (started) { //Bounding box activation key if (k.IsKeyDown(Keys.B) && prevK.IsKeyUp(Keys.B)) { showbb = !showbb; } //Game over is a bool that is used to ensure the player can't move horse after it is dead, causes errors otherwise if (!gameOver) { //Player movement down if (k.IsKeyDown(Keys.Down)) { if (horseRun.getSprite(0).getPosY() < bot - horse.getHeight()) { horseRun.getSprite(0).setPosY(horseRun.getSprite(0).getPosY() + movementSpeed); } } //Player movement up if (k.IsKeyDown(Keys.Up)) { if (horseRun.getSprite(0).getPosY() > top) { horseRun.getSprite(0).setPosY(horseRun.getSprite(0).getPosY() - movementSpeed); } } } for (int e = 0; e < enemies.count(); e++) { if (enemies[e].getVisible()) { enemies[e].setPosX(enemies[e].getPosX() - enemyMovementSpeed); } if (enemies[e].getPosX() < 0 - texEnemy.Width) { enemies[e].setVisible(false); } } //Collision detection, arrow to enemy for (int ea = 0; ea < quiver.count(); ea++) { int ac = enemies.collisionWithRect(quiver[ea].getBoundingBoxAA()); if (ac != -1) { score++; Blood(enemies.getSprite(ac).getPosX(), enemies.getSprite(ac).getPosY(), false); enemies.getSprite(ac).setVisible(false); quiver[ea].setVisible(false); quiver[ea].setPos(new Vector2(0, 0)); //LoadEnemies(); } } //Allow player to increase or decrease speed if (Keyboard.GetState().IsKeyDown(Keys.Right)) { enemyMovementSpeed = fastEnemy; horse.setAnimationSequence(anim, 0, 7, fastPlayer); } else if (Keyboard.GetState().IsKeyDown(Keys.Left)) { enemyMovementSpeed = slowEnemy; horse.setAnimationSequence(anim, 0, 7, slowPlayer); } else if (started) { enemyMovementSpeed = normalEnemy; horse.setAnimationSequence(anim, 0, 7, normalPlayer); } //Scrolling background functionality if (scrolling1.rectangle.X + scrolling1.texture.Width <= 0) { scrolling1.rectangle.X = scrolling2.rectangle.X + scrolling2.texture.Width; } if (scrolling2.rectangle.X + scrolling2.texture.Width <= 0) { scrolling2.rectangle.X = scrolling1.rectangle.X + scrolling1.texture.Width; } scrolling1.Update(); scrolling2.Update(); //Game over is a bool that is used to ensure the player can't move horse after it is dead, causes errors otherwise if (!gameOver) { //Collision detection, enemy to player int rc = enemies.collisionWithRect(horse.getBoundingBoxAA()); if (rc != -1) { Blood(horse.getPosX(), horse.getPosY(), true); movementSpeed = 0; enemyMovementSpeed = 0; scrolling1.speed = 0; scrolling2.speed = 0; for (int i = 0; i < horseRun.count(); i++) { horseRun.deleteSprite(i); } gameOver = true; } } //Shooting arrow functionality if (Keyboard.GetState().IsKeyDown(Keys.Space) && !arrowShot) { Arrow(); arrowShot = true; } for (int a = 0; a < quiver.count(); a++) { if (quiver[a].getVisible()) { quiver[a].savePosition(); quiver[a].moveByDeltaXY(); } if (quiver[a].getPosX() > 800) { quiver[a].setVisible(false); } } if (arrowShot) { arrowTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (arrowTimer > 0.5f) { arrowTimer = 0; arrowShot = false; } } //Animation ticks for anything that is being animated horseRun.animationTick(gameTime); enemies.animationTick(gameTime); bloodSplat.animationTick(gameTime); if (gameOver) { deathTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } break; } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } prevK = k; k = Keyboard.GetState(); if (k.IsKeyDown(Keys.B) && prevK.IsKeyUp(Keys.B)) // *** { showbb = !showbb; } if (k.IsKeyDown(Keys.Right)) { if (paddle.getPosX() < rhs - texpaddle.Width) { paddle.setPosX(paddle.getPosX() + paddleSpeed); } } if (k.IsKeyDown(Keys.Left)) { if (paddle.getPosX() > lhs) { paddle.setPosX(paddle.getPosX() - paddleSpeed); } } if (ballStuck) { ball.setPos(paddle.getPos() + ballOffset); if (k.IsKeyDown(Keys.Space) && prevK.IsKeyUp(Keys.Space)) { ballStuck = false; ball.setDeltaSpeed(new Vector2(2, -3)); } } else { // move ball ball.savePosition(); ball.moveByDeltaXY(); Rectangle ballbb = ball.getBoundingBoxAA(); if (ballbb.X + ballbb.Width > rhs) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(-1, 1)); } if (ballbb.X < lhs) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(-1, 1)); } if (ballbb.Y < top) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(1, -1)); } if (ballbb.Intersects(paddle.getBoundingBoxAA())) { ball.setDeltaSpeed(ball.getDeltaSpeed() * new Vector2(1, -1)); } } // TODO: Add your update logic here 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 InputManager.Instance.Update(); //deltaTime is in seconds, so calculations = pixels per second. deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; //Press enter to start game/level if (!startGame) { if (InputManager.Instance.KeyPressed(Keys.Enter)) { startGame = true; } return; } timer += deltaTime; //update player spritePlayer.Update(gameTime); //update backgrounds scrollBack.Update(gameTime); scrollFore.Update(gameTime); scrollBack.setScrollSpeed(playerSpeeeeeed - 2); scrollFore.setScrollSpeed(playerSpeeeeeed); //limit scroll speed if (playerSpeeeeeed > -1) { playerSpeeeeeed = -1; } else if (playerSpeeeeeed < -10) { playerSpeeeeeed = -10; } //Every 2 seconds, spawn an enemy randomly. if (timer > spawnInterval) { AddEnemy(enemyList); timer = 0; } //inputs if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (InputManager.Instance.KeyPressed(Keys.B)) { showbb = !showbb; } //PLAYER MOVEMENT if (InputManager.Instance.KeyDown(Keys.Left)) { if (spritePlayer.getPosX() > playArea.Left) { spritePlayer.setPosX(spritePlayer.getPosX() - playerSpeed); } playerSpeeeeeed += 0.1f; } if (InputManager.Instance.KeyDown(Keys.Right)) { if (spritePlayer.getPosX() < playArea.Right - spritePlayer.getWidth()) { spritePlayer.setPosX(spritePlayer.getPosX() + playerSpeed); } playerSpeeeeeed += -0.1f; } if (InputManager.Instance.KeyDown(Keys.Up)) { if (spritePlayer.getPosY() > playArea.Bottom) { spritePlayer.setPosY(spritePlayer.getPosY() - playerSpeed); } } if (InputManager.Instance.KeyDown(Keys.Down)) { if (spritePlayer.getPosY() < playArea.Top - spritePlayer.getHeight()) { spritePlayer.setPosY(spritePlayer.getPosY() + playerSpeed); } } //PLAYER SHOOT if (InputManager.Instance.KeyPressed(Keys.Space)) { AddBullet((int)spritePlayer.getPosX(), (int)spritePlayer.getPosY()); } //Update all enemies and bullets to move. also inactive if outofbounds //In the past I called this instead of having an extended class with custom update // enemyList.moveDeltaX(-3); //Update/remove enemies enemyList.Update(gameTime); enemyList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200)); //update/remove bullets playerBulletList.moveDeltaX(5); playerBulletList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200)); //If player collides with enemy int collisionIndex = enemyList.collisionAA(spritePlayer); if (collisionIndex != -1) { Sprite3 temp = enemyList.getSprite(collisionIndex); //player collides with boss if (temp.getName() == "boss") { } else { //enemy dies? temp.active = false; temp.visible = false; AddExplosion((int)temp.getPosX(), (int)temp.getPosY()); score -= 5; textScore.text = "Score : " + score.ToString(); } } //if a bullet collides with enemy for (int i = 0; i < playerBulletList.count(); i++) { Sprite3 currentBullet = playerBulletList.getSprite(i); if (currentBullet == null) { continue; } if (currentBullet.active != true) { continue; } if (currentBullet.visible != true) { continue; } //if collision success int collision = enemyList.collisionAA(currentBullet); if (collision != -1) { Sprite3 currentEnemy = enemyList.getSprite(collision); //kill both enemy and bullet currentEnemy.active = false; currentEnemy.visible = false; currentBullet.active = false; currentBullet.visible = false; //add particle AddExplosion((int)currentEnemy.getPosX(), (int)currentEnemy.getPosY()); //add score score += 10; textScore.text = "Score : " + score.ToString(); } } //update particles particleList.animationTick(gameTime); base.Update(gameTime); }
public override void Update(GameTime gameTime) { //int mgStates = 0; // game state // 0=waiting startup // 1=playing // 2=waiting for respawn // 3=Ending count You Win // 4=Ending count You Looser // 5=Game over You Winner // 6=Game over You Looser ticks++; if (mgState == 2) { timeIn2++; } if (mgState == 5 || mgState == 6) { timeIn5or6++; } if (playStyle == 1) { return; // frozen } KeyboardState keyState = Keyboard.GetState(); if (mgState == 3 || mgState == 4) { endingCount++; } if (mgState == 3 && targets.countActive() == 0 && endingCount > endingCountMax) { mgState = 5; } if (mgState == 4 && endingCount > endingCountMax) { mgState = 6; } if (mgState == 1 || mgState == 3) { checkIfIAmShot(); } if (mgState == 2) { if (targets.countActive() == 0 && timeIn2 > timeIn2Max) { mgState = 1; } } prevX = shooter.getPosX(); if (playStyle == 2) { automove(); // auto playing fire(); } if (playStyle == 0) // playing using keyboard { if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyUp(Keys.Space)) { fire(); } if (keyState.IsKeyDown(Keys.Left)) { shooter.setPosX(shooter.getPosX() - shooterSpeed); if (shooter.getPosX() < innerRectangle.X) { shooter.setPosX(prevX); } } if (keyState.IsKeyDown(Keys.Right)) { shooter.setPosX(shooter.getPosX() + shooterSpeed); if (shooter.getPosX() + actualShooterSize.X > innerRectangle.X + innerRectangle.Width) { shooter.setPosX(prevX); } } if (keyState.IsKeyDown(Keys.B) && !prevKeyState.IsKeyDown(Keys.B)) { showbb = !showbb; } //moveBullets(); //if (mgState == 1) generateAliens(); //targets.moveDeltaXY_A(true); //int i = targets.removeIfOutside(innerRectangle); //adjustScore(i * scoreIfTargetEscapes, -900, -900); //targets.animationTick(); } if (playStyle == 2 || playStyle == 0) { moveBullets(); if (mgState == 1) { generateAliens(); } targets.moveDeltaXY(); int i = targets.removeIfOutside(innerRectangle); adjustScore(i * scoreIfTargetEscapes, -900, -900); targets.animationTick(gameTime); explosions.Update(gameTime); eyeCandy.Update(gameTime); } if (backGround != null) { backGround.Update(gameTime); } if (mgState == 0) { UpdateMG0(); } if (mgState == 5) { UpdateMG5(); } if (mgState == 6) { UpdateMG6(); } prevKeyState = keyState; }
/// <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(); } // TODO: Add your update logic here prevK = k; k = Keyboard.GetState(); delTime = (float)gameTime.ElapsedGameTime.TotalSeconds; redTimer += delTime; timer += delTime; if ((timer > (float)spawnTimer)) { NewEnemy(enemyXX, bot - texEnemy.Height); timer = 0; } back1.Update(gameTime); grass1.Update(gameTime); //player collision int plCol = enemyList.collisionAA(paddle); if (plCol != -1) { Sprite3 temp = enemyList.getSprite(plCol); createExplosion((int)temp.getPosX(), (int)temp.getPosY()); temp.active = false; temp.visible = false; score += 1; if (spawnTimer > 1) { spawnTimer -= 1; } else { spawnTimer = 1; } } for (int j = 0; j < enemyList.count(); j++) { Sprite3 currentEnemy = enemyList.getSprite(j); currentEnemy.varInt0 += delTime; float prevPosX = currentEnemy.getPosX(); if (currentEnemy.varInt0 > 0.2f) { currentEnemy.setColor(Color.White); //redTimer = 0; currentEnemy.setPosX(prevPosX); currentEnemy.varInt0 = 0; } } for (int i = 0; i < ballList.count(); i++) { Sprite3 tempBullet = ballList.getSprite(i); int enemyCollided = enemyList.collisionAA(tempBullet); if (enemyCollided != -1) { Sprite3 tempEnemy = enemyList.getSprite(enemyCollided); tempBullet.active = false; tempBullet.visible = false; tempEnemy.setColor(Color.Red); tempEnemy.setPosX(tempEnemy.getPosX() + 10); tempEnemy.hitPoints -= 1; if (tempEnemy.hitPoints < 1) { createExplosion((int)tempBullet.getPosX(), (int)tempBullet.getPosY()); tempEnemy.active = false; tempEnemy.visible = false; score += 1; if (spawnTimer > 1) { spawnTimer -= 1; } else { spawnTimer = 1; } } } } enemyList.moveDeltaXY(); enemyList.removeIfOutside(playArea); ballList.moveDeltaXY(); ballList.removeIfOutside(playArea); if (k.IsKeyDown(Keys.Left)) { scrollSpeed = 0.5f; back1.setScrollSpeed(scrollSpeed); grass1.setScrollSpeed(1); } if (k.IsKeyDown(Keys.Up)) { if (paddle.getPosY() > top + 1) { paddle.setPosY(paddle.getPosY() - paddleSpeed); } } else if (k.IsKeyDown(Keys.Down)) { //if (paddle.getPosY() < ((bot- top) - (texpaddle.Height + 2))) paddle.setPosY(paddle.getPosY() + paddleSpeed); if (paddle.getPosY() <= playArea.Height - texpaddle.Height + 7) { paddle.setPosY(paddle.getPosY() + paddleSpeed); } } else if (k.IsKeyDown(Keys.Right)) { scrollSpeed = -1; back1.setScrollSpeed(scrollSpeed); grass1.setScrollSpeed(-30); } else if (k.IsKeyDown(Keys.Left)) { scrollSpeed = 0.5f; back1.setScrollSpeed(scrollSpeed); grass1.setScrollSpeed(1); } if (k.IsKeyDown(Keys.Space) && prevK.IsKeyUp(Keys.Space)) { NewBall(xx, yy); } if (k.IsKeyDown(Keys.B) && prevK.IsKeyUp(Keys.B)) // *** { showbb = !showbb; } booms.animationTick(gameTime); base.Update(gameTime); }
public override void Update(GameTime gameTime) { prevKeyState = keyState; keyState = Keyboard.GetState(); timer += 1; explostionTimer += 1; explosionAnimation.animationTick(gameTime); mineExplostionAnimation.animationTick(gameTime); missileSmoke.Update(gameTime); exLimSound.Update(gameTime); previousMouseState = currentMouseState; currentMouseState = Mouse.GetState(); mouse_x = currentMouseState.X; mouse_y = currentMouseState.Y; // Sets up mine if (currentMouseState.LeftButton == ButtonState.Pressed) { minePos.X = mouse_x; minePos.Y = mouse_y; drawMines = true; mine.active = true; mineCount = 1; } // Controls player movements if (keyState.IsKeyDown(Keys.Left)) { if (player.getPosX() > Dir.leftBoundary) { player.setPosX(player.getPosX() - 4); // Moves player 3 units left player.setFlip(SpriteEffects.FlipHorizontally); } } if (keyState.IsKeyDown(Keys.Right)) { if (player.getPosX() < (Dir.rightBoundary - (player.getWidth() - 5))) { player.setPosX(player.getPosX() + 4); // Moves player 3 units right player.setFlip(SpriteEffects.None); } } // Moves enemy sprite enemy1.setPosX(enemy1.getPosX() + Dir.rnd.Next(1, 3)); enemy2.setPosX(enemy2.getPosX() - Dir.rnd.Next(1, 3)); // If enemy runs off screen, reset position to random x postion if (enemy1.getPosX() > Dir.rightBoundary + 60) { enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (enemy2.getPosX() < Dir.leftBoundary - (enemy2.getWidth() + 50)) { enemy2.setPos(Dir.rnd.Next(Dir.rightBoundary, Dir.rightBoundary + 200), Dir.rnd.Next(200, 700)); } // Shoot missile if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyUp(Keys.Space)) { missile.setPos(player.getPosX() + player.getWidth() / 2, player.getPosY() - missile.getHeight()); missile.active = true; drawSmoke(); drawMissile = true; if (missileCount > 0) { missileCount--; } } if (missileCount == 0) { missileCount = 5; } if (missile.getPosY() < 185 + missile.getHeight()) { missile.setPos(player.getPos()); drawMissile = false; missileSmoke.deActivate(); } if (timer % 361 == 360) { anchor.setPos(enemy1.getPosX() + enemy1.getWidth() / 2, enemy1.getPosY() + enemy1.getHeight() / 2); anchorDraw = true; } if (anchor.getPosY() > Dir.bottomBoundary + anchor.getHeight()) { anchor.active = false; anchorDraw = false; } // Collision Control enemy1Collision = missile.collision(enemy1); enemy2Collision = missile.collision(enemy2); if (enemy1Collision) { enemy1.hitPoints = enemy1.hitPoints - 10; currXMissile = enemy1.getPosX() + enemy1.getWidth() / 2; currYMissile = enemy1.getPosY() - enemy1.getHeight() / 2; if (enemy1.hitPoints <= 0) { collision1 = true; enemy1.hitPoints = 10; Dir.enemy1Count++; missileSmoke.deActivate(); } getSetMissile(); } if (enemy2Collision) { missileSmoke.deActivate(); enemy2.hitPoints = enemy2.hitPoints - 10; drawMissile = false; if (enemy2.hitPoints <= 0) { currXMissile = enemy2.getPosX() + enemy2.getWidth() / 2; currYMissile = enemy2.getPosY() - enemy2.getHeight() / 2; collision2 = true; Dir.enemy2Count++; enemy2.hitPoints = 20; } getSetMissile(); } // Gain 10 hp if missile hits the anchor preserve bool lifeGain = missile.collision(life); if (lifeGain == true) { if (player.hitPoints < 50) { player.hitPoints = player.hitPoints + 10; } lifeGain = false; drawMissile = false; life.active = false; life.setPos(new Vector2(Dir.rnd.Next(250, 800), Dir.rnd.Next(400, 600))); missile.active = false; missile.setPos(player.getPosX(), player.getPosY()); missileSmoke.deActivate(); } bool anchorCollision = anchor.collision(player); if (anchorCollision) { anchorDraw = false; anchor.setActiveAndVisible(false); anchor.setPos(new Vector2(-100, -100)); player.hitPoints = player.hitPoints - 20; } mineCollission = mine.collision(enemy1); if (mineCollission) { mine.active = false; drawMines = false; mineCurrPos = mine.getPos(); mine.setPos(new Vector2(-100, 0)); enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle = true; score += 150; mineCount = 0; Dir.enemy1Count += 1; exLimSound.playSoundIfOk(); } mineCollission2 = mine.collision(enemy2); if (mineCollission2) { mine.active = false; drawMines = false; mineCurrPos = mine.getPos(); mine.setPos(new Vector2(-100, 0)); enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle2 = true; score += 200; mineCount = 0; Dir.enemy2Count += 1; exLimSound.playSoundIfOk(); } // Toggle BB if (keyState.IsKeyDown(Keys.B) && prevKeyState.IsKeyUp(Keys.B)) { showBB = !showBB; } // Pause if (keyState.IsKeyDown(Keys.P) && !prevKeyState.IsKeyDown(Keys.P)) { gameStateManager.pushLevel(3); } // Help State if (keyState.IsKeyDown(Keys.F1) && !prevKeyState.IsKeyDown(Keys.F1)) { gameStateManager.pushLevel(5); } if (player.hitPoints < 0) { player.hitPoints = 0; } // End State if (score >= 1000 || player.hitPoints <= 0) { if (timer % 181 == 180) { gameStateManager.setLevel(4); } } }
public override void Update(GameTime gameTime) { prevKeyState = keyState; keyState = Keyboard.GetState(); timer += 1; explosionAnimation.animationTick(gameTime); missileSmoke.Update(gameTime); if (keyState.IsKeyDown(Keys.Left)) { if (player.getPosX() > Dir.leftBoundary) { player.setPosX(player.getPosX() - 4); // Moves player 4 units left player.setFlip(SpriteEffects.FlipHorizontally); } } if (keyState.IsKeyDown(Keys.Right)) { if (player.getPosX() < (Dir.rightBoundary - (player.getWidth() - 5))) { player.setPosX(player.getPosX() + 4); // Moves player 4 units right player.setFlip(SpriteEffects.None); } } enemy1.setPosX(enemy1.getPosX() + Dir.rnd.Next(1, 3)); enemy2.setPosX(enemy2.getPosX() - Dir.rnd.Next(1, 3)); enemy3.setPosX(enemy3.getPosX() + Dir.rnd.Next(2, 4)); // If enemy runs off screen, reset position to random x postion if (enemy1.getPosX() > Dir.rightBoundary + 60) { enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (enemy2.getPosX() < Dir.leftBoundary - (enemy2.getWidth() + 50)) { enemy2.setPos(Dir.rnd.Next(Dir.rightBoundary, Dir.rightBoundary + 200), Dir.rnd.Next(200, 700)); } if (enemy3.getPosX() > Dir.rightBoundary + 60) { enemy3.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyUp(Keys.Space)) { missile.setPos(player.getPosX() + player.getWidth() / 2, player.getPosY() - missile.getHeight()); missile.active = true; drawMissile = true; drawSmoke(); if (missileCount > 0) { missileCount--; } } if (missileCount == 0) { missileCount = 5; } if (missile.getPosY() < 185 + missile.getHeight()) { missile.setPos(player.getPos()); drawMissile = false; missileSmoke.deActivate(); } enemy1Collision = missile.collision(enemy1); enemy2Collision = missile.collision(enemy2); enemy3Collision = missile.collision(enemy3); explostionTimer += 1; if (enemy1Collision) { enemy1.hitPoints = enemy1.hitPoints - 10; currXMissile = enemy1.getPosX() + enemy1.getWidth() / 2; currYMissile = enemy1.getPosY() - enemy1.getHeight() / 2; if (enemy1.hitPoints <= 0) { collision1 = true; Dir.enemy1Count++; enemy1.hitPoints = 10; } getSetMissile(); } if (enemy2Collision) { missileSmoke.deActivate(); enemy2.hitPoints = enemy2.hitPoints - 10; drawMissile = false; if (enemy2.hitPoints <= 0) { currXMissile = enemy2.getPosX() + enemy2.getWidth() / 2; currYMissile = enemy2.getPosY() - enemy2.getHeight() / 2; collision2 = true; Dir.enemy2Count++; enemy2.hitPoints = 20; } getSetMissile(); } if (enemy3Collision) { enemy3.hitPoints = enemy3.hitPoints - 10; currXMissile = enemy3.getPosX() + enemy3.getWidth() / 2; currYMissile = enemy3.getPosY() - enemy3.getHeight() / 2; if (enemy3.hitPoints <= 0) { collision3 = true; Dir.enemy3Count++; enemy3.hitPoints = 10; } getSetMissile(); } // Toggle showing Boundary Box if (keyState.IsKeyDown(Keys.B) && prevKeyState.IsKeyUp(Keys.B)) { showBoundary = !showBoundary; } if (keyState.IsKeyDown(Keys.P) && !prevKeyState.IsKeyDown(Keys.P)) { gameStateManager.pushLevel(3); } if (keyState.IsKeyDown(Keys.F1) && !prevKeyState.IsKeyDown(Keys.F1)) { gameStateManager.pushLevel(5); } if (score >= 300) { if (timer % 181 == 180) { gameStateManager.setLevel(2); } } exLimSound.Update(gameTime); }