For my Breakout-Game for a schoolproject I have to place a ball, a paddle, to bounce the ball back, and many blocks.
My first problem now is that I cant place the paddle. There isnt an error or something like that. It`s just nothing there. And the ball is every time there, with and without the lines which has to place it.
Maybe you can tell me my mistake
My second problem is that i dont now how to place many objects with one command.
My task here: The area is 640x480. A block is 40x15 and between the block there has to be a distance of 5 pixels. So i can place 16 blocks per line and i want 5 lines with blocks.
How i have to write that ?
The area:
The ball:
public class Ball extends Actor
{
private int dx;
private int dy;
public Ball() {
dx = 10 - Greenfoot.getRandomNumber(21);
dy = -5 - Greenfoot.getRandomNumber(6);
}
/*
* Check the 4 sides
*/
private void pruefeKontaktRandRechts() {
if (getX() >= getWorld().getWidth()-1) {
dx = -dx;
}
}
private void pruefeKontaktRandLinks() {
if (getX() <= 0 ){
dx = -dx;
}
}
private void pruefeKontaktRandUnten() {
if (getY() >= getWorld().getHeight()-1) {
getWorld().removeObject(this);
}
}
private void pruefeKontaktRandOben() {
if(getY() <= 0) {
dy = -dy;
}
}
private void pruefeKontaktPaddel() {
if (getOneIntersectingObject(Paddel.class) !=null){
dy = -dy;
}
}
private void pruefeKontaktBlock(){
Actor block = getOneIntersectingObject(Block.class);
if(block !=null){
dy = -dy;
getWorld().removeObject(block);
}
}
public void act()
{
pruefeKontaktPaddel();
pruefeKontaktBlock();
pruefeKontaktRandRechts();
pruefeKontaktRandLinks();
pruefeKontaktRandUnten();
pruefeKontaktRandOben();
setLocation (getX() + dx, getY() + dy);
pruefeKontaktRandUnten();
}
}
The paddle:
public void act()
{
if(Greenfoot.isKeyDown("right")){
setLocation (getX() + 6 , getY());
}
if(Greenfoot.isKeyDown("left")){
setLocation (getX() - 6 , getY());
}
}
}
And the block hasnt code
(Im sorry for english-language mistakes...)
public class Spielfeld extends World
{
/**
* Constructor for objects of class Spielfeld.
*/
public Spielfeld()
{
super(640, 480, 1);
}
public void act() {
if (Greenfoot.isKeyDown("space") &&
getObjects(Ball.class).size() == 0) {
Ball ball = new Ball ();
addObject(ball, 320, 240);
}
fuellen();
if (Greenfoot.isKeyDown("space") &&
getObjects(Paddel.class).size() == 0){
Paddel paddel = new Paddel();
addObject(paddel,320, 100);
}
}
private void fuellen(){
if (Greenfoot.isKeyDown("space") &&
getObjects(Block.class).size() != 16) {
Block block = new Block ();
addObject(block, 320, 240);
}
}
}
