Loading Initial State
Once you've copied the project bundle to your device, you're ready to begin programming Karel. By default, the code.py file will load the world state for Chapter 1 of the Stanford guide. To load a different puzzle, simply update this line of the code to use a different valid puzzle data file name.Â
chapter_data = load_state_file("chapters/karel_ch01.json")
Look inside of the chapters/ directory within the project files to find the names of all of the puzzle data files. Here are all the ones that come with the project code:
karel_ch01.json karel_ch02.json karel_ch03a.json karel_ch03b.json karel_ch04.json karel_ch05a.json karel_ch05b.json karel_ch05c.json karel_ch06a.json karel_ch06b.json karel_ch07.json karel_ch08.json
Write Code For Karel
Your code goes inside of the main()
function body. There are comments that designate the area to put your code.
## START OF MAIN FUNCTION, YOUR CODE GOES BELOW HERE ## ## END OF MAIN FUNCTION, YOUR CODE GOES ABOVE HERE ##
Move your cursor to one of the blank lines between these comments when you start writing code for Karel.
CircuitPython runs your code automatically when you save it, so ave your file (click Save or press ctrl-S or cmd-S) when you're ready, and then watch the display to see how your Karel behaves with the current code.
At the end of the program, it will check the state of Karel and the world to see if they match the goal state for the current puzzle data. Look in the serial console for print messages like Goal state reached? [True or False]
. Try to write the programs necessary to successfully reach the goal state and make it print True
.
Example Karel Program
Here is an example program that will move a few steps while picking up and then putting back down a beeper.
## START OF MAIN FUNCTION, YOUR CODE GOES BELOW HERE ## # take one step move() # if there is a beeper at this location if beepers_present(): # pick up the beeper pick_beeper() # turn Karel to the left turn_left() # take one step move() # put down a beeper put_beeper() ## END OF MAIN FUNCTION, YOUR CODE GOES ABOVE HERE ##
See the Stanford Python Karel reference page for a complete list of available commands. Remember that you can also write your own functions to achieve more complex behaviors.
Text editor powered by tinymce.