import pygame import sys import random from pygame.locals import * # Define the Crosshair class from Project 1 class Crosshair(pygame.sprite.Sprite): def __init__(self, picture_path): super().__init__() self.image = pygame.image.load(picture_path) self.rect = self.image.get_rect() self.gunshot = pygame.mixer.Sound("pop.mp3") def shoot(self, target_group): targets_hit = pygame.sprite.spritecollide(self, target_group, True) if targets_hit: self.gunshot.play() global score score += 1 def update(self): self.rect.center = pygame.mouse.get_pos() # Define the Target class from Project 1 class Target(pygame.sprite.Sprite): def __init__(self, picture_path, pos_x, pos_y, float_speed): super().__init__() self.image = pygame.image.load(picture_path) self.rect = self.image.get_rect() self.rect.center = [pos_x, pos_y] self.float_speed = float_speed def update(self): self.rect.y -= self.float_speed if self.rect.bottom < 0: self.kill() global lives lives -= 1 # Initialize pygame pygame.init() # Screen dimensions WIDTH = 800 HEIGHT = 600 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Fonts font = pygame.font.Font(None, 36) # Menu options options = ["Shooter", "Platformer", "Exit"] # Set up the screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pygame Title Page") def display_menu(): screen.fill(WHITE) title_text = font.render("Games List", True, BLACK) title_rect = title_text.get_rect(center=(WIDTH // 2, 100)) screen.blit(title_text, title_rect) for i, option in enumerate(options): text = font.render(option, True, BLACK) text_rect = text.get_rect(center=(WIDTH // 2, 250 + i * 50)) screen.blit(text, text_rect) def run_project(project): if project == "Shooter": # Run the main loop of Project 1 run_shooter() def run_shooter(): global score, lives score = 0 lives = 5 # Game state game_over = False # Create sprites crosshair = Crosshair("crosshair.png") target_group = pygame.sprite.Group() all_sprites = pygame.sprite.Group() all_sprites.add(crosshair) # Main game loop clock = pygame.time.Clock() while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return_to_title_page() # Return to the title page when Esc is pressed elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: crosshair.shoot(target_group) # Update all_sprites.update() target_group.update() # Add new target if random.randint(1, 100) == 1: target = Target("target.png", random.randint(0, WIDTH), HEIGHT, 5) target_group.add(target) # Draw screen.fill(WHITE) target_group.draw(screen) all_sprites.draw(screen) # Display score and lives score_text = font.render("Score: " + str(score), True, BLACK) screen.blit(score_text, (10, 10)) lives_text = font.render("Lives: " + str(lives), True, BLACK) screen.blit(lives_text, (10, 40)) pygame.display.flip() clock.tick(60) # Function to return to the title page def return_to_title_page(): global score, lives # Reset any game variables or state score = 0 lives = 5 # Return to the title page loop title_page_loop() # Title page loop def title_page_loop(): # Main loop for the title page running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: pos = pygame.mouse.get_pos() for i, _ in enumerate(options): if (WIDTH // 2 - 100) < pos[0] < (WIDTH // 2 + 100) and (225 + 50 * i) < pos[1] < (275 + 50 * i): selected_option = options[i] if selected_option == "Exit": pygame.quit() sys.exit() else: run_project(selected_option) display_menu() pygame.display.flip() # Start with the title page loop title_page_loop()