Paint Corner
In the Stanford Python Karel guide chapter 9, there is information about a paint_corner()
function. At the time of writing this guide, I could not get the paint_corner()
function to work for the in-browser Karel code environment on their web pages. However, the CircuitPython implementation of Karel does support paint_corner()
. Call it and pass a color name to paint the color at Karel's current location. e.g. paint_corner("blue")
.
Painted corners cannot be part of the input
or goal
states, but they sure are fun to play with and pretty to look at. The supported colors are listed below. You can also see them in the serial console by adding this statement inside your main()
function: print(COLOR_NAMES)
['white', 'black', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'light_gray', 'gray', 'brown', 'dark_green', 'turquoise', 'dark_blue', 'dark_red']
Creating Custom Puzzles
The puzzles are encoded into JSON files. If you're not familiar with JSON, this Real Python page can serve as a great guide covering the basics. The file contains an input
state and goal
state. Each state contains a grid representing the tiles in the world, the location and direction of Karel, and an optional list of locations that contain more than 1 beeper.Â
The numbers in the grid correspond to indexes within the sprite sheet.Â
-
7
= Empty black void tile -
5
= Open corner tile that Karel can move to -
4
= Beeper tile -
6
= Wall tile that Karel cannot move to.
For Karel's location, the x
and y
use a zero-based origin of the bottom left corner. "x":0
, "y":0
is the bottom left corner of the world.
For beeper_counts
, the configuration is a dictionary that has string keys which are comma separated x and y coordinates using the same zero-based origin as Karel. Â "1, 0": 42
means 42 beepers located at tile x:1
, y:0
To create your own custom puzzles, simply copy/paste an existing configuration file and change the grid tile indices as well as Karel's location and direction. If your puzzle requires multiple beepers at the same location, then add a beeper_counts
object with appropriate values. See chapters/karel_ch05a.json for an example configuration that uses multiple beepers at a location.
The configuration file for Chapter 1 is embedded below for reference.
{ "input": { "karel": { "x": 0, "y": 0, "direction": "east" }, "world": [ [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [5, 5, 5, 7, 7, 7, 7, 7, 7, 7], [6, 6, 5, 7, 7, 7, 7, 7, 7, 7], [5, 4, 5, 7, 7, 7, 7, 7, 7, 7] ]}, "goal": { "karel": { "x": 0, "y": 2, "direction": "west" }, "world": [ [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [4, 5, 5, 7, 7, 7, 7, 7, 7, 7], [6, 6, 5, 7, 7, 7, 7, 7, 7, 7], [5, 5, 5, 7, 7, 7, 7, 7, 7, 7] ]} }
Text editor powered by tinymce.