import pygame import random # Initialize Pygame pygame.init() # Set up the screen WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Aim Trainer") # Colors WHITE = (255, 255, 255) RED = (255, 0, 0) # Target class class Target: def __init__(self): self.radius = 15 self.x = random.randint(self.radius, WIDTH - self.radius) self.y = random.randint(self.radius, HEIGHT - self.radius) self.speed = 2 def draw(self): pygame.draw.circle(screen, RED, (self.x, self.y), self.radius) def move(self): self.x += random.randint(-self.speed, self.speed) self.y += random.randint(-self.speed, self.speed) self.x = max(self.radius, min(self.x, WIDTH - self.radius)) self.y = max(self.radius, min(self.y, HEIGHT - self.radius)) # Instantiate the target target = Target() # Function to detect if click is within target def is_hit(target, click_pos): distance_sq = (target.x - click_pos[0]) ** 2 + (target.y - click_pos[1]) ** 2 return distance_sq <= target.radius ** 2 # Game variables score = 0 accuracy = 0 total_shots = 0 total_hits = 0 time_limit = 30 # seconds start_time = pygame.time.get_ticks() font = pygame.font.Font(None, 36) # Game loop running = True while running: screen.fill(WHITE) # Draw and move target target.draw() target.move() # Display score and accuracy score_text = font.render(f"Score: {score}", True, (0, 0, 0)) accuracy_text = font.render(f"Accuracy: {accuracy:.2f}%", True, (0, 0, 0)) screen.blit(score_text, (10, 10)) screen.blit(accuracy_text, (10, 50)) # Timer current_time = pygame.time.get_ticks() elapsed_time = (current_time - start_time) / 1000 remaining_time = max(0, time_limit - elapsed_time) timer_text = font.render(f"Time: {int(remaining_time)}", True, (0, 0, 0)) screen.blit(timer_text, (10, 90)) if remaining_time <= 0: running = False # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() elif event.type == pygame.MOUSEBUTTONDOWN: total_shots += 1 if is_hit(target, pygame.mouse.get_pos()): total_hits += 1 score += 1 # Update accuracy if total_shots > 0: accuracy = (total_hits / total_shots) * 100 pygame.display.flip() pygame.time.delay(10) # Add a small delay to prevent target from moving too fast # Game over game_over_text = font.render("Game Over", True, (255, 0, 0)) screen.blit(game_over_text, (WIDTH // 2 - 100, HEIGHT // 2 - 20)) pygame.display.flip() # Wait for a few seconds before quitting pygame.time.delay(2000) pygame.quit()