Source code for src.menu

import pygame
import sys

from settings import *


screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


[docs] def get_font(size): """ Get a Pygame font object with a specified size. Parameters ---------- size : int The font size. Returns ------- pygame.font.Font A Pygame font object. """ return pygame.font.Font("src\graphics\button\font.ttf", size)
[docs] class Button: """ Represents a clickable button in the game. Attributes ---------- image : pygame.Surface The image representing the button. x_pos : int The x-coordinate of the button's center. y_pos : int The y-coordinate of the button's center. font : pygame.font.Font The font used for rendering text on the button. base_color : str The base color of the button's text. hovering_color : str The color of the button's text when hovering. text_input : str The text displayed on the button. text : pygame.Surface The rendered text surface. rect : pygame.Rect The rectangular area of the button. text_rect : pygame.Rect The rectangular area of the rendered text. Methods ------- update(screen) Updates and renders the button on the given screen. check_for_input(position) Checks if a given position is within the button's area. change_color(position) Changes the color of the button's text based on the mouse position. """ def __init__(self, image, pos, text_input, font, base_color, hovering_color): """ Initializes the Button instance. Parameters ---------- image : pygame.Surface The image representing the button. pos : tuple The x, y coordinates of the button's center. text_input : str The text displayed on the button. font : pygame.font.Font The font used for rendering text on the button. base_color : str The base color of the button's text. hovering_color : str The color of the button's text when hovering. Returns ------- None. """ self.image = image self.x_pos = pos[0] self.y_pos = pos[1] self.font = font self.base_color, self.hovering_color = base_color, hovering_color self.text_input = text_input self.text = self.font.render(self.text_input, True, self.base_color) if self.image is None: self.image = self.text self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos)) self.text_rect = self.text.get_rect(center=(self.x_pos, self.y_pos))
[docs] def update(self, screen): """ Updates and renders the button on the given screen. Parameters ---------- screen : pygame.Surface The screen where the button will be rendered. Returns ------- None. """ if self.image is not None: screen.blit(self.image, self.rect) screen.blit(self.text, self.text_rect)
[docs] def check_for_input(self, position): """ Checks if a given position is within the button's area. Parameters ---------- position : tuple The x, y coordinates of the position to check. Returns ------- bool True if the position is within the button's area, False otherwise. """ return self.rect.collidepoint(position)
[docs] def change_color(self, position): """ Changes the color of the button's text based on the mouse position. Parameters ---------- position : tuple The x, y coordinates of the mouse position. Returns ------- None. """ if self.check_for_input(position): self.text = self.font.render(self.text_input, True, self.hovering_color) else: self.text = self.font.render(self.text_input, True, self.base_color)