import pygame, sys, random from pygame.sprite import AbstractGroup 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("assets/gunshot.mp3") 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] # General setup pygame.init() clock = pygame.time.Clock() # Game Screen screen_width = 1920 screen_height = 1080 screen = pygame.display.set_mode((screen_width,screen_height)) background = pygame.image.load("projectfolder/bg_blue.png").convert() pygame.mouse.set_visible(False) # Crosshair crosshair = Crosshair("projectfolder/crosshair_white_large.png") crosshair_group = pygame.sprite.Group() crosshair_group.add(crosshair) # 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 # Target target_group = pygame.sprite.Group() for target in range(20): new_target = Target("projectfolder/target_red3.png",random.randrange(0,screen_width),random.randrange(0,screen_height)) target_group.add(new_target) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: crosshair.shoot() # 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())) screen.blit(background,(0,0)) # Draw the background image on the screen target_group.draw(screen) # Draw the targets on the screen crosshair_group.draw(screen) # Draw the crosshair on the screen crosshair_group.update() # Update the crosshair position pygame.display.flip() # Update the display clock.tick(60) # Limit the frame rate to 60 frames per second