Python Turtle Graphics stamps are awesome.
They are not that well known, and learner python programmers often do things in a more complicated way than they need to as a result. Take this classic learner-programmer exercise:
Make a chessboard pattern using Python Turtle Graphics.
You might want to try the challenge for yourself before continuing to read…
There are several approaches available for tackling this challenge:
- Do the whole thing manually by moving and rotating the turtle, using
begin_fill()
andend_fill()
as required. - Make a function to draw squares with a corner or center at given coordinates and do this in a [nested
for
loop][1]. - Make a
Square
class and generate instances at the required locations, with the required color. - Use
turtle.stamp()
.
We are going to look at the last approach from the above list in this article.
Python Turtle Graphics Stamp Example
I have had mixed success with embedding code from repl.it. Here is an iframe which should work fine, but I’ve also included the code listing below in case you want to copy/paste or even try typing it into a new Python file yourself (probably the best way to learn!)
import turtle
NUM_SQUARES = 8 # Number of squares along one size of board.
SQUARE_SIZE = 40 # Pixels
BOARD_SIZE = SQUARE_SIZE * NUM_SQUARES
BORDER_FRACTION = 1.025 # Add a slight border to the board.
STAMP_SIZE = 20 # Size of turtle square image.
screen = turtle.Screen()
screen.title("Turtle Stamps")
screen.setup(400, 400)
screen.tracer(0) # Disable animation.
pen = turtle.Turtle(shape='square', visible=False)
pen.shapesize(BOARD_SIZE / STAMP_SIZE * BORDER_FRACTION)
pen.color('red')
pen.stamp()
pen.shapesize(SQUARE_SIZE / STAMP_SIZE)
pen.color('green')
pen.penup()
for y in range(-NUM_SQUARES // 2, NUM_SQUARES // 2):
parity = y % 2 == 0
for x in range(-NUM_SQUARES // 2, NUM_SQUARES // 2):
if parity:
pen.goto(x * SQUARE_SIZE + SQUARE_SIZE // 2, y * SQUARE_SIZE + SQUARE_SIZE // 2)
pen.stamp()
parity = not parity
turtle.done()
How Does Python Turtle Stamp Work?
To explore the placement of turtle.stamp()
, check out this very useful bit of Python turtle code for getting the coordinates from the screen. I’ve also printed the coordinates of the pen (which begins at the defualt location of (0, 0)
.
import turtle
def show_pos(x, y):
print(x, y)
screen = turtle.Screen()
pen = turtle.Turtle()
screen.listen()
screen.onclick(show_pos)
pen.shape("square")
pen.shapesize(50 / 20) # Trust me on this one. Just take the size you want and / by 20.
pen.stamp()
print(pen.pos())
turtle.done()
A little bit of experimentation will show that the stamp has been placed with its center at the position of the turtle which made it. This makes a lot of calculations involving positions on screen a whole lot easier, I find, than drawing a square with a corner a some position.
Exploring with Python Turtle Graphics Stamping
There is enough in this article to get you started with stamping in Python Turtle Graphics. I expect you can see some of the possibilities this method allows. Here’s just a few I can think of off the bat.
- Board games
- 2d games such and Snake/Tron clones
- Exploring mazes
- Pixel art
I hope you find fun ways to make use of Python Turtle stamps in your coding.
As always, happy computing.
Robin Andrews.