Skip to content

Commit a655b86

Browse files
committed
Make snake open mouth when approaching food
1 parent e70a239 commit a655b86

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

main.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,37 @@ def increment_score():
4848

4949
def render_point(point, color):
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+
return canvas.create_rectangle(coord_x, coord_y,
52+
coord_x + TILE_WIDTH,
53+
coord_y + TILE_WIDTH,
54+
fill=color,
55+
outline='white')
5656

5757
def render_food(food):
5858
render_point(food, 'red')
5959

60+
def render_head(snake):
61+
if snake.next_position() == food or snake.head == food:
62+
x, y = snake.head
63+
x, y = x * TILE_WIDTH, y * TILE_WIDTH
64+
midpoint = [x + (TILE_WIDTH / 2), y + (TILE_WIDTH / 2)]
65+
66+
bbox = [[x, y],
67+
[x + TILE_WIDTH, y],
68+
[x + TILE_WIDTH, y + TILE_WIDTH],
69+
[x, y + TILE_WIDTH]]
70+
71+
bbox.insert(snake.direction.value + 1, midpoint)
72+
73+
bbox = [coord for point in bbox for coord in point]
74+
75+
canvas.create_polygon(bbox, fill="blue", outline="white")
76+
else:
77+
render_point(snake.head, "blue")
78+
6079
def render_snake(snake):
61-
render_point(snake[-1], 'blue')
80+
render_head(snake)
81+
6282
for point in snake.tail:
6383
render_point(point, 'black')
6484

@@ -72,15 +92,14 @@ def game_loop():
7292
else:
7393
snake.move()
7494

75-
if snake[-1] in snake.tail:
95+
if snake.head in snake.tail:
7696
text = canvas.create_text(SCREEN_WIDTH/2, SCREEN_WIDTH/2,
7797
fill='black',font='courier 80 bold',
7898
text=GAME_OVER_MSG)
7999
text_frame = canvas.create_rectangle(canvas.bbox(text),
80100
fill='white',
81101
outline='white')
82102
canvas.tag_lower(text_frame, text)
83-
84103
else:
85104
canvas.delete('all')
86105
render_food(food)

0 commit comments

Comments
 (0)