-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
54 lines (45 loc) · 2.19 KB
/
button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pygame.font
class Button:
"""A class to manage buttons in the game."""
def __init__(self, game_instance, message, color):
"""Initialize the button."""
# Setup main surface
self.screen = game_instance.screen
self.screen_rectangle = self.screen.get_rect()
# Setup button properties
self.width, self.height = 200, 50
self.button_color = color
self.text_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 48)
# Rectangular container of button
self.rect = pygame.Rect(0, 0, self.width, self.height)
# Positions button based on color
self.position_button(self.button_color, game_instance)
# Add message onto button
self._prepare_message(message)
def position_button(self, color, game_instance):
"""Positions the button based on color"""
match color:
case game_instance.settings.play_button_color:
self.rect.center = self.screen_rectangle.center
case game_instance.settings.easy_button_color:
new_center = list(self.screen_rectangle.center)
new_center[-1] += 300
self.rect.center = tuple(new_center)
case game_instance.settings.normal_button_color:
new_center = list(self.screen_rectangle.center)
new_center[-1] += 200
self.rect.center = tuple(new_center)
case game_instance.settings.hard_button_color:
new_center = list(self.screen_rectangle.center)
new_center[-1] += 100
self.rect.center = tuple(new_center)
def _prepare_message(self, message):
"""Turns message into rendered image and centers onto the button."""
self.message_image = self.font.render(message, True, self.text_color, self.button_color)
self.message_image_rectangle = self.message_image.get_rect()
self.message_image_rectangle.center = self.rect.center
def draw_button(self):
"""Draws the button and then the message."""
self.screen.fill(self.button_color, self.rect)
self.screen.blit(self.message_image, self.message_image_rectangle)