|
| 1 | +import pygame |
| 2 | +from constants import RgbColors, GameSettings |
| 3 | + |
| 4 | + |
| 5 | +class Display: |
| 6 | + """Manages the display of the game.""" |
| 7 | + def __init__(self): |
| 8 | + pygame.init() |
| 9 | + self.width = GameSettings.WIDTH |
| 10 | + self.height = GameSettings.HEIGHT |
| 11 | + self.font = pygame.font.Font(None, 25) |
| 12 | + self.window = pygame.display.set_mode((self.width, self.height)) |
| 13 | + pygame.display.set_caption("Snake") |
| 14 | + self.clock = pygame.time.Clock() |
| 15 | + |
| 16 | + def update_ui(self, snake, food, score, high_score): |
| 17 | + """Updates the UI with the current game state. |
| 18 | +
|
| 19 | + Args: |
| 20 | + snake (Snake): The snake object that contains the snake body (Snake.blocks). |
| 21 | + food (Point): The food object to be displayed. |
| 22 | + score (int): The current game score. |
| 23 | + high_score: The highest score achieved so far. |
| 24 | + """ |
| 25 | + self.window.fill(RgbColors.BLACK) |
| 26 | + self.draw_snake(snake) |
| 27 | + self.draw_food(food) |
| 28 | + self.draw_score(score) |
| 29 | + self.render_high_score(high_score) |
| 30 | + pygame.display.flip() |
| 31 | + |
| 32 | + def draw_snake(self, snake): |
| 33 | + for block in snake.blocks: |
| 34 | + pygame.draw.rect( |
| 35 | + self.window, RgbColors.BLUE1, |
| 36 | + pygame.Rect(block.x, block.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE) |
| 37 | + ) |
| 38 | + pygame.draw.rect( |
| 39 | + self.window, RgbColors.BLUE2, pygame.Rect(block.x + 4, block.y + 4, 12, 12) |
| 40 | + ) |
| 41 | + |
| 42 | + def draw_food(self, food): |
| 43 | + pygame.draw.rect( |
| 44 | + self.window, |
| 45 | + RgbColors.RED, |
| 46 | + pygame.Rect(food.x, food.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE), |
| 47 | + ) |
| 48 | + |
| 49 | + def draw_score(self, score): |
| 50 | + self.font = pygame.font.Font(None, 25) |
| 51 | + score_display = self.font.render(f"Score: {score}", True, RgbColors.WHITE) |
| 52 | + self.window.blit(score_display, [0, 0]) |
| 53 | + |
| 54 | + def render_game_over(self): |
| 55 | + self.font = pygame.font.Font(None, 48) |
| 56 | + game_over_display = self.font.render("GAME OVER", True, RgbColors.WHITE) |
| 57 | + text_width = game_over_display.get_width() |
| 58 | + text_height = game_over_display.get_height() |
| 59 | + text_x = (self.width - text_width) // 2 |
| 60 | + text_y = (self.height // 4) - (text_height // 2) |
| 61 | + self.window.blit(game_over_display, |
| 62 | + [text_x, text_y]) |
| 63 | + pygame.display.flip() |
| 64 | + |
| 65 | + def render_play_again(self): |
| 66 | + self.font = pygame.font.Font(None, 32) |
| 67 | + play_again_display = self.font.render("Play again? (Y/N)", True, RgbColors.WHITE) |
| 68 | + display_box = play_again_display.get_rect(center=(self.width // 2, self.height // 2)) |
| 69 | + self.window.blit(play_again_display, display_box) |
| 70 | + pygame.display.flip() |
| 71 | + |
| 72 | + def render_high_score(self, high_score): |
| 73 | + high_score_display = self.font.render(f"High Score: {high_score}", True, RgbColors.WHITE) |
| 74 | + self.window.blit(high_score_display, [self.width - high_score_display.get_width(), 0]) |
| 75 | + |
| 76 | + def render_new_high_score(self, new_high_score): |
| 77 | + new_high_score_display = self.font.render(f"New High Score: {new_high_score}", True, RgbColors.WHITE) |
| 78 | + text_width = new_high_score_display.get_width() |
| 79 | + text_height = new_high_score_display.get_height() |
| 80 | + text_x = (self.width - text_width) // 2 |
| 81 | + text_y = (self.height // 3) - (text_height // 2) |
| 82 | + self.window.blit(new_high_score_display, |
| 83 | + [text_x, text_y]) |
| 84 | + pygame.display.flip() |
0 commit comments