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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 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 ; } } |