import pygame, sys, random class Crosshair(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) def update(self): self.rect.center = pygame.mouse.get_pos() def shoot(self, target_group): pygame.sprite.spritecollide(crosshair, target_group, True) 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 = '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' screen.blit(background, (0, 0)) screen.blit(ready_text, (screen_width/2 - 155, screen_height/2 -33)) 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(target_group) screen.blit(background, (0, 0)) target_group.draw(screen) crosshair_group.draw(screen) crosshair_group.update() pygame.display.flip() def state_manager(self): if self.state == 'intro': self.intro() if self.state == 'main_game': self.main_game() #General Setup pygame.init() clock = pygame.time.Clock() game_state = GameState() #Game Screen screen_width = 1900 screen_height = 1000 screen = pygame.display.set_mode((screen_width, screen_height)) background = pygame.image.load("animation/bg_blue.png") ready_text = pygame.image.load("animation/text_ready.png") pygame.mouse.set_visible(False) #Crosshair crosshair = Crosshair("animation/crosshair_red_small.png", screen_width // 2, screen_height // 2) crosshair_group = pygame.sprite.Group() crosshair_group.add(crosshair) num_tiles_x = (screen_width//background.get_width()) + 1 num_tiles_y = (screen_height//background.get_height()) + 1 target_group = pygame.sprite.Group() for target in range(20): new_target = Target("animation/target_red2.png", random.randrange(0, screen_width), random.randrange(0, screen_height)) target_group.add(new_target) while True: game_state.state_manager() 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())) clock.tick(60)