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

Discussions

You need to login to take part
Rss

Current Discussions

theDoctor

Making the Paddle Smaller

By theDoctor, with no replies.
I'm currently working on a Breakout game and need some help with something. I'm trying to make my Paddle (the object the Ball bounces off of) smaller everytime the Ball hits the bottom edge of the World. I have a set number of Balls (my method is numBalls) and I need to make it say this: "If the number of Balls is greater than 1, set the width of the Paddle to 40". The width of my Paddle at the beginning of the game is 60. I've posted my BreakoutWorld code below. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; import java.util.List; /** * Holds a game of breakout. In breakout there are 10 rows of bricks that the user * must get rid of to win. To get rid of bricks hit them with the ball. Use the paddle * to hit the ball. Use the left and right arrow keys to move the paddle. The user has * 3 balls to use to try to win the game and if the last ball gets past the paddle the user * loses. * * This game is based on Breakout by Eric Roberts who presented it as a nifty assignment * at SIGCSE in 2006 using the Java Task Force libraries. * * @author Barbara Ericson, Georgia Tech * @version 1.0, April 6, 2007 */ public class BreakoutWorld extends World { /** the width of the bricks in pixels (cells) */ public static final int BRICK_WIDTH = 36; /** the height of the bricks in pixels (cells) */ public static final int BRICK_HEIGHT = 8; /** the distance between bricks in pixels (cells) */ public static final int BRICK_SEP = 4; /** the number of bricks per row */ public static final int NUM_BRICKS_PER_ROW = 10; /** distance from the top edge in pixels (cells) */ public static final int BRICK_Y_OFFSET = 50; /** the number of pixels per cell */ public static final int RESOLUTION = 1; /** world width in pixels (cells) */ public static final int WIDTH = (BRICK_WIDTH + BRICK_SEP) * NUM_BRICKS_PER_ROW + BRICK_SEP; /** world height in pixels (cells) */ public static final int HEIGHT = 500; /** number of rows of bricks */ public static final int NUM_ROWS = 10; /** the colors to use for each row of bricks */ public static final Color colorArray = {Color.BLACK, Color.BLACK, Color.ORANGE, Color.ORANGE, Color.RED, Color.RED, Color.GREEN, Color.GREEN, Color.BLUE, Color.BLUE}; /** the number of balls created in the game so far */ private int numBalls = 0; /** a message displayed for the user */ private Message message = null; /** * No argument constructor */ public BreakoutWorld() { super (WIDTH, HEIGHT, RESOLUTION); setUpBreakout(); //createBrightStars(150); //createColorStars(150); } /** * Method to get the number of balls created * @return the number of balls created in the game */ public int getNumBalls() { return numBalls; } /** * Method to add a new ball */ public void newBall() { /* increment the number of balls created */ numBalls++; /* check if used 3 or more */ if (numBalls > 3) { message.setText("Game over! You fail. LOL!"); addObject(new LOLGuy(), 200, 310); Greenfoot.stop(); } /* create new ball and tell the user the number of balls created */ else { addObject(new Ball(), (WIDTH / 2), 222); message.setText("Try " + numBalls); } } /** * Method to check if the game is over and if so tell the user * and stop the simulation */ public void checkIfWon() { List brickList = this.getObjects(Brick.class); if (brickList.size() == 0) { message.setText("You didn't fail! Congratulations!"); Greenfoot.stop(); } } /** * Method to set up the breakout game */ public void setUpBreakout() { /* update image */ updateImage(); /* set up the message */ message = new Message("Creating the game... patience is a virtue."); addObject(message,WIDTH/2, 35); /* set up the bricks */ setUpBricks(); /* create the paddle */ addObject(new Paddle(),(WIDTH / 2),HEIGHT - 30); /* create a new ball */ newBall(); } /* * Method to set up the bricks in the world */ private void setUpBricks() { int yPos = BRICK_HEIGHT / 2 + BRICK_Y_OFFSET; int halfWidth = BRICK_WIDTH / 2; // addObject(new Brick(Color.RED),4,0); for (int row = 0; row < NUM_ROWS; row++, yPos = yPos + BRICK_HEIGHT + BRICK_SEP) { for (int col = 0, xPos = BRICK_SEP + halfWidth; col < NUM_BRICKS_PER_ROW; col++, xPos = xPos + BRICK_WIDTH + BRICK_SEP) { addObject(new Brick(colorArray),xPos,yPos); } } } /** * Method to create and set the image for the background */ public void updateImage() { GreenfootImage image = new GreenfootImage(WIDTH,HEIGHT); image.setColor(Color.WHITE); image.fillRect(0,0,WIDTH,HEIGHT); setBackground(image); } /** * Creates stars of random brightness to be displayed on the background image. * The parameter shows the number of stars on the background image. */ public void createBrightStars(int number) { GreenfootImage background = getBackground(); for(int i = 0; i < number; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); GreenfootImage image = new GreenfootImage(4, 4); background.setColor(new Color(200, 200, 200, Greenfoot.getRandomNumber(256))); background.fillOval(Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()), 4, 4); } } /** * Creates stars of random color to be displayed on the background image. * The parameter shows the number of stars on the background image. */ public void createColorStars(int number) { GreenfootImage background = getBackground(); for(int i = 0; i < number; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); GreenfootImage image = new GreenfootImage(4, 4); background.setColor(new Color(Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256))); background.fillOval(Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()), 4, 4); } } } And here's my Paddle class code: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; /** * A paddle is a rectangular object that can be moved via the left * and right arrow keys. * * @author Barbara Ericson, Georgia Tech * @version 1.0 April 6, 2007 */ public class Paddle extends Actor { /** Width of the paddle */ private int width = 60; /** Height of the paddle */ private int height = 10; /** amount to move */ private int moveAmount = 5; /** color of this paddle */ private Color color = Color.BLACK; /** * No argument constructor */ public Paddle() { updateImage(); } /** * Constructor that takes the width, height, color, and moveAmount * @param theWidth the width to use * @param theHeight the height to use * @param theColor the color to use * @param theAmount the number of cells (pixels) to move */ public Paddle(int theWidth, int theHeight, Color theColor, int theAmount) { width = theWidth; height = theHeight; color = theColor; moveAmount = theAmount; updateImage(); } /** * Act - a paddle will move in reaction to a left or right * arrow key being pressed */ public void act() { if(Greenfoot.isKeyDown("left")) { setLocation(getX()-moveAmount, getY()); } if(Greenfoot.isKeyDown("right")) { setLocation(getX()+moveAmount, getY()); } } /** * Method to create and set the image for this paddle. Invoke * this method again when the width, height, or color change. */ public void updateImage() { GreenfootImage image = new GreenfootImage(width, height); image.setColor(color); image.fillRect(0, 0, width, height); setImage(image); } }