Skip to content

Commit e20a8b4

Browse files
authored
Merge pull request #2 from procaconsul/open-mouth
Make snake open mouth to consume food
2 parents 99ff79e + 6917de2 commit e20a8b4

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

main.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,40 @@ def increment_score():
4646
score += 1
4747
score_var.set(f"Score: {score}")
4848

49-
def render_point(point, color):
49+
def render_point(point, color, square=True):
5050
coord_x, coord_y = point.x * TILE_WIDTH, point.y * TILE_WIDTH
51-
canvas.create_rectangle(coord_x, coord_y,
52-
coord_x + TILE_WIDTH,
53-
coord_y + TILE_WIDTH,
54-
fill=color,
55-
outline='white')
51+
renderer = canvas.create_rectangle if square else canvas.create_oval
52+
return renderer(coord_x, coord_y,
53+
coord_x + TILE_WIDTH,
54+
coord_y + TILE_WIDTH,
55+
fill=color,
56+
outline='white')
5657

5758
def render_food(food):
58-
render_point(food, 'red')
59+
render_point(food, 'red', square=False)
60+
61+
def render_head(snake):
62+
if snake.next_position() == food or snake.head == food:
63+
x, y = snake.head
64+
x, y = x * TILE_WIDTH, y * TILE_WIDTH
65+
midpoint = [x + (TILE_WIDTH / 2), y + (TILE_WIDTH / 2)]
66+
67+
bbox = [[x, y],
68+
[x + TILE_WIDTH, y],
69+
[x + TILE_WIDTH, y + TILE_WIDTH],
70+
[x, y + TILE_WIDTH]]
71+
72+
bbox.insert(snake.direction.value + 1, midpoint)
73+
74+
bbox = [coord for point in bbox for coord in point]
75+
76+
canvas.create_polygon(bbox, fill="blue", outline="white")
77+
else:
78+
render_point(snake.head, "blue")
5979

6080
def render_snake(snake):
61-
render_point(snake[-1], 'blue')
81+
render_head(snake)
82+
6283
for point in snake.tail:
6384
render_point(point, 'black')
6485

@@ -72,15 +93,14 @@ def game_loop():
7293
else:
7394
snake.move()
7495

75-
if snake[-1] in snake.tail:
96+
if snake.head in snake.tail:
7697
text = canvas.create_text(SCREEN_WIDTH/2, SCREEN_WIDTH/2,
7798
fill='black',font='courier 80 bold',
7899
text=GAME_OVER_MSG)
79100
text_frame = canvas.create_rectangle(canvas.bbox(text),
80101
fill='white',
81102
outline='white')
82103
canvas.tag_lower(text_frame, text)
83-
84104
else:
85105
canvas.delete('all')
86106
render_food(food)

snake.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@ def __getitem__(self, index):
2323
return list(self.body)[index]
2424

2525
def change_direction(self, new_direction):
26-
if new_direction.value + self.direction.value:
26+
if not self.direction.is_opposite(new_direction):
2727
self.direction = new_direction
2828

2929
def move(self, grow=False):
30-
tail = self.body[0] if grow else self.body.popleft()
31-
x, y = self.body[-1] if len(self.body) else tail
32-
d_x, d_y = DIRECTION_VECTOR[self.direction]
33-
self.body.append(Point((x + d_x) % self.env_width,
34-
(y + d_y) % self.env_width))
30+
new_head = self.next_position()
31+
if not grow:
32+
self.body.popleft()
33+
self.body.append(new_head)
3534

35+
def next_position(self):
36+
x, y = self.head
37+
dx, dy = DIRECTION_VECTOR[self.direction]
38+
return Point((x + dx) % self.env_width,
39+
(y + dy) % self.env_width)
40+
41+
@property
42+
def head(self):
43+
return self[-1]
3644

3745
@property
3846
def tail(self):

utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
from collections import namedtuple
33

44
class Direction(Enum):
5-
NORTH = 1
6-
EAST = 2
7-
SOUTH = -1
8-
WEST = -2
5+
NORTH = 0
6+
EAST = 1
7+
SOUTH = 2
8+
WEST = 3
9+
10+
def is_opposite(self, other):
11+
return self.value + other.value % 2 == 0
912

1013
Point = namedtuple('Point', 'x, y')

0 commit comments

Comments
 (0)