Question
I am creating a Brick breaker game in java using the Greenfoot IDE. I need to create a LEVEL1 class that puts the bricks in
I am creating a Brick breaker game in java using the Greenfoot IDE. I need to create a LEVEL1 class that puts the bricks in a rectangle. The outside bricks (green color) should be destroyed with 2 hits while the inside bricks (red) should be destroyed with 1 hit. The brick size is 30 by 30 as well. The code for this should go imnto the Level1 class. The classes are Brick, Ball, Paddle, MyWorld, and Level1. Any help will be greatly appreciated thanks. The classes are posted below.
import greenfoot.*; // public class Level1 extends MyWorld {
/** * Constructor for objects of class Level1. * */ public Level1() { } }
import greenfoot.*;public class MyWorld extends World
{
/**
* 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 Brick(1), 100, 150);
}
}
public class Brick extends Actor { private int health; GreenfootImage image; private int delay; public Brick(int h) { health = h; delay = 25; this.getImage().clear(); image = new GreenfootImage(30, 30); image.fill(); setImage(image); } /** * Act - do whatever the Brick wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (health == 2) { image.setColor(Color.GREEN); } else { image.setColor(Color.RED); } if (getOneIntersectingObject(Ball.class) != null) { if (delay <= 0) { health--; delay = 25; } } delay --; image.fill(); setImage(image); if (health <= 0 && delay <= 0) { getWorld().removeObject(this); } } }
public class Ball extends Actor { private int xMove; private int yMove; private int speed; private int delay; public Ball() { speed = 3; xMove = 0; yMove = speed; delay = 0; } public void checkCollisionWithPlayer() { if (getOneIntersectingObject(paddle.class) != null) { if (Greenfoot.isKeyDown("left")) { xMove = -speed; } else if (Greenfoot.isKeyDown("right")) { xMove = speed; } else { xMove = xMove; } yMove = -speed; } }
public void checkCollisionWithBlock() { if (getOneIntersectingObject(Brick.class) != null) { Brick b = (Brick) getOneIntersectingObject(Brick.class); if (getY() >= b.getY() - 10 && getY() <= b.getY() + 10) { if (xMove == -speed && delay <= 0) { xMove = speed; delay = 25; } else if (xMove == speed && delay <= 0) { xMove = -speed; delay = 25; } } else if(b.getX() - 10 <= getX() && b.getX() + 10 >= getX()) { if (yMove == -speed && delay <= 0) { yMove = speed; delay = 25; } else if (yMove == speed && delay <= 0) { yMove = -speed; delay = 25; } } } delay --; } public void checkOutOfBounds() { if (getX() <= 5 || getX() >= 595) { xMove *= -1; } if (getY() <= 5) { yMove *= -1; } if (getY() >= 395) { Greenfoot.stop(); } } public void act() { setLocation(getX() + xMove, getY() + yMove); checkCollisionWithPlayer(); checkCollisionWithBlock(); checkOutOfBounds(); } }
public class paddle extends Actor { /** * 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() { checkKey(); } public void checkKey() { if (getX() > 50) { if(Greenfoot.isKeyDown("a")) { move(-10); } } if (getX() < 550) { if(Greenfoot.isKeyDown("d")) { move(10); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started