spectrum_targets.py
Anonymous
2.99 KB
3 views
1/19/2026
Python
Public
3,062 chars • 95 lines
1import argparse
2import math
3import random
4import pygame
5import sys
6
7def parse_args():
8 p = argparse.ArgumentParser()
9 p.add_argument("--targets", type=int, default=3)
10 p.add_argument("--speed", type=float, default=200)
11 p.add_argument("--size", type=int, default=10)
12 p.add_argument("--mode", choices=["bounce", "oscillate"], default="bounce")
13 return p.parse_args()
14
15class Ball:
16 def __init__(self, w, h, r, speed, mode):
17 self.r = r
18 self.mode = mode
19 self.x = random.uniform(r, w - r)
20 self.y = random.uniform(r, h - r)
21
22 if mode == "bounce":
23 ang = random.uniform(0, math.tau)
24 self.vx = math.cos(ang) * speed
25 self.vy = math.sin(ang) * speed
26 else:
27 self.base_x = self.x
28 self.base_y = self.y
29 self.phase = random.uniform(0, math.tau)
30 self.amp = random.uniform(30, 120)
31 self.speed = speed
32
33 def update(self, dt, w, h):
34 if self.mode == "bounce":
35 self.x += self.vx * dt
36 self.y += self.vy * dt
37
38 if self.x < self.r or self.x > w - self.r:
39 self.vx = -self.vx
40 self.x = max(self.r, min(self.x, w - self.r))
41
42 if self.y < self.r or self.y > h - self.r:
43 self.vy = -self.vy
44 self.y = max(self.r, min(self.y, h - self.r))
45 else:
46 self.phase += dt * self.speed
47 self.x = self.base_x + math.cos(self.phase) * self.amp
48 self.y = self.base_y + math.sin(self.phase) * self.amp
49
50 def draw(self, surf):
51 pygame.draw.circle(surf, (255, 0, 0), (int(self.x), int(self.y)), self.r)
52
53def main():
54 args = parse_args()
55
56 pygame.init()
57 screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
58 pygame.display.set_caption("spectrum tracker test")
59 clock = pygame.time.Clock()
60
61 w, h = screen.get_size()
62 balls = [Ball(w, h, args.size, args.speed, args.mode) for _ in range(args.targets)]
63
64 pygame.mouse.set_visible(False)
65
66 cursor_radius = 4
67 cursor_surface = pygame.Surface((cursor_radius * 2, cursor_radius * 2), pygame.SRCALPHA)
68 pygame.draw.circle(cursor_surface, (255, 255, 255, 120), (cursor_radius, cursor_radius), cursor_radius)
69
70 while True:
71 dt = clock.tick(180) / 1000.0
72
73 for e in pygame.event.get():
74 if e.type == pygame.QUIT:
75 pygame.quit()
76 sys.exit()
77 if e.type == pygame.VIDEORESIZE:
78 screen = pygame.display.set_mode(e.size, pygame.RESIZABLE)
79 w, h = e.size
80
81 screen.fill((0, 0, 0))
82
83 for b in balls:
84 b.update(dt, w, h)
85 b.draw(screen)
86
87 if pygame.mouse.get_focused():
88 mx, my = pygame.mouse.get_pos()
89 screen.blit(cursor_surface, (mx - cursor_radius, my - cursor_radius))
90
91 pygame.display.flip()
92
93if __name__ == "__main__":
94 main()
95