Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/freegames/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@
2. How can you make the snake go around the edges?
3. How would you move the food?
4. Change the snake to respond to mouse clicks.
5. How can you change the design of the start menu?
6. How would you change the button required to start the game?
"""

from random import randrange
from turtle import *

from freegames import square, vector

food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)

game_started = False
"""Variable to determine game state, needed to prevent infinite loop"""

def start_game():
"""Clear the screen and start the game loop."""
global game_started
if game_started:
return
game_started = True
clear()
move()

def draw_start_screen():
"""Draws the initial menu text."""
clear()
up()
goto(0, 50)
color('black')
write("SNAKE", align="center", font=("Courier", 30, "bold"))

goto(0, -50)
write("Press SPACE to Start", align="center", font=("Courier", 16, "normal"))
update()

def change(x, y):
"""Change snake direction."""
Expand Down Expand Up @@ -66,5 +88,7 @@ def move():
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
"""Sets up start menu"""
onkey(start_game, 'space')
draw_start_screen()
done()