// zasah do bunkru void ShotBunker(ref Bunker bunker, Shot shot) { int r = 3; for (int i0 = 0; i0 < shot.cells.GetLength(0); i0++) { for (int j0 = 0; j0 < shot.cells.GetLength(1); j0++) { if (shot.cells[i0, j0] == 0) { continue; } for (int i = 0; i <= r; i++) { for (int j = 0; j <= r; j++) { int x = shot.x + j0 - bunker.x + j - r / 2; int y = shot.y + i0 - bunker.y + i - r / 2; if (x < 0 || x >= bunker.cells.GetLength(1) || y < 0 || y >= bunker.cells.GetLength(0)) { continue; } if (random.NextDouble() > 0.4) { bunker.cells[y, x] = 0; } } } } } }
// vytváření bunkrů void InitBunkers() { bunkers = new Bunker[bunkersCount]; int w = bunkerWidth + bunkerDst; for (int k = 0; k < bunkersCount; k++) { Bunker bunker = new Bunker { x = k * w + (gameWidth - w * bunkersCount) / 2 + bunkerWidth / 2, y = bunkerPosition, cells = new int[bunkerCells.GetLength(0), bunkerCells.GetLength(1)] }; for (int i = 0; i < bunkerCells.GetLength(0); i++) { for (int j = 0; j < bunkerCells.GetLength(1); j++) { bunker.cells[i, j] = bunkerCells[i, j]; } } bunkers[k] = bunker; } }
/// <summary> /// Initializes le jeu, en effacant son ancien état /// </summary> public void initGame() { //On ajoute le joueur this.gameObjects.Clear(); LevelController.reset(); this.player = new Player(); AddNewGameObject(this.player); Bunker.generateBunkers(this, 3); }
/// <summary> /// Private constructor /// </summary> /// <param name="gameSize">Size of the game area</param> private Game(Size gameSize) { Bitmap imageShip = new Bitmap("../../Resources/Ship4.png"); playerShip = new SpaceShip(new Vecteur2D(gameSize.Width / 2-30, gameSize.Height-35), 5, imageShip); bunkers = new List<Bunker>(); for(int i=0;i<3;i++) { bunker = new Bunker(new Vecteur2D(i * 250, gameSize.Height - 150)); bunkers.Add(bunker); } this.gameSize = gameSize; }
/// <summary> /// Determines Collision between an enemy in the block and a bunker /// </summary> /// <param name="b">Colliding bunler</param> public bool Collision(Bunker b) { if (b.Position.x > Position.x + Size.Width) { return(false); } else if (b.Position.y > Position.y + Size.Height) { return(false); } else if (Position.x > b.Position.x + b.imageWidth) { return(false); } else if (Position.y > b.Position.y + b.imageHeight) { return(false); } else { bool collision = false; foreach (SpaceShip s in Ships) { if (b.Position.x > s.Position.x + s.imageWidth) { collision = false; } else if (b.Position.y > s.Position.y + s.imageHeight) { collision = false; } else if (s.Position.x > b.Position.x + b.imageWidth) { collision = false; } else if (s.Position.y > b.Position.y + b.imageHeight) { collision = false; } else { return(true); } } return(collision); } }
public List <GameEntity> InitializeEntities() { Score = 0; //Ajout du joueur p1 = null; p1 = new PlayerShip(new Vecteur2D(gameSize.Width / 2, 550), 150, 2, GameSprites.Player, GameVariables.defaultShootSpeed); List <GameEntity> ge = new List <GameEntity>(); ge.Add(p1); Bunker b1 = new Bunker(new Vecteur2D(gameSize.Width * 0.7, 500), 0, 3, GameSprites.Bunker); Bunker b2 = new Bunker(new Vecteur2D(gameSize.Width * 0.4, 500), 0, 3, GameSprites.Bunker); Bunker b3 = new Bunker(new Vecteur2D(gameSize.Width * 0.1, 500), 0, 3, GameSprites.Bunker); ge.Add(b1); ge.Add(b2); ge.Add(b3); return(ge); }
/// <summary> /// Update game /// </summary> public void Update(double deltaT) { // keyboard events if (keyPressed.Contains(Keys.Space)) { if (playerMissile == null) { playerMissile = new Missile(new Vecteur2D(playerShip.position.x + 30, playerShip.position.y + 50), new Vecteur2D(0, -5), 1); } } if (playerMissile != null) { if (playerMissile.position.y > 0) { playerMissile.move(); bunkers.ForEach(delegate (Bunker bunker) { if (bunker.collision(playerMissile.position.x, playerMissile.position.y)) { playerMissile.lives--; bunker.vie--; Console.WriteLine("--> vie --> " + bunker.vie); if (bunker.vie == 0) { bunkers.Remove(bunker); bunker = null; } } }); if (playerMissile.lives == 0) { playerMissile = null; } } else { playerMissile = null; } } if (keyPressed.Contains(Keys.Right)) { if (playerShip.position.x < gameSize.Width - 65) { playerShip.MoveRight(deltaT); } //if (ball != null) // { // ball.MoveRight(deltaT); // if (!ball.Alive) // ball = null; // } } if (keyPressed.Contains(Keys.Left)) { if (playerShip.position.x > 0) { playerShip.MoveLeft(deltaT); } // if (ball != null) // { // ball.MoveLeft(deltaT); // if (!ball.Alive) // { // ball = null; //} // } } //move ball // if (ball != null) // { // ball.Move(deltaT); // maybe dead // if (!ball.Alive) // ball = null; // } }
// zpracování křížení výstřely void HitProcessing() { // zasah do vetřelce for (int i = alienRows - 1; i >= 0; i--) { for (int j = 0; j < alienCols; j++) { Alien alien = aliens[i][j]; if (alien == null) { continue; } int[,] cells = alien.state ? aliensCells[alien.index] : aliensCells2[alien.index]; for (int k = 0; k < shots.Count; k++) { if (CheckHit(shots[k].x, shots[k].y, shots[k].cells, alien.x, alien.y, cells)) { alien.shooted = 10; scores += alien.score; gameTime -= deltaTime; if (gameTime < endTime) { gameTime = endTime; } aliens[i][j] = alien; shots.RemoveAt(k); break; } } } } if (ufo != null) { for (int i = 0; i < shots.Count; i++) { if (CheckHit(shots[i].x, shots[i].y, shots[i].cells, ufo.x, ufo.y, ufoCells)) { scores += ufoScores; // body za zasah do UFO ufo.shooted = 20; shots.RemoveAt(i); break; } } } // zpracování zasáhu do bunkru for (int i = 0; i < bunkersCount; i++) { Bunker bunker = bunkers[i]; for (int j = alienShots.Count - 1; j >= 0; j--) { if (CheckHit(alienShots[j].x, alienShots[j].y, alienShots[j].cells, bunker.x, bunker.y, bunker.cells)) { ShotBunker(ref bunker, alienShots[j]); alienShots.RemoveAt(j); bunkers[i] = bunker; } } for (int j = shots.Count - 1; j >= 0; j--) { if (CheckHit(shots[j].x, shots[j].y, shots[j].cells, bunker.x, bunker.y, bunker.cells)) { ShotBunker(ref bunker, shots[j]); shots.RemoveAt(j); bunkers[i] = bunker; } } } // zpracování zasahu dvou výstřelu for (int i = shots.Count - 1; i >= 0; i--) { for (int j = alienShots.Count - 1; j >= 0; j--) { if (CheckHit(shots[i].x, shots[i].y, shots[i].cells, alienShots[j].x, alienShots[j].y, alienShots[j].cells)) { shots.RemoveAt(i); alienShots.RemoveAt(j); break; } } } // zpracování zasáhu do zbraňe for (int i = alienShots.Count - 1; i >= 0; i--) { if (CheckHit(alienShots[i].x, alienShots[i].y, alienShots[i].cells, gun.x - gunWidth / 2, gun.y, gunCells)) { lifes--; gunShooted = 20; alienShots.RemoveAt(i); } } }