import pygame import sys import random 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("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] class Gamestate(): def __init__(self): self.state = 'main_game' def intro(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: crosshair.shoot() screen.blit(background, (0, 0)) target_group.draw(screen) crosshair_group.draw(screen) crosshair_group.update() pygame.display.flip() def main_game(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: crosshair.shoot() screen.blit(background, (0, 0)) target_group.draw(screen) crosshair_group.draw(screen) crosshair_group.update() pygame.display.flip() # general setup pygame.init() clock = pygame.time.Clock() game_state = Gamestate() # game screen screen_width = 1600 screen_height = 900 screen = pygame.display.set_mode((screen_width, screen_height)) background = pygame.image.load("images/bg_blue.png") pygame.mouse.set_visible(False) # targets target_group = pygame.sprite.Group() for _ in range(20): new_target = Target("images/target_red2.png", random.randrange(0, screen_width), random.randrange(0, screen_height)) target_group.add(new_target) # crosshair crosshair = Crosshair("images/crosshair_red_large.png") crosshair_group = pygame.sprite.Group() crosshair_group.add(crosshair) while True: game_state.main_game() clock.tick(60)