CodeNatter
On this page you can run small Python programs right in the browser. The site was built to collect as little data as possible, and it can be embedded straight into other web pages. Every program you type can be shared at once through the web address: the source code travels only inside the link and so stays private.
Guides
- Splitting programs – fold away the preparation, several starting positions, title and task
- Sharing, embedding and printing – the link, the snippet for other sites, the projector view
- Kara – the world, the commands and an editor to click worlds together
- Turtle – the turtle that draws
Games with images and sounds
You write games with Pygame Zero (import pgzrun … pgzrun.go()). For them there is a library of about 600 images and 14 sounds in one consistent, flat style. You do not have to upload anything: every game finds the images by their name. The image gallery shows all of them with search, preview and ready-made code.
All commands for Turtle, Kara, games, windows, numpy, charts, requests and files, with short examples to try out, are in the command reference.
Images
An image becomes a game character with Actor("name"), a background or a tile is drawn with screen.blit("name", (x, y)); images.name works too. Only the images whose names appear in the program are loaded.
import pgzrun
WIDTH = 800
HEIGHT = 480
duck = Actor("ente", (400, 240))
def draw():
screen.blit("pf_bg_day", (0, 0))
duck.draw()
def on_mouse_down():
sounds.platsch.play()
pgzrun.go()
Sounds
sounds.coin.play() plays a sound, for example coin, jump, hit, laser, explosion, win, platsch (a splash). The sound only starts after the first click or key press (that is what browsers want).
Names and animations
The names follow the pattern <thing>_<action>_<n>: heroine_run_1 to heroine_run_8 are the eight frames of the running heroine. Show them one after another and you get an animation. If the beginning of the name is in a string, all matching images are loaded:
RUN = ["heroine_run_" + str(i) for i in range(1, 9)]
heroine = Actor(RUN[0], (200, 300))
time = 0
def update():
global time
time = time + 1
heroine.image = RUN[time // 4 % 8]
You can tell the packs by the beginning: pf_ platformer (tiles pf_grass_mid, coins pf_coin_1 …), sp_ space, sea_ sea, nat_ nature, an_ animals, ui_ game interface. The images of the book have German names: ente (duck), kueken (duckling), boot (boat). In the expert mode a file of your own in the folder images/ (for example images/ente.png) wins over the library image of the same name.
Games to build on
Among the examples on the start page there are four finished games with images from the library:
- Jumping heroine: a platformer with the running frames
heroine_run_*, a jump and a fall image, grass tiles and spinning coins. - Space defense: spaceship
sp_ship, lasers, enemiessp_enemy_*and an explosion of six frames. - Duck pond: the game of the book with
ente,kuekenandboot. - Memory: 16 cards with
memory_*, played with the mouse or a finger.
Kara
Kara is like Sokoban. In German-speaking countries it is very popular for teaching students to program. It was originally designed by SwissEduc.
The world is described with world("""…""") as a picture made of characters, and the commands are available without an import (as in the book). The syntax of the worlds is compatible with GreenfootKara and Pygame Zero Simple Game Tutorials.
This is how you create a world, for example:
world("""
###
#.#
# ####
###$ $.#
#. $@###
####$#
#.#
###
""")
turnLeft()
move()
The characters mean:
@- Kara#- tree- empty cell.- berry+- Kara on a berry$- mushroom*- mushroom on a berry
Kara senses her world with these functions, which return True or False:
onBerry() | Am I sitting on a berry? |
treeFront() | Is there a tree in front of me? |
treeLeft() | Is there a tree to my left? |
treeRight() | Is there a tree to my right? |
mushroomFront() | Is there a mushroom in front of me? |
With these functions Kara changes her world:
move() | I take one step forward. |
turnLeft() | I turn left. |
turnRight() | I turn right. |
putBerry() | I put down a berry. |
removeBerry() | I eat a berry. |
If Kara gets hurt, the program crashes.
Turtle
Turtle is a little turtle that, armed with a pen, can draw the most intricate patterns.
Turtle uses the same API as Python.
This is how you draw your first work of art:
import turtle
t = turtle.Turtle()
print("Building the snowball...")
a = 1
for i in range(12):
t.forward(50)
a = a+1
t.right(30)
print("The snowball is ready 🙂")
Here is the AI-generated summary of the turtle commands:
| Command | Description |
|---|---|
back(distance) |
Moves the turtle backward by the given distance. |
begin_fill() |
Call it before drawing a shape that is to be filled. |
bgcolor(*args) |
Sets or returns the background color of the TurtleScreen. |
bk(distance) |
Moves the turtle backward by the given distance. |
circle(radius, extent=None, steps=None) |
Draws a circle with the given radius. |
clear() |
Deletes the turtle’s drawings from the screen without moving the turtle. |
color(*args) |
Returns or sets the pen color and the fill color. |
colormode(cmode=None) |
Returns the color mode or sets it to 1.0 or 255. |
delay(delay=None) |
Returns or sets the drawing delay in milliseconds. |
distance(x, y=None) |
Returns the distance from the turtle to (x,y) in turtle steps. |
dot(size=None, *color) |
Draws a dot with diameter size and color color. |
down() |
Puts the pen down – draws while moving. |
end_fill() |
Fills the shape drawn after the call of begin_fill(). |
exitonclick() |
Enters the main loop until the mouse is clicked. |
fd(distance) |
Moves the turtle forward by the given distance. |
fill(flag=None) |
Call fill(True) before drawing a shape to fill, and fill(False) when done. |
fillcolor(*args) |
Returns or sets the fill color. |
forward(distance) |
Moves the turtle forward by the given distance. |
goto(x, y=None) |
Moves the turtle to an absolute position. |
heading() |
Returns the turtle’s current heading. |
hideturtle() |
Makes the turtle invisible. |
home() |
Moves the turtle to the origin – coordinates (0,0). |
isdown() |
Returns True if the pen is down, False otherwise. |
isvisible() |
Returns True if the turtle is visible, False otherwise. |
left(angle) |
Turns the turtle left by the given number of units. |
pencolor(*args) |
Returns or sets the pen color. |
pendown() |
Puts the pen down – draws while moving. |
pensize(width=None) |
Sets or returns the line thickness. |
penup() |
Lifts the pen – does not draw while moving. |
pos() |
Returns the turtle’s current position (x,y) as a Vec2D vector. |
reset() |
Deletes the turtle’s drawings and restores its defaults. |
right(angle) |
Turns the turtle right by the given number of units. |
setheading(to_angle) |
Sets the turtle’s heading to to_angle. |
setpos(x, y=None) |
Moves the turtle to an absolute position. |
setup(width=0.5, height=0.75, startx=None, starty=None) |
Sets the size and position of the main window. |