public class Player extends Charcters
{
private int speed = 7;
private int vSpeed = 0;
private int acceleration = 1;
private int jumpStrength = 7;
public void act()
{
controls();
checkFall();
}
public void controls()
{
if(Greenfoot.isKeyDown("left"))
{
moveLeft();
}
if(Greenfoot.isKeyDown("right"))
{
moveRight();
}
if(Greenfoot.isKeyDown("space"))
{
jump();
}
if(Greenfoot.isKeyDown("up"))
{
jump();
}
}
public void moveLeft()
{
setLocation(getX() - speed, getY());
}
public void moveRight()
{
setLocation(getX() + speed, getY());
}
public void jump()
{
vSpeed = -jumpStrength;
fall();
}
public void checkFall()
{
if(onGround()) {
vSpeed = 0;
}
else {
fall();
}
}
public boolean onGround()
{
Actor under = getOneObjectAtOffset (0, getImage().getHeight() / 2, Ground.class);
return under != null;
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
}

