# Import's important parts import random import pygame import sys # Collects important images for lter use score_text = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_score.png") two_dot_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_dots.png") text_0_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_0.png") text_1_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_1.png") text_2_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_2.png") text_3_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_3.png") text_4_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_4.png") text_5_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_5.png") text_6_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_6.png") text_7_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_7.png") text_8_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_8.png") text_9_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_9.png") text_ready_png = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\HUD\text_ready.png") # makes them in a easy to grab library that set's it up for the score board by setting the number of that digit to corresponding text digit_images = { '0': text_0_png, '1': text_1_png, '2': text_2_png, '3': text_3_png, '4': text_4_png, '5': text_5_png, '6': text_6_png, '7': text_7_png, '8': text_8_png, '9': text_9_png, } # Makes the crosshair work by making it a class with a function class Crosshair(pygame.sprite.Sprite): def __init__(self, picture_path): super().__init__() self.image = pygame.image.load(picture_path).convert_alpha() # Usea convert_alpha() for transparency self.rect = self.image.get_rect() # Let's it update itself def update(self): """ Update crosshair position to follow the mouse """ self.rect.center = pygame.mouse.get_pos() # Give's it a shooting function def shoot(self): """ Handle shooting action (currently just prints to console) """ print("Bang!") # General Setup pygame.init() clock = pygame.time.Clock() score = 0 # Game Screen screen_width = 1920 screen_height = 1080 screen = pygame.display.set_mode((screen_width, screen_height)) # target setup class Target(pygame.sprite.Sprite): def __init__(self, picture_path, pos_x, pos_y): super().__init__() self.image = pygame.image.load(r"W:\11\jayden.harris\pygame\PYGAME SHOOTING RANGE FOLDER\PNG\Objects\target_red2_outline.png").convert_alpha() #^loads this image since it's only needed for the Target self.rect = self.image.get_rect() self.rect.x = pos_x self.rect.y = pos_y self.velocity = random.choice([-3, 3]) # Start moving left or right def update(self): # Move target and update itself self.rect.x += self.velocity # Bounce off screen edges if self.rect.left <= 0 or self.rect.right >= screen_width: self.velocity *= -1 # Score function that uses the archive def draw_score(surface, score, x, y): score_str = str(score) surface.blit(score_text, (x, y)) # Draw 'SCORE' label surface.blit(two_dot_png, (x + score_text.get_width(), y)) # Draw colon image offset_x = x + score_text.get_width() + two_dot_png.get_width() + 10 for digit in score_str: digit_img = digit_images[digit] surface.blit(digit_img, (offset_x, y)) offset_x += digit_img.get_width() # pause menu def pause_menu(): font = pygame.font.SysFont(None, 72) prompt_text = font.render("Quit? Press Y to exit or N to continue", True, (255, 255, 255)) prompt_rect = prompt_text.get_rect(center=(screen_width // 2, screen_height // 2)) # Create a semi-transparent overlay overlay = pygame.Surface((screen_width, screen_height)) overlay.fill((0, 0, 0)) # Black overlay while True: screen.blit(overlay, (0, 0)) # Draw overlay screen.blit(prompt_text, prompt_rect) # Draw prompt text pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_y: pygame.quit() sys.exit() elif event.key == pygame.K_n: return # Resume game # Setup target group target_group = pygame.sprite.Group() # Timer event for spawning targets every 600 milliseconds (a sixth of a second) SPAWN_TARGET_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(SPAWN_TARGET_EVENT, 600) # Load background image background = pygame.image.load(r"W:\11\jayden.harris\pygame\PNG\Stall\bg_wood.png").convert() # Hide default cursor pygame.mouse.set_visible(True) # Crosshair setup (fix: use Crosshair class) crosshair = Crosshair(r"W:\11\jayden.harris\pygame\PNG\HUD\crosshair_blue_large.png") crosshair_group = pygame.sprite.GroupSingle(crosshair) # Fix: Now a Sprite object # 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 # Game Loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: crosshair.shoot() # Now it works because crosshair is a Sprite for target in target_group: if crosshair.rect.colliderect(target.rect): target.kill() # Remove the target score += 1 # Increase score when a target is hit pygame.mouse.set_visible(False) # Hide the mouse cursor # Check for key presses if event.type == pygame.KEYDOWN: if event.key == pygame.K_f: # If 'F' key is pressed pygame.mouse.set_visible(True) # Hide the mouse cursor if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pause_menu() MAX_TARGETS = 10 # Set your spawn cap here # Inside your game loop, under SPAWN_TARGET_EVENT: if event.type == SPAWN_TARGET_EVENT: if len(target_group) < MAX_TARGETS: # Random position within screen bounds pos_x = random.randint(0, screen_width - 100) pos_y = random.randint(0, screen_height - 100) # Create and add new target new_target = Target(r"W:\11\jayden.harris\pygame\PNG\Targets\target1.png", pos_x, pos_y) target_group.add(new_target) # 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())) # Update and draw crosshair #crosshair_group.update() #crosshair_group.draw(screen) # Draw and update all targets #target_group.draw(screen) # Draw and update all targets target_group.draw(screen) # Update and draw crosshair (AFTER targets) crosshair_group.update() target_group.update() crosshair_group.draw(screen) draw_score(screen, score, 50, 20) # Adjust position as needed pygame.display.flip() clock.tick(120)