I am making a breakout game and I cannot figure out how to add Lives to my game. I have tried many different methods to add lives but cannot get it. I have reverted all of my code to before I tried to add lives.
SubWorld-my world class
Ball
I think this is all the code that is needed to make the lives.
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;
/**
* 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();
prepare();
}
public void act()
{
checkLives();
blockCount();
}
private void checkLives()
{
if (lives == 3)
{
removeObject(lave);
}
if (lives == 2)
{
removeObject(love);
}
if (lives == 1)
{
removeObject(live);
Greenfoot.stop();
}
}
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();
lifeLost();
}
public void lifeLost()
{
lives--;
}
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()
{
Live live = new Live();
addObject(live, 30, 484);
live.setLocation(19, 468);
Live live2 = new Live();
addObject(live2, 61, 483);
live2.setLocation(48, 469);
Live live3 = new Live();
addObject(live3, 96, 459);
live3.setLocation(32, 487);
removeObject(live3);
removeObject(live);
removeObject(live2);
}
}
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 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);
}
}
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;
}
}
