예제 #1
0
        public void OnKeyPress(object sender, KeyEventArgs e)
        {
            if (sender is Kolobok)
            {
                switch (e.KeyData)
                {
                case Keys.Up:
                case Keys.W:
                {
                    ((Kolobok)sender).IdentifyDirection((int)Direction.Up);
                    break;
                }

                case Keys.Down:
                case Keys.S:
                {
                    ((Kolobok)sender).IdentifyDirection((int)Direction.Down);
                    break;
                }

                case Keys.Left:
                case Keys.A:
                {
                    ((Kolobok)sender).IdentifyDirection((int)Direction.Left);
                    break;
                }

                case Keys.Right:
                case Keys.D:
                {
                    ((Kolobok)sender).IdentifyDirection((int)Direction.Right);
                    break;
                }

                case Keys.F:
                {
                    ShootOff?.Invoke(this);
                    break;
                }

                default:
                    break;
                }
            }
        }
예제 #2
0
        public void Move(List <Wall> Walls, List <Tank> Tanks)
        {
            if (rnd.NextDouble() < 0.3)
            {
                IdentifyDirection(TanksForm.rnd.Next(0, 4));
            }

            if (rnd.NextDouble() < 0.15)
            {
                ShootOff?.Invoke(this);
            }


            switch (DirectionTo)
            {
            case (int)Direction.Down:
            {
                OldY = Y;
                OldX = X;
                Y++;
                if (CollidesWithWalls(Walls) || CollidesWithTanks(Tanks))
                {
                    Y--;
                    IdentifyDirection((int)Direction.Up);
                }
                break;
            }

            case (int)Direction.Left:
            {
                OldY = Y;
                OldX = X;
                X--;
                if (CollidesWithWalls(Walls) || CollidesWithTanks(Tanks))
                {
                    X++;
                    IdentifyDirection((int)Direction.Right);
                }
                break;
            }

            case (int)Direction.Right:
            {
                OldY = Y;
                OldX = X;
                X++;
                if (CollidesWithWalls(Walls) || CollidesWithTanks(Tanks))
                {
                    X--;
                    IdentifyDirection((int)Direction.Left);
                }
                break;
            }

            case (int)Direction.Up:
            {
                OldY = Y;
                OldX = X;
                Y--;
                if (CollidesWithWalls(Walls) || CollidesWithTanks(Tanks))
                {
                    Y++;
                    IdentifyDirection((int)Direction.Down);
                }
                break;
            }

            default:
                break;
            }
            ChangePicture();
        }