1+ import pg_main , pygame , time , math
2+ from pynput .mouse import Listener
3+ from cfg import *
4+
5+ prev_time = time .time ()
6+ prev_pos = None
7+
8+ vx = 0
9+ vy = 0
10+
11+ px = 0
12+ py = 0
13+
14+ mx = 0
15+ my = 0
16+
17+ speed_level = 2
18+
19+ def event_func (event ):
20+ global mx , my
21+ if event .type == pygame .KEYDOWN :
22+ if event .key == pygame .K_RETURN :
23+ print (mx ,my )
24+
25+ last_dx = 0.0
26+ last_dy = 0.0
27+
28+ def on_move (x ,y ):
29+ global prev_time , prev_pos , vx , vy , px , py , mx , my , last_dx , last_dy
30+
31+ mouse_mult = 1 - math .exp (- speed_level )
32+
33+ current_time = time .time ()
34+ if prev_pos is not None :
35+ dx = x - prev_pos [0 ]
36+ dy = y - prev_pos [1 ]
37+
38+ if dx != 0 and last_dx != 0 and ( dx / abs (dx ) != last_dx / abs (last_dx ) ):
39+ dx = 0
40+ if dy != 0 and last_dy != 0 and ( dy / abs (dy ) != last_dy / abs (last_dy ) ):
41+ dy = 0
42+
43+ last_dx = dx
44+ last_dy = dy
45+
46+ dt = current_time - prev_time
47+ if dt > 0 :
48+ vx = dx / (dt * 1000 )
49+ vy = dy / (dt * 1000 )
50+
51+ px += vx
52+ py += vy
53+ mx += vx * 0.1 * mouse_mult
54+ my += vy * 0.1 * mouse_mult
55+
56+ prev_pos = (x , y )
57+ prev_time = current_time
58+
59+ surface1 = pygame .Surface ((config ["SCREEN_WIDTH" ], config ["SCREEN_HEIGHT" ]))
60+ surface2 = pygame .Surface ((config ["SCREEN_WIDTH" ], config ["SCREEN_HEIGHT" ]))
61+
62+ surface1 .set_alpha (100 )
63+ surface2 .set_alpha (100 )
64+
65+ def render_func (screen ):
66+
67+ global vx , vy , px , py , mx , my , timey
68+
69+ mouse_mult = 1 - math .exp (- speed_level )
70+ px *= mouse_mult
71+ py *= mouse_mult
72+
73+ sw = config ["SCREEN_WIDTH" ]
74+ sh = config ["SCREEN_HEIGHT" ]
75+ s = pygame .Surface ((sw ,sh ))
76+ s .set_alpha (1 )
77+ s .fill ((0 , 0 , 0 ))
78+ surface2 .blit (s , (0 , 0 ))
79+ screen .fill ((0 , 0 , 0 ))
80+
81+ shift_x = - vx * 0.1 * mouse_mult
82+ shift_y = - vy * 0.1 * mouse_mult
83+
84+ # Scroll the trail surface
85+ surface2 .scroll (int (shift_x ), int (shift_y ))
86+
87+ pygame .draw .circle (surface2 ,(255 ,255 ,255 ),(sw * 0.5 ,sh * 0.5 ), 3 )
88+ surface1 .blit (surface2 , (0 , 0 ))
89+
90+ screen .blit (surface1 , (0 , 0 ))
91+ pygame .draw .line (screen , (255 , 255 , 255 ), (sw * 0.5 , sh * 0.5 ),(sw * 0.5 + px ,sh * 0.5 + py ), 2 )
92+
93+
94+ if __name__ == "__main__" :
95+ listener = Listener (on_move = on_move )
96+ listener .start ()
97+ pg_main .render (event_func , render_func )
0 commit comments