This site requires JavaScript, please enable it in your browser!
Greenfoot
Username
Password
Remember Me?
Sign Up, Lost Password
Activity
About
Documentation
Download
Discuss
Scenarios
Discussions
You need to login to take part
Current Discussions
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); } }
Picture from different sides
By martijn13039, with no replies.
how can you make from a picture a front, rear, left and right side I hope somebody has an idea martijn
Source Code Link broken?
By lex404, with 2 replies.
Last reply by davmac, over 13 years ago:
I've fixed the link now, sorry about that.
Using a Kinect with Greenfoot
By mik, with 11 replies.
Last reply by davmac, over 13 years ago:
It's fixed now - sorry for the inconvenience.
BufferedImages and hackyness in Greenfoot
By Builderboy2005, with 4 replies.
Last reply by davmac, over 13 years ago:
Yep, not possible, sorry.
LearningGame help
By chris4800, with 3 replies.
Last reply by rkzkIMP, over 13 years ago:
yanks101 wrote...
chris any interest in helping me out with the 2 labs due tonight, ill pay you.
come to you-know-where-the-linux is. ill help you out with practice 4
Flip a card one at a time and guess
By miko122, with 16 replies.
Last reply by rkzkIMP, over 13 years ago:
yanks101// come to the comp area you can (you kno where) i can definately help you with the practice 4.
HELP ASAp
By yanks101, with 1 reply.
Replied to by tylers, over 13 years ago:
what is it
Resetting the User's Info
By SPower, with 1 reply.
Replied to by SPower, over 13 years ago:
Nevermind: I've got it working.
play wav files in loop...
By Busch2207, with 4 replies.
Last reply by SPower, over 13 years ago:
I also thought there might be a bug doing this with the beta, but I thought that my code was wrong. Sorry for not reporting this :(, next time, I'll do.
How to make actor jump animatedly?JUMP not FLY like penguin tutorial.
By snow13, with no replies.
I need the code when the button is pressed the actor jump and then fall,not fly.
dot notation
By USBest, with 11 replies.
Last reply by davmac, over 13 years ago:
Yes. Furthermore, worrying about performance of different source constructs is, in general, pointless. In general you're not going to see a measurable performance difference without completely changing your algorithm.
Using Scanner class.
By Omniscience, with 10 replies.
Last reply by davmac, over 13 years ago:
Ah snap, forgot to reply! Well, the reason why the code wasn't running was to do with the control flow in the end. It was the rogue code button = player.next(). When I removed it, the code worked again and all the problem was solved.
Great that you've got it solved. I don't think the fix was just to remove 'button = player.next()' though, since if you remove that you're no longer reading anything from the scanner (unless you're doing that earlier).
And I realised, fortunately, that you can just add strings together!
My point about this was that you don't
Maze gen using a 2-D array
By RedPhoneBooth, with 13 replies.
Last reply by RedPhoneBooth, over 13 years ago:
Okay, I got this, lol sorry, I was having trouble understanding all your fancy computer talk :D lol jk, thanks a bunch tho
Equations
By thefutureisnow, with 6 replies.
Last reply by danpost, over 13 years ago:
Actually, you can use those commands anytime, without having to specify 'import java.lang.Math;', being it is part of the java.lang package.
982
983
984
985
986
987
988
X