52 lines
1.1 KiB
Java
52 lines
1.1 KiB
Java
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
|
|
|
|
/**
|
|
* Write a description of class Player here.
|
|
*
|
|
* @author (your name)
|
|
* @version (a version number or a date)
|
|
*/
|
|
public class Player extends Actor
|
|
{
|
|
int velocityY = 0;
|
|
int speed = 5;
|
|
public void act()
|
|
{
|
|
player_movement();
|
|
collision();
|
|
}
|
|
|
|
public void player_movement(){
|
|
if (Greenfoot.isKeyDown("D")){
|
|
setLocation(getX()+speed, getY());
|
|
}
|
|
if (Greenfoot.isKeyDown("A")){
|
|
setLocation(getX()-speed, getY());
|
|
}
|
|
}
|
|
|
|
public boolean onGround(){
|
|
int height = getImage().getHeight();
|
|
|
|
Actor floor = getOneObjectAtOffset(
|
|
0,
|
|
height / 2 + 1,
|
|
Ground.class
|
|
);
|
|
|
|
return floor != null;
|
|
}
|
|
|
|
public boolean collision(){
|
|
if (getIntersectingObjects(Projectile.class).size()>0)
|
|
{
|
|
Greenfoot.setWorld(new GameOverWorld());
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|