This site requires JavaScript, please enable it in your browser!
Greenfoot back
mole2003
mole2003 wrote ...

2017/7/13

Help my pacman game

mole2003 mole2003

2017/7/13

#
I'm trying to make a pacman game but my strawberries (that give you one extral life) don't work. Please help.
 if (Greenfoot.getRandomNumber(1000) < 1)
        {
           getWorld().addObject( new strawberry(), 536, 204); 
           //there is a 0.01% that a strawberry will spawn at 536, 204
        }
        if (isTouching(strawberry.class))
        {
            lives = lives + 1;
            removeTouching(strawberry.class);
            //if pacman touches a strawberry increase his lives by one
        }
        if (lives == 0)
        {
            setImage("pacmandeath1.png");
            Greenfoot.delay(4);
           setImage("pacmandeath2.png");
           Greenfoot.delay(4);
           setImage("pacmandeath3.png");
           Greenfoot.delay(4);
           setImage("pacmandeath4.png");
           Greenfoot.delay(4);
           setImage("pacmandeath5.png");
           Greenfoot.delay(4);
           getWorld().removeObject(this);
           //if lives = 0 then play death animation
        }
        getWorld().showText(Integer.toString(cherrytimer),524, 10);
        //display pacman's lives at 524, 10
Super_Hippo Super_Hippo

2017/7/13

#
What "doesn't work"? It should spawn a strawberry about every 18 minutes (average) if this is in the act method of PacMan.
mole2003 mole2003

2017/7/13

#
when ever pacman eats the strawberry his lives got to 100 wich slowly countdown to 0
Super_Hippo Super_Hippo

2017/7/13

#
Show the entire PacMan class.
mole2003 mole2003

2017/7/13

#
public class pacman extends Actor
{
    private int score = 0;
    //set a score variable and set it to 0
    private int cherrytimer = 0;
    //set a cherrytimer variable and set it to 0
    private int lives = 1;
    //set a lives variable and set it to one
    /**
     * Act - do whatever the pacman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
        if (Greenfoot.isKeyDown("up"))
        {
          setRotation(270);
          move(3); 
          //if the up arrow key is pressed move up
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move(3); 
            //if the down arrow key is pressed move down
        }
        if (Greenfoot.isKeyDown("left"))
        {
             setRotation(180);
              move(3);
              //if the left arrow key is pressed move left
        }
   
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(3); 
            //if the right arrow key is pressed move right
        }
       if (isTouching(wallh.class))
       {
         lives = lives -1;
         
         //if pacman is touching the wall minus one life
        }
       if (isTouching(wallv.class))
       {
         lives = lives - 1;
         
         //if pacman is touching the wall minus one life
        }
        if (isTouching(blinky.class))
        {
           if (cherrytimer == 0)
           {
            lives = lives - 1;
           
           // if pacman is touching blinky and he hasn't eaten a cherry minus one life
          }
          else 
          {
             getWorld().addObject( new blinky(), 570, 207); 
              removeTouching(blinky.class); 
              //remove blinky is pacman has eaten a cherry then respawn him at 570, 207
          }
        }
        if (isTouching(clyde.class))
        {
           if (cherrytimer == 0)
           {
            lives = lives - 1;
           // if pacman is touching clyde and he hasn't eaten a cherry minus one life
          }
          else
          {
              getWorld().addObject( new clyde(), 486, 207); 
              removeTouching(clyde.class);
            //remove clyde is pacman has eaten a cherry then respawn him at 570, 207
          }
        }
       
        if  (isTouching(coin.class))
        {
          removeTouching(coin.class);
          setImage("pacmanclose.png");
          Greenfoot.delay(10);
          setImage("pacmanopen.png");
          score = score + 100; 
          // eat the coin and +100 to the score if pacman eats a coin
        }
        if (score == 7000)
        {
            Greenfoot.stop(); 
            // if all the coins are eaten finish the game
        }
        getWorld().showText(Integer.toString(score), 20, 10);
        //convert the score into text and display it at 20,10
        
        if (Greenfoot.getRandomNumber(100) < 1)
        {
           getWorld().addObject( new cherry(), 536, 204);
           // there is a 1% chance that a cherry will spawn at 536, 204     
        }
        if (isTouching(cherry.class))
        {
           removeTouching(cherry.class); 
           cherrytimer = cherrytimer + 100; 
           // if pacman eats a cherry start a cherrycounydown
        }
        if (cherrytimer > 0)
        {
           cherrytimer = cherrytimer - 1; 
           //countdown the cherrytimer if it is above 0
        }
        getWorld().showText(Integer.toString(cherrytimer),970, 10);
        //display the cherrytimer at 970,10
        if (Greenfoot.getRandomNumber(1000) < 1)
        {
           getWorld().addObject( new strawberry(), 536, 204); 
           //there is a 0.01% that a strawberry will spawn at 536, 204
        }
        if (isTouching(strawberry.class))
        {
            lives = lives + 1;
            removeTouching(strawberry.class);
            //if pacman touches a strawberry increase his lives by one
        }
        if (lives == 0)
        {
            setImage("pacmandeath1.png");
            Greenfoot.delay(4);
           setImage("pacmandeath2.png");
           Greenfoot.delay(4);
           setImage("pacmandeath3.png");
           Greenfoot.delay(4);
           setImage("pacmandeath4.png");
           Greenfoot.delay(4);
           setImage("pacmandeath5.png");
           Greenfoot.delay(4);
           getWorld().removeObject(this);
           //if lives = 0 then play death animation
        }
        getWorld().showText(Integer.toString(cherrytimer),524, 10);
        //display pacman's lives at 524, 10
    }
    
}
Super_Hippo Super_Hippo

2017/7/14

#
I think line 144 is not correct. It does not display the lives variable, it displays the cherrytimer. (You probably copied line 116 and only changed the comment.)
mole2003 mole2003

2017/7/16

#
thank you I'm so stupid.
You need to login to post a reply.