示例#1
0
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            // create a temporary location of pac-man
            int tempX = player.rect.X;
            int tempY = player.rect.Y;

            int tempX2 = ghosts[0].rect.X;
            int tempY2 = ghosts[0].rect.Y;

            // set pac-man's direction
            if (WDown)
            {
                player.setSpeed(0, -SPEED);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }
            else if (ADown)
            {
                player.setSpeed(-SPEED, 0);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }
            else if (SDown)
            {
                player.setSpeed(0, SPEED);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }
            else if (DDown)
            {
                player.setSpeed(SPEED, 0);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }

            // move pac-man
            player.move();

            foreach (Ghost g in ghosts)
            {
                g.move();
            }

            // check collisions with pellets
            foreach (Pellet p in pellets)
            {
                // check collision
                if (player.Collision(p))
                {
                    removePellets.Add(p);

                    score += p.score;
                }
            }

            // remove all pellets that have to be removed
            foreach (Pellet p in removePellets)
            {
                pellets.Remove(p);
            }

            // check for collision with each wall
            foreach (Wall wall in walls)
            {
                if (player.Collision(wall))
                {
                    player.setPosition(tempX, tempY);
                    collided = true;

                    // end loop
                    break;
                }
                else
                {
                    collided = false;
                }
            }

            // reset pac-man's positon when collided
            if (collided)
            {
                player.setPosition(tempX, tempY);
            }

            // increase counter
            counter++;

            // animation for mouth
            if (counter - 10 > previousCounter && !animate)
            {
                animate = true;
            }
            else if (counter - 20 > previousCounter)
            {
                animate = false;

                previousCounter = counter = 0;
            }

            Refresh();
        }
示例#2
0
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            // create a temporary location of pac-man
            int tempX = player.rect.X;
            int tempY = player.rect.Y;

            // portal code for when he goes
            // through a portal
            if (tempX < -32)
            {
                tempX = 780;
            }
            else if (tempX > 800)
            {
                tempX = 3;
            }

            // set position to temp positions
            player.setPosition(tempX, tempY);

            int tempX2 = ghosts[0].rect.X;
            int tempY2 = ghosts[0].rect.Y;

            // set pac-man's direction
            if (WDown)
            {
                player.setSpeed(0, -SPEED);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }
            else if (ADown)
            {
                player.setSpeed(-SPEED, 0);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }
            else if (SDown)
            {
                player.setSpeed(0, SPEED);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }
            else if (DDown)
            {
                player.setSpeed(SPEED, 0);
                tmpXSpeed = player.getXSpeed();
                tmpYSpeed = player.getYSpeed();
            }

            // move pac-man
            player.move();

            foreach (Ghost g in ghosts)
            {
                g.move();

                if (player.Collision(g) && !g.edible)
                {
                    // play death sound
                    death.Play();

                    Thread.Sleep(2000);

                    // decrease lives
                    player.lives--;

                    // set position back to original
                    player.setPosition(startX, startY);

                    // reset ghosts positions
                    ghosts[0].setPosition(ghostX, ghostY);
                    ghosts[0].setSpeed(GHOST_SPEED, 0);

                    ghosts[1].setPosition(ghostBoxX, ghostBoxY);
                    ghosts[1].setSpeed(GHOST_SPEED, 0);

                    // if there are no more lives
                    // go to namescreen
                    if (player.lives == 0)
                    {
                        // change to the namescreen
                        Form1.ChangeScreen(this, "NameScreen");
                        gameTimer.Enabled = false;
                    }
                }
                else if (player.Collision(g) && g.edible)
                {
                    // when the ghost is eaten,
                    // add the ghosts score and reset everything about that ghost
                    score += g.score;
                    Thread.Sleep(250);

                    g.setPosition(ghostX, ghostY);
                    g.setSpeed(GHOST_SPEED, 0);

                    g.edible = false;
                }

                foreach (Wall wall in walls)
                {
                    if (g.collision(wall))
                    {
                        // run the collision method of the ghost
                        // when it hits a wall
                        g.changeDirection(player, tempX2, tempY2);
                    }
                }

                //edible timer
                if (g.edible == true)
                {
                    edibleCounter++;
                    if (edibleCounter >= 30 * 15)
                    {
                        g.edible      = false;
                        edibleCounter = 0;
                    }
                }
            }

            // check collisions with pellets
            foreach (Pellet p in pellets)
            {
                // check collision
                if (player.Collision(p))
                {
                    removePellets.Add(p);

                    munch.Play();

                    score += p.score;
                }
            }

            // If any power pellets are collided
            // then remove it, add score, and make ghosts edible
            foreach (PowerPellet pp in powerPellets)
            {
                // check collision
                if (player.Collision(pp))
                {
                    removePowerPellets.Add(pp);

                    score += pp.score;

                    foreach (Ghost g in ghosts)
                    {
                        g.edible = true;
                    }
                }
            }

            // remove all pellets that have to be removed
            foreach (Pellet p in removePellets)
            {
                pellets.Remove(p);
            }

            // remove power pellets
            foreach (PowerPellet pp in removePowerPellets)
            {
                powerPellets.Remove(pp);
            }

            // check for collision with each wall
            foreach (Wall wall in walls)
            {
                if (player.Collision(wall))
                {
                    player.setPosition(tempX, tempY);
                    collided = true;

                    // end loop
                    break;
                }
                else
                {
                    collided = false;
                }
            }

            // reset pac-man's positon when collided
            if (collided)
            {
                player.setPosition(tempX, tempY);
            }

            // increase counter
            counter++;

            // animation for mouth
            if (counter - 10 > previousCounter && !animate)
            {
                animate = true;
            }
            else if (counter - 20 > previousCounter)
            {
                animate = false;

                previousCounter = counter = 0;
            }

            // reset level when all pellets are gone
            if (pellets.Count == 0 && powerPellets.Count == 0)
            {
                resetLevel();
            }

            Refresh();
        }