-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
213 lines (156 loc) · 7.14 KB
/
snake.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pygame as pg
import os
from pygame import Vector2,Surface
from enums import Direction
from common import Global,SOUNDS_DIR,ASSETS_DIR
class Snake:
def __init__(self,
screen : Surface,
initial_position : tuple[int,int],
initial_length : int,
initial_direction : Direction
) -> None:
x, y = initial_position
self.screen = screen
self.body = [Vector2(x + i - initial_length + 1, y) for i in range(initial_length)]
self.direction = initial_direction
self.assets = self.load_assets()
self.eat_sound = pg.mixer.Sound(os.path.join(SOUNDS_DIR, 'eat.mp3'))
self.wall_sound = pg.mixer.Sound(os.path.join(SOUNDS_DIR, 'wall.mp3'))
def load_assets(self) -> dict[str,Surface]:
result = {}
for asset_name in os.listdir(ASSETS_DIR):
name,_ = os.path.splitext(asset_name)
result[name] = pg.transform.scale(
pg.image.load(os.path.join(ASSETS_DIR, asset_name)), (Global.CELL_WIDTH,Global.CELL_WIDTH)
)
return result
def position(self):
return self.body[-1]
def get_segments(self) -> list[int]:
corners = [0]
tmp = self.body[0]
for i in range(1, len(self.body)):
if self.body[i].x != tmp.x and self.body[i].y != tmp.y:
corners.append(i-1)
tmp = self.body[i-1]
corners.append(len(self.body) - 1)
return corners
def draw_tail(self):
cell = self.body[0]
rect = pg.Rect(
cell.x * Global.CELL_WIDTH + Global.OFFSET,
cell.y * Global.CELL_WIDTH + Global.OFFSET,
Global.CELL_WIDTH,
Global.CELL_WIDTH
)
direction = self.body[1] - self.body[0]
match direction:
case Direction.UP.value:
self.screen.blit(self.assets['tail_down'], rect)
case Direction.DOWN.value:
self.screen.blit(self.assets['tail_up'], rect)
case Direction.LEFT.value:
self.screen.blit(self.assets['tail_right'], rect)
case Direction.RIGHT.value:
self.screen.blit(self.assets['tail_left'], rect)
def draw_head(self):
cell = self.body[-1]
rect = pg.Rect(
cell.x * Global.CELL_WIDTH + Global.OFFSET,
cell.y * Global.CELL_WIDTH + Global.OFFSET,
Global.CELL_WIDTH,
Global.CELL_WIDTH
)
match self.direction:
case Direction.UP:
self.screen.blit(self.assets['head_up'], rect)
case Direction.DOWN:
self.screen.blit(self.assets['head_down'], rect)
case Direction.LEFT:
self.screen.blit(self.assets['head_left'], rect)
case Direction.RIGHT:
self.screen.blit(self.assets['head_right'], rect)
def draw_corners(self, corners : list[int]):
for corner in corners:
cell = self.body[corner]
rect = pg.Rect(
cell.x * Global.CELL_WIDTH + Global.OFFSET,
cell.y * Global.CELL_WIDTH + Global.OFFSET,
Global.CELL_WIDTH,
Global.CELL_WIDTH
)
direction_1 = self.body[corner] - self.body[corner-1]
direction_2 = self.body[corner+1] - self.body[corner]
if direction_1 == Direction.RIGHT.value and direction_2 == Direction.UP.value:
self.screen.blit(self.assets['body_topleft'], rect)
if direction_1 == Direction.RIGHT.value and direction_2 == Direction.DOWN.value:
self.screen.blit(self.assets['body_bottomleft'], rect)
if direction_1 == Direction.LEFT.value and direction_2 == Direction.UP.value:
self.screen.blit(self.assets['body_topright'], rect)
if direction_1 == Direction.LEFT.value and direction_2 == Direction.DOWN.value:
self.screen.blit(self.assets['body_bottomright'], rect)
if direction_1 == Direction.UP.value and direction_2 == Direction.RIGHT.value:
self.screen.blit(self.assets['body_bottomright'], rect)
if direction_1 == Direction.UP.value and direction_2 == Direction.LEFT.value:
self.screen.blit(self.assets['body_bottomleft'], rect)
if direction_1 == Direction.DOWN.value and direction_2 == Direction.RIGHT.value:
self.screen.blit(self.assets['body_topright'], rect)
if direction_1 == Direction.DOWN.value and direction_2 == Direction.LEFT.value:
self.screen.blit(self.assets['body_topleft'], rect)
def draw_body_parts(self, corners : list[int]):
for start,end in zip(corners[:-1],corners[1:]):
segment_direction = self.body[start+1] - self.body[start]
for i in range(start + 1,end):
cell = self.body[i]
rect = pg.Rect(
cell.x * Global.CELL_WIDTH + Global.OFFSET,
cell.y * Global.CELL_WIDTH + Global.OFFSET,
Global.CELL_WIDTH,
Global.CELL_WIDTH
)
match segment_direction:
case Direction.UP.value:
self.screen.blit(self.assets['body_vertical'], rect)
case Direction.DOWN.value:
self.screen.blit(self.assets['body_vertical'], rect)
case Direction.LEFT.value:
self.screen.blit(self.assets['body_horizontal'], rect)
case Direction.RIGHT.value:
self.screen.blit(self.assets['body_horizontal'], rect)
def draw(self):
corners = self.get_segments()
self.draw_body_parts(corners)
self.draw_corners(corners[1:-1])
self.draw_tail()
self.draw_head()
def update(self):
head = self.body[-1].copy()
head += self.direction.value
self.body.pop(0)
self.body.append(head)
def set_direction(self, direction : Direction):
if self.direction == Direction.RIGHT and direction == Direction.LEFT:
return False
if self.direction == Direction.LEFT and direction == Direction.RIGHT:
return False
if self.direction == Direction.UP and direction == Direction.DOWN:
return False
if self.direction == Direction.DOWN and direction == Direction.UP:
return False
self.direction = direction
return True
def grow(self):
tail = self.body[0].copy()
direction = self.body[1] - self.body[0]
tail -= direction
self.body.insert(0, tail)
self.eat_sound.play()
def check_for_collision(self, translation : Vector2 | None = None) -> bool:
if translation is None:
translation = Vector2(0,0)
head = self.body[-1] + translation
if translation == Vector2(0,0):
return head in self.body[:-1]
else:
return head in self.body[1:]