forked from PyMLGame/pymlsnake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
85 lines (76 loc) · 2.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
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
# -*- coding: utf-8 -*-
"""
Snake game object
"""
__author__ = 'Ricardo Band'
__copyright__ = 'Copyright 2014, Ricardo Band'
__credits__ = ['Ricardo Band']
__license__ = 'MIT'
__version__ = '1.0.0'
__maintainer__ = 'Ricardo Band'
__email__ = '[email protected]'
__status__ = 'Development'
# direction for snake travel
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
class Snake(object):
"""
The snake on the screen eating apples and crashing into things
"""
def __init__(self, parts, direction, dimensions):
"""
Create a snake with its initial parts and direction.
"""
self.parts = parts # last part is head
self.direction = direction
self.dimensions = dimensions
self.grow = False
def get_pos(self):
"""
Returns the position of the head of the snake.
"""
return self.parts[-1]
def check_bite(self):
"""
Checks if you bite yourself in the tail.
"""
if len(self.parts) != len(set(self.parts)):
return True
else:
return False
def move(self):
"""
Add a new pixel to the snakes head and delete the last one.
"""
# add new tile to front
head = self.parts[-1]
if self.direction == UP:
if head[1] == 0:
self.parts.append((head[0], self.dimensions[1] - 1))
else:
self.parts.append((head[0], head[1] - 1))
elif self.direction == DOWN:
if head[1] == self.dimensions[1] - 1:
self.parts.append((head[0], 0))
else:
self.parts.append((head[0], head[1] + 1))
elif self.direction == LEFT:
if head[0] == 0:
self.parts.append((self.dimensions[0] - 1, head[1]))
else:
self.parts.append((head[0] - 1, head[1]))
elif self.direction == RIGHT:
if head[0] == self.dimensions[0] - 1:
self.parts.append((0, head[1]))
else:
self.parts.append((head[0] + 1, head[1]))
if self.check_bite():
return False
else:
# delete last part (first in the list)
if not self.grow:
self.parts.pop(0)
self.grow = False
return True