Advanced — Day 1
Moving Snake & Controls
Day Schedule
Online
| Time | Activity |
|---|---|
| 10:00 | Welcome + Demo Run the full AI snake game; explain what the week looks like |
| 10:20 | Pre-Assessment Link provided separately |
| 10:35 | Fast Review Variables, libraries, turtle setup — 30–45 min max |
| 11:05 | Snake Head + Movement Direction functions, key bindings, game loop, wall collision |
| 11:10 | Break |
| 11:20 | Breakout Groups Moving snake with arrow keys and wall collision |
| 12:20 | Reminders |
In-Person
| Time | Activity |
|---|---|
| 8:00 | Welcome + Demo Run the full AI snake game; explain what the week looks like |
| 8:20 | Pre-Assessment Link provided separately |
| 8:35 | Fast Review Variables, libraries, turtle setup — 45 min max |
| 9:20 | Snake Head + Movement Direction functions, key bindings, game loop, wall collision |
| 9:40 | Break |
| 9:50 | Breakout Groups Moving snake with arrow keys and wall collision |
| 11:00 | Break |
| 11:10 | Open Coding Extend or polish Day 1 code; experiment with speed and step size |
| 12:20 | Reminders |
Learning Objectives
- Set up screen, snake head, direction functions, and key bindings
- Run a working game loop with movement and wall collision
Project
# INSTRUCTOR — Day 1: Snake — Moving Head & Controls (Advanced)
# -----------------------------------------------
# TEACH: Advanced students have prior Python experience.
# Spend 30–45 min max on a fast review of variables, conditionals, and functions.
# Then go straight into Turtle. The real Day 1 goal is a moving snake with controls.
#
# KEY PATTERN: setheading() + forward() is the entire movement system.
# head.setheading(90) points the snake up. head.forward(20) moves it 20px that way.
# Everything else in snake is built around this two-line pattern.
#
# Headings: 0 = right (east) 90 = up (north) 180 = left (west) 270 = down (south)
#
# DIRECTION GUARD: we block the REVERSE heading, not the current one.
# go_up blocks 270 (DOWN). If heading is already UP and you press UP again, nothing bad
# happens. If heading is DOWN and you press UP, you'd reverse — that's what we block.
#
# CHECK: all four arrow keys work and direction guards prevent reversal.
# COMMON MISTAKE: head drawn with pendown — it trails ink as it moves.
# Fix: call head.penup() before head.goto(0, 0).
import turtle
import time
screen = turtle.Screen()
screen.title("Snake Game")
screen.bgcolor("black")
screen.setup(width=600, height=600)
screen.tracer(0) # TEACH: tracer(0) = we call screen.update() manually each frame
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
def go_up():
if head.heading() != 270: # 270 = DOWN = the reverse of UP
head.setheading(90)
def go_down():
if head.heading() != 90: # 90 = UP = the reverse of DOWN
head.setheading(270)
def go_left():
if head.heading() != 0: # 0 = RIGHT = the reverse of LEFT
head.setheading(180)
def go_right():
if head.heading() != 180: # 180 = LEFT = the reverse of RIGHT
head.setheading(0)
screen.listen()
screen.onkeypress(go_up, "Up")
screen.onkeypress(go_down, "Down")
screen.onkeypress(go_left, "Left")
screen.onkeypress(go_right, "Right")
# TEACH: head runs off screen until Day 2 adds wall collision — that's fine for now.
while True:
screen.update()
head.forward(20)
time.sleep(0.1)
Extra — Draw Border
# INSTRUCTOR — Day 1: Extra Activity: Draw the Border (Advanced)
# -----------------------------------------------
# TEACH: The final snake game has a gold border. Students reproduce it using a for loop.
# This reinforces: turtle setup, goto() to position a starting corner,
# for loop with a fixed number of iterations, forward() and left() to draw a square.
# Answers: start corner (-300, -300), 4 sides, 600 forward, 90 degrees left.
import turtle
halfSize = 300
drawer = turtle.Turtle()
drawer.color("gold")
drawer.width(15) # TEACH: width() sets line thickness
drawer.speed(0)
drawer.penup()
drawer.goto(-halfSize, -halfSize) # bottom-left corner
drawer.pendown()
for i in range(4): # a square has 4 sides
drawer.forward(halfSize * 2) # each side is 600px (halfSize * 2)
drawer.left(90) # TEACH: left() turns counterclockwise; right() turns clockwise
drawer.hideturtle()
# TEACH: challenge — change border color based on a score variable
score = 0
border_color = "gold"
if score > 5:
border_color = "cyan"
elif score > 10:
border_color = "red"
# TEACH: they'd need to redraw the border each time score changes
# (or use a pen turtle that writes on top — hint toward Day 2's score display)
turtle.done()