-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle_spawner.py
170 lines (120 loc) · 4.65 KB
/
particle_spawner.py
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
""" drawDemo.py
demonstrate using the drawing
features in pygame"""
import pygame, math, random
pygame.init()
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
MAXDISTANCE = 250
POINTS = []
C = 1
ADDCHANCE = 0.2
START_NUM = 100
START_SIZE = 2
class Track():
#m = 0
#c = 0
#o = 0
#positive = False
def __init__(self,*args, **kwargs):
#yeah there degrees so what!
self.o = kwargs.pop('center')
degree = random.randint(0,360)
self.m = math.tan(math.radians(degree))
self.c = (self.m *self.o[0])-self.o[1]
if random.randint(0,1) == 1:
self.positive = True
else:
self.positive = False
def __str__(self):
return "Track <M:" + str(self.m) + " C:" +str(self.c) + " O:" +str(self.o[0]) + "," + str(self.o[1]) +">"
class Point():
#track = None
#position = None
#life_distance = None
#colour = (0,0,0)
def __init__(self, *args, **kwargs):
self.track = kwargs.pop('track')
self.colour = self.set_colour()
self.life_distance = random.randint(1,MAXDISTANCE)
self.position = self.track.o
def calc_position(self):
if not self.track.positive:
x = float(self.position[0])-(C/math.sqrt(1+math.pow(self.track.m,2)))
else:
x = float(self.position[0])+(C/math.sqrt(1+math.pow(self.track.m,2)))
y = (x*self.track.m)-self.track.c
#print self.position, CENTER
self.position = (x,y)
def set_colour(self):
return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
def calc_distance_travelled(self):
deltaX = self.position[0] - self.track.o[0]
deltaY = self.position[1] - self.track.o[1]
#print "X:",deltaX,"Y:", deltaY ,"---", math.sqrt((deltaX*deltaX) + (deltaY*deltaY))
return math.sqrt((deltaX*deltaX) + (deltaY*deltaY))
def __str__(self):
print self.position, self.life_distance, self.colour, self.track
def drawCircle(background, point):
pygame.draw.circle(background, point.colour, (int(point.position[0]),int(point.position[1])), START_SIZE)
def addPoint(center):
POINTS.append(Point(track=Track(center = center)))
def getPoints():
return POINTS
def removePoint(i):
del POINTS[i]
class Debug_text():
def __init__(self, *args, **kwargs):
self.points = kwargs.pop('points')
self.screen = kwargs.pop('screen')
self.clock = kwargs.pop('clock')
self.font = pygame.font.SysFont("Monospaced", 20)
def print_text(self):
label_numOfPoint = self.font.render("# Points: " + str(len(self.points)), 1, (255,255,255))
label_frameRate = self.font.render("FPS: " + str(self.clock.get_fps()), 1, (255,255,255))
self.screen.blit(label_numOfPoint, (10, 10))
self.screen.blit(label_frameRate, (10, 30))
#def main():
# track = generateTrack()
# print track
# for tick in range(1,100):
# print getPointonTrack(track,tick)
def main():
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Drawing commands")
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
clock = pygame.time.Clock()
debug =Debug_text(points=getPoints(), screen = screen, clock = clock)
keepGoing = True
center = WINDOW_WIDTH/2, WINDOW_HEIGHT/2
for i in xrange(0,START_NUM):
addPoint(center)
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.MOUSEBUTTONDOWN:
center = pygame.mouse.get_pos()
addPoint(center)
print pygame.mouse.get_pos()
background.fill((0, 0, 0))
for i, point in enumerate(getPoints()) :
point.calc_position()
if point.calc_distance_travelled() < point.life_distance:
drawCircle(background, point = point)
else:
removePoint(i)
if ADDCHANCE >= 1:
if random.randint(1, ADDCHANCE) == 1:
addPoint(center)
else:
for i in xrange(0,int(1/ADDCHANCE)):
addPoint(center)
screen.blit(background, (0, 0)) # put the label object on the screen at point x=100, y=100
debug.print_text()
pygame.display.flip()
if __name__ == "__main__":
main()