class Board: """Generic board class for displaying score type information""" def __init__(self, config, screen, text, text_color, border_color): # display info and config self.screen = screen self.screen_rect = screen.get_rect() self.config = config # font, text, image settings self.text = str(text) self.text_color = text_color self.font = SysFont(None, 48) self.border = True self.width, self.height = self.font.size(text) self.width, self.height = (self.width * 2), (self.height + self.height / 6) self.rect = Rect(0, 0, self.width, self.height) self.border_color = border_color self.image = None self.image_rect = None def prep_board(self): """Render text as a displayable image""" raise NotImplementedError('Objects inheriting from Board must implement a prep_board method') def place_board(self): """Place the board on the screen""" raise NotImplementedError('Objects inheriting from Board must implement a place_board method') def show(self): """Draw the board to the screen""" if self.border: draw.rect(self.screen, self.border_color, self.rect, 3) self.screen.blit(self.image, self.image_rect)
class Button: """Represents a click-able button""" def __init__(self, config, screen, msg): self.config = config self.screen = screen self.screen_rect = screen.get_rect() # Dimensions and properties of the button self.button_color = (0, 255, 0) self.text_color = (255, 255, 255) self.font = SysFont(None, 48) # Build button rect and center it self.width, self.height = self.font.size(msg) self.width, self.height = (self.width + self.width / 4), (self.height + self.height / 4) self.rect = Rect(0, 0, self.width, self.height) self.rect.centerx = (self.config.screen_width // 2) self.rect.centery = int(self.config.screen_height * 0.75) # Prep button message only once self.msg_image, self.msg_image_rect = None, None self.prep_msg(msg) def check_button(self, mouse_x, mouse_y): """Check if the given button has been pressed""" if self.rect.collidepoint(mouse_x, mouse_y): return True else: return False def prep_msg(self, msg): """Turn msg into a rendered image and center it on the button""" self.msg_image = self.font.render(msg, True, self.text_color, self.config.bg_color) self.msg_image_rect = self.msg_image.get_rect() self.msg_image_rect.center = self.rect.center def draw_button(self): """Draw button and then draw message""" draw.rect(self.screen, self.button_color, self.rect, 3) self.screen.blit(self.msg_image, self.msg_image_rect)