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("assets/shot.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] 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)) 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 = 1920 screen_height= 1020 screen = pygame.display.set_mode((screen_width,screen_height)) background = pygame.image.load("assets/full-bg.png") pygame.mouse.set_visible(False) #Crosshair crosshair = Crosshair("assets/circle-02.png") crosshair_group = pygame.sprite.Group() crosshair_group.add(crosshair) # Target target_group = pygame.sprite.Group() for target in range(20): new_target = Target("assets/target.png", random.randrange(0,screen_width - 100), random.randrange(0,screen_height)) target_group.add(new_target) while True: game_state.intro() clock.tick(60)