Table of Contents

Using Turtle graphics: a Tkinter-based turtle graphics module for Python

Import choice from Turtle graphics:


In [1]:
from random import choice

In [2]:
choice([1,2,3])


Out[2]:
2

In [3]:
choice([1,2,3])


Out[3]:
2

Import everything from Turtle graphics:


In [4]:
from turtle import *

Import floor and vector from Free Games:


In [5]:
from freegames import floor, vector # Install freegames with 'pip install freegames'


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-5a054e38efc5> in <module>
----> 1 from freegames import floor, vector # Install freegames with 'pip install freegames'

ModuleNotFoundError: No module named 'freegames'

In [6]:
floor(1,10) # value to floor, the floor


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-8f3b4355c9ab> in <module>
----> 1 floor(1,10) # value to floor, the floor

NameError: name 'floor' is not defined

In [ ]:
floor(9,10)

In [ ]:
floor(11,10)

In [ ]:
floor(-1,10)

In [ ]:
floor(3,2)

In [ ]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [ ]:
v1 = vector(1, 2)
v2 = v1.copy()
v2.move(1)

In [ ]:
print(v1, v2)

In [ ]:
plt.figure()
ax = plt.gca()
ax.quiver((0,0), (0,0), (v1.x, v2.x), (v1.y, v2.y), angles='xy', scale_units='xy', scale=1)
ax.set_xlim([-5, 5])
ax.set_ylim([-5, 5])
ax.set_xticks(np.arange(-5, 5, 1))
ax.set_yticks(np.arange(-5, 5, 1))
plt.grid()
plt.draw()
plt.show()

In [ ]:
v1 = vector(1,2)
v2 = v1.copy()
v2.rotate(90)
print(v1, v2)

In [ ]:
plt.figure()
ax = plt.gca()
ax.quiver((0,0), (0,0), (v1.x, v2.x), (v1.y, v2.y), angles='xy', scale_units='xy', scale=1)
ax.set_xlim([-5, 5])
ax.set_ylim([-5, 5])
ax.set_xticks(np.arange(-5, 5, 1))
ax.set_yticks(np.arange(-5, 5, 1))
plt.grid()
plt.draw()
plt.show()

Declare a dictionary with only one entry:


In [ ]:
state = {'score': 0}

In [ ]:
'score' in state

In [ ]:
state['score']

Instance Turtle object in instance path. Set is an invisible:


In [ ]:
path = Turtle(visible=False)

Another instance:


In [ ]:
writer = Turtle(visible=False)

In [ ]:
aim = vector(5, 0)
pacman = vector(-40, -80)

In [ ]:
ghosts = [
    [vector(-180, 160), vector(5, 0)],
    [vector(-180, -160), vector(0, 5)],
    [vector(100, 160), vector(0, -5)],
    [vector(100, -160), vector(-5, 0)],
]

In [ ]:
type(ghosts)

In [ ]:
type(ghosts[0])

In [ ]:
type(ghosts[0][0])

The maze:


In [ ]:
tiles = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
    0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0,
    0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]

In [ ]:
len(tiles)

In [ ]:
type(tiles)

In [ ]:
import matplotlib.pyplot as plt
import numpy as np
plt.matshow(np.array(tiles).reshape(20, 20))
plt.show()

In [ ]:
def square(x, y):
    "Draw square using path at (x, y)."
    path.up()
    path.goto(x, y)
    path.down()
    path.begin_fill()

    for count in range(4):
        path.forward(20)
        path.left(90)

    path.end_fill()

In [ ]:
square(5,7)
path.isvisible()

In [ ]:
path.showturtle()

In [ ]:
def offset(point):
    "Return offset of point in tiles."
    x = (floor(point.x, 20) + 200) / 20
    y = (180 - floor(point.y, 20)) / 20
    index = int(x + y * 20)
    return index

In [ ]:
print(offset(vector(2,3)))

In [ ]:
def valid(point):
    "Return True if point is valid in tiles."
    index = offset(point)

    if tiles[index] == 0:
        return False

    index = offset(point + 19)

    if tiles[index] == 0:
        return False

    return point.x % 20 == 0 or point.y % 20 == 0

In [ ]:
print(valid(vector(2,3)))

See:


In [ ]:
def world():
    "Draw world using path."
    bgcolor('black')
    path.color('blue')

    for index in range(len(tiles)):
        tile = tiles[index]

        if tile > 0:
            x = (index % 20) * 20 - 200
            y = 180 - (index // 20) * 20
            square(x, y)
            print(x, y, " ",)
            if tile == 1:
                path.up()
                path.goto(x + 10, y + 10)
                path.dot(2, 'white')

In [ ]:
#path.speed(10)
#world()

See:

  • turtle.undo(): Undoes (repeatedly) the last turtle action(s).
  • turtle.write(): Writes text - the string representation of arg - at the current turtle position.
  • turtle.clear(): Deletes the turtle’s drawings from the screen.

In [ ]:
def move():
    "Move pacman and all ghosts."
    writer.undo()
    writer.write(state['score'])

    clear()

    if valid(pacman + aim):
        pacman.move(aim)

    index = offset(pacman)

    if tiles[index] == 1:
        tiles[index] = 2
        state['score'] += 1
        x = (index % 20) * 20 - 200
        y = 180 - (index // 20) * 20
        square(x, y)

    up()
    goto(pacman.x + 10, pacman.y + 10)
    dot(20, 'yellow')

    for point, course in ghosts:
        if valid(point + course):
            point.move(course)
        else:
            options = [
                vector(5, 0),
                vector(-5, 0),
                vector(0, 5),
                vector(0, -5),
            ]
            plan = choice(options)
            course.x = plan.x
            course.y = plan.y

        up()
        goto(point.x + 10, point.y + 10)
        dot(20, 'red')

    update()

    for point, course in ghosts:
        if abs(pacman - point) < 20:
            return

    ontimer(move, 100)

In [ ]:
#move()

In [ ]:
def change(x, y):
    "Change pacman aim if valid."
    if valid(pacman + vector(x, y)):
        aim.x = x
        aim.y = y

See:


In [ ]:
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
writer.goto(160, 160)
writer.color('white')
writer.write(state['score'])
listen()
onkey(lambda: change(5, 0), 'Right')
onkey(lambda: change(-5, 0), 'Left')
onkey(lambda: change(0, 5), 'Up')
onkey(lambda: change(0, -5), 'Down')
world()
move()
done()

Exercises (proposed by the author):

  1. Change the board.
  2. Change the number of ghosts.
  3. Change where pacman starts.
  4. Make the ghosts faster/slower.
  5. Make the ghosts smarter.