-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
152 lines (135 loc) · 5.75 KB
/
snake.py
File metadata and controls
152 lines (135 loc) · 5.75 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pygame
class Direction:
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
STILL = (0, 0)
class Snake:
BLOCK = 40
SPEED = 4 # The lower the slower (1 is slowest, 10 is fastest)
def __init__(self, x0, y0, images, color_snake=(0, 0, 0), size=BLOCK):
self.length = 1
self.head = [x0, y0]
self.direction = Direction.STILL
self.positions = []
self.color_snake = color_snake
self.images = images
self.size = size
@property
def score(self):
return max(0, self.length - 1)
def draw(self, dis):
for i, x in enumerate(self.positions):
pygame.draw.rect(dis, self.color_snake, [x[0], x[1], self.size, self.size])
if i == self.length - 1:
# Rotate head image based on direction
if self.direction == Direction.UP:
head_image = pygame.transform.rotate(self.images["head"], 90)
elif self.direction == Direction.DOWN:
head_image = pygame.transform.rotate(self.images["head"], -90)
elif self.direction == Direction.LEFT:
head_image = pygame.transform.rotate(self.images["head"], 180)
else:
head_image = self.images["head"]
dis.blit(head_image, (x[0], x[1]))
elif i == 0:
# Rotate tail image based on direction
if self.length > 1: # Ensure there is a tail to orient
next_pos = self.positions[1]
if x[0] < next_pos[0]:
tail_image = pygame.transform.rotate(self.images["tail"], 180)
elif x[0] > next_pos[0]:
tail_image = self.images["tail"]
elif x[1] < next_pos[1]:
tail_image = pygame.transform.rotate(self.images["tail"], 90)
else:
tail_image = pygame.transform.rotate(self.images["tail"], -90)
dis.blit(tail_image, (x[0], x[1]))
else:
# Rotate body image based on direction
prev_pos = self.positions[i - 1]
next_pos = self.positions[i + 1]
if prev_pos[0] == next_pos[0]:
body_image = pygame.transform.rotate(self.images["body"], 90)
elif prev_pos[1] == next_pos[1]:
body_image = self.images["body"]
else:
if (prev_pos[0] < x[0] and next_pos[1] < x[1]) or (
next_pos[0] < x[0] and prev_pos[1] < x[1]
):
body_image = pygame.transform.rotate(self.images["turn"], +90)
elif (prev_pos[0] < x[0] and next_pos[1] > x[1]) or (
next_pos[0] < x[0] and prev_pos[1] > x[1]
):
body_image = pygame.transform.rotate(self.images["turn"], 180)
elif (prev_pos[0] > x[0] and next_pos[1] < x[1]) or (
next_pos[0] > x[0] and prev_pos[1] < x[1]
):
body_image = pygame.transform.rotate(self.images["turn"], 0)
else:
body_image = pygame.transform.rotate(self.images["turn"], -90)
dis.blit(body_image, (x[0], x[1]))
def show_score(self, dis, font, prefix="", color=(255, 255, 102)):
"""Display the current score on the screen."""
value = font.render(prefix + str(self.score), True, color)
dis.blit(value, [0, 0])
def update(self):
self.positions.append(self.head.copy())
if len(self.positions) > self.length:
del self.positions[0]
def handle(self, event):
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
return Direction.LEFT
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
return Direction.RIGHT
elif event.key == pygame.K_UP or event.key == pygame.K_w:
return Direction.UP
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
return Direction.DOWN
return Direction.STILL
def out_of_bounds(self, dis_width, dis_height):
return (
self.head[0] >= dis_width
or self.head[0] < 0
or self.head[1] >= dis_height
or self.head[1] < 0
)
def self_collision(self):
"""
Check if the snake has collided with itself.
Returns True if a collision is detected, otherwise False.
"""
for pos in self.positions[:-1]:
if pos == self.head:
return True
return False
def grow(self):
self.length += 1
def shrink(self):
"""
Halve the snake's length, ensuring it doesn't go below 1.
Returns True if the snake's length is now 1 or less, indicating game over.
"""
self.length = max(1, self.length // 2)
# Reduce the positions list to match the new length
self.positions = self.positions[-self.length:]
return self.length <= 1
def move(self, direction):
self.direction = direction
x_change, y_change = Snake.delta(direction)
self.head[0] += x_change
self.head[1] += y_change
def collide(self, x, y):
return self.head[0] == x and self.head[1] == y
@staticmethod
def delta(direction):
if direction == Direction.UP:
return [0, -Snake.BLOCK]
elif direction == Direction.DOWN:
return [0, Snake.BLOCK]
elif direction == Direction.LEFT:
return [-Snake.BLOCK, 0]
elif direction == Direction.RIGHT:
return [Snake.BLOCK, 0]
return [0, 0]