-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnake.py
More file actions
71 lines (53 loc) · 1.99 KB
/
Copy pathsnake.py
File metadata and controls
71 lines (53 loc) · 1.99 KB
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
from enum import Enum
class Directions(Enum):
UP = 1
DOWN = 2
RIGHT = 3
LEFT = 4
class Snake:
def __init__(self, block_size, border, color):
self.block_size = block_size
self.border = border
self.score = 0
self.color = color
self.respawn()
def respawn(self):
self.length = 3
self.body = [(20, 20), (40, 20), (60, 20)]
self.direction = Directions.RIGHT
def draw(self, game, root):
for block in self.body:
game.draw.rect(root, self.color, (block[0], block[1], self.block_size, self.block_size))
def move(self):
head = self.body[-1]
if self.direction == Directions.DOWN :
next_block = (head[0], head[1] + self.block_size)
self.body.append(next_block)
if self.direction == Directions.UP :
next_block = (head[0], head[1] - self.block_size)
self.body.append(next_block)
if self.direction == Directions.LEFT :
next_block = (head[0] - self.block_size, head[1])
self.body.append(next_block)
if self.direction == Directions.RIGHT:
next_block = (head[0] + self.block_size, head[1])
self.body.append(next_block)
if self.length < len(self.body):
self.body.pop(0)
def control(self, direction):
if self.direction == Directions.UP:
self.direction = direction
if self.direction == Directions.DOWN:
self.direction = direction
if self.direction == Directions.LEFT:
self.direction = direction
if self.direction == Directions.RIGHT:
self.direction = direction
def grow_up(self):
self.length += 1
self.score += 10
def eat_food(self, food):
head = self.body[-1]
if head[0] == food.x and head[1] == food.y:
self.grow_up()
food.respawn()