@@ -48,17 +48,37 @@ def increment_score():
48
48
49
49
def render_point (point , color ):
50
50
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' )
56
56
57
57
def render_food (food ):
58
58
render_point (food , 'red' )
59
59
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
+
60
79
def render_snake (snake ):
61
- render_point (snake [- 1 ], 'blue' )
80
+ render_head (snake )
81
+
62
82
for point in snake .tail :
63
83
render_point (point , 'black' )
64
84
@@ -72,15 +92,14 @@ def game_loop():
72
92
else :
73
93
snake .move ()
74
94
75
- if snake [ - 1 ] in snake .tail :
95
+ if snake . head in snake .tail :
76
96
text = canvas .create_text (SCREEN_WIDTH / 2 , SCREEN_WIDTH / 2 ,
77
97
fill = 'black' ,font = 'courier 80 bold' ,
78
98
text = GAME_OVER_MSG )
79
99
text_frame = canvas .create_rectangle (canvas .bbox (text ),
80
100
fill = 'white' ,
81
101
outline = 'white' )
82
102
canvas .tag_lower (text_frame , text )
83
-
84
103
else :
85
104
canvas .delete ('all' )
86
105
render_food (food )
0 commit comments