import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Paddle here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Paddle extends Animals
{
private Ball myBall;
public void addedToWorld(World world)
{
newBall();
}
/**
* Act - do whatever the Paddle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("left"))
{
moveSideways(-9); // Add your action code here.
}
if (Greenfoot.isKeyDown("right"))
{
moveSideways(9);
}
if (haveBall() && Greenfoot.isKeyDown("space"))
{
releaseBall();
}
}
public void moveSideways(int dist)
{
setLocation (getX() + dist, getY());
if (myBall != null)
{
myBall.move (dist);
}
}
public void newBall()
{
myBall = new Ball(1);
getWorld().addObject (myBall, getX(), getY()-20);
}
public boolean haveBall()
{
return myBall != null;
}
public void releaseBall()
{
myBall.release();
myBall = null;
}
}