-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pygame | ||
|
||
class Button: | ||
"""A class to build buttons for the game.""" | ||
|
||
def __init__(self, ai_game, msg): | ||
"""Initialize button attributes.""" | ||
self.screen = ai_game.screen | ||
self.screen_rect = self.screen.get_rect() | ||
|
||
# Set the dimensions and properties of the button. | ||
self.width, self.height = 200, 100 | ||
self.button_image = pygame.image.load('images/gradient.png') | ||
self.button_image = pygame.transform.scale(self.button_image, | ||
(self.width, self.height)) | ||
self.text_color = (255, 255, 255) | ||
self.font = pygame.font.SysFont('microsoftyahei', 48) | ||
|
||
# Build the button's rect object and center it. | ||
self.rect = pygame.Rect(0, 0, self.width, self.height) | ||
self.rect.center = self.screen_rect.center | ||
|
||
# The button message needs to be prepared only once. | ||
self._prep_msg(msg) | ||
|
||
def _prep_msg(self, msg): | ||
"""Turn msg into a rendered image and center text on the button.""" | ||
self.msg_image = self.font.render(msg, True, self.text_color) | ||
self.msg_image_rect = self.msg_image.get_rect() | ||
self.msg_image_rect.center = self.rect.center | ||
|
||
def draw_button(self): | ||
"""Draw blank button and then draw message.""" | ||
self.screen.blit(self.button_image, self.rect) | ||
self.screen.blit(self.msg_image, self.msg_image_rect) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.