MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / Display

Class Display

projects/Snake Game/src/display.py:5–100  ·  view source on GitHub ↗

Manages the display of the game.

Source from the content-addressed store, hash-verified

3
4
5class Display:
6 """Manages the display of the game."""
7
8 def __init__(self):
9 pygame.init()
10 self.width = GameSettings.WIDTH
11 self.height = GameSettings.HEIGHT
12 self.font = pygame.font.Font(None, 25)
13 self.window = pygame.display.set_mode((self.width, self.height))
14 pygame.display.set_caption("Snake")
15 self.clock = pygame.time.Clock()
16
17 def update_ui(self, snake, food, score, high_score):
18 """Updates the UI with the current game state.
19
20 Args:
21 snake (Snake): The snake object that contains the snake body (Snake.blocks).
22 food (Point): The food object to be displayed.
23 score (int): The current game score.
24 high_score: The highest score achieved so far.
25 """
26 self.window.fill(RgbColors.BLACK)
27 self.draw_snake(snake)
28 self.draw_food(food)
29 self.draw_score(score)
30 self.render_high_score(high_score)
31 pygame.display.flip()
32
33 def draw_snake(self, snake):
34 for block in snake.blocks:
35 pygame.draw.rect(
36 self.window,
37 RgbColors.BLUE1,
38 pygame.Rect(
39 block.x, block.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE
40 ),
41 )
42 pygame.draw.rect(
43 self.window,
44 RgbColors.BLUE2,
45 pygame.Rect(block.x + 4, block.y + 4, 12, 12),
46 )
47
48 def draw_food(self, food):
49 pygame.draw.rect(
50 self.window,
51 RgbColors.RED,
52 pygame.Rect(
53 food.x, food.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE
54 ),
55 )
56
57 def draw_score(self, score):
58 self.font = pygame.font.Font(None, 25)
59 score_display = self.font.render(f"Score: {score}", True, RgbColors.WHITE)
60 self.window.blit(score_display, [0, 0])
61
62 def render_game_over(self):

Callers 3

__init__Method · 0.90
setUpMethod · 0.90
test_initMethod · 0.90

Calls

no outgoing calls

Tested by 2

setUpMethod · 0.72
test_initMethod · 0.72