-
Notifications
You must be signed in to change notification settings - Fork 2
/
snake.py
52 lines (42 loc) · 1.36 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
from collections import deque
from random import randrange
from settings import MAX_X
from settings import MAX_Y
class Snake:
def __init__(self, body, vector, dot):
self.body = deque(body)
self.vector = vector
self.dot = dot
self.alive = True
self.init_len = len(self.body)
self._previous_step = self.vector
def check_state(self):
head = self.body[0]
if self.body.count(head) > 1:
self.alive = False
elif not -MAX_X <= head.real <= MAX_X:
self.alive = False
elif not -MAX_Y <= head.imag <= MAX_Y:
self.alive = False
def move_forward(self):
head = self.body[0]
next_brick = complex(
head.real + self.vector.real, head.imag + self.vector.imag
)
self.body.appendleft(next_brick)
if head == self.dot:
self.gen_dot()
if next_brick != self.dot:
self.body.pop()
self._previous_step = self.vector
def turn(self, direction):
if self._previous_step + direction != 0:
self.vector = direction
def gen_dot(self):
while self.dot in self.body:
self.dot = complex(
randrange(- MAX_X, MAX_X),
randrange(-MAX_Y, MAX_Y)
)
def get_score(self):
return len(self.body) - self.init_len