from typing import Any import pygame import sys import random class Crosshair(pygame.sprite.Sprite): def __init__(self, picture_path): super().__init__() self.image = pygame.image.load("crosshair_blue_large.png") self.rect = self.image.get_rect() self.gunshot = pygame.mixer.Sound("boom.wav") def shoot(self): self.gunshot.play() pygame.sprite.spritecollide(crosshair, target_group, True) def update(self): self.rect.center = pygame.mouse.get_pos() class Target(pygame.sprite.Sprite): def __init__(self, picture_path, pos_x, pos_y): super().__init__() self.image = pygame.image.load(picture_path) self.rect = self.image.get_rect() self.rect.center = [pos_x, pos_y] self.speed = random.randint(1, 3) def update(self): self.rect.x += self.speed if self.rect.left > screen_width: self.rect.right = 0 self.rect.centery = random.randint(0, screen_height) def respawn_targets(): for _ in range(20): new_target = Target("target_red1.png", random.randrange(0, screen_width), random.randrange(0, screen_height)) target_group.add(new_target) # Setup pygame.init() clock = pygame.time.Clock() # Screen for game screen_width = 1000 screen_height = 800 screen = pygame.display.set_mode((screen_width, screen_height)) # The background background = pygame.image.load("bg_wood.png").convert() pygame.mouse.set_visible(False) # The crosshair crosshair = Crosshair("crosshair_blue_large.png") crosshair_group = pygame.sprite.Group() crosshair_group.add(crosshair) # Targets target_group = pygame.sprite.Group() respawn_targets() # Calculate number of tiles needed to cover the screen num_tiles_x = (screen_width // background.get_width()) + 1 num_tiles_y = (screen_height // background.get_height()) + 1 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: crosshair.shoot() # Update targets target_group.update() # Draw background tiles for y in range(num_tiles_y): for x in range(num_tiles_x): screen.blit(background, (x * background.get_width(), y * background.get_height())) target_group.draw(screen) crosshair_group.update() crosshair_group.draw(screen) pygame.display.flip() clock.tick(60) # Respawn targets if all are gone if len(target_group) == 0: respawn_targets() target_group.draw(screen) crosshair_group.update() crosshair_group.draw(screen) pygame.display.flip() clock.tick(60)