#shooting gallery import pygame import sys import random from pygame.locals import * class Crosshair(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(center=(pos_x, pos_y)) def shoot(self): print("hit") 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.original_image = pygame.image.load(picture_path) self.image = self.original_image.copy() self.rect = self.image.get_rect(center=(pos_x, pos_y)) self.reset_position(screen_width, screen_height) def reset_position(self, screen_width, screen_height): new_x = random.randint(0, screen_width) new_y = random.randint(0, screen_height) self.rect.center = (new_x, new_y) # Randomly resize the target scale_factor = random.uniform(0.5, 1.5) # Scale between 50% and 150% new_size = (int(self.original_image.get_width() * scale_factor), int(self.original_image.get_height() * scale_factor)) self.image = pygame.transform.scale(self.original_image, new_size) self.rect = self.image.get_rect(center=(new_x, new_y)) pygame.init() clock = pygame.time.Clock() screen_width = 1920 screen_height = 1080 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.mouse.set_visible(False) # Load background image background = pygame.image.load("Shooting gallery assets/shooting-gallery-pack/PNG/Stall/bg_blue.png") background_width = background.get_width() background_height = background.get_height() # Calculate how many times the background needs to repeat horizontally and vertically repeat_times_x = screen_width // background_width + 1 repeat_times_y = screen_height // background_height + 1 # Create a grid of background positions background_positions = [(i * background_width, j * background_height) for j in range(repeat_times_y) for i in range(repeat_times_x)] # Create Crosshair object crosshair = Crosshair("Shooting gallery assets/shooting-gallery-pack/PNG/HUD/crosshair_white_large.png", screen_width // 2, screen_height // 2) crosshair_group = pygame.sprite.Group() crosshair_group.add(crosshair) # Create Target object target = Target("Shooting gallery assets/shooting-gallery-pack/PNG/Objects/target_red1.png", 500, 500) target_group = pygame.sprite.Group() target_group.add(target) while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if pygame.sprite.spritecollide(crosshair, target_group, False): crosshair.shoot() target.reset_position(screen_width, screen_height) screen.fill((0, 0, 0)) # Clear the screen # Draw background images for pos in background_positions: screen.blit(background, pos) target_group.draw(screen) # Draw targets first crosshair.update() # Update crosshair position crosshair_group.draw(screen) # Draw crosshair last pygame.display.flip() clock.tick(60)