This site requires JavaScript, please enable it in your browser!
Greenfoot back
hdeforest
hdeforest wrote ...

2013/12/13

End Breakout Game

hdeforest hdeforest

2013/12/13

#
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
1) Please use "" and "" (without spaces or quotes) to place your code in a easier-to-read format. 2) Do you want the game to end as if you lost a life, or as you beat the game? (I haven't read through your code yet to find out) After these questions are answered, I could be able to help.
danpost danpost

2013/12/13

#
I am not sure I understand something: you are calling a method 'RemoveLife' from the ball class, yet I did not see the method there. I did see a 'removeLife' method in the LifeCounter class, but that is not only in a different class, but the case is wrong as well. Another thing that is not easy for me to understand is that you have a 'lives' field in your SubWorld class, a 'lifevalue' field in your Ball class, and both a 'life' and a 'value' field in your LifeCounter class. It must be very hard to keep all those fields up-to-date. I believe they all refer to the same value and only one is needed; it can remain in the LifeCounter class. With your reference to the LifeCounter object in your SubWorld class, the value of the LifeCounter object will be accessible to all the actors in your world.
hdeforest hdeforest

2013/12/16

#
FlyingRabidUnicornPig wrote...
1) Please use "" and "" (without spaces or quotes) to place your code in a easier-to-read format. 2) Do you want the game to end as if you lost a life, or as you beat the game? (I haven't read through your code yet to find out) After these questions are answered, I could be able to help.
We already got it to end the game and to win the game. We are just trying to get the lives to work. Meaning we start at X amount of lives, and every time we leave the bottom of the world, we want it to count down. At zero, we want it to end.
You need to login to post a reply.