Exemple #1
0
        public void moveEnemies(EnemyTank tank, PictureBox gameBorders, List <BrickWall> gameWalls, Player player, List <EnemyTank> enemyTanks, Timer timer1)
        {
            Random            random   = new Random(Guid.NewGuid().GetHashCode());
            CollisionDetecter detecter = new CollisionDetecter();
            int temp;

            switch (currentState)
            {
            case Direction.Right:
                if (tank.Location.X + tank.Size.Width + 20 < gameBorders.Bounds.Right &&
                    !detecter.detectCollision(gameWalls, new Rectangle(tank.Location.X + 1, tank.Location.Y, 50, 50), enemyTanks, tank) &&
                    !tank.Bounds.IntersectsWith(new Rectangle(player.Location.X - 1, player.Location.Y, 50, 50)))
                {
                    //shooting chance
                    if ((temp = random.Next(0, 101)) < shootingChance)
                    {
                        Cannon cannon = new Cannon(tank.Location);
                        gameBorders.Controls.Add(cannon);
                        cannon.Location = new Point(tank.Location.X + 51, tank.Location.Y + 20);
                        timer1.Tick    += new EventHandler((sender1, e1) => shooter.shootRight(sender1, e1, cannon, gameWalls, enemyTanks, gameBorders, player, true));
                    }
                    if ((temp = random.Next(0, 101)) < changeDirection)
                    {
                        currentState = (Direction)random.Next(0, 4);
                        break;
                    }
                    tank.Image    = Properties.Resources.enemy_tank_right;
                    tank.Location = new Point(tank.Location.X + 5, tank.Location.Y);
                    currentState  = Direction.Right;
                }
                else
                {
                    currentState = (Direction)random.Next(0, 4);
                }
                break;

            case Direction.Left:
                if (tank.Location.X + 10 > gameBorders.Bounds.Left &&
                    !detecter.detectCollision(gameWalls, new Rectangle(tank.Location.X - 1, tank.Location.Y, 50, 50), enemyTanks, tank) &&
                    !tank.Bounds.IntersectsWith(new Rectangle(player.Location.X + 1, player.Location.Y, 50, 50)))
                {
                    if ((temp = random.Next(0, 101)) < shootingChance)
                    {
                        Cannon cannon = new Cannon(tank.Location);
                        gameBorders.Controls.Add(cannon);
                        cannon.Location = new Point(tank.Location.X + 51, tank.Location.Y + 20);
                        timer1.Tick    += new EventHandler((sender1, e1) => shooter.shootLeft(sender1, e1, cannon, gameWalls, enemyTanks, gameBorders, player, true));
                    }
                    if ((temp = random.Next(0, 101)) < changeDirection)
                    {
                        currentState = (Direction)random.Next(0, 4);
                        break;
                    }
                    tank.Image    = Properties.Resources.enemy_tank_left;
                    tank.Location = new Point(tank.Location.X - 5, tank.Location.Y);
                    currentState  = Direction.Left;
                }
                else
                {
                    currentState = (Direction)random.Next(0, 4);
                }
                break;

            case Direction.Up:
                if (tank.Location.Y + 75 > gameBorders.Bounds.Top &&
                    !detecter.detectCollision(gameWalls, new Rectangle(tank.Location.X, tank.Location.Y - 1, 50, 50), enemyTanks, tank) &&
                    !tank.Bounds.IntersectsWith(new Rectangle(player.Location.X, player.Location.Y + 1, 50, 50)))
                {
                    if ((temp = random.Next(0, 101)) < shootingChance)
                    {
                        Cannon cannon = new Cannon(tank.Location);
                        gameBorders.Controls.Add(cannon);
                        cannon.Location = new Point(tank.Location.X + 51, tank.Location.Y + 20);
                        timer1.Tick    += new EventHandler((sender1, e1) => shooter.shootUp(sender1, e1, cannon, gameWalls, enemyTanks, gameBorders, player, true));
                    }
                    if ((temp = random.Next(0, 101)) < changeDirection)
                    {
                        currentState = (Direction)random.Next(0, 4);
                        break;
                    }
                    tank.Image    = Properties.Resources.enemy_tank_up;
                    tank.Location = new Point(tank.Location.X, tank.Location.Y - 5);
                    currentState  = Direction.Up;
                }
                else
                {
                    currentState = (Direction)random.Next(0, 4);
                }
                break;

            case Direction.Down:
                if (tank.Location.Y + tank.Size.Height + 85 < gameBorders.Bounds.Bottom &&
                    !detecter.detectCollision(gameWalls, new Rectangle(tank.Location.X, tank.Location.Y + 1, 50, 50), enemyTanks, tank) &&
                    !tank.Bounds.IntersectsWith(new Rectangle(player.Location.X, player.Location.Y - 1, 50, 50)))
                {
                    if ((temp = random.Next(0, 101)) < shootingChance)
                    {
                        Cannon cannon = new Cannon(tank.Location);
                        gameBorders.Controls.Add(cannon);
                        cannon.Location = new Point(tank.Location.X + 51, tank.Location.Y + 20);
                        timer1.Tick    += new EventHandler((sender1, e1) => shooter.shootDown(sender1, e1, cannon, gameWalls, enemyTanks, gameBorders, player, true));
                    }
                    if ((temp = random.Next(0, 101)) < changeDirection)
                    {
                        currentState = (Direction)random.Next(0, 4);
                        break;
                    }
                    tank.Image    = Properties.Resources.enemy_tank_down;
                    tank.Location = new Point(tank.Location.X, tank.Location.Y + 5);
                    currentState  = Direction.Down;
                }
                else
                {
                    currentState = (Direction)random.Next(0, 4);
                }
                break;
            }
        }
        private void tank_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            case Keys.Left:
                player.Image     = Properties.Resources.tank_left;
                currentDirection = Direction.Left;
                if (navigateHorizontally(currentDirection, player, gameBorders.Bounds.Left))
                {
                    player.Location = new Point(player.Location.X - 5, player.Location.Y);
                    break;
                }
                break;

            case Keys.Right:
                player.Image     = Properties.Resources.tank_right;
                currentDirection = Direction.Right;
                if (navigateHorizontally(currentDirection, player, gameBorders.Bounds.Right))
                {
                    player.Location = new Point(player.Location.X + 5, player.Location.Y);
                    break;
                }
                break;

            case Keys.Up:
                player.Image     = Properties.Resources.tank_up;
                currentDirection = Direction.Up;
                if (navigateVertically(currentDirection, player, gameBorders.Bounds.Top))
                {
                    player.Location = new Point(player.Location.X, player.Location.Y - 5);
                    break;
                }
                break;

            case Keys.Down:
                player.Image     = Properties.Resources.tank_down;
                currentDirection = Direction.Down;
                if (navigateVertically(currentDirection, player, gameBorders.Bounds.Bottom))
                {
                    player.Location = new Point(player.Location.X, player.Location.Y + 5);
                    break;
                }
                break;

            case Keys.Space:
                Cannon cannon = new Cannon(player.Location);
                gameBorders.Controls.Add(cannon);
                cannon.Location = new Point(player.Location.X + 20, player.Location.Y + 20);
                switch (currentDirection)
                {
                case Direction.Right:
                    timer1.Tick += new EventHandler((sender1, e1) => shooter.shootRight(sender, e, cannon, gameWalls, enemyTanks, gameBorders, player, false));
                    break;

                case Direction.Left:
                    timer1.Tick += new EventHandler((sender1, e1) => shooter.shootLeft(sender, e, cannon, gameWalls, enemyTanks, gameBorders, player, false));
                    break;

                case Direction.Up:
                    timer1.Tick += new EventHandler((sender1, e1) => shooter.shootUp(sender, e, cannon, gameWalls, enemyTanks, gameBorders, player, false));
                    break;

                case Direction.Down:
                    timer1.Tick += new EventHandler((sender1, e1) => shooter.shootDown(sender, e, cannon, gameWalls, enemyTanks, gameBorders, player, false));
                    break;
                }
                break;
            }
        }