Guest User

Untitled

a guest
Dec 16th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | Source Code | 0 0
  1. import pygame as py
  2. from sys import exit
  3.  
  4. # Initialize Pygame
  5. py.init()
  6.  
  7. # Constants
  8. WIDTH, HEIGHT = 900, 600
  9. SCREEN = py.display.set_mode((WIDTH, HEIGHT))
  10. py.display.set_caption("Keyboard Trainer")
  11. CLOCK = py.time.Clock()
  12.  
  13. # Color palettes
  14. PRIMARY_C = [
  15.     "#2C6E49",
  16.     "#556B78",
  17.     "#264653",
  18.     "#3A6351",
  19.     "#FF6F61",
  20.     "#F94144",
  21.     "#F72585",
  22.     "#FFAD05",
  23. ]
  24. SECONDARY_C = [
  25.     "#A2C5AC",
  26.     "#8CA1A5",
  27.     "#2A9D8F",
  28.     "#74B49B",
  29.     "#F9C74F",
  30.     "#F3722C",
  31.     "#4CC9F0",
  32.     "#FFE156",
  33. ]
  34. NEUTRAL_C = [
  35.     "#3E4E50",
  36.     "#424B54",
  37.     "#4F4F4F",
  38.     "#5B6D5B",
  39.     "#2B2B2B",
  40.     "#343A40",
  41.     "#3D3D3D",
  42.     "#4A4A4A",
  43. ]
  44. ACCENT_C = [
  45.     "#F4A261",
  46.     "#D4A373",
  47.     "#E9C46A",
  48.     "#E9A16E",
  49.     "#90BE6D",
  50.     "#43AA8B",
  51.     "#7209B7",
  52.     "#6A4C93",
  53. ]
  54. BACKGROUND_C = [
  55.     "#1E1E1E",
  56.     "#1C1C21",
  57.     "#1A202C",
  58.     "#20231F",
  59.     "#121212",
  60.     "#1A1A1A",
  61.     "#0F0F0F",
  62.     "#171717",
  63. ]
  64. ALT_C = [
  65.     "#D78DAB",
  66.     "#A99287",
  67.     "#D99A8E",
  68.     "#C59AAB",
  69.     "#009D9E",
  70.     "#06BEBB",
  71.     "#08DA7A",
  72.     "#004ED2",
  73. ]
  74.  
  75. i_color = 4
  76.  
  77. def draw_button(screen, color, rect, text, textsize, textcolor, colorindex, border_radius=None, **kwargs):
  78.     """Draw a button with the given parameters."""
  79.     button_rect = py.Rect(rect)
  80.     for position, value in kwargs.items():
  81.         if hasattr(button_rect, position):
  82.             setattr(button_rect, position, value)
  83.  
  84.     mouse_pos = py.mouse.get_pos()
  85.     if button_rect.collidepoint(mouse_pos):
  86.         color = ALT_C
  87.  
  88.     if border_radius is None:
  89.         py.draw.rect(screen, color[colorindex], button_rect)
  90.     else:
  91.         py.draw.rect(screen, color[colorindex], button_rect, border_radius=border_radius)
  92.  
  93.     font = py.font.Font(None, textsize)
  94.     rendered_text = font.render(text, True, textcolor[colorindex])
  95.     text_rect = rendered_text.get_rect(center=button_rect.center)
  96.     screen.blit(rendered_text, text_rect)
  97.     return button_rect
  98.  
  99. def render_text(screen, text, size, color, colorindex, **kwargs):
  100.     """Render text on the screen with the given parameters."""
  101.     font = py.font.Font(None, size)
  102.     rendered_text = font.render(text, True, color[colorindex])
  103.     text_rect = rendered_text.get_rect(**kwargs)
  104.     return rendered_text, text_rect
  105.  
  106. def menu(colorindex):
  107.     """Render the main menu screen."""
  108.     SCREEN.fill(BACKGROUND_C[colorindex])
  109.    
  110.     # Title and Finger text
  111.     title, title_rect = render_text(SCREEN, "Keyboard Trainer", 50, PRIMARY_C, colorindex, center=(WIDTH / 2, HEIGHT / 10))
  112.     finger, finger_rect = render_text(SCREEN, "Fingers", 35, SECONDARY_C, colorindex, center=(WIDTH / 2, HEIGHT / 3))
  113.    
  114.     # Buttons
  115.     prev_button = draw_button(SCREEN, PRIMARY_C, (0, 0, 100, 50), "Prev.", 35, SECONDARY_C, colorindex, bottomleft=(5, HEIGHT - 5), border_radius=5)
  116.     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)
  117.     start_button = draw_button(SCREEN, PRIMARY_C, (WIDTH / 2 - 100, HEIGHT / 2 - 25, 200, 50), "Play", 50, ALT_C, colorindex, border_radius=5)
  118.    
  119.     return title, title_rect, finger, finger_rect, prev_button, next_button, start_button
  120.  
  121. def main():
  122.     global i_color
  123.     current_state = "menu"
  124.    
  125.     while True:
  126.         for event in py.event.get():
  127.             if event.type == py.QUIT:
  128.                 py.quit()
  129.                 exit()
  130.             if current_state == "menu":
  131.                 if event.type == py.KEYDOWN:
  132.                     if event.key == py.K_RIGHT:
  133.                         i_color = (i_color + 1) % len(PRIMARY_C)
  134.                     elif event.key == py.K_LEFT:
  135.                         i_color = (i_color - 1) % len(PRIMARY_C)
  136.                 if event.type == py.MOUSEBUTTONDOWN:
  137.                     mouse_pos = py.mouse.get_pos()
  138.                     title, title_rect, finger, finger_rect, prev_button, next_button, start_button = menu(i_color)
  139.                     if prev_button.collidepoint(mouse_pos):
  140.                         i_color = (i_color - 1) % len(PRIMARY_C)
  141.                     elif next_button.collidepoint(mouse_pos):
  142.                         i_color = (i_color + 1) % len(PRIMARY_C)
  143.                     elif start_button.collidepoint(mouse_pos):
  144.                         print("Start game!")
  145.        
  146.         if current_state == "menu":
  147.             title, title_rect, finger, finger_rect, prev_button, next_button, start_button = menu(i_color)
  148.             SCREEN.blit(title, title_rect)
  149.             SCREEN.blit(finger, finger_rect)
  150.  
  151.         py.display.update()
  152.         CLOCK.tick(60)
  153.  
  154. if __name__ == "__main__":
  155.     main()
  156.  
Advertisement
Add Comment
Please, Sign In to add comment