Here is some code to create a maze:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bath here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bath extends World
{
Counter counter = new Counter("Score: ");
/**
* Constructor for objects of class bath.
*
*/
public bath()
{
// Create a new world with 20x20 cells with a cell size of 10x10 pixels.
super(8, 8, 60);
setPaintOrder(ScoreBoard.class, Counter.class, bug.class, rock.class, food.class);
populate();
}
private void populate()
{
addObject(new rock(), 4, 3);
addObject(new rock(), 1, 1);
addObject(new rock(), 1, 2);
addObject(new rock(), 1, 3);
addObject(new rock(), 1, 5);
addObject(new rock(), 2, 1);
addObject(new rock(), 2, 2);
addObject(new rock(), 1, 1);
addObject(new rock(), 1, 2);
addObject(new rock(), 1, 3);
addObject(new rock(), 1, 5);
addObject(new rock(), 2, 1);
addObject(new food(), 1, 4);
addObject(new bug(), 0,0);
addObject(counter, 1, 7);
}
public void countFood()
{
counter.add(1);
}
public void gameOver()
{
addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
Greenfoot.playSound("buzz.wav");
Greenfoot.stop();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Font;
/**
* Counter that displays a number for maze
*
* @author Michael Kolling
* @version 1.0.1
*/
public class Counter extends Actor
{
private int value = 0;
private int target = 0;
private String text;
private int stringLength;
public Counter()
{
this("");
}
public Counter(String prefix)
{
text = prefix;
stringLength = (text.length() + 2) * 16;
setImage(new GreenfootImage(stringLength, 24));
GreenfootImage image = getImage();
Font font = image.getFont();
image.setFont(font.deriveFont(24.0F)); // use larger font
updateImage();
}
public void act() {
if(value < target) {
value++;
updateImage();
}
else if(value > target) {
value--;
updateImage();
}
}
public void add(int score)
{
target += score;
}
public void subtract(int score)
{
target -= score;
}
public int getValue()
{
return value;
}
/**
* Make the image
*/
private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 18);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
* Write a description of class bug here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bug extends Mover
{
/**
* Act - do whatever the bug wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private static final int EAST = 0;
private static final int WEST = 1;
private static final int NORTH = 2;
private static final int SOUTH = 3;
private int direction;
private int foodEaten;
public bug()
{
foodEaten = 0;
setDirection (EAST);
}
public void act()
{
if (Greenfoot.isKeyDown("right"))
{
if (direction != EAST) {setDirection(EAST);}
if( canMove()) {move();
}
}
if (Greenfoot.isKeyDown("left"))
{
if (direction != WEST) {setDirection(WEST);}
if( canMove()) {move();
}
}
if (Greenfoot.isKeyDown("up"))
{
if (direction != NORTH) {setDirection(NORTH);}
if( canMove()) {move();
}
}
if (Greenfoot.isKeyDown("down"))
{
if (direction != SOUTH) {setDirection(SOUTH);}
if( canMove()) {move();
}
}
if(foundFood()) {
eatFood();
}
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean canMove()
{
World myWorld = getWorld();
int x = getX();
int y = getY();
boolean blocked=false;
this.direction = direction;
switch (direction) {
case NORTH :
Actor rocks1 = getOneObjectAtOffset(0, -1, rock.class);
if(rocks1 != null ) {
blocked = true;}
break;
case SOUTH :
Actor rocks2 = getOneObjectAtOffset(0, 1, rock.class);
if(rocks2 != null ) {
blocked = true;}
break;
case EAST :
Actor rocks3 = getOneObjectAtOffset(1, 0, rock.class);
if(rocks3 != null ) {
blocked = true;}
break;
case WEST :
Actor rocks4 = getOneObjectAtOffset(-1, 0, rock.class);
if(rocks4 != null ) {
blocked = true;}
break;
}
if(blocked == true) {
return false;
}
else {
return true;
}
}
public void move()
{
if (!canMove()) {
return;
}
switch(direction) {
case SOUTH :
setLocation(getX(), getY() + 1);
break;
case EAST :
setLocation(getX() + 1, getY());
break;
case NORTH :
setLocation(getX(), getY() - 1);
break;
case WEST :
setLocation(getX() - 1, getY());
break;
}
}
public boolean foundFood()
{
Actor food = getOneObjectAtOffset(0, 0, food.class);
if(food != null) {
return true;
}
else {
return false;
}
}
/**
* Eat a leaf.
*/
public void eatFood()
{
Actor food = getOneObjectAtOffset(0, 0, food.class);
if(food != null) {
// eat the leaf...
((bath) getWorld()).countFood();
getWorld().removeObject(food);
foodEaten = foodEaten + 1;
}
}
/**
* Sets the direction we're facing.
*/
public void setDirection(int direction)
{
this.direction = direction;
switch(direction) {
case EAST :
setRotation(0);
break;
case SOUTH :
setRotation(90);
break;
case NORTH :
setRotation(270);
break;
case WEST :
setRotation(180);
break;
default :
break;
}
}
public int getFoodEaten()
{
return foodEaten;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class rock here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class rock extends Actor
{
/**
* Act - do whatever the rock wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public rock()
{
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class food here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class food extends Actor
{
/**
* Act - do whatever the food wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
/**
* The ScoreBoard is used to display results on the screen. It can display some
* text and several numbers.
*
* @author M Kolling
* @version 1.0
*/
public class ScoreBoard extends Actor
{
public static final float FONT_SIZE = 48.0f;
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
/**
* Create a score board with dummy result for testing.
*/
public ScoreBoard()
{
this(100);
}
/**
* Create a score board for the final result.
*/
public ScoreBoard(int score)
{
makeImage("Game Over", "Score: ", score);
}
/**
* Make the score board image.
*/
private void makeImage(String title, String prefix, int score)
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(new Color(0, 0, 0, 160));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(255, 255, 255, 100));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(FONT_SIZE);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
}