-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakes.py
More file actions
74 lines (60 loc) · 2.37 KB
/
snakes.py
File metadata and controls
74 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Snake Game based on the FreeGames collection, with modifications made
# for a better gaming experience.
# Authors: Regina Luna, A01655821
# Diego Samperio, A01662935
# Abigail Curiel, A01655892
# Date: 23/03/2023
from turtle import *
from random import randrange
from freegames import square, vector
# Set up initial positions and direction for snake and food
food = vector(0, 0) # Position of the food
snake = [vector(10, 0)] # List of vectors representing the snake's body
aim = vector(0, -10) # Direction of the snake's movement
# Function to change the direction of the snake
def change(x, y):
"Change snake direction."
aim.x = x
aim.y = y
# Function to check if the snake's head is within the boundaries
def inside(head):
"Return True if head is inside the boundaries."
return -200 < head.x < 190 and -200 < head.y < 190
# Function to move the snake and check for collisions
def move():
"Move snake forward one segment."
head = snake[-1].copy() # Copy the current head of the snake
head.move(aim) # Move the head in the direction of the aim
# Check if the snake collides with the boundary or itself
if not inside(head) or head in snake:
square(head.x, head.y, 9, 'red') # Draw the collision point
update()
return # End the game
# Add the new head to the snake
snake.append(head)
# Check if the snake eats the food
if head == food:
print('Snake:', len(snake))
food.x = randrange(-15, 15) * 10 # Move the food to a new random position
food.y = randrange(-15, 15) * 10
else:
snake.pop(0) # Remove the tail of the snake
# Clear the screen and draw the snake and food
clear()
for body in snake:
square(body.x, body.y, 9, 'green') # Draw each part of the snake
square(food.x, food.y, 9, 'red') # Draw the food
update()
ontimer(move, 100) # Schedule the next move
# Set up the game window and key bindings for snake direction
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
# Bind arrow keys to change direction
onkey(lambda: change(10, 0), 'Right') # Move right
onkey(lambda: change(-10, 0), 'Left') # Move left
onkey(lambda: change(0, 10), 'Up') # Move up
onkey(lambda: change(0, -10), 'Down') # Move down
move() # Start the game loop
done()