import turtle
= {'b':'brick.gif', 'g':'grass.gif', 's': 'sky.gif'}
tiles
for i in tiles:
turtle.register_shape(tiles[i])
= []
level = open("level.txt", "r")
f for x in f:
0:len(x)-1])
level.append(x[
= turtle.Turtle() pen
Example: tile drawing
Let’s use some loops to draw a tiled scenery using Turtle graphics!
Download the images for the three tiles we will be using: grass, sky and brick. Notice that all tiles are of size 42x42.
Now, look at this starter code:
It is loading a file called level.txt
. This file could have different arrangements of characters, which are depicting the three types of blocks. For example, your level.txt
file could be:
ssssssss
sssbbsss
ssssssss
sbbssbbs
ssssssss
gggggggg
gggggggg
gggggggg
These characters are loaded and stored in the list of lists (matrix!) level
.
Now, how can we draw these tiles in the screen? To make the turtle “print” an image — for example, a grass tile —, you have to set:
'g']) # sets the shape of the turtle of type 'grass'
pen.shape(level[# "stamps" the turtle pen.stamp()
Let’s then construct a loop to draw all the tiles in the matrix level
!