@@ -14,7 +14,12 @@ def __init__(self):
14
14
15
15
def update_ui (self , snake , food , score ):
16
16
self .window .fill (RgbColors .BLACK )
17
- # Draw snake
17
+ self .draw_snake (snake )
18
+ self .draw_food (food )
19
+ self .draw_score (score )
20
+ pygame .display .flip ()
21
+
22
+ def draw_snake (self , snake ):
18
23
for block in snake .blocks :
19
24
pygame .draw .rect (
20
25
self .window , RgbColors .BLUE1 ,
@@ -23,13 +28,32 @@ def update_ui(self, snake, food, score):
23
28
pygame .draw .rect (
24
29
self .window , RgbColors .BLUE2 , pygame .Rect (block .x + 4 , block .y + 4 , 12 , 12 )
25
30
)
26
- # Draw food
31
+
32
+ def draw_food (self , food ):
27
33
pygame .draw .rect (
28
34
self .window ,
29
35
RgbColors .RED ,
30
36
pygame .Rect (food .x , food .y , GameSettings .BLOCK_SIZE , GameSettings .BLOCK_SIZE ),
31
37
)
32
- # Draw score
38
+
39
+ def draw_score (self , score ):
33
40
score_display = self .font .render (f"Score: { score } " , True , RgbColors .WHITE )
34
- self .window .blit (score_display , [0 , 0 ]) # score in top left corner of window
41
+ self .window .blit (score_display , [0 , 0 ]) # score in top left window corner
42
+
43
+ def render_game_over (self ):
44
+ self .font = pygame .font .Font (None , 48 )
45
+ game_over_display = self .font .render ("GAME OVER" , True , RgbColors .WHITE )
46
+ text_width = game_over_display .get_width ()
47
+ text_height = game_over_display .get_height ()
48
+ text_x = (self .width - text_width ) // 2
49
+ text_y = (self .height // 4 ) - (text_height // 2 )
50
+ self .window .blit (game_over_display ,
51
+ [text_x , text_y ])
52
+ pygame .display .flip ()
53
+
54
+ def render_play_again (self ):
55
+ self .font = pygame .font .Font (None , 32 )
56
+ play_again_display = self .font .render ("Play again? (Y/N)" , True , RgbColors .WHITE )
57
+ display_box = play_again_display .get_rect (center = (self .width // 2 , self .height // 2 ))
58
+ self .window .blit (play_again_display , display_box )
35
59
pygame .display .flip ()
0 commit comments