"""Menus and game states: a title menu, playing, paused and game over, with mouse and keyboard buttons.""" import pygame WIDTH, HEIGHT = 640, 480 BG = (20, 26, 46) WHITE = (230, 236, 247) YELLOW = (244, 197, 49) BLUE = (91, 147, 230) class Button: def __init__(self, text, center, action): self.text = text self.rect = pygame.Rect(0, 0, 240, 52) self.rect.center = center self.action = action def draw(self, surface, font, selected): pygame.draw.rect(surface, YELLOW if selected else BLUE, self.rect, border_radius=8) label = font.render(self.text, True, BG) surface.blit(label, label.get_rect(center=self.rect.center)) class Menu: """A vertical list of buttons: arrow keys + Enter, or hover + click.""" def __init__(self, buttons): self.buttons = buttons self.selected = 0 def handle(self, event): if event.type == pygame.KEYDOWN: if event.key in (pygame.K_DOWN, pygame.K_s): self.selected = (self.selected + 1) % len(self.buttons) elif event.key in (pygame.K_UP, pygame.K_w): self.selected = (self.selected - 1) % len(self.buttons) elif event.key in (pygame.K_RETURN, pygame.K_SPACE): return self.buttons[self.selected].action elif event.type == pygame.MOUSEMOTION: for i, b in enumerate(self.buttons): if b.rect.collidepoint(event.pos): self.selected = i elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: for b in self.buttons: if b.rect.collidepoint(event.pos): return b.action return None def draw(self, surface, font): for i, b in enumerate(self.buttons): b.draw(surface, font, i == self.selected) pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() title_font = pygame.font.Font(None, 72) font = pygame.font.Font(None, 36) state = "menu" # menu | playing | paused | game_over main_menu = Menu([Button("Play", (WIDTH // 2, 230), "play"), Button("Quit", (WIDTH // 2, 300), "quit")]) pause_menu = Menu([Button("Resume", (WIDTH // 2, 230), "resume"), Button("Main menu", (WIDTH // 2, 300), "menu")]) over_menu = Menu([Button("Play again", (WIDTH // 2, 260), "play"), Button("Main menu", (WIDTH // 2, 330), "menu")]) time_left = 0.0 score = 0 target = pygame.Rect(0, 0, 40, 40) def start_game(): global time_left, score time_left, score = 10.0, 0 target.center = (WIDTH // 2, HEIGHT // 2) running = True while running: dt = clock.tick(60) / 1000 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif state == "menu": action = main_menu.handle(event) if action == "play": start_game() state = "playing" elif action == "quit": running = False elif state == "playing": if event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_p): state = "paused" elif event.type == pygame.MOUSEBUTTONDOWN and target.collidepoint(event.pos): score += 1 target.x = (target.x * 7 + 131) % (WIDTH - 40) # move somewhere else target.y = 60 + (target.y * 5 + 97) % (HEIGHT - 120) elif state == "paused": action = pause_menu.handle(event) if action == "resume" or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): state = "playing" elif action == "menu": state = "menu" elif state == "game_over": action = over_menu.handle(event) if action == "play": start_game() state = "playing" elif action == "menu": state = "menu" if state == "playing": # only the game updates time_left -= dt if time_left <= 0: state = "game_over" screen.fill(BG) if state == "menu": t = title_font.render("Click Rush", True, YELLOW) screen.blit(t, t.get_rect(center=(WIDTH // 2, 130))) main_menu.draw(screen, font) elif state in ("playing", "paused"): pygame.draw.rect(screen, YELLOW, target, border_radius=6) screen.blit(font.render(f"Score {score} Time {max(0, time_left):.1f}", True, WHITE), (16, 12)) if state == "paused": shade = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) shade.fill((0, 0, 0, 160)) screen.blit(shade, (0, 0)) pause_menu.draw(screen, font) elif state == "game_over": t = title_font.render(f"Score: {score}", True, YELLOW) screen.blit(t, t.get_rect(center=(WIDTH // 2, 150))) over_menu.draw(screen, font) pygame.display.flip() pygame.quit()