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

2013/12/5

End Breakout Game

hdeforest hdeforest

2013/12/5

#
We are trying to end our game of Breakout. Once we hit all the bricks we want to end the game or have something saying winner on the screen. Not sure where to even start. Once we have added the game over code, and play it, we hit one brick and an error occurs. We need help! Here is our ball code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The ball of the game. It moves and bounces off the walls and the paddle. * * @author mik * @version 1.0 */ 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; // stuck to paddle private Counter counter; private int blocksBroken = 0; private Block block; /** * Act. Move if we're not stuck. */ public void act() { if (!stuck) { move(); makeSmoke(); checkOut(); checkBlock(); } } /** * Move the ball. Then check what we've hit. */ public void move() { setLocation (getX() + deltaX, getY() + deltaY); checkPaddle(); checkWalls(); } /** * Check whether we've hit one of the three walls. Reverse direction if necessary. */ private void checkWalls() { if (getX() == 0 || getX() == getWorld().getWidth()-1) { deltaX = -deltaX; } if (getY() == 0) { deltaY = -deltaY; } } /** * Check whether we're out (bottom of screen). */ private void checkOut() { if (getY() == getWorld().getHeight()-1) { ((Board) getWorld()).ballIsOut(); getWorld().removeObject(this); } } private void checkBlock() { Actor block = getOneIntersectingObject(Block.class); if (block != null) { getWorld().removeObject(block); deltaY = -deltaY; Board myBoard = (Board) getWorld(); myBoard.score(); if (counter.getValue() == 10) { 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; } } } /** * Move the ball a given distance sideways. */ public void move(int dist) { setLocation (getX() + dist, getY()); } /** * Put out a puff of smoke (only on every second call). */ private void makeSmoke() { count--; if (count == 0) { getWorld().addObject ( new Smoke(), getX(), getY()); count = 2; } } /** * Release the ball from the paddle. */ public void release() { deltaX = Greenfoot.getRandomNumber(11) - 5; deltaY = -5; stuck = false; } }
shrucis1 shrucis1

2013/12/6

#
Just from looking at your ball code, I can't definitely tell where your error is. Did you put the game over code inside your world class? If so, could you share that? By the way, how I would do game over code in this application is like this:
if(getObjects(Block.class).size()==0) {
    gameOver();
}
And put that in the world code, or something similar. I'm not sure if getObjects returns null or an empty list when there are no Block.class, you'd have to looking into that, but something to the effect of what I put above would probably work.
danpost danpost

2013/12/6

#
The problem is that the counter declared in your Ball class is never set to anything. Actually, since your the 'score' method in your Board class to change the score, you should also have a 'getScore' method in your Board class to return the current score and use it -- instead of 'if (counter.getValue() == 10)', use 'if (myBoard.getScore() == 10)'.
hdeforest hdeforest

2013/12/6

#
Here is my world class... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * The game board. The board had a paddle that can move. * * @author mik * @version 1.0 */ public class Board extends World { private static final int GAP = 12; private Paddle paddle; private Counter counter; /** * Constructor for objects of class Board. * */ public Board() { super(460, 460, 1); setPaintOrder ( Ball.class, Smoke.class ); counter = new Counter(); addObject (counter, 113, 440); paddle = new Paddle(); addObject ( paddle, getWidth() / 2, getHeight() - 40); createBlocks(); drawRandomCircles(20); prepare(); } public void drawRandomCircles(int howMany) { int i = 0; while (i < howMany) { i = i+1; } } /** * Create the blocks at the top of the world. */ private void createBlocks() { int y = 30; while ( y <= 160) { createRow(y); y = y + 20 + GAP; } } private void createRow(int y) { int x = 50; while ( x < 460 ) { addObject( new Block(), x, y); x = x + 60 + GAP; } } 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() { } }
hdeforest hdeforest

2013/12/6

#
.
You need to login to post a reply.