import pygame
import random
import math
pygame.init()
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Aim Trainer")
font = pygame.font.SysFont("Arial", 30)
big_font = pygame.font.SysFont("Arial", 50)
clock = pygame.time.Clock()
TARGET_MIN = 20
TARGET_MAX = 60
score = 0
shots = 0
game_time = 30
start_ticks = pygame.time.get_ticks()
def new_target():
r = random.randint(TARGET_MIN, TARGET_MAX)
x = random.randint(r, WIDTH-r)
y = random.randint(r, HEIGHT-r)
return x, y, r
target_x, target_y, target_r = new_target()
running = True
while running:
screen.fill((30,30,30))
seconds = (pygame.time.get_ticks()-start_ticks)/1000
remain = max(0, game_time-seconds)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and remain > 0:
shots += 1
mx, my = pygame.mouse.get_pos()
dist = math.sqrt((mx-target_x)**2+(my-target_y)**2)
if dist <= target_r:
score += 1
target_x, target_y, target_r = new_target()
if remain > 0:
pygame.draw.circle(screen, (220,40,40), (target_x,target_y), target_r)
pygame.draw.circle(screen, (255,255,255), (target_x,target_y), target_r//2)
pygame.draw.circle(screen, (255,0,0), (target_x,target_y), target_r//5)
accuracy = 0 if shots == 0 else score/shots*100
screen.blit(font.render(f"Score : {score}", True, (255,255,255)), (20,20))
screen.blit(font.render(f"Shots : {shots}", True, (255,255,255)), (20,60))
screen.blit(font.render(f"Accuracy : {accuracy:.1f}%", True, (255,255,255)), (20,100))
screen.blit(font.render(f"Time : {remain:.1f}", True, (255,255,255)), (20,140))
else:
accuracy = 0 if shots == 0 else score/shots*100
over = big_font.render("GAME OVER", True, (255,50,50))
screen.blit(over, (WIDTH//2-over.get_width()//2,150))
screen.blit(font.render(f"Final Score : {score}", True, (255,255,255)), (380,260))
screen.blit(font.render(f"Shots : {shots}", True, (255,255,255)), (380,310))
screen.blit(font.render(f"Accuracy : {accuracy:.1f}%", True, (255,255,255)), (380,360))
pygame.display.flip()
clock.tick(144)
pygame.quit()
To embed this project on your website, copy the following code and paste it into your website's HTML: