Hi, I was working on a Greenfoot project, and my first step was creating two boats with cannonballs that can hit each other. For some reason, my program will not work. I created a LeftBoat and a RightBoat with Cannonball (for the LeftBoat) and RightCannonball subclasses. I will post the code for my LeftBoat, Cannonball, and World classes. Thank you so much!
public class Cannonball extends Actor
{
private int life = 1000;
/**
* Act - do whatever the Cannonball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(5);
life--;
if (getX()==0 || getX() == getWorld().getWidth()-1)
{
setRotation(180-getRotation());
}
if (getY() == 0 || getY() == getWorld().getHeight()-1)
{
setRotation(360-getRotation());
}
if(life == 0)
{
getWorld().removeObject(this);
}
}
} public void act()
{
checkKeys();
if(Greenfoot.isKeyDown("s"))
{
turn(-3);
}
if( Greenfoot.isKeyDown("f"))
{
turn(3);
}
if( Greenfoot.isKeyDown("e"))
{
move(3);
}
if( Greenfoot.isKeyDown("d"))
{
move(-3);
}
}
private void checkKeys()
{
String key = Greenfoot.getKey();
if ("q".equals(key))
{
shoot();
}
}
public void shoot()
{
Cannonball rCannonball = new Cannonball();
getWorld().addObject(rCannonball, getX(), getY());
rCannonball.setRotation(getRotation());
}
}
public class Ocean extends World
{
public static int leftWins = 0;
public static int rightWins = 0;
/**
* Constructor for objects of class Ocean.
*
*/
public Ocean()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
RightBoat rboat = new RightBoat();
addObject(rboat, 598, 398);
LeftBoat lboat = new LeftBoat();
addObject(lboat, 2, 2);
}
}
