Esempio n. 1
0
        private void PacmanTimer_Tick(object sender, EventArgs e)
        {
            HighScore    hs;
            DialogResult dr;

            if (!Dead)
            {
                if (PacmanPicState == 1)
                {
                    Pman.SetImage(Properties.Resources.Pacman2);
                    PacmanPicState = 2;
                }
                else
                {
                    Pman.SetImage(Properties.Resources.Pacman1);
                    PacmanPicState = 1;
                }

                Pman.RotatePacman();
                Pman.MovePacman(maze.GetMaze());
                if (maze.GameWin())
                {
                    Pman.SetScore(Pman.GetTries() * 200);
                    PacmanTimer.Stop();
                    GhostTimer.Stop();
                    hs = new HighScore(Pman);
                    hs.ShowDialog();
                    hs.Close();

                    dr = MessageBox.Show("Do you want to play again?",
                                         "Game Over",
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        StartGame();
                    }
                    else
                    {
                        DrawingTimer.Stop();
                        Close();
                    }
                }
            }
            else
            {
                if (i < 10)
                {
                    Pman.ResetDeathAnimationFrames();
                    Pman.SetImage(Pman.GetAnimation()[i]);
                    Pman.RotatePacman();
                    i++;
                }
                else
                {
                    i = 0;
                    Pman.SetImage(Properties.Resources.Pacman1);
                    ResetLocations();
                    PacmanTimer.Stop();
                    Dead = false;
                }
            }

            if (Pman.GetTries() != 0 || Dead)
            {
                return;
            }
            hs = new HighScore(Pman);
            hs.ShowDialog();
            hs.Close();

            dr = MessageBox.Show("Do you want to play again?",
                                 "Game Over",
                                 MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                StartGame();
            }
            else
            {
                DrawingTimer.Stop();
                Close();
            }
        }
Esempio n. 2
0
        private void GameForm_KeyDown(object sender, KeyEventArgs e)
        {
            int i = Pman.GetY() / 20;
            int j = Pman.GetX() / 20;

            if (Dead)
            {
                return;
            }
            if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.W))
            {
                if (maze.GetMaze()[i - 1, j] != 1)
                {
                    Pman.SetDirection((int)Directions.Up);
                }
                PacmanTimer.Start();
                GhostTimer.Start();
            }

            if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.S))
            {
                if (maze.GetMaze()[i + 1, j] != 1)
                {
                    Pman.SetDirection((int)Directions.Down);
                }
                PacmanTimer.Start();
                GhostTimer.Start();
            }

            if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.A))
            {
                if (maze.GetMaze()[i, j - 1] != 1)
                {
                    Pman.SetDirection((int)Directions.Left);
                }
                PacmanTimer.Start();
                GhostTimer.Start();
            }

            if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.D))
            {
                if (maze.GetMaze()[i, j + 1] != 1)
                {
                    Pman.SetDirection((int)Directions.Right);
                }
                PacmanTimer.Start();
                GhostTimer.Start();
            }

            if (e.KeyCode != Keys.Space || Pman.GetAmmo() <= 0 || ShootOnce)
            {
                return;
            }
            bullet.SetImage(Properties.Resources.Bullet);
            Pman.ReduceAmmo();
            bullet.SetX(Pman.GetX() + 5);
            bullet.SetY(Pman.GetY() + 5);
            ShootOnce = true;
            bullet.SetDirection(Pman.GetDirection());
            bullet.RotateBullet();
            BulletTimer.Start();
        }