java.lang.Objectgreenfoot.World
KarelWorld
public final class KarelWorld
The KarelWorld contains robots, beepers, and walls. It is a grid of streets (horizontal) and avenues (vertical). The standard world has 20 streets and 20 avenues.
First street is at the bottom and the numbers increase as you go up. First avenue is at the left and the numbers increase as you go to the right.
Robots and beepers are at the intersections. Horizontal walls cross avenues between streets. Vertical walls cross streets between avenues.
This class is final so no subclasses are possible.
The explosion sound file came from code written by Poul Henriksen
The other sounds are from the Scratch system: Copyright (c) 2007 Massachusetts Institute of Technology and used under their permissive license.
| Nested Class Summary | |
|---|---|
static class |
KarelWorld.Direction
Directions in the Karel world. |
| Field Summary | |
|---|---|
static KarelWorld.Direction |
EAST
The east direction in the world |
static int |
INFINITE
Value for beepers that represents an infinite number of beepers on a corner or in a beeper bag. |
static KarelWorld.Direction |
NORTH
The north direction in the world |
static KarelWorld.Direction |
SOUTH
The south direction in the world |
static KarelWorld.Direction |
WEST
The west direction in the world |
| Constructor Summary | |
|---|---|
KarelWorld()
Constructor for a default world (20 Streets and 20 Avenues unless the value has been changed with another world constructor). |
|
KarelWorld(int numberOfStreetsAndAvenues)
Construct a world of any size 5 or larger with the current cell size. |
|
KarelWorld(int streets,
int avenues)
Create a world with a specified number of streets and avenues and the current cell size |
|
KarelWorld(int streets,
int avenues,
int cellSize)
Construct a world with a given number of streets (>= 5) and avenues (>= 5) |
|
| Method Summary | |
|---|---|
void |
clearWorldOfBeepers()
Remove all of the beepers from the world |
void |
clearWorldOfProps()
Remove all of the Props from the world |
static int |
delay()
Get the value of the delay between robot operations. |
static void |
initializeThreads()
Initialize the robot thread system for a new execution. |
static KarelWorld |
itself()
Obtain a reference to the current world object |
static void |
pauseThreads()
Suspend all the threads in the world. |
void |
placeBeepers(int street,
int avenue,
int howMany)
Place any number of beepers on a corner |
void |
placeWallEastOf(int street,
int avenue)
Place a one segment vertical wall East of a given corner |
void |
placeWallNorthOf(int street,
int avenue)
Place a one segment horizontal wall North of a given corner |
void |
readWorld(java.lang.String filename)
Read a world from a file in the current directory. |
void |
readWorld(java.lang.String directoryPath,
java.lang.String filename)
Read a world from a file in the given directory path. |
void |
rereadLastWorld()
Read in the most recently read world file if any |
static void |
resumeThreads()
Resume all the threads after stopping them. |
void |
saveWorld(java.lang.String filename)
Save a world to a file in the current directory. |
void |
saveWorld(java.lang.String directoryPath,
java.lang.String filename)
Save the world in a file in the given directory path. |
protected static void |
scaleImage(greenfoot.GreenfootImage image,
int divide)
The image files are 40 pixels on a side. |
static void |
setDelay(int delay)
Set the value of the delay between robot operations. |
static void |
setupThread(java.lang.Runnable runnableObject)
Let a robot run in its own thread. |
protected static void |
sleep()
Pause the operation of robots briefly so that the user can follow the changes visually. |
static void |
startThreads()
Start all the threads initially. |
| Methods inherited from class greenfoot.World |
|---|
act, addObject, getBackground, getCellSize, getColorAt, getHeight, getObjects, getObjectsAt, getWidth, numberOfObjects, removeObject, removeObjects, repaint, setActOrder, setBackground, setBackground, setPaintOrder, started, stopped |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final KarelWorld.Direction EAST
public static final int INFINITE
public static final KarelWorld.Direction NORTH
public static final KarelWorld.Direction SOUTH
public static final KarelWorld.Direction WEST
| Constructor Detail |
|---|
public KarelWorld()
public KarelWorld(int numberOfStreetsAndAvenues)
numberOfStreetsAndAvenues - the number of streets and avenues (>= 5)
public KarelWorld(int streets,
int avenues)
streets - the number of streetsavenues - the number of avenues
public KarelWorld(int streets,
int avenues,
int cellSize)
streets - the number of streets in the world >= 5avenues - the number of avenues in the world >= 5cellSize - the cell size of the world (> 20)| Method Detail |
|---|
public void clearWorldOfBeepers()
public void clearWorldOfProps()
public static int delay()
public static void initializeThreads()
public static KarelWorld itself()
public static final void pauseThreads()
public void placeBeepers(int street,
int avenue,
int howMany)
street - the street on which to place the beepersavenue - the avenue on which to place the beepershowMany - how many beepers to place (-1 means infinity)
public void placeWallEastOf(int street,
int avenue)
street - the street that the wall will crossavenue - the avenue that will be just west of the new wall segment
public void placeWallNorthOf(int street,
int avenue)
street - the street that will be just south of the new wall segmentavenue - the avenue that the wall will crosspublic final void readWorld(java.lang.String filename)
filename - the name of the file in the current directory to read
public final void readWorld(java.lang.String directoryPath,
java.lang.String filename)
directoryPath - a full path to the file containing the worldfilename - the nameof the file to readpublic void rereadLastWorld()
public static final void resumeThreads()
public final void saveWorld(java.lang.String filename)
filename - the name of the file to write
public final void saveWorld(java.lang.String directoryPath,
java.lang.String filename)
directoryPath - a full path to the directory in which to save the filefilename - the name of the file to be saved
protected static void scaleImage(greenfoot.GreenfootImage image,
int divide)
image - the image to scaledivide - the factor to shrink the image by (>= 1).public static void setDelay(int delay)
public static final void setupThread(java.lang.Runnable runnableObject)
runnableObject - any runnable object, not just a robotprotected static void sleep()
public static final void startThreads()