Download a new Greenfoot world here: MazeWorld . Save it in Greenworld/Scenarios. Then you can open it.
Greenfoot has lots of pictures, including backgrounds that look like bricks . You can build brick walls by putting lots of these together.
Once you have some walls, you will want the actors to "bounce" away when they hit the walls. This involves some tricky calculations. The code below does the correct calculations.
void bounce(java.lang.Class block)
{
if (getOneIntersectingObject(block)!=null)
{
Actor ob = getOneIntersectingObject(block);
move(-moveX,-moveY);
int x = getX();
int y = getY();
int bx = ob.getX();
int by = ob.getY();
int dx = x-bx;
int dy = y-by;
int horiz = Math.abs(dx);
int vert = Math.abs(dy);
if (horiz >= vert)
{ moveX = -moveX; }
if(horiz <= vert)
{ moveY = -moveY; }
}
}
You can copy this code into the Girl and Boy actors (and any others that should bounce).
This is a new method. It must be placed after the end of the act method. You must get the brackets correct. Look at this sample to see how to insert in properly : Girl program
The Bounce method tells the actor how to bounce - what to do to bounce
off. But it doesn't actually say that it should bounce. You
also need to write a command inside the act method so that the
actor actually bounces - like this:
public void act()
{
move(moveX,moveY);
bounce(Wall.class); // this is the BOUNCE command
if (getOneIntersectingObject(RedHouse.class)!=null)
{ moveX = 0; moveY = 0;
print("Girl Wins",300,20);
}
}
The bricks picture might be too big for your program. You can use the scale command to change the size. Here is the code for a brick wall that is 50 pixels wide and 40 pixels high:
import greenfoot.*;
public class Wall extends Actor
{
public Wall()
{
getImage().scale(50,40);
}
}
You can make lots of walls by typing lots of addObject commands in the HomeWorld editor, like this:
addObject(new Wall(),100,250);
addObject(new Wall(),150,250);
addObject(new Wall(),200,250);
addObject(new Wall(),250,250);
The walls are placed every 50 pixels, because they are 50 pixels wide.
You can copy and paste the addObject commands to make lots of walls - but there is an easier way. You can write a loop - this means a set of commands that automatically repeat.
First you need a variable for the position of each new wall segment (we abbreviate it as pos).
int pos = 100; // pos is an integer (whole number) starting at 100
Then you make a loop and place tha addObject command inside the loop.
do
{
addObject(new Wall(),pos,250);
Notice that you must use the pos variable (instead of writing a number for the x-position).
Now you must make pos count by 50's :
pos = pos + 50;
Finally, you must tell the loop when it should stop counting:
} while (pos <= 500);
Althogether, it looks like this:
int pos = 100;
do
{
addObject(new Wall(),pos,250);
pos = pos + 50;
} while (pos <= 500);
That creates 9 pieces of brick wall, at 100, 150, 200, 250, 300, 350, 400, 450, 500. It looks something like this:
==> Make a new simulation