Karel The Robot was invented in th 1970s by Richard E. Pattis at Standford Univeristy. Karel The Robot is a program, that should make the intruduction to coding for newcomers and beginners more simple. Its aim is to teach basic principles such as top-down programming and establish them from the beginning. Futhermore is learning programming with Karel The Robot a way more visual and rewarding experience then just printing out some strings and numbers onto the command-line.
Since its original development in th 1970s many projects have replicated and improved on the originial idea. The biggest drawback of the original Karel The Robot is however, that he has its own programming-language. This is usefull if you primary focus is on establishing programmically thinking, as mentioned above. If there should however be taught a programming language besides that, Karel The Robot had to implemented in this language.
For this reason there is quite a limited number of ports of Karel The Robot to different programming languages. And even if there is a port of Karel The Robot for your programming language, cross system compatablility is quite rare. This is why I started this project. The cross system compatability of Python and pygame covers Linux, MacOS and Windows. The project will feature precompiled versions for all of those systems. Then you can choose a suitable Frontend for your language and if none exists you can easily and fast create one (see 3.1. Create your own).
You can find a list of frontend under 3. Frontends. Most frontends will provide a starter project for most systems and IDEs under the releases tab.
If you are creating a frontend or want to update your bundled PBE, than you can download a precompiled version of the PBE for your system under the Releases tab. The downloaded directory will have the following structure. If you want to add maps just place your .xml-files in PBE_PKG/assets/map
.
PBE_PKG
├── assets
│ ├── map
│ │ ├── 1x1.xml
│ │ ├── 1x8.xml
│ │ └── ...
│ └── pbe.yaml
├── karel_pbe
└── run_karel_pbe.sh
# clone the repo and cd into into it
git clone https://github.com/hendrikboeck/karel_the_robot_python3_backend.git
cd karel_the_robot_python3_backend
# create a virtual env and download dependecies
python3 -m venv env
./env/bin/activate
pip install -r requirements.txt
# build the project
pyinstaller karel_pbe.spec
Now you have to create a folder assets
in the folder, where you want to execute ./karel_pbe
, and copy the file assets/pbe.yaml
to your assets
folder and your .xml
-files into the assets/map
folder.
New maps can be added by adding the file to the assets/map
folder. If you want to create a new map use the following example:
<?xml version="1.0" encoding="UTF-8"?>
<!-- DOCUMENT START -->
<map size="(10, 7)" speed="1.00">
<!-- METADATA -->
<metadata name="TestRoom" version="1.0" author="Hendrik Boeck" />
<!-- MAP OBJECTS -->
<karel position="(1, 2)" orientation="SOUTH" beeperbag="inf" />
<wall start="(7, 1)" length="3" orientation="WEST" />
<wall start="(7, 4)" length="4" orientation="SOUTH" />
<beeper position="(8, 4)" n="2" />
</map>
<!-- DOCUMENT END -->
Language | Language Version | Project |
---|---|---|
Java | 11 =< | hendrikboeck/karel_the_robot_java_frontend |
C | 99 =< | hendrikboeck/karel_the_robot_c_frontend |
The project uses a websocket as its communication. It can be configured over the configuration (assets/pbe.yaml
) file. The backend supports UDP as well as TCP-sockets. The commands use a variation of JSON-RPC as its format (more accurate information and all the commands can be found under 3.2. API). You dont have to check if an instance of the Karel is already running. The backend will not start if the port is occupied (this also means, do not use a port pre-occupied on your system).
The API uses json-encoded messages inspired by JSON-RPC v2. Every request to the server will need a autoincrementing id
for identifying the corresponding response. If the request resembles a Karel-Action (e.g. move
) or System-Update (e.g. EOS
) the returned type will null
; for Karel-Questions (e.g. frontIsClear
) the returned type will be a boolean
. If an error was thown in the backend the returned type will always be string
identifying the error.
ActionExecutionError
: an Karel-Action could not be performed (e.g. "Karel hit a wall")MapLoadingError
: map could not found or could not be read correctlyUnallowedActionError
: A command has been received, even though the game is already finished or another error, that was sent early, has been ignored.
{
"id": 0, // autoincrement
"function": <functionname>,
"args": <arguments>
}
{
"id": 0, // same id as request
"result": <return>
}
- loadWorld
-
loads a
mapname.xml
file as World into the game. (mapname.xml
can eighter be loaded from local file inassets/map/
or from a embedded maps in the exe).- arguments:
dictionary
- map:
string
, name of map that should be loaded
- map:
- return:
null
- arguments:
- EOS
-
terminates command sequence for backend. Has to be called as last command in program.
- arguments:
null
- return:
null
- arguments:
- move
-
is a Karel-Action. Makes Karel move 1 tile forward in the direction he is looking at. If Karel can not execute
move
aActionExecutionError
is thrown.- arguments:
null
- return:
null
- arguments:
- turnLeft
-
is a Karel-Action. Makes Karel turn left.
- arguments:
null
- return:
null
- arguments:
- pickBeeper
-
is a Karel-Action. Makes Karel pick a beeper from current position. If Karel can not execute
pickBeeper
aActionExecutionError
is thrown.- arguments:
null
- return:
null
- arguments:
- putBeeper
-
is a Karel-Action. Makes Karel put a beeper at current position. If Karel can not execute
putBeeper
aActionExecutionError
is thrown.- arguments:
null
- return:
null
- arguments:
- frontIsClear
-
is a Karel-Question. Returns wether there is a wall in front of Karel.
- arguments:
null
- return:
boolean
- arguments:
- rightIsClear
-
is a Karel-Question. Returns wether there is a wall to the right of Karel.
- arguments:
null
- return:
boolean
- arguments:
- leftIsClear
-
is a Karel-Question. Returns wether there is a wall to the left of Karel.
- arguments:
null
- return:
boolean
- arguments:
- beeperInBag
-
is a Karel-Question. Returns wether Karel has at least one beeper left in his bag.
- arguments:
null
- return:
boolean
- arguments:
- beeperPresent
-
is a Karel-Question. Returns wether at least one beeper is present on the position Karel is at.
- arguments:
null
- return:
boolean
- arguments:
- facingNorth
-
is a Karel-Question. Returns wether Karel is currently facing north.
- arguments:
null
- return:
boolean
- arguments:
- facingEast
-
is a Karel-Question. Returns wether Karel is currently facing east.
- arguments:
null
- return:
boolean
- arguments:
- facingSouth
-
is a Karel-Question. Returns wether Karel is currently facing south.
- arguments:
null
- return:
boolean
- arguments:
- facingWest
-
is a Karel-Question. Returns wether Karel is currently facing west.
- arguments:
null
- return:
boolean
- arguments: