-
Notifications
You must be signed in to change notification settings - Fork 2
/
snake.py
54 lines (46 loc) · 1.29 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
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 11 17:54:41 2017
@authors: emma, jona
"""
from direction import Direction
from collections import deque
class Snake(object):
def __init__(self, max_x, max_y):
self.body = deque()
self.body.append((max_x // 2, max_y // 2))
self.color = "blue"
def grow(self, direction):
(x, y) = self.body[-1]
if direction == Direction.UP:
y -= 1
elif direction == Direction.DOWN:
y += 1
elif direction == Direction.LEFT:
x -= 1
else:
x += 1
self.body.append((x, y))
def intersects_with_self(self):
if len(self.body) == 1:
return False
else:
x1, y1 = self.body[0]
for i in range(1, len(self.body)):
x2, y2 = self.body[i]
if x1 == x2 and y1 == y2:
return True
return False
def move(self, direction):
(x, y) = self.body[0]
old = self.body.pop()
if direction == Direction.UP:
y -= 1
elif direction == Direction.DOWN:
y += 1
elif direction == Direction.LEFT:
x -= 1
else:
x += 1
self.body.appendleft((x, y))
return old, (x, y)