Okay, so a while ago I posted asking for a code for infinite loop. So how do I alter that code so that every time it resets, the ball is smaller?
Here's my code:
Board:
Ball:
Paddle
BonusBlock
and the regular Block:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.util.List; /** * Write a description of class Board here. * * @author (your name) * @version (a version number or a date) */ public class Board extends World { private static final int GAP = 12; private Paddle paddle; private Block block; public Counter counter; public int totalblocks = 0; /** * Constructor for objects of class Board. * */ public Board() { super(460, 460, 1); counter = new Counter(); addObject (counter, 113,400); setPaintOrder (Counter.class, Ball.class, Smoke.class); paddle = new Paddle(); addObject (paddle, getWidth() / 2, getHeight() - 40); createCircles(); createBlocks(); prepare(); } public void ballIsOut() { paddle.newBall(); } public void score() { counter.addScore(); } public void score(int x) { counter.addScore(x); } private void createBlocks() { int y = 30; int x = 50; while ( y <= 94) { createRow(y); y = y + 20 + GAP; } } private void createBonusBlocks() { int y = 30; int x = 50; while ( y <= 94) { createRow(y); y = y + 20 + GAP; } } private void createRow(int y) { int x = 50; while (x < 460) { addObject ( new Block(counter), x, y); totalblocks++; x = x + 60 + GAP; } } public void drawCircle() { GreenfootImage image = getBackground(); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); int r = Greenfoot.getRandomNumber(256); int g = Greenfoot.getRandomNumber(256); int b = Greenfoot.getRandomNumber(256); int size = Greenfoot.getRandomNumber(100) +50; Color color = new Color(r,g,b,20); image.setColor (color); image.fillOval(x,y,size,size); } private void createCircles() { for (int x = 0; x< 50; x++) { drawCircle(); } } public void act() { if (getObjects(Block.class).isEmpty()) { createBlocks(); // add new blocks createBonusBlocks(); } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { BonusBlock bonusblock = new BonusBlock(); addObject(bonusblock, 94, 175); BonusBlock bonusblock2 = new BonusBlock(); addObject(bonusblock2, 320, 179); removeObject(bonusblock2); removeObject(bonusblock); } public boolean isLevelCompleted(){ List<Block> blocks = getObjects(Block.class); if (blocks.size() == 0){ return true; } return false; } public void gameOver() { Greenfoot.stop(); } }
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; private int deltaY; private int count = 2; private boolean stuck = true; private Counter counter; /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Ball() { } public Ball(Counter newCounter) { counter = newCounter; } public void move(int dist) { setLocation (getX() + dist, getY()); } 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; } } } private void checkWalls() { if (getX() == 0 || getX() == getWorld().getWidth()-1) { deltaX = -deltaX; } if (getY() == 0) { deltaY = -deltaY; } } public void move () { setLocation (getX() + deltaX, getY() + deltaY); checkPaddle(); checkWalls(); } private void checkOut() { if (getY() == getWorld().getHeight()-1) { ((Board) getWorld()). ballIsOut(); getWorld().removeObject(this); } } private void makeSmoke() { count--; if (count == 0) { getWorld().addObject (new Smoke(), getX(), getY()); count = 2; } } public void act() { if (!stuck) { move(); makeSmoke(); checkBlock(); //checkBonusBlock(); checkOut(); } } public void release () { deltaX = Greenfoot.getRandomNumber(11) - 5; deltaY = -5; stuck = false; } private void checkBlock() { Actor block = getOneIntersectingObject(Block.class); if (block != null) { if (block instanceof BonusBlock) checkBonusBlock(); else { getWorld().removeObject(block); deltaY = -deltaY; Board myBoard = (Board)getWorld(); myBoard.score(); } } } private void checkBonusBlock() { Actor block = getOneIntersectingObject(BonusBlock.class); if (block != null) { getWorld().removeObject(block); deltaY = -deltaY; Board myBoard = (Board)getWorld(); myBoard.score(5); } } }
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; /** * Act - do whatever the Paddle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown ("left")) { moveSideways(-9); } if (Greenfoot.isKeyDown ("right")) { moveSideways(9); } if (haveBall() && Greenfoot.isKeyDown ("space")) { releaseBall(); } } public void newBall() { myBall = new Ball(); getWorld().addObject (myBall, getX(), getY()-20); } public void addedToWorld(World world) { newBall(); } private void moveSideways(int dist) { setLocation (getX() + dist, getY()); if (myBall != null) { myBall.move (dist); } } public void releaseBall() { myBall.release(); myBall = null; } public boolean haveBall() { return myBall != null; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class BonusBlock here. * * @author (your name) * @version (a version number or a date) */ public class BonusBlock extends Block { public BonusBlock() {} /** * Act - do whatever the BonusBlock wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Block here. * * @author (your name) * @version (a version number or a date) */ public class Block extends Actor { private Counter counter; public Block() {} public Block(Counter count) { counter = count; } /** * Act - do whatever the Block wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }