protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } previousKB = currentKB; currentKB = Keyboard.GetState(); if (currentKB.IsKeyUp(Keys.P) && previousKB.IsKeyDown(Keys.P)) { gamePaused = !gamePaused; } if (gamePaused) { return; } if (game_over) { return; } if (Keyboard.GetState().IsKeyDown(Keys.Up)) { player.Mover(Player.Direcoes.Cima); } else if (Keyboard.GetState().IsKeyDown(Keys.Down)) { player.Mover(Player.Direcoes.Baixo); } else if (Keyboard.GetState().IsKeyDown(Keys.Left)) { player.Mover(Player.Direcoes.Esquerda); } else if (Keyboard.GetState().IsKeyDown(Keys.Right)) { player.Mover(Player.Direcoes.Direita); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { Vector2 v = player.GetPosicao(); int size_x = Constantes.PLAYER_WIDTH; int shot_size_x = Constantes.PLAYER_SHOT_WIDTH; v.X = v.X + (size_x - shot_size_x) / 2; player_laser_x = (int)v.X; player_laser_y = (int)v.Y; playerLaser.SetPosicao(v); //player.ShotSound(); } enemy_ship_y += 3; if (enemy_ship_y > Constantes.SCREEN_HEIGHT) { SetEnemyShipPosition(); } if (enemy_increment) { enemy_ship_x += 6; } else { enemy_ship_x -= 6; } if (enemy_ship_x >= (Constantes.SCREEN_WIDTH - Constantes.ENEMY_WIDTH)) { enemy_increment = false; enemy_ship_x -= 6; } if (enemy_ship_x <= 0) { enemy_increment = true; enemy_ship_x += 6; } meteor_big_y += 3; if (meteor_big_y > Constantes.SCREEN_HEIGHT) { SetMeteorBigPosition(); } meteor_small_y += 3; if (meteor_small_y > Constantes.SCREEN_HEIGHT) { SetMeteorSmallPosition(); } meteorBig.SetPosicao(new Vector2(meteor_big_x, meteor_big_y)); meteorSmall.SetPosicao(new Vector2(meteor_small_x, meteor_small_y)); enemyShip.SetPosicao(new Vector2(enemy_ship_x, enemy_ship_y)); player.Update(gameTime); playerLaser.Update(gameTime); meteorBig.Update(gameTime); meteorSmall.Update(gameTime); enemyShip.Update(gameTime); if (show_enemy_ufo) { enemy_ufo_x += 10; if (enemy_ufo_x > Constantes.SCREEN_WIDTH) { SetEnemyUfoPosition(); show_enemy_ufo = false; } enemyUfo.SetPosicao(new Vector2(enemy_ufo_x, enemy_ufo_y)); enemyUfo.Update(gameTime); } // Verifica se os meteoros não estão sobrepostos if (meteorBig.boundingBox.Intersects(meteorSmall.boundingBox)) { SetMeteorBigPosition(); } // Testa colisão do laser com o meteoro grande if (playerLaser.boundingBox.Intersects(meteorBig.boundingBox)) { SetMeteorBigPosition(); player_laser_x = -50; player_laser_y = -50; player.Pontuar(5); } // Testa colisão do player com o meteoro grande if (player.boundingBox.Intersects(meteorBig.boundingBox)) { SetMeteorBigPosition(); game_over = true; HidePlayer(); } // Testa colisão do laser com o meteoro pequeno if (playerLaser.boundingBox.Intersects(meteorSmall.boundingBox)) { SetMeteorSmallPosition(); player_laser_x = -50; player_laser_y = -50; player.Pontuar(10); } // Testa colisão do player com o meteoro pequeno if (player.boundingBox.Intersects(meteorSmall.boundingBox)) { SetMeteorSmallPosition(); game_over = true; HidePlayer(); } // Testa colisão do laser com o meteoro pequeno if (playerLaser.boundingBox.Intersects(enemyUfo.boundingBox)) { SetEnemyUfoPosition(); player_laser_x = -50; player_laser_y = -50; show_enemy_ufo = false; player.Pontuar(50); } // Testa colisão do player com o meteoro pequeno if (player.boundingBox.Intersects(enemyUfo.boundingBox)) { SetEnemyUfoPosition(); show_enemy_ufo = false; game_over = true; HidePlayer(); } // Testa colisão do laser com a nave inimiga if (playerLaser.boundingBox.Intersects(enemyShip.boundingBox)) { SetEnemyShipPosition(); player_laser_x = -50; player_laser_y = -50; player.Pontuar(25); } // Testa colisão do player com a nave inimiga if (player.boundingBox.Intersects(enemyShip.boundingBox)) { SetEnemyShipPosition(); game_over = true; HidePlayer(); } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } previousKB = currentKB; currentKB = Keyboard.GetState(); if (currentKB.IsKeyUp(Keys.P) && previousKB.IsKeyDown(Keys.P)) { gamePaused = !gamePaused; } if (gamePaused) { return; } if (!game_over) { if (Keyboard.GetState().IsKeyDown(Keys.Up)) { player.Mover(Player.Direcoes.Cima); } else if (Keyboard.GetState().IsKeyDown(Keys.Down)) { player.Mover(Player.Direcoes.Baixo); } else if (Keyboard.GetState().IsKeyDown(Keys.Left)) { player.Mover(Player.Direcoes.Esquerda); } else if (Keyboard.GetState().IsKeyDown(Keys.Right)) { player.Mover(Player.Direcoes.Direita); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { Vector2 v = player.GetPosicao(); int size_x = Constantes.PLAYER_WIDTH; int shot_size_x = Constantes.PLAYER_SHOT_WIDTH; v.X = v.X + (size_x - shot_size_x) / 2; for (int i = 0; i < Constantes.SHOT_COUNT; i++) { if (Constantes.DEFAULT_POSITION == player_laser_y[i]) { // posiciona o laser junto a nave player_laser_x[i] = (int)v.X; player_laser_y[i] = (int)v.Y; shots[i].SetPosicao(v); break; } } player.ShotSound(); } enemy_ship_y += 2; if (enemy_ship_y > Constantes.SCREEN_HEIGHT) { SetEnemyShipPosition(); } if (enemy_increment) { enemy_ship_x += 4; } else { enemy_ship_x -= 4; } if (enemy_ship_x >= (Constantes.SCREEN_WIDTH - Constantes.ENEMY_WIDTH)) { enemy_increment = false; enemy_ship_x -= 4; } if (enemy_ship_x <= 4) { enemy_increment = true; enemy_ship_x += 4; } meteor_big_y += 3; if (meteor_big_y > Constantes.SCREEN_HEIGHT) { SetMeteorBigPosition(); } meteor_small_y += 3; if (meteor_small_y > Constantes.SCREEN_HEIGHT) { SetMeteorSmallPosition(); } meteorBig.SetPosicao(new Vector2(meteor_big_x, meteor_big_y)); meteorSmall.SetPosicao(new Vector2(meteor_small_x, meteor_small_y)); enemyShip.SetPosicao(new Vector2(enemy_ship_x, enemy_ship_y)); // Laser do inimigo if (enemy_laser_x != Constantes.DEFAULT_POSITION) { if (enemy_laser_y > Constantes.ENEMY_HEIGHT && enemy_laser_y < Constantes.SCREEN_HEIGHT) { enemy_laser_y += 5; } else { enemy_laser_x = Constantes.DEFAULT_POSITION; enemy_laser_y = Constantes.DEFAULT_POSITION; } } else { Vector2 v = enemyShip.GetPosicao(); int size_x = Constantes.ENEMY_WIDTH; int shot_size_x = Constantes.ENEMY_SHOT_WIDTH; v.X = v.X + (size_x - shot_size_x) / 2; v.Y += Constantes.ENEMY_HEIGHT; enemy_laser_x = (int)v.X; enemy_laser_y = (int)v.Y; } player.Update(gameTime); for (int i = 0; i < Constantes.SHOT_COUNT; i++) { shots[i].Update(gameTime); } meteorBig.Update(gameTime); meteorSmall.Update(gameTime); enemyShip.Update(gameTime); enemyLaser.Update(gameTime); if (show_enemy_ufo) { enemy_ufo_x += 10; if (enemy_ufo_x > Constantes.SCREEN_WIDTH) { SetEnemyUfoPosition(); show_enemy_ufo = false; } enemyUfo.SetPosicao(new Vector2(enemy_ufo_x, enemy_ufo_y)); enemyUfo.Update(gameTime); } // Verifica se os meteoros não estão sobrepostos if (meteorBig.boundingBox.Intersects(meteorSmall.boundingBox)) { SetMeteorBigPosition(); } // Testa colisão do laser com o meteoro grande for (int i = 0; i < Constantes.SHOT_COUNT; i++) { if (shots[i].boundingBox.Intersects(meteorBig.boundingBox)) { SetMeteorBigPosition(); player_laser_x[i] = Constantes.DEFAULT_POSITION; player_laser_y[i] = Constantes.DEFAULT_POSITION; player.Pontuar(5); break; } } // Testa colisão do player com o meteoro grande if (player.boundingBox.Intersects(meteorBig.boundingBox)) { SetMeteorBigPosition(); game_over = true; HidePlayer(); } // Testa colisão do laser com o meteoro pequeno for (int i = 0; i < Constantes.SHOT_COUNT; i++) { if (shots[i].boundingBox.Intersects(meteorSmall.boundingBox)) { SetMeteorSmallPosition(); player_laser_x[i] = Constantes.DEFAULT_POSITION; player_laser_y[i] = Constantes.DEFAULT_POSITION; player.Pontuar(10); break; } } // Testa colisão do player com o meteoro pequeno if (player.boundingBox.Intersects(meteorSmall.boundingBox)) { SetMeteorSmallPosition(); game_over = true; HidePlayer(); } // Testa colisão do laser com o meteoro pequeno for (int i = 0; i < Constantes.SHOT_COUNT; i++) { if (shots[i].boundingBox.Intersects(enemyUfo.boundingBox)) { SetEnemyUfoPosition(); player_laser_x[i] = Constantes.DEFAULT_POSITION; player_laser_y[i] = Constantes.DEFAULT_POSITION; show_enemy_ufo = false; player.Pontuar(50); break; } } // Testa colisão do player com o meteoro pequeno if (player.boundingBox.Intersects(enemyUfo.boundingBox)) { SetEnemyUfoPosition(); show_enemy_ufo = false; game_over = true; HidePlayer(); } // Testa colisão do laser com a nave inimiga for (int i = 0; i < Constantes.SHOT_COUNT; i++) { if (shots[i].boundingBox.Intersects(enemyShip.boundingBox)) { SetEnemyShipPosition(); player_laser_x[i] = Constantes.DEFAULT_POSITION; player_laser_y[i] = Constantes.DEFAULT_POSITION; player.Pontuar(25); break; } } // Testa colisão do player com a nave inimiga if (player.boundingBox.Intersects(enemyShip.boundingBox)) { SetEnemyShipPosition(); game_over = true; HidePlayer(); } // O tiro do inimigo acertou a nave do jogador if (player.boundingBox.Intersects(enemyLaser.boundingBox)) { SetEnemyShipPosition(); game_over = true; HidePlayer(); } enemyLaser.SetPosicao(new Vector2(enemy_laser_x, enemy_laser_y)); for (int i = 0; i < Constantes.SHOT_COUNT; i++) { shots[i].SetPosicao(new Vector2(player_laser_x[i], player_laser_y[i])); } } else { gameOver.Update(gameTime); } base.Update(gameTime); }