We are trying to end a Breakout game, but we can't get it. Here is our code for the subclass, the ball, and the life counter. Can someone help? If you need something else, please let us know.
***SubWorld***
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SubWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SubWorld extends World
{
private static final int GAP = 15;
public int lives = 4;
private Paddle paddle;
private Counter counter;
public LifeCounter life;
/**
* Constructor for objects of class SubWorld.
*
*/
public SubWorld()
{
super(460, 500, 1);
counter = new Counter();
addObject (counter, 113, 440);
paddle = new Paddle();
addObject ( paddle, getWidth() / 2, getHeight() - 40);
createBlocks();
lifeCounter = new LifeCounter();
addObject(lifeCounter, 113, 470);
prepare();
}
public void act()
{
blockCount();
}
public LifeCounter getCounter()
{
return life;
}
private void createBlocks()
{
int x;
int y = 30;
while( y <= 120 )
{ x = 45;
while( x < 460 )
{
addObject( new Block(), x, y);
x = x + 60 + GAP;
}
y = y + 20 + GAP;
}
}
public void blockCount()
{
if(getObjects(Block.class).size()==0) {
Greenfoot.setWorld(new YouWin());
}
}
public void gameOver()
{
Greenfoot.stop();
}
public void ballIsOut()
{
paddle.newBall();
}
public void score()
{
counter.addScore();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
}
}
***Ball***
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends Actor
{
private int deltaX; // x movement speed
private int deltaY; // y movement speed
private int count = 2;
private boolean stuck = true;
private Counter counter;
public int lifevalue = 3;
public LifeCounter life;
public Ball()
{
}
/**
* Act - do whatever the Ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (!stuck)
{
move();
makeSmoke();
checkOut();
checkBlock();
}
}
public void move()
{
setLocation (getX() + deltaX, getY() + deltaY);
checkPaddle();
checkWalls();
}
private void checkWalls()
{
if (getX() == 0 || getX() == getWorld().getWidth()-1) {
deltaX = -deltaX;
}
if (getY() == 0 || getY() == getWorld().getHeight()-1) {
deltaY = -deltaY;
}
}
private void checkOut()
{
if (getY() == getWorld().getHeight()-1) {
((SubWorld) getWorld()).ballIsOut();
getWorld().removeObject(this);
RemoveLife();
}
}
private void checkBlock()
{
Actor block = getOneIntersectingObject(Block.class);
if (block != null)
{
getWorld().removeObject(block);
SubWorld myBoard = (SubWorld) getWorld();
myBoard.score();
deltaY = -deltaY;
}
}
public void gameOver()
{
Greenfoot.stop();
}
private void checkPaddle()
{
Actor paddle = getOneIntersectingObject(Paddle.class);
if (paddle != null) {
deltaY = -deltaY;
int offset = getX() - paddle.getX();
deltaX = deltaX + (offset/10);
if (deltaX > 7) {
deltaX = 7;
}
if (deltaX < -7) {
deltaX = -7;
}
}
}
public void move(int dist)
{
setLocation (getX() + dist, getY());
}
private void makeSmoke()
{
count--;
if (count == 0) {
getWorld().addObject ( new Smoke(), getX(), getY());
count = 2;
}
}
public void release()
{
deltaX = Greenfoot.getRandomNumber(11) - 5;
deltaY = -5;
stuck = false;
}
}
***LifeCounter***
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LifeCounter extends Actor
{
private int life;
private int value;
public LifeCounter()
{
life = 3;
setImage (new GreenfootImage(200, 30));
updateLife();
}
public int getValue()
{
return value;
}
public void removeLife()
{
life--;
updateLife();
}
private void updateLife()
{
GreenfootImage img = getImage();
img.clear();
img.setColor(Color.BLUE);
img.setFont(img.getFont().deriveFont(Font.BOLD));
img.setFont(img.getFont().deriveFont(16.0F));
img.drawString("Life: " + life, 4, 20);
}
}
***Paddle***
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 Actor
{
private Ball myBall;
public Paddle()
{
}
public void addedToWorld(World world)
{
newBall();
}
public void act()
{
if (Greenfoot.isKeyDown("left"))
{
move(-9);
}
if (Greenfoot.isKeyDown("right"))
{
move(9);
}
if (haveBall() && Greenfoot.isKeyDown ("space")) {
releaseBall();
}
}
private void moveSideways(int dist)
{
setLocation (getX() + dist, getY());
if (myBall != null) {
myBall.move (dist);
}
}
public void newBall()
{
myBall = new Ball();
getWorld().addObject (myBall, getX(), getY()-26);
}
public boolean haveBall()
{
return myBall != null;
}
public void releaseBall()
{
myBall.release();
myBall = null;
}
}
Thanks in advanc