-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
267 lines (232 loc) · 8.54 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from random import randint
from kivy.animation import Animation
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import (
NumericProperty, ReferenceListProperty, ObjectProperty, BooleanProperty, StringProperty
)
from kivy.uix.widget import Widget
from kivy.vector import Vector
STEP_SIZE = 20
WINDOW_HEIGHT = 600
WINDOW_WIDTH = 600
def random_pos_on_grid(step_size, width, height):
"""
Give a random position on the window and at a multiple of step_size
:param step_size: The step size
:param width: Window width
:param height: Windows height
:return: A random position on the map. It will be on a multiple of step_size
"""
return [
randint(0, (int((width - step_size) / step_size))) * step_size,
randint(0, (int((height - step_size) / step_size))) * step_size]
class SnakeCell(Widget):
"""
A snake cell
"""
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
"""
Move the snake cell
"""
self.pos = Vector(*self.velocity) + self.pos
def move_pos(self, pos):
"""
Move the snake cell at a new position
"""
self.pos = pos
def go_to(self, direction, step_size):
"""
Modify the object direction
:param step_size: Step size of a cell
:param direction: right, left, up, down or stop to move the object at this direction
"""
if direction == "right":
self.velocity = (step_size, 0)
elif direction == "left":
self.velocity = (-step_size, 0)
elif direction == "up":
self.velocity = (0, step_size)
elif direction == "down":
self.velocity = (0, -step_size)
elif direction == "stop":
self.velocity = (0, 0)
class Fruit(Widget):
"""
The fruit
"""
def move(self, pos):
"""
Move a fruit to a new position
:param pos: The new pos of the fruit
"""
self.pos = pos
class SnakeGame(Widget):
"""
The snake game
"""
snake_head = ObjectProperty(None)
fruit = ObjectProperty(None)
score = NumericProperty(0)
label_game_over = ObjectProperty(None)
tail = []
position_to_go = StringProperty("right")
is_game_over = BooleanProperty(False)
def __init__(self, step_size, width, height, **kwargs):
"""
Initialise the game
:param step_size: Size of the step of a snake
:param width: Window's width
:param height: Window's height
:param kwargs:
"""
super(SnakeGame, self).__init__(**kwargs)
Window.size = (width, height)
self.step_size = step_size
self._init_keyboard()
self._set_attr_values()
def _init_keyboard(self):
"""
Initialise the keyboard
"""
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
"""
Check if there is an arrow input
"""
if ((keycode[1] == 'up' and not self._is_going_to("down"))
or (keycode[1] == 'down' and not self._is_going_to("up"))
or (keycode[1] == 'left' and not self._is_going_to("right"))
or (keycode[1] == 'right' and not self._is_going_to("left"))):
self.position_to_go = keycode[1]
return True
return False
def _keyboard_closed(self):
"""
Close the keyboard
"""
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _set_attr_values(self):
"""
Set the attributes value
"""
self.snake_head.size = (self.step_size, self.step_size)
self.fruit.size = self.snake_head.size
self.score = 0
def start(self):
"""
Start the game.
"""
self._add_objects_on_grid()
self._add_cells_to_tail()
def _add_objects_on_grid(self):
"""
Add snake head and fruit on the grid
"""
self.snake_head.pos = random_pos_on_grid(self.step_size, Window.width, Window.height)
self.fruit.move(random_pos_on_grid(self.step_size, Window.width, Window.height))
def _add_cells_to_tail(self):
"""
Add 2 cells to the tail.
:return:
"""
self._growth_tail((self.snake_head.pos[0] - self.step_size, self.snake_head.pos[1]))
self._growth_tail((self.snake_head.pos[0] - (2 * self.step_size), self.snake_head.pos[1]))
def _growth_tail(self, pos):
"""
Growth the snake's tail
:param pos: The position where we want to add the new cell snake
"""
self.tail.append(
SnakeCell(
pos=pos,
size=self.snake_head.size
)
)
self.add_widget(self.tail[-1])
def update(self, dt):
"""
Move the snake and check if it touch the fruit or if it's the end of the game
"""
if not self.is_game_over:
self.snake_head.go_to(self.position_to_go, self.step_size)
self._on_touch_wall()
self._move_snake()
if not self.is_game_over:
if self._snake_head_is_touching(self.fruit):
self._on_touch_fruit()
def _on_touch_wall(self):
"""
Check if the snake head touch a wall
"""
if (self.snake_head.y < 0) or (self.snake_head.y + self.step_size > Window.height):
self.snake_head.y = (int(Window.height / self.step_size) * self.step_size) - self.step_size \
if (self.snake_head.y < 0) else 0
if (self.snake_head.x < 0) or (self.snake_head.x + self.step_size > Window.width):
self.snake_head.x = (int(Window.width / self.step_size) * self.step_size) - self.step_size \
if (self.snake_head.x < 0) else 0
def _move_snake(self):
for i in range(1, len(self.tail)):
if self._snake_head_is_touching(self.tail[i]):
self.game_over()
self.tail[-i].move_pos(self.tail[-(i + 1)].pos)
self.tail[0].move_pos(self.snake_head.pos)
self.snake_head.move()
def _snake_head_is_touching(self, obj):
"""
Check if snake head is touching an object
:param obj: the object we want to check is snake head is touching it
:return: True if snake head is touching it
"""
return obj.pos[0] <= self.snake_head.pos[0] < obj.pos[0] + self.step_size and \
obj.pos[1] <= self.snake_head.pos[1] < obj.pos[1] + self.step_size
def _on_touch_fruit(self):
"""
The snake get the point. The fruit respawn and the snake tail growth.
"""
self.score += 1
self.fruit.move(random_pos_on_grid(self.step_size, Window.width, Window.height))
self._growth_tail(self.tail[-1].pos)
def game_over(self):
self.snake_head.go_to("stop", self.step_size)
for cell in self.tail:
cell.go_to("stop", self.step_size)
self._flash_screen(0.5)
self.is_game_over = True
self.label_game_over.text = "Game Over"
def _flash_screen(self, time):
"""
Flash the window
"""
anim = Animation(opacity=0, duration=time)
anim += Animation(opacity=1, duration=time)
anim.repeat = True
anim.start(self)
def _is_going_to(self, direction):
"""
check if the snake is going to a specific direction
:param direction: right, left, up or down
:return: true if the snake is going to this direction otherwise else
"""
if direction == "right":
return self.snake_head.velocity_x == self.step_size and self.snake_head.velocity_y == 0
if direction == "left":
return self.snake_head.velocity_x == -self.step_size and self.snake_head.velocity_y == 0
if direction == "up":
return self.snake_head.velocity_x == 0 and self.snake_head.velocity_y == self.step_size
if direction == "down":
return self.snake_head.velocity_x == 0 and self.snake_head.velocity_y == -self.step_size
class SnakeApp(App):
def build(self):
game = SnakeGame(STEP_SIZE, WINDOW_WIDTH, WINDOW_HEIGHT)
game.start()
Clock.schedule_interval(game.update, 10.0 / 60.0)
return game
if __name__ == '__main__':
SnakeApp().run()