Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as py
- from sys import exit
- # Initialize Pygame
- py.init()
- # Constants
- WIDTH, HEIGHT = 900, 600
- SCREEN = py.display.set_mode((WIDTH, HEIGHT))
- py.display.set_caption("Keyboard Trainer")
- CLOCK = py.time.Clock()
- # Color palettes
- PRIMARY_C = [
- "#2C6E49",
- "#556B78",
- "#264653",
- "#3A6351",
- "#FF6F61",
- "#F94144",
- "#F72585",
- "#FFAD05",
- ]
- SECONDARY_C = [
- "#A2C5AC",
- "#8CA1A5",
- "#2A9D8F",
- "#74B49B",
- "#F9C74F",
- "#F3722C",
- "#4CC9F0",
- "#FFE156",
- ]
- NEUTRAL_C = [
- "#3E4E50",
- "#424B54",
- "#4F4F4F",
- "#5B6D5B",
- "#2B2B2B",
- "#343A40",
- "#3D3D3D",
- "#4A4A4A",
- ]
- ACCENT_C = [
- "#F4A261",
- "#D4A373",
- "#E9C46A",
- "#E9A16E",
- "#90BE6D",
- "#43AA8B",
- "#7209B7",
- "#6A4C93",
- ]
- BACKGROUND_C = [
- "#1E1E1E",
- "#1C1C21",
- "#1A202C",
- "#20231F",
- "#121212",
- "#1A1A1A",
- "#0F0F0F",
- "#171717",
- ]
- ALT_C = [
- "#D78DAB",
- "#A99287",
- "#D99A8E",
- "#C59AAB",
- "#009D9E",
- "#06BEBB",
- "#08DA7A",
- "#004ED2",
- ]
- i_color = 4
- def draw_button(screen, color, rect, text, textsize, textcolor, colorindex, border_radius=None, **kwargs):
- """Draw a button with the given parameters."""
- button_rect = py.Rect(rect)
- for position, value in kwargs.items():
- if hasattr(button_rect, position):
- setattr(button_rect, position, value)
- mouse_pos = py.mouse.get_pos()
- if button_rect.collidepoint(mouse_pos):
- color = ALT_C
- if border_radius is None:
- py.draw.rect(screen, color[colorindex], button_rect)
- else:
- py.draw.rect(screen, color[colorindex], button_rect, border_radius=border_radius)
- font = py.font.Font(None, textsize)
- rendered_text = font.render(text, True, textcolor[colorindex])
- text_rect = rendered_text.get_rect(center=button_rect.center)
- screen.blit(rendered_text, text_rect)
- return button_rect
- def render_text(screen, text, size, color, colorindex, **kwargs):
- """Render text on the screen with the given parameters."""
- font = py.font.Font(None, size)
- rendered_text = font.render(text, True, color[colorindex])
- text_rect = rendered_text.get_rect(**kwargs)
- return rendered_text, text_rect
- def menu(colorindex):
- """Render the main menu screen."""
- SCREEN.fill(BACKGROUND_C[colorindex])
- # Title and Finger text
- title, title_rect = render_text(SCREEN, "Keyboard Trainer", 50, PRIMARY_C, colorindex, center=(WIDTH / 2, HEIGHT / 10))
- finger, finger_rect = render_text(SCREEN, "Fingers", 35, SECONDARY_C, colorindex, center=(WIDTH / 2, HEIGHT / 3))
- # Buttons
- prev_button = draw_button(SCREEN, PRIMARY_C, (0, 0, 100, 50), "Prev.", 35, SECONDARY_C, colorindex, bottomleft=(5, HEIGHT - 5), border_radius=5)
- next_button = draw_button(SCREEN, PRIMARY_C, (0, 0, 100, 50), "Next", 35, SECONDARY_C, colorindex, bottomleft=(prev_button.right + 2, HEIGHT - 5), border_radius=5)
- start_button = draw_button(SCREEN, PRIMARY_C, (WIDTH / 2 - 100, HEIGHT / 2 - 25, 200, 50), "Play", 50, ALT_C, colorindex, border_radius=5)
- return title, title_rect, finger, finger_rect, prev_button, next_button, start_button
- def main():
- global i_color
- current_state = "menu"
- while True:
- for event in py.event.get():
- if event.type == py.QUIT:
- py.quit()
- exit()
- if current_state == "menu":
- if event.type == py.KEYDOWN:
- if event.key == py.K_RIGHT:
- i_color = (i_color + 1) % len(PRIMARY_C)
- elif event.key == py.K_LEFT:
- i_color = (i_color - 1) % len(PRIMARY_C)
- if event.type == py.MOUSEBUTTONDOWN:
- mouse_pos = py.mouse.get_pos()
- title, title_rect, finger, finger_rect, prev_button, next_button, start_button = menu(i_color)
- if prev_button.collidepoint(mouse_pos):
- i_color = (i_color - 1) % len(PRIMARY_C)
- elif next_button.collidepoint(mouse_pos):
- i_color = (i_color + 1) % len(PRIMARY_C)
- elif start_button.collidepoint(mouse_pos):
- print("Start game!")
- if current_state == "menu":
- title, title_rect, finger, finger_rect, prev_button, next_button, start_button = menu(i_color)
- SCREEN.blit(title, title_rect)
- SCREEN.blit(finger, finger_rect)
- py.display.update()
- CLOCK.tick(60)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment