import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class playerOne here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class playerOne extends Mover
{
/**
* Constructor
*/
public playerOne()
{
this.velocity = 4;
}
/**
* Act - do whatever the playerOne wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys();
}
/**
* Check if a key is pressed
*/
public void checkKeys()
{
// move the player right
if(Greenfoot.isKeyDown("right"))
{
setLocation(getX()+velocity, getY());
}
//move the player left
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX()-velocity, getY());
}
//move the player up
if(Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-velocity);
}
//move the player down
if(Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+velocity);
}
}
}
____________________________________________________
Player Two
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class playerOne here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PlayerTwo extends Mover
{
/**
* Constructor
*/
public PlayerTwo()
{
this.velocity = 4;
}
/**
* Act - do whatever the playerOne wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys();
}
/**
* Check if a key is pressed
*/
public void checkKeys()
{
// move the player right
if(Greenfoot.isKeyDown("d"))
{
setLocation(getX()+velocity, getY());
}
//move the player left
if(Greenfoot.isKeyDown("a"))
{
setLocation(getX()-velocity, getY());
}
//move the player up
if(Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY()-velocity);
}
//move the player down
if(Greenfoot.isKeyDown("s"))
{
setLocation(getX(), getY()+velocity);
}
}
}
