import pygame, sys, random class Crosshair(pygame.sprite.Sprite): def __init__(self, pos_x, pos_y, picture_path): super().__init__() self.image = pygame.image.load('crosshair.png') self.rect = self.image.get_rect() self.rect.x = pos_x self.rect.x = pos_y self.gunshoot = pygame.mixer.Sound("gunshoot.wav") def shoot(self): self.gunshoot.play() pygame.sprite.spritecollide(crosshair,target_sprites,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('target.png') self.rect = self.image.get_rect() self.rect.x = pos_x self.rect.y = pos_y class GameState(): def __init__(self): self.state = 'intro' def intro(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.QUIT() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: self.state = 'main_game' def mian_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)) screen.blit(ready_text,(screen_width/2 -115 ,screen_height/2 - 33)) player_group.draw(screen) player_group.update() pygame.display.flip() # 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())) def state_manager(self): if self.state == 'intro': self.intro() if self.state == 'main_game': self.mian_game() #general setup pygame.init() clock = pygame.time.Clock() Game_state = GameState() #game screen screen_width = 1920 screen_height = 1080 screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption("Sprites") background = pygame.image.load("BG.png") ready_text = pygame.image.load("ready_text.png") pygame.mouse.set_visible(False) #crosshair crosshair = Crosshair(200,200,"crosshair.png") player_group = pygame.sprite.Group() player_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_sprites = pygame.sprite.Group() for target in range(50): new_target = Target("target.png",random.randrange(0,screen_width),random.randrange(0,screen_height)) target_sprites.add(new_target) while True: Game_state.state_manager() clock.tick(60)