/// <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); }
/// <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) { //deltaTime is in seconds, so calculations = pixels per second. deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; //Press enter to start game/level switch (levelState) { case LEVELSTATE.LEVELSTART: if (InputManager.Instance.KeyPressed(Keys.Enter)) { levelState = LEVELSTATE.LEVELPLAY; } return; case LEVELSTATE.LEVELPLAY: timer += deltaTime; //update player player.Update(gameTime); //PAUSE if (InputManager.Instance.KeyPressed(Keys.P)) { gameStateManager.pushLevel(3); } //PLAYER SHOOT if (InputManager.Instance.KeyPressed(Keys.Space)) { AddBullet((int)player.getPosX(), (int)player.getPosY()); soundBulletLimit.playSoundIfOk(); } break; case LEVELSTATE.LEVELFINISH: player.Update(gameTime); if (InputManager.Instance.KeyPressed(Keys.Enter)) { gameStateManager.pushLevel(1); } break; case LEVELSTATE.LEVELGAMEOVER: timer += deltaTime; if (InputManager.Instance.KeyPressed(Keys.Q)) { gameStateManager.setLevel(4); } break; default: throw new Exception("hahaha"); } //Every 2 seconds, spawn an enemy randomly. if (timer > spawnInterval && enemyCounter < enemyToSpawn) { AddEnemy(enemyList); timer = 0; enemyCounter++; } //update backgrounds scrollBack.Update(gameTime); scrollFore.Update(gameTime); scrollBack.setScrollSpeed(player.playerBackgroundSpeed - 2); scrollFore.setScrollSpeed(player.playerBackgroundSpeed); //Update/remove enemies enemyList.Update(gameTime); enemyList.removeIfOutside(new Rectangle(-100, -100, Game1.SCREEN_WIDTH + 200, Game1.SCREEN_HEIGHT + 200)); if (enemyList.countActive() == 0 && enemyCounter >= enemyToSpawn) { levelState = LEVELSTATE.LEVELFINISH; } //update/remove bullets playerBulletList.moveDeltaX(5); playerBulletList.removeIfOutside(new Rectangle(-100, -100, Game1.SCREEN_WIDTH + 200, Game1.SCREEN_HEIGHT + 200)); //If player collides with enemy int collisionIndex = enemyList.collisionAA(player); 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(); player.TakeDamage(); if (player.hitPoints <= 0) { levelState = LEVELSTATE.LEVELGAMEOVER; AddExplosion((int)player.getPosX(), (int)player.getPosY()); player.setPos(0, -300); // offscreen player.setActive(false); } //TODO: GET HITSOUND soundEnemyHitLimit.playSoundIfOk(); } } //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); currentEnemy.hitPoints -= 1; //DEAD ENEMY if (currentEnemy.hitPoints <= 0) { currentEnemy.active = false; currentEnemy.visible = false; //add particle AddExplosion((int)currentEnemy.getPosX(), (int)currentEnemy.getPosY()); //add score score += 10; textScore.text = "Score : " + score.ToString(); } //DAMAGED ENEMY else { currentEnemy.varInt0 = 100; soundEnemyHitLimit.playSoundIfOk(); } //REMOVE BULLET currentBullet.active = false; currentBullet.visible = false; } } //update particles particleList.animationTick(gameTime); base.Update(gameTime); }