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

2019/3/16

How do you restart the level when the player touches the ground?

MickeyMouse123 MickeyMouse123

2019/3/16

#
Hi, I am new to Greenfoot and I am creating a platform game. I want the level to restart when my player falls down onto the ground. I tried to do this by adding in the code private boolean atBottom() and private void gameRestart(). When the player touches the ground, nothing happens except when the player touches a specific point, then it restarts. I think the problem is in the private boolean atBottom() but I don't know what I am doing wrong. This is my code for my player and please note, onGround refers to the platform.
public class Player extends Actor
{
    
    private int vSpeed = 0;                  //How fast the player falls down. Vertical speed.
    private int acceleration = 2;           //Acceleration when falling
    private boolean jumping;            //Keeps track of whether the player is jumping or not
    private int jumpStrength = 20;         //How high the player jumps
    private int speed = 4;         //How fast the player will move around
        
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();            //Plays a loop. Checks for ground...
        checkKey();               //Ables basic movement
        platformAbove();            // Stops the player from jumping through a platform
        eat();               //"Eats" the points
        
    }    
    
    public void checkKey()                                  //Check the keyboard
    {
        if(Greenfoot.isKeyDown("up") && jumping == false)               // When the up key is pressed the player will jump
        {
            jump();
        }
        if(Greenfoot.isKeyDown("right"))               //Press the right key to go right
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("left"))                 // Press the left key to go left
        {
            moveLeft();
        }
    }
    
    public void moveRight()                             //make the player move right
    {
        setLocation(getX()+speed, getY());                 //Where he is on the screen with the y co-ordinate
    }
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());                     // Same as above except goes in different direction
    }
    
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);                  //Checks if player is on the ground
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;                    // falls as fast as the speed combined with the acceleration
            if (atBottom())
                gameRestart();
        }
        jumping = true;                               // Will stop the player from jumping while falling   
    }
    
    private boolean atBottom()                         //This method will decide if Player has fallen
    {
        return getY() >= getWorld().getHeight()-2;
        
    }
    
    private void gameRestart()                       //Restarts the world
    {
        Greenfoot.setWorld(new JurrasicPark());
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage(). getHeight();                      //Finds out how big the player is
        int lookForGround = (int)(spriteHeight/2) + 5;                  //Looks for ground 5 by 5 pixels under the player
        
        Actor ground = getOneObjectAtOffset(0, lookForGround, Platform.class);
        if(ground == null)            // If there is no ground...
        {
            jumping = true;
            return false;                //Keeps on falling
        }
        else
        {
            moveToGround(ground);               //Moves to the top of the platform
            return true;
        }
    }
    
    public boolean platformAbove()
    {
        int spriteHeight = getImage(). getHeight();                //Finds out how big the player is
        int yDistance = (int)(spriteHeight/ -2);                         //Divide by a negative number so it looks above not below the player

        Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class);               //Looks for y distance above 
        if(ceiling != null)              // If it is not null...
        {
            vSpeed =1;                  //Make the player go down
            bumpHead(ceiling); 
            return false;                //Keeps on falling
        }
        else
        {
            return true;
        }
    }
    
    public void bumpHead(Actor ceiling)                 //Stops the player from jumping through a platform
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
    }
        
    public void moveToGround(Actor ground)                     //Makes the player touch the platform on the surface
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
        jumping = false;                  // Not jumping and touching ground so that he is ready to jump
    }
    
    public void checkFall()           // This codes checks whether the player is on the ground or not
                                                  // If it is then it makes the vertical speed 0 which stops the player from falling. If not then continues to fall
    { 
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;           //Puts the player in the air
        jumping = true;            //Checks whether the player is jumping or not
        fall();        // Makes the player fall after it has jumped
    }
    
    public void eat()
    {
        Actor point;
        point = getOneObjectAtOffset(0, 0, Point.class);
        if (point != null)
        {
            World world;
            world = getWorld();
            world.removeObject(point);
        }
    }
}

danpost danpost

2019/3/16

#
Maybe the falling speed was greater than 9 when it hit the ground.
MickeyMouse123 MickeyMouse123

2019/3/17

#
Thank you very much it works now! I changed line 51 to:
if(vSpeed <=30)
danpost danpost

2019/3/17

#
MickeyMouse123 wrote...
Thank you very much it works now! I changed line 51 to: << Code Omitted >>
I do not know why that condition is there at all. You could (or should) just remove lines 51, 52 and 56.
MickeyMouse123 MickeyMouse123

2019/3/17

#
Yep, I did that. But I keep falling through platforms from a great height. I know that the direction of movement will determine whether the player should be placed above or below the object it collided with but I don't know how I can alter my original code.
danpost danpost

2019/3/17

#
MickeyMouse123 wrote...
Yep, I did that. But I keep falling through platforms from a great height. I know that the direction of movement will determine whether the player should be placed above or below the object it collided with but I don't know how I can alter my original code.
Line 76 ;should probably use the getOneIntersectingObject method instead of the getOneObjectAtOffset one. You can add another condition to line 77 such that the vertical speed must be positive.
You need to login to post a reply.