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

2022/6/24

BrickBreaker Game

amberlin24 amberlin24

2022/6/24

#
Hey! I am currently coding a simple brick breaker game in Greenfoot and I am struggling with getting the ball to reappear on the screen after falling off the world at the bottom. I want the ball to reappear twice, once after each death (the player has 3 lives). Can you help me figure this out? Here is my code for the Ball: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Ball is the spherical object in which the player controlling the paddle will be able to deflect the ball off their paddle to * destroy bricks. * * @AmberLinLocasto * May 25th 2022 */ public class Ball extends Actor { int xMoveVal = 3; int yMoveVal = 3; int lives = 3; /** * Act - Ball will bounce off paddle and bricks in order to destroy bricks. */ public void act() { setLocation(getX() - xMoveVal, getY() - yMoveVal); if (getX() < 3 || getX() > 595) { xMoveVal = xMoveVal * -1; }// end if (to make ball bounce off left wall upwards and bounce off right wall.) if (getY() < 3 ) { yMoveVal = yMoveVal * -1; }// end if (to make ball bounce off left wall as well as the top wall,) Paddle p = (Paddle) getOneIntersectingObject(Paddle.class); if (p != null) { yMoveVal = Math.abs(yMoveVal); }// end if (to make ball bounce off paddle) Brick b = (Brick) getOneIntersectingObject(Brick.class); if (b != null) { yMoveVal = yMoveVal * -1; getWorld().removeObject(b); }// end if (remove the brick after the ball hits it), first line is to make ball bounce verticle. if (getY() > 395) { getWorld().removeObject(this); //to remove ball from sceen when goes through bottom. MyWorld.playerLifeCount.add(-1); }// end if }// end act method public Ball () { getImage().scale(30, 30); }// make the ball smaller }// end class Ball Here is my code for MyWorld is: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * MyWorld is the world in which the simple brick breaker game will be played. * * @amberlocasto * @May 25 2022 */ public class MyWorld extends World { public static Counter playerLifeCount = new Counter(); private boolean gameStarted = false; public static int Counter = 3; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new Paddle(), 300, 350); addObject(new Ball(), 300, 300); addObject(new Brick(), 545, 30); addObject(new Brick(), 445, 30); addObject(new Brick(), 345, 30); addObject(new Brick(), 245, 30); addObject(new Brick(), 145, 30); addObject(new Brick(), 45, 30); addObject(new Brick(), 545, 70); addObject(new Brick(), 445, 70); addObject(new Brick(), 345, 70); addObject(new Brick(), 245, 70); addObject(new Brick(), 145, 70); addObject(new Brick(), 45, 70); addObject(new Brick(), 545, 110); addObject(new Brick(), 445, 110); addObject(new Brick(), 345, 110); addObject(new Brick(), 245, 110); addObject(new Brick(), 145, 110); addObject(new Brick(), 45, 110); addObject(playerLifeCount, 565, 360); playerLifeCount.setValue(3); // add a life counter, everytime the ball goes passed the paddle, the player loses a life. }// end constructor public void act() { while (!gameStarted) { if (Greenfoot.isKeyDown("space")) { gameStarted = true; }// end if... Saying if space bar is down, game will start/ }// end while }// end method act }// end class // end class MyWorld Here is my counter code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A Counter class that allows you to display a numerical value of lives left for the player on screen. * * * @Amber Lin Locasto * @May 26 2022 */ public class Counter extends Actor { private static final Color transparent = new Color(0,0,0,0); private GreenfootImage background; private int value; private int target; private String prefix; public Counter() { this(new String()); } public Counter(String prefix) { background = getImage(); // got image from class value = 0; target = 0; this.prefix = prefix; updateImage(); }// Create a new counter, initialised to 0. public void act() { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); }// Animate the counter to count up/down to resemble the current life value. }// adding and subtracting lives as the game moves on. public void add(int score) { target += score; }// Add a new score to the current counter value. This will animate the counter as the frame moves along public int getValue() { return target; }// Return the current counter value. public void setValue(int newValue) { target = newValue; value = newValue; updateImage(); }// Set a new counter value. This will not animate the counter. public void setPrefix(String prefix) { this.prefix = prefix; updateImage(); }// Sets a text prefix that should be displayed before the counter value (e.g. "Score: "). private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent); if (text.getWidth() > image.getWidth() - 20) { image.scale(text.getWidth() + 20, image.getHeight()); }// end if image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); }// Update the image on screen to show the current value. }// end class Counter
RcCookie RcCookie

2022/6/24

#
You don’t even need to remove the ball, instead put it back to the default position and reset the velocity.
You need to login to post a reply.