"""Star Catcher: the complete game from the "Build your first game" tutorial.""" import random import pygame # --- Settings --------------------------------------------------------------- WIDTH, HEIGHT = 640, 480 FPS = 60 PLAYER_SPEED = 400 # pixels per second START_LIVES = 3 BG = (20, 26, 46) WHITE = (230, 236, 247) YELLOW = (244, 197, 49) BLUE = (91, 147, 230) RED = (232, 93, 82) # --- Game objects ------------------------------------------------------------ class Basket: def __init__(self): self.rect = pygame.Rect(0, 0, 90, 20) self.rect.midbottom = (WIDTH // 2, HEIGHT - 20) self.x = float(self.rect.x) def update(self, dt, keys): direction = (keys[pygame.K_RIGHT] or keys[pygame.K_d]) - (keys[pygame.K_LEFT] or keys[pygame.K_a]) self.x += direction * PLAYER_SPEED * dt self.x = max(0, min(WIDTH - self.rect.width, self.x)) self.rect.x = round(self.x) def draw(self, surface): pygame.draw.rect(surface, BLUE, self.rect, border_radius=6) class Star: def __init__(self, speed): self.rect = pygame.Rect(random.randint(10, WIDTH - 30), -20, 20, 20) self.y = float(self.rect.y) self.speed = speed def update(self, dt): self.y += self.speed * dt self.rect.y = round(self.y) def draw(self, surface): pygame.draw.circle(surface, YELLOW, self.rect.center, 10) # --- Setup ------------------------------------------------------------------- pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Star Catcher") clock = pygame.time.Clock() font = pygame.font.Font(None, 36) big_font = pygame.font.Font(None, 72) def new_game(): return {"basket": Basket(), "stars": [], "score": 0, "lives": START_LIVES, "spawn_timer": 0.0, "over": False} game = new_game() # --- Main loop --------------------------------------------------------------- running = True while running: dt = clock.tick(FPS) / 1000 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and game["over"]: game = new_game() if not game["over"]: keys = pygame.key.get_pressed() game["basket"].update(dt, keys) # Spawn stars faster as the score goes up. game["spawn_timer"] -= dt if game["spawn_timer"] <= 0: speed = 180 + game["score"] * 6 game["stars"].append(Star(speed)) game["spawn_timer"] = max(0.35, 1.0 - game["score"] * 0.02) for star in game["stars"][:]: # iterate over a copy so we can remove star.update(dt) if star.rect.colliderect(game["basket"].rect): game["stars"].remove(star) game["score"] += 1 elif star.rect.top > HEIGHT: game["stars"].remove(star) game["lives"] -= 1 if game["lives"] <= 0: game["over"] = True # Draw screen.fill(BG) for star in game["stars"]: star.draw(screen) game["basket"].draw(screen) screen.blit(font.render(f"Score: {game['score']}", True, WHITE), (16, 12)) lives_text = font.render(f"Lives: {game['lives']}", True, RED) screen.blit(lives_text, (WIDTH - lives_text.get_width() - 16, 12)) if game["over"]: text = big_font.render("Game over", True, YELLOW) screen.blit(text, text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 20))) hint = font.render("Press Space to play again", True, WHITE) screen.blit(hint, hint.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 30))) pygame.display.flip() pygame.quit()