Package: karel-robot
Karel the Robot (now in Python)Karel is a pretty snazzy environment for learning to program.
You canread about it here.I decided to write a Karel environment for Python, after seeing that all of the others
had too many dependencies for beginners.Stephen Altamirano (alts/karel)InstallationInstalling should be as simple as opening your terminal and writing:pip3installkarel_robotWrite simple Karel programsCoding in Python issuper fast and easy!
Save a text fileexample.pyin this folder, use import and start coding!fromkarel_robot.runimport*# you can call imported functionsturn_left()# or use Python, like loops ('while') or logical 'not'whilenotfront_is_blocked():move()For a true unix executable, add theshebang,
then the right to execute withchmod +x example.pyand run it as./example.py.If you break a Karel program, it should not leave your terminal screen broken in retaliation, otherwise please
file anIssue.
More advanced or courageous programmers are welcome to look into thekarel_robotfolder and read about the details.Karel functionsThese are the functions you can use to command Karel after importing fromkarel_robot.run:FunctionResultMovementmove()Karel moves in the direction he is facingturn_left()Karel turns leftturn_right()Karel turns rightBeeperspick_beeper()Karel tries to pick up a beeperput_beeper()Karel puts down a beeper (if he has any)beeper_is_present()True iff Karel stands on a beeperWallsfront_is_blocked()True iff Karel can't move forwardfront_is_treasure()True iff Karel is standing in front of a TreasureDirectionfacing_north()True iff Karel is facing north (^)facing_south()True iff Karel is facing south (v)facing_east()True iff Karel is facing east (>)facing_west()True iff Karel is facing west (<)Executionset_karel_beepers(None)Set Karel's beepers, withNoneas inf.set_speed(100)How fast Karel moves, 0 to 100pause()Pause execution, press any key to continuemessage(text, pause)Show a text message to user.save()Save the map in file specified by--output.exit()End executionNote that the map is loaded and screen started in the moment of import:fromkarel_robot.runimport*If you only need raw objects and methods see the directorykarel_robot.Karel worldThere are many maps inworlddirectory. Karel maps are simple text files and look like this one:1..#...
#....^.Karel is represented by the arrow (^) looking up on the empty tile (.).There are two walls (#) and one beeper in the upper right corner (1).
There is no treasure ($).This does not allow us to use more then 9 beepers and place Karel on beeper or other non-empty tile.New Karel map formatA new map format has file extension.km2but it is still plain text file − you can change it to.txt:KAREL 2 1 > N
#  1  .  #  #  .  . 21
#  #  9  .  #  .  #  #
6  .  .  .  .  .  .  #The above map places Karel right on the 9 beepers.
Note that there are 21 beepers in the upper right corner in one tile, which the original format does not support.
The mysteriousNin Karel's header sets the number of beepers he starts with to unlimited, which is the most fun.Editor supportDo you want to write or edit maps? Check out the vim highlighting!Run your programOpen the terminal and write this command:python3YOUR_PROGRAM.py# -m YOUR_MAP.km or other optionsPressQto quit orPto pause program.
Program pauses when Karel tries to make an illegal move.Example treasure 💰Click to expand!Run the program `treasure.py` (also below) with worlds `00` - `03_window`.
Karel will walk to the wall and then search for a treasure in the walls.The idea comes from apaper on cooperative learning in CS1.Karel searching for treasure Python codefromkarel_robot.runimport*whilenotfront_is_blocked():move()whilenotfront_is_treasure():turn_left()iffront_is_blocked():turn_left()# FIX: add elsemove()turn_right()Langton's ant 🐜Click to expand!Here is a short animation of Karel playing
Langton's ant.[wiki]The program `ant.py` (also below)  uses a single beeper to mark a tile as "Black"
and Karel can pick it up to make it "White".
The ant moves seemingly randomly, but makes a nice picture in about 11000 steps.
The command to recreate the recording is:programs/ant.py-x70-y50--karelpos3525--kareldir^--outputlangton.km2Langton's ant Python codefromkarel_robot.runimport*set_speed(100)whileTrue:# repeatifbeeper_is_present():# At a black squarepick_beeper()# flip the color of the squareturn_left()# turn 90° leftmove()# move forward one unitelse:# At a white squareput_beeper()# flip the color of the squareturn_right()# turn 90° rightmove()# move forward one unitTry out your map withinteractiveRun thekarelscript with any of the options:karel--help# 0. prints the actual usagekarel# 1. opens in infinite map, fills the whole screenkarel-m"world/1_window.km"# 2. opens the simple text file map in world directorykarel--ix-m"world/easy/tiny.km2"# 3. simillar to 2., but forces theYou can now use your keyboard to control Karel.KeyFunction↑move()←turn_left()→turn_right()Ipick_beeper()Uput_beeper()Qstop()There is alsoWfor saving to file (specified by--output) and couple of keys forcheatingtesting!Recursive KarelYou can try your wits in creating iterative programs with onlyifstatements, procedure definition and recursion!
It is pretty mind bending so lets look at an example:DEFINE MAIN
    IFWALL PUT MOVE
    IFWALL SKIP MAIN
END
RUN MAINAfter the program is parsed and run, theMAINfunction will call itself again until it reaches a wall.
When that happens, Karel will put down a beeper and the execution stops.Here is a more complex recursive program searching through a maze for a tile with two beepers:You can define new procedures by writingDEFINEnew_nameand then writing names of procedures and statements into its body,
which you thenEND.These statements are the simple building blocks you can use
and combine with your new procedures:StatementNoteMOVEKarel steps forward.LEFTKarel turns 90° left - note that there is noRIGHT:)PUTKarel puts down one beeper.PICKKarel picks up one beeper.SKIPDoes nothing - likepassin Python.IFWALL✓𐄂Iffront_is_blocked()then run the first procedure else the secondIFMARK✓𐄂Ifbeeper_is_present()then run the first procedure else the secondThe interpret inkarel_robot.parsers.interpretis used by thekarelexecutable script:karel--programprograms/graph/3_treasure.ks--mapworld/maze/treasure/07.km2--speed20Credits 🤖The original author is Stephen Altamirano (@alts).
Recently this has been updated by @Tetragramm and @xsebek.The package on PyPI is maintained by @xsebek.If you want to contribute, check out thekarel_robotfolderREADME.LICENSEThis project is released under GNU GPLv3 (or later) license in hopes that
it will be useful. You are encouraged to share thisfreelyand can
even sell it provided everyone will still be able to read and modify
the source code (just like you are) and keeps the license. :wink: