-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparkles.pyw
904 lines (795 loc) · 45.5 KB
/
sparkles.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
# Copyright (c) 2024. \
# LtqxWYEG <[email protected]> \
# \
# This program is free software: you can redistribute it and/or modify \
# it under the terms of the GNU General Public License as published by \
# the Free Software Foundation, either version 3 of the License, or \
# (at your option) any later version. \
# \
# This program is distributed in the hope that it will be useful, \
# but WITHOUT ANY WARRANTY; without even the implied warranty of \
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE or SIGNIFICANCE. \
# See the GNU General Public License for more details. \
# \
# You should have received a copy of the GNU General Public License \
# along with this program. If not, see <https://www.gnu.org/licenses/>. \
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
import configparser
import math
import random
from ctypes import byref, c_int, Structure, windll
import pygame
import pygame.gfxdraw
import pygame.math
import colorsys
#from pygame.math import Vector2
from os import path, listdir, environ
from pygame.locals import * # for Color
from win32gui import SetWindowLong, SetLayeredWindowAttributes, GetWindowLong, SetWindowPos, GetDC
from win32con import HWND_TOPMOST, GWL_EXSTYLE, SWP_NOMOVE, SWP_NOSIZE, WS_EX_TRANSPARENT, LWA_COLORKEY, WS_EX_LAYERED, WS_EX_TOOLWINDOW
from win32api import RGB
from time import sleep, process_time
from math import sqrt, sin
from random import uniform, randrange
# pyximport.install(pyimport = True, inplace = False) # change "inplace" to true to start compiling everytime to cython
# from functools import lru_cache
# from multiprocessing import Pool, Process, Pipe
# numpy is slower than random.random()
def cleanup_mei():
"""
Rudimentary workaround for https://github.com/pyinstaller/pyinstaller/issues/2379
"""
import sys
from shutil import rmtree
mei_bundle = getattr(sys, "_MEIPASS", False)
if mei_bundle:
dir_mei, current_mei = mei_bundle.split("_MEI")
for file in listdir(dir_mei):
if file.startswith("_MEI") and not file.endswith(current_mei):
try:
rmtree(path.join(dir_mei, file))
except PermissionError: # mainly to allow simultaneous pyinstaller instances
pass
class CaseConfigParser(configparser.ConfigParser):
def optionxform(self, optionstr): # For things to keep case
return optionstr
def getlist(self, section, option): # More case keeping
value = self.get(section, option)
return list(filter(None, (x.strip() for x in value.split(","))))
def getlistint(self, section, option): # The keeper of cases
return [int(x) for x in self.getlist(section, option)]
def getlistfloat(self, section, option): # Case caseing of case
return [float(x) for x in self.getlist(section, option)]
def setDefaults(): # Set Defaults and/or write ini-file if it doesn't exist
global config
config.read("defaults.ini")
with open("config.ini", "w") as configfile:
config.write(configfile)
def readVariables():
global settings
settings = dict() # config.items('OTHER')
settings["transparentColor"] = str(config.get("SPARKLES", "transparentColor"))
settings["particleSize"] = int(config.get("SPARKLES", "particleSize"))
settings["particleAge"] = int(config.get("SPARKLES", "particleAge"))
settings["ageBrightnessMod"] = float(config.get("SPARKLES", "ageBrightnessMod"))
settings["ageBrightnessNoise"] = int(config.get("SPARKLES", "ageBrightnessNoise"))
settings["velocityFactorVector"] = float(config.get("SPARKLES", "velocityFactorVector"))
settings["softClampVelocityVector"] = int(config.get("SPARKLES", "softClampVelocityVector"))
settings["manualSecondVector"] = config.getlistfloat("SPARKLES", "manualSecondVector")
settings["drag"] = float(config.get("SPARKLES", "drag"))
settings["FPS"] = int(config.get("SPARKLES", "FPS"))
settings["multitasking"] = int(config.get("SPARKLES", "multitasking"))
settings["interpolateMouseMovement"] = config.getboolean("SPARKLES", "interpolateMouseMovement")
settings["useOffset"] = config.getboolean("SPARKLES", "useOffset")
settings["offsetX"] = int(config.get("SPARKLES", "offsetX"))
settings["offsetY"] = int(config.get("SPARKLES", "offsetY"))
settings["markPosition"] = config.getboolean("SPARKLES", "markPosition")
settings["numParticles"] = int(config.get("SPARKLES", "numParticles"))
settings["addRandomParticleVector"] = float(config.get("SPARKLES", "addRandomParticleVector"))
settings["particleColor"] = str(config.get("SPARKLES", "particleColor"))
settings["useColorUnderMouse"] = config.getboolean("SPARKLES", "useColorUnderMouse")
settings["particleColorRandom"] = config.getboolean("SPARKLES", "particleColorRandom")
settings["ageColor"] = config.getboolean("SPARKLES", "ageColor")
settings["colorRollover"] = config.getboolean("SPARKLES", "colorRollover")
settings["ageLinear"] = config.getboolean("SPARKLES", "ageLinear")
settings["ageLinearSpeed"] = float(config.get("SPARKLES", "ageLinearSpeed"))
settings["ageColorSpeed"] = float(config.get("SPARKLES", "ageColorSpeed"))
settings["ageColorSlope"] = config.getboolean("SPARKLES", "ageColorSlope")
settings["ageColorSlopeConcavity"] = float(config.get("SPARKLES", "ageColorSlopeConcavity"))
settings["ageColorNoise"] = int(config.get("SPARKLES", "ageColorNoise"))
settings["ageColorNoiseMod"] = float(config.get("SPARKLES", "ageColorNoiseMod"))
settings["addRandomMouseInfluenceVector"] = config.getboolean("SPARKLES", "addRandomMouseInfluenceVector")
settings["randomSecondVector"] = float(config.get("SPARKLES", "randomSecondVector"))
settings["chaoticSecondVector"] = float(config.get("SPARKLES", "chaoticSecondVector"))
settings["addChaosSecondVector"] = config.getboolean("SPARKLES", "addChaosSecondVector")
settings["clampVelocitySecondVector"] = config.getboolean("SPARKLES", "clampVelocitySecondVector")
settings["vectorRotation"] = float(config.get("SPARKLES", "vectorRotation"))
settings["randomRotation"] = config.getboolean("SPARKLES", "randomRotation")
settings["cumulativeVectorRotation"] = config.getboolean("SPARKLES", "cumulativeVectorRotation")
settings["secondVectorRotation"] = config.getboolean("SPARKLES", "secondVectorRotation")
settings["particleVectorRotation"] = config.getboolean("SPARKLES", "particleVectorRotation")
settings["dynamic"] = config.getboolean("SPARKLES", "dynamic")
settings["strengthMouseInfluenceVector"] = float(config.get("SPARKLES", "strengthMouseInfluenceVector"))
settings["levelVelocity"] = config.getlistint("SPARKLES", "levelVelocity")
settings["levelNumParticles"] = config.getlistint("SPARKLES", "levelNumParticles")
class Particle(object):
"""
Superclass for other particle types.
"""
def __init__(self):
pass
def updateParticle(self):
pass
class ParticleClass(Particle):
"""
Draw particles.
"""
# @lru_cache() # TypeError: unhashable type: 'list'
def __init__(self, surface, pos, vector, container, color, randomHuePart, mouse_Speed_Pixel_Per_Frame):
"""
surface : Surface : The Surface to draw on.
pos : (x,y) : tuple/list x,y position at time of creation.
vector : (x,y) : tuple/list x,y velocity at time of creation.
secondVector : (x,y) : tuple/list x,y secondVector effecting the particle.
container : list : The passed in list that contains all the particles to draw.
color : Color : Used so particles can be deleted.
mouse_Speed_Pixel_Per_Frame : selfexplanatory
"""
self.ageStep = None # I don't understand why this IDE wants me to do this
self.surface = surface
self.pos = pygame.math.Vector2(pos)
self.rotationRate = 0
if settings["chaoticSecondVector"] > 0 and settings["clampVelocitySecondVector"]:
self.maxVelocitySecondVector = settings["softClampVelocityVector"] / (33 / settings["chaoticSecondVector"]) # Add slider to adjust!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if settings["dynamic"]:
if settings["addRandomMouseInfluenceVector"]:
vector = [vector[0] + uniform(-(mouse_Speed_Pixel_Per_Frame * settings["strengthMouseInfluenceVector"]), (mouse_Speed_Pixel_Per_Frame * settings["strengthMouseInfluenceVector"])),
vector[1] + uniform(-(mouse_Speed_Pixel_Per_Frame * settings["strengthMouseInfluenceVector"]), (mouse_Speed_Pixel_Per_Frame * settings["strengthMouseInfluenceVector"])),]
else:
vector = [vector[0] + mouse_Vector[0],
vector[1] + mouse_Vector[1]]
elif settings["addRandomParticleVector"] > 0:
vector = [vector[0] + uniform(-settings["addRandomParticleVector"], settings["addRandomParticleVector"]),
vector[1] + uniform(-settings["addRandomParticleVector"], settings["addRandomParticleVector"])]
else:
vector = [vector[0], vector[1]] # ... then just initialize with mouse_Vector
if settings["velocityFactorVector"] > 0:
self.particleVector = pygame.math.Vector2(vector) * settings["velocityFactorVector"]
else:
self.particleVector = pygame.math.Vector2(0)
# Set second vector
if settings["manualSecondVector"]:
self.secondVector = pygame.math.Vector2(settings["manualSecondVector"])
if settings["randomSecondVector"] > 0: # Reset Vector2 to random.
self.secondVector[0] = uniform(-settings["randomSecondVector"], settings["randomSecondVector"]) #/ 2
self.secondVector[1] = uniform(-settings["randomSecondVector"], settings["randomSecondVector"]) #/ 2
if settings["vectorRotation"] > 0: # IMPOSSIBLE?: and self.counter == 0: # initialize rotation rate
if settings["randomRotation"]:
self.rotationRate = uniform(-settings["vectorRotation"], settings["vectorRotation"]) / 100
else:
self.rotationRate = random.choice([settings["vectorRotation"] / 100, -settings["vectorRotation"] / 100]) # flicks between left and reight rotation
# IMPOSSIBLE?: self.counter = 1 # set rotation speed only once at time of creation
self.container = container
self.color = Color(color)
self.colorStatic = Color(color)
hsva = self.color.hsva # H = [0, 360], S = [0, 100], V = [0, 100], A = [0, 100]
if settings["particleColorRandom"]:
hue = randomHuePart
else:
hue = hsva[0] # unpack hue from hsva tuple
hue += uniform(-settings["ageColorNoise"] + shiftAgeColorNoise, settings["ageColorNoise"] + shiftAgeColorNoise)
hue = clamp(hue, 0, 359) # Clamp hue within limits
self.color.hsva = (hue, int(hsva[1]), int(hsva[2])) # pack new hue value into hsva tuple
self.surfSize = surface.get_size()
self.drag = settings["drag"]
self.age = settings["particleAge"]
def updateParticle(self):
#self.particleVector = (self.particleVector + self.secondVector) * self.drag
self.particleVector *= self.drag
self.pos += self.particleVector
#self.rotationRate = self.rotationRate # what?
# if settings["randomSecondVector"] > 0:
# self.secondVector[0] = self.gravityX
# self.secondVector[1] = self.gravityY
if settings["chaoticSecondVector"] > 0:
if settings["addChaosSecondVector"]:
self.secondVector[0] += uniform(-settings["chaoticSecondVector"], settings["chaoticSecondVector"]) * 0.05 # Add slider to adjust!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
self.secondVector[1] += uniform(-settings["chaoticSecondVector"], settings["chaoticSecondVector"]) * 0.05 # modify second vector and tame it
else:
self.secondVector[0] = uniform(-settings["chaoticSecondVector"], settings["chaoticSecondVector"]) * 2 # Reset second vector to random values
self.secondVector[1] = uniform(-settings["chaoticSecondVector"], settings["chaoticSecondVector"]) * 2 # Reset second vector to random values
if settings["clampVelocitySecondVector"]:
self.secondVector[0] = clamp(self.secondVector[0], -self.maxVelocitySecondVector, self.maxVelocitySecondVector)
self.secondVector[1] = clamp(self.secondVector[1], -self.maxVelocitySecondVector, self.maxVelocitySecondVector)
if settings["vectorRotation"] > 0: # Maybe bring particle vector rotation settings down here????
if settings["secondVectorRotation"]:
if settings["randomRotation"]:
self.rotationRate = uniform(-settings["vectorRotation"], settings["vectorRotation"]) / 20 # Add slider to adjust!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if settings["cumulativeVectorRotation"]: # and not settings["particleVectorRotation"]:
self.rotationRate += self.rotationRate * 0.01 # Add slider to adjust!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
self.secondVector.rotate_rad_ip(self.rotationRate)
if settings["particleVectorRotation"]: # randomRotation for particle vector is in __init__
if settings["cumulativeVectorRotation"]: # and not (settings["secondVectorRotation"] and settings["particleVectorRotation"]):
self.rotationRate += self.rotationRate * 0.01 # Add slider to adjust!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Popupmenu???? Add rotation decay!!!
self.particleVector.rotate_rad_ip(self.rotationRate)
self.rotationRate = clamp(self.rotationRate, -359, 359)
# if self.particleVector.length > settings['softClampVelocityVector']: # Clamp any huge velocities
# self.particleVector.length = settings['softClampVelocityVector'] # old version for vec2d.py
if self.particleVector.length() > settings["softClampVelocityVector"]: # Clamp any huge velocities
self.particleVector = self.particleVector.normalize() * settings["softClampVelocityVector"]
self.particleVector += self.secondVector # Add second vector
# Last motion related ------------------------------------------------------
# Color manipulation -------------------------------------------------------
self.ageStep = 100.0 / float(self.age)
# Update color, and existence based on color:
hsva = self.color.hsva # Limits: H = [0, 360], S = [0, 100], V = [0, 100], A = [0, 100]
hue = hsva[0]
if settings["ageColor"]:
if settings["ageLinear"]:
hue += settings["ageLinearSpeed"]
if settings["colorRollover"]:
if hue < 1 and settings["ageLinearSpeed"] < 0:
hue = 360
if hue > 359 and settings["ageLinearSpeed"] > 0:
hue = 0
elif settings["ageColorSlope"]:
hue += self.ageStep * (self.ageStep * ((self.ageStep / (10 ** settings["ageColorSlopeConcavity"])) / (10 ** settings["ageColorSlopeConcavity"])))
if settings["colorRollover"]:
if hue < 1 and settings["ageColorSlopeConcavity"] < 0:
hue = 360
if hue > 359 and settings["ageColorSlopeConcavity"] > 0:
hue = 0
else:
hue += self.ageStep * settings["ageColorSpeed"]
if settings["colorRollover"]:
if hue < 1 and settings["ageColorSpeed"] < 0:
hue = 360
if hue > 359 and settings["ageColorSpeed"] > 0:
hue = 0
brightness = hsva[2]
if settings["ageBrightnessMod"] > 0:
brightness -= self.ageStep / settings["ageBrightnessMod"]
if settings["ageBrightnessNoise"] > 0:
brightness += uniform(-settings["ageBrightnessNoise"], settings["ageBrightnessNoise"])
if brightness < 9 and self.age > 10: # to avoid killing them with this
brightness = 9
brightness = clamp(brightness, 0, 99)
hue = clamp(hue, 1, 357) # Clamp hue within limits. Has to be 357, otherwise it will roll over with high ageBrightnessNoise for some reason
self.age -= 1
if brightness < 1 or self.age == 0: # If brightness falls below 7, remove particle
try: # It's possible this particle was removed already.
self.color.hsva = (0, 0, 0) # , alpha)
self.container.remove(self)
except ValueError:
pass
else:
self.color.hsva = (hue, int(hsva[1]), brightness) # , alpha)
if self.pos[0] < 0 or self.pos[0] > self.surfSize[0]: # remove if outside surface bounds
try:
self.color.hsva = (0, 0, 0) # , alpha)
self.container.remove(self)
except ValueError:
pass
elif self.pos[1] < 0 or self.pos[1] > self.surfSize[1]: # remove if outside surface bounds
try:
self.color.hsva = (0, 0, 0) # , alpha)
self.container.remove(self)
except ValueError:
pass
# if self.particleVector == [0, 0] or self.particleVector == [-0, 0] and settings["dynamic"]:
# try: # It's possible this particle was removed already.
# self.color.hsva = (0, 0, 0) # , alpha)
# self.container.remove(self)
# except ValueError:
# pass
if settings["particleSize"] <= 2:
# Draw just a simple point:
pygame.draw.rect(self.surface, self.color, (*self.pos, settings["particleSize"], settings["particleSize"]))
else:
pygame.draw.circle(self.surface, self.color, self.pos, settings["particleSize"] - 1)
class POINT(Structure):
_fields_ = [("x", c_int), ("y", c_int)]
def clamp(val, minval, maxval):
if val < minval:
return minval
if val > maxval:
return maxval
return val
def setWindowAttributes(hwnd): # set all kinds of option for win32 windows
windll.user32.SetWindowLongPtrW(hwnd, GWL_EXSTYLE, windll.user32.GetWindowLongPtrW(hwnd, GWL_EXSTYLE) | WS_EX_TOOLWINDOW) # no taskbar button
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT,)
SetLayeredWindowAttributes(hwnd, RGB(*transparentColorTuple), 255, LWA_COLORKEY)
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
# HWND_TOPMOST: Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated. (Well, it SHOULD. But doesn't.)
# It's not necessary to set the SWP_SHOWWINDOW flag.
# SWP_NOMOVE: Retains the current position (ignores X and Y parameters).
# SWP_NOSIZE: Retains the current size (ignores the cx and cy parameters).
# GWL_EXSTYLE: Retrieve the extended window styles of the window.
# WS_EX_TRANSPARENT: The window should not be painted until siblings beneath the window have been painted, making it transparent.
# WS_EX_LAYERED: The window is a layered window, so that we can set attributes like color with SetLayeredWindowAttributes ...
# LWA_COLORKEY: ... and make that color the transparent color of the window.
def HEXtoRGB(hexa):
rgb = []
hexa = hexa.removeprefix('#') # hexa.replace('#', ''); # = filter(lambda ch: ch not in "#", hexa) not working
for n in (0, 2, 4):
decimal = int(hexa[n:n + 2], 16)
rgb.append(decimal)
return tuple(rgb)
def rgbIntToTuple(RGBint):
red = RGBint & 255
green = (RGBint >> 8) & 255
blue = (RGBint >> 16) & 255
return red, green, blue
# @lru_cache(maxsize = 1024) # TypeError: unhashable type: 'list' because particleContainer?
def loop(first_Run, transparent_Color, interpolate_Mouse_Movement, particle_Container, particle_Color, random_Hue, offset_X, offset_Y, mark_Position, num_Particles, level_Velocity, level_Num_Particles, second_Pos, mouse_Position, draw_Particles):
global looping, devMeasureLoopLength, timer, mouse_Vector
particle_Container_append = particle_Container.append
ONE_THIRD = 1.0 / 3.0 # optimization, if I remember correctly
oldActiveRect = pygame.Rect((0, 0), (0, 0))
try:
while looping:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("loop false, return")
looping = False
return # probably redundant
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print("loop false, return")
looping = False
return # probably redundant
# if event.type == pygame.MOUSEMOTION:
# # mouse_Position = pygame.mouse.get_pos()
# distanceX, distanceY = event.rel
# mouse_Speed_Pixel_Per_Frame = (distanceX ** 2 + distanceY ** 2) **0.5 # Pythagoras theorem.
# if devPrintMouseSpeed:
# print("Mouse speed by pygame: ", pygame.mouse.get_rel()) # --- Note: doesn't work because window is usually not focused or something like that
windll.user32.GetCursorPos(byref(mouse_Position)) # get mouse cursor position and save it in the POINT() structure
# mouse_Position = pygame.mouse.get_pos()
first_Pos = (mouse_Position.x - offset_X, mouse_Position.y - offset_Y)
if first_Run:
mouse_Vector = (0, 0)
first_Run = False
else:
mouse_Vector = ((first_Pos[0] - second_Pos[0]), (first_Pos[1] - second_Pos[1]))
# fastest tuple arithmatic solution: (a[0] - b[0], a[1] - b[1]). NOT np, sub, lambda, zip...
mouse_Speed_Pixel_Per_Frame = (mouse_Vector[0] ** 2 + mouse_Vector[1] ** 2) ** 0.5 # Pythagoras theorem
if interpolate_Mouse_Movement:
firstMiddlePos = (first_Pos[0] - (mouse_Vector[0] * ONE_THIRD), first_Pos[1] - (mouse_Vector[1] * ONE_THIRD)) # To triple resolution
secondMiddlePos = (first_Pos[0] - (mouse_Vector[0] - (mouse_Vector[0] * ONE_THIRD)), first_Pos[1] - (mouse_Vector[1] - (mouse_Vector[1] * ONE_THIRD)))
#thirdMiddlePos = (first_Pos[0] - (mouse_Vector[0] - (mouse_Vector[0] * 0.5)), first_Pos[1] - (mouse_Vector[1] - (mouse_Vector[1] * 0.5)))
# old position mouse speed pixel
if devPrintMouseSpeed:
print("Mouse speed in pixel distance traveled this frame: ", mouse_Speed_Pixel_Per_Frame)
# print("Mouse speed by pygame: ", pygame.mouse.get_rel()) # --- Note: doesn't work because window is usually not focused or something like that
if settings["dynamic"]:
if mouse_Speed_Pixel_Per_Frame == 0: # For dynamic behaviour. No movement = no particles spawned
draw_Particles = False
elif mouse_Speed_Pixel_Per_Frame < level_Velocity[0]: # For dynamic behaviour.
num_Particles = numParticlesBackup
draw_Particles = True
elif mouse_Speed_Pixel_Per_Frame < level_Velocity[1]: # For dynamic behaviour.
num_Particles = level_Num_Particles[0]
draw_Particles = True
elif mouse_Speed_Pixel_Per_Frame < level_Velocity[2]: # For dynamic behaviour.
num_Particles = level_Num_Particles[1]
draw_Particles = True
elif mouse_Speed_Pixel_Per_Frame < level_Velocity[3]: # For dynamic behaviour.
num_Particles = level_Num_Particles[2]
draw_Particles = True
else:
num_Particles = level_Num_Particles[3]
draw_Particles = True
# ---multi processing entry
# ------------
if settings["useColorUnderMouse"]:
particle_Color = rgbIntToTuple(windll.gdi32.GetPixel(handleDeviceContext, mousePosition.x, mousePosition.y))
x = 0
y = 0
while x < num_Particles and draw_Particles:
if settings["particleColorRandom"]:
random_Hue = randrange(360)
if not interpolate_Mouse_Movement:
particle_Container_append(ParticleClass(display_window, first_Pos, mouse_Vector, particle_Container, particle_Color, random_Hue, mouse_Speed_Pixel_Per_Frame))
else:
match y:
case 0:
particle_Container_append(ParticleClass(display_window, second_Pos, mouse_Vector, particle_Container, particle_Color, random_Hue, mouse_Speed_Pixel_Per_Frame))
y = 1
case 1:
particle_Container_append(ParticleClass(display_window, firstMiddlePos, mouse_Vector, particle_Container, particle_Color, random_Hue, mouse_Speed_Pixel_Per_Frame))
y = 2
case 2:
particle_Container_append(ParticleClass(display_window, secondMiddlePos, mouse_Vector, particle_Container, particle_Color, random_Hue, mouse_Speed_Pixel_Per_Frame))
y = 3
# case 3:
# particle_Container_append(ParticleClass(display_window, thirdMiddlePos, mouse_Vector, particle_Container, particle_Color, mouse_Speed_Pixel_Per_Frame))
# y = 4
case 3:
particle_Container_append(ParticleClass(display_window, first_Pos, mouse_Vector, particle_Container, particle_Color, random_Hue, mouse_Speed_Pixel_Per_Frame))
y = 0
x += 1
for part in particle_Container:
part.updateParticle()
# pool.map(part.updateParticle(), ())
# if n == 0:
# pool.apply_async(part.updateParticle(), args=())
# n += 1
# if n == 1:
# pool.apply_async(part.updateParticle(), args=())
# n += 1
# if n == 2:
# pool.apply_async(part.updateParticle(), args=())
# n += 1
# if n == 3:
# pool.apply_async(part.updateParticle(), args=())
# n += 1
# if n == 4:
# n = 0
# Doesn't work because it spawns a process for only one particle, then after 4 it stops creating new processes
# until thex are finished with simulating theri one particle. :(
# existing processes use only ~1% CPU time because they only have to update one particle.
# So I'd need four processes that never stop.
# But I've read that they aren't closing anyway, so why only 1% cpu?
# can it really be that updateParticle() is not the culprit, even though it takes over 50 to 70% of the process time?
#
second_Pos = first_Pos # for getting mouse velocity
#first_Run = False
if mark_Position:
pygame.draw.circle(display_window, "#ff0000", first_Pos, 2) # Circle at origin point used by user for tuning offset
partsXcoordinates = list()
partsYcoordinates = list()
if particle_Container:
for part in particle_Container:
partsXcoordinates.append(int(part.pos.x)) # fill lists with all particle positions X and Y
partsYcoordinates.append(int(part.pos.y))
else:
partsXcoordinates = list((0, 0)) # at least initialize if no movement/particles
partsYcoordinates = list((0, 0))
# -------------------- SPACIAL HASHING TRIAL -------------------------------
#r# ---------------------------- START ---------------------------------------
#
# # numTiles = 16 + 9
# # n = 0
# # while n < numTiles:
# # tile_map = []
# # n += 1
# # tileSize = 120 # [1920 / 16, 1080 / 9]
# # tile_map = [[[0, 0], [tileSize, tileSize]]] # list of rects
# # origin = [0, 0]
# # counte
# counterY = 0
# counterN = 1
# while counterX < 16:
# tile_map[counterN].append([[counterX * tileSize, counterY * tileSize], [tileSize, tileSize]])
# counterX += 1
# counterY = 0
# counterN += 1
# while counterY < 9 and (counterX * tileSize) > 1920:
# tile_map[counterN].append([[counterX * tileSize, counterY * tileSize], [tileSize, tileSize]])
# counterY += 1
# counterN += 1
#
# for particle in particle_Container:
# particle[0][0] += particle[1][0]
# loc_str = str(int(particle[0][0] / tileSize)) + ';' + str(int(particle[0][1] / tileSize))
# if loc_str in tile_map:
# particle[1][0] = -0.7 * particle[1][0]
# particle[1][1] *= 0.95
# particle[0][0] += particle[1][0] * 2
# particle[0][1] += particle[1][1]
# loc_str = str(int(particle[0][0] / tileSize)) + ';' + str(int(particle[0][1] / tileSize))
# if loc_str in tile_map:
# particle[1][1] = -0.7 * particle[1][1]
# particle[1][0] *= 0.95
# particle[0][1] += particle[1][1] * 2
# particle[2] -= 0.035
# particle[1][1] += 0.15
#
# for tile in tile_map:
# activeRect.append(tile_map[tile][2])
#
# pygame.display.update((oldActiveRect, activeRect))
# oldActiveRect = pygame.Rect((activeRect[0] - 2, activeRect[1] - 2), (activeRect[2] + 4, activeRect[3] + 4))
# display_window.fill(transparent_Color, oldActiveRect) # instead of filling the whole screen, fill only a small rect
# pygame.draw.circle(screen, (255, 255, 255), [int(particle[0][0]), int(particle[0][1])], int(particle[2]))
# if particle[2] <= 0:
# particle_Container.remove(particle)
#
# # Render Tiles ------------------------------------------- #
# for tile in tile_map:
# pygame.draw.rect(screen, tile_map[tile][2], pygame.Rect(tile_map[tile][0] * tileSize, tile_map[tile][1] * tileSize, tileSize, tileSize))
# -------------------- SPACIAL HASHING TRIAL -------------------------------
# bin_size = 3 # ??
# shift = bin_size
# bins = {}
#
# for part in particle_Container:
# key = part.pos.x >> shift, part.pos.y >> shift
# if key not in bins:
# bins[key] = []
# bins[key].append(part)
#
# side = int(2 ** shift) # power of two = 3??
# bounding_box = [int(x) - side, int(y) - side,
# int(x) + side, int(y) + side]
# bounding_box = [int(x) - side, int(y) - side,
# int(x) + side, int(y) + side]
# min_x, min_y, max_x, max_y = bounding_box
# min_hash_x, min_hash_y = min_x >> shift, min_y >> shift
# max_hash_x, max_hash_y = max_x >> shift, max_y >> shift
#
# activeRect = []
# for hash_x in range(min_hash_x, max_hash_x + 1):
# for hash_y in range(min_hash_y, max_hash_y + 1):
# if (hash_x, hash_y) in bins:
# activeRect.extend(bins[(hash_x, hash_y)])
#
# activeRect.extend(oldActiveRect)
#
# pygame.display.update(activeRect)
# oldActiveRect = pygame.Rect(activeRect)
# display_window.fill(transparent_Color, oldActiveRect) # instead of filling the whole screen, fill only a small rect
# -------------------- SPACIAL HASHING TRIAL -------------------------------
# ----------------------------- END ----------------------------------------
rightestPart = max(partsXcoordinates)
leftestPart = min(partsXcoordinates)
lowestPart = max(partsYcoordinates)
highestPart = min(partsYcoordinates)
activeRect = pygame.Rect((leftestPart - 1, highestPart - 1), (rightestPart - leftestPart + 1, lowestPart - highestPart + 1))
activeRect[0] -= settings['particleSize'] # otherwise bigger particles would leave render-area
activeRect[1] -= settings['particleSize']
activeRect[2] += 2*settings['particleSize']
activeRect[3] += 2*settings['particleSize'] # otherwise bigger particles would leave render-area
if mark_Position:
if activeRect[2] < 50: # if rect size less than 50 pixel, increase by 5
activeRect[2] += 5 # for the red dot. It otherwise leaves red marks
if activeRect[3] < 50:
activeRect[3] += 5
if devVisibleUpdateRect:
highlight = pygame.Surface((activeRect.w + 2, activeRect.h + 2))
highlight.fill("#ff0000")
display_window.blit(highlight, activeRect)
pygame.display.update((oldActiveRect, activeRect))
oldActiveRect = pygame.Rect((activeRect[0] - 2, activeRect[1] - 2), (activeRect[2] + 4, activeRect[3] + 4))
display_window.fill(transparent_Color, oldActiveRect) # instead of filling the whole screen, fill only a small rect
# display_window.fill(transparent_Color)
# pygame.display.update() # old. Keep for now
if devMeasureLoop: # Activates a timer and exec ceiling for this loop
if timer == devMeasureLoopLength:
looping = False
return
timer += 1
if devFPSUnlimited:
if devPrintFPS:
clock.tick(0)
print(int(clock.get_fps())) # print fps in console (average)
else:
clock.tick(0) # limit the fps of the program
else:
if devPrintFPS:
clock.tick(settings["FPS"])
print(int(clock.get_fps())) # print fps in console (average)
else:
clock.tick(settings["FPS"])
finally: # catch every exception and make sure to leave correctly, however it suppresses all tracebacks :(
print("loop ended") # Wait, does it? IDK anymore
# looping = False
# return # Should be redundant
#
#
#
#
# --------- DEV FLAGS ----------
timer = 0 # Initialization only
devNotTopmost = False # remove TOPMOST flag from window properties. Fix black screen using debugger
devDebugging = False # To make it no longer TOPMOST and 400x400 in the upper left corner
devMeasureLoop = False # don't forget to make fps unlimited too
devMeasureLoopLength = 300 # if true
devMeasureLoopParticlesAmount = 20
devShowOwnCPUPercentInstead = False # Maybe wrong? Taskmanager different, lower.
devPrintMouseSpeed = False
devPrintFPS = False
devFPSUnlimited = False
devSnakevizIt = True # if devMeasureLoop is True: no sleep(10) at the end of execution
devVisibleUpdateRect = False
# --------- DEV FLAGS ----------
#
#
#
#
if __name__ == "__main__":
# ---------- multiproc initialisation
#
# pool=Pool(processes=4) # not yet working or wasting processing power itself
# res=pool.apply_async(square,(10,))
# print(res.get())
cleanup_mei() # see comment inside
environ["SDL_VIDEO_WINDOW_POS"] = "0,0" # Set window position to (0,0) as that is necessary now for some reason
pygame.init()
pygame.display.set_caption("ShitStuckToYourMouse - Sparkles") # title. (stupid)
# pygame.mouse.set_visible(False) # set mouse cursor visibility --- Note: This does NOT work
# --------- Initiatlize variables:
startTime = 0
looping = True
firstRun = True
# settings['numParticles'] = 0 # initialization no longer necessary since dictionary??
# settings['ageColorNoiseMod'] = 0
# settings['ageColorNoise'] = 0
# settings['transparentColor'] = ""
# settings['GRAVITY'] = 0
# settings['FPS'] = 0
# settings['interpolateMouseMovement'] = False
particleContainer = []
# settings['particleColor'] = ""
# settings['particleColorRandom'] = 0
# settings['useOffset'] = False
# settings['offsetX'] = 0
# settings['offsetY'] = 0
# settings['markPosition'] = 0
# settings['dynamic'] = False
# settings['levelVelocity'] = []
config = CaseConfigParser()
parseList = CaseConfigParser(converters={"list": lambda x: [i.strip() for i in x.split(",")]})
config.read("config.ini") # Read config file
config.optionxform = str # Read/write case-sensitive (Actually, read/write as string, which is case-sensitive)
if not config.has_section("SPARKLES"):
setDefaults()
readVariables()
drawParticles: bool = True # For dynamic: Don't draw particles if mouse doesn't move
particleContainer = []
mostUppereLeftPart = []
# Multitasking particle number reduction
i = 0
if settings["multitasking"] > 1:
settings["numParticles"] = int(settings["numParticles"] / settings["multitasking"])
if settings["numParticles"] == 0:
settings["numParticles"] = 1
#print("numParticles = ", settings["numParticles"])
while i < 4:
settings["levelNumParticles"][i] = int(settings["levelNumParticles"][i] / settings["multitasking"])
if settings["levelNumParticles"][i] == 0:
settings["levelNumParticles"][i] = 1
#print("levelNumParticles ", i, " = ", settings["levelNumParticles"][i])
i += 1
#colorRGB = colorRGB
colorRGB = Color(HEXtoRGB(settings["particleColor"]))
hsva = colorRGB.hsva # Limits: H = [0, 360], S = [0, 100], V = [0, 100], A = [0, 100]
randomHue = hsva[0]
if settings["useColorUnderMouse"]:
handleDeviceContext = GetDC(0)
numParticlesBackup = settings["numParticles"]
mousePosition = POINT()
mouseSpeedPixelPerFrame = 0
firstPos = (mousePosition.x, mousePosition.y) # Initiate positions
secondPos = firstPos
#mouseVelocity = ((firstPos[0] - secondPos[0]), (firstPos[1] - secondPos[1]))
mouseVelocity = (0, 0)
blitRectSize = 5
clock = pygame.time.Clock() # for FPS limiting
info = pygame.display.Info() # get screen information like size, to set in pygame.display.set_mode
if devMeasureLoop:
settings["numParticles"] = devMeasureLoopParticlesAmount
settings["particleSize"] = 2
settings["dynamic"] = False
# ---------- Correct input errors and precalculate things:
settings["ageColorNoiseMod"] = clamp(settings["ageColorNoiseMod"], 0.0, 1.0)
ageColorNoiseRange = [x for x in range(-settings["ageColorNoise"], settings["ageColorNoise"] + 1)]
shiftAgeColorNoise = ageColorNoiseRange[round((settings["ageColorNoise"] * 2) * settings["ageColorNoiseMod"])]
transparentColorTuple = tuple(int(settings["transparentColor"].lstrip("#")[i : i + 2], 16) for i in (0, 2, 4)) # convert settings['transparentColor'] to tuple for win32api.RGB(), to reduce hard-coded values. Thanks John1024
# ---------- Set things up:
if (numDisplays := pygame.display.get_num_displays()) > 1: # numDisplays declaration AND if
print()
print("----------- More than one displays detected -----------")
print("pygame.display.get_num_displays = ", pygame.display.get_num_displays())
print("pygame.display.Info = ", pygame.display.Info())
print("pygame.display.get_desktop_sizes = ", pygame.display.get_desktop_sizes())
print()
info = pygame.display.get_desktop_sizes()
# Get the highest display height and widest width
listHeights = []
combinedWidth = 0
i = 0
while i < numDisplays:
combinedWidth = combinedWidth + info[i][0]
i += 1
highestHeight = max(info, key=lambda x: x[1])
print("biggestHeight = ", highestHeight[1])
print("combinedWidth = ", combinedWidth)
print()
if devDebugging:
display_window = pygame.display.set_mode((400, 400), pygame.RESIZABLE, vsync=0) # not TOPMOST
else:
display_window = pygame.display.set_mode((combinedWidth, highestHeight[1]), 0, vsync=0) # vsync only works with OPENGL flag, so far. Might change in the future
else:
if devDebugging:
display_window = pygame.display.set_mode((400, 400), pygame.RESIZABLE, vsync=0) # not TOPMOST
else:
display_window = pygame.display.set_mode((0, 0), 0, vsync=0) # vsync only works with OPENGL flag, so far. Might change in the future
display_window.fill(settings["transparentColor"]) # fill with transparent color set in win32gui.SetLayeredWindowAttributes
setFocus = windll.user32.SetFocus # sets focus to
hWnd = pygame.display.get_wm_info()["window"] # get window handle (hWnd => handleWindow) about this pygame window, in order to address it in setWindowAttributes()
if devDebugging:
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE)
else:
windll.user32.SetWindowLongPtrW(hWnd, GWL_EXSTYLE, windll.user32.GetWindowLongPtrW(hWnd, GWL_EXSTYLE) | WS_EX_TOOLWINDOW) # no taskbar button
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT,)
SetLayeredWindowAttributes(hWnd, RGB(*transparentColorTuple), 255, LWA_COLORKEY)
if devNotTopmost:
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
else:
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
# HWND_TOPMOST: Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated. (Well, it SHOULD. But doesn't.)
# It's not necessary to set the SWP_SHOWWINDOW flag.
# SWP_NOMOVE: Retains the current position (ignores X and Y parameters).
# SWP_NOSIZE: Retains the current size (ignores the cx and cy parameters).
# GWL_EXSTYLE: Retrieve the extended window styles of the window.
# WS_EX_TRANSPARENT: The window should not be painted until siblings beneath the window have been painted, making it transparent.
# WS_EX_LAYERED: The window is a layered window, so that we can set attributes like color with SetLayeredWindowAttributes ...
# LWA_COLORKEY: ... and make that color the transparent color of the window.
# setWindowAttributes(handleWindowDeviceContext) # set all kinds of option for win32 windows. Makes it transparent and clickthrough
if not settings["useOffset"]:
settings["offsetX"] = 0
settings["offsetY"] = 0
# --------- Optimizations:
# ------- RECONSIDER THIS --------
# clock_tick = clock.tick
# display_window_fill = display_window.fill
# windll_user32_GetCursorPos = windll.user32.GetCursorPos
# pygame_display_update = pygame.display.update
# ------- RECONSIDER THIS --------
# ---------- Start the lööp:
# setFocus(handleWindowDeviceContext) # sets focus on pygame window
# with cProfile.Profile() as pr:
if devMeasureLoop:
startTime = process_time()
loop( # looks better
firstRun,
settings["transparentColor"],
settings["interpolateMouseMovement"],
particleContainer,
settings["particleColor"],
randomHue,
settings["offsetX"],
settings["offsetY"],
settings["markPosition"],
settings["numParticles"],
settings["levelVelocity"],
settings["levelNumParticles"],
secondPos,
mousePosition,
drawParticles,
)
# loop( # looks better
# firstRun,
# settings["transparentColor"],
# settings["interpolateMouseMovement"],
# particleContainer,
# settings["particleColor"],
# settings["particleColorRandom"],
# settings["offsetX"],
# settings["offsetY"],
# settings["markPosition"],
# settings["numParticles"],
# mouseSpeedPixelPerFrame,
# mouseVelocity,
# settings["levelVelocity"],
# settings["levelNumParticles"],
# secondPos,
# mousePosition,
# drawParticles,
# )
if devMeasureLoop:
endTime = process_time()
# get execution time
result = endTime - startTime
print("CPU Execution time:", result, "seconds")
# stats = pstats.Stats(pr)
# stats.sort_stats(pstats.SortKey.TIME)
# stats.print_stats()
# loop()
# ...
pygame.quit()
if devMeasureLoop and not devSnakevizIt:
sleep(10)