forked from seoLegna/E07a-Particles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main1.py
156 lines (120 loc) · 4.45 KB
/
main1.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
import sys, logging, os, random, open_color, arcade
version = (3,7)
assert sys.version_info >= version, "This script requires at least Python {0}.{1}".format(version[0],version[1])
logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
MARGIN = 30
SCREEN_TITLE = "Particle Exercise"
PARTICLE_MIN_SCALE = 0.01
PARTICLE_MAX_SCALE = 0.15
PARTICLE_MIN_X = -20
PARTICLE_MAX_X = 20
PARTICLE_VELOCITY_X = 0
PARTICLE_VELOCITY_Y = 1
PARTICLE_MIN_AX = -0.1
PARTICLE_MAX_AX = 0.1
PARTICLE_MIN_AY = -0.01
PARTICLE_MAX_AY = 0.15
PARTICLE_MIN_DECAY = 0.0001
PARTICLE_MAX_DECAY = 0.05
class Particle(arcade.Sprite):
def __init__(self, asset, scale, x, y, dx, dy, ax, ay, decay):
super().__init__("assets/{}.png".format(asset), scale)
self.center_x = x
self.center_y = y
self.dx = dx
self.dy = dy
self.ax = ax
self.ay = ay
self.decay = decay
self.color_pos = 0
self.particle_colors = [
(open_color.cyan_3, 3)
,(open_color.yellow_3, 4)
,(open_color.yellow_4, 4)
,(open_color.yellow_5, 4)
,(open_color.orange_5, 5)
,(open_color.orange_6, 5)
,(open_color.orange_7, 7)
,(open_color.orange_8, 7)
,(open_color.red_8, 8)
,(open_color.red_7, 8)
,(open_color.red_6, 8)
,(open_color.red_5, 8)
,(open_color.gray_7, 8)
,(open_color.gray_9, 8)
]
(self.color, self.lifetime) = self.particle_colors[self.color_pos]
self.alive = True
def update(self):
self.dx += self.ax
self.dy += self.ay
self.center_x += self.dx
self.center_y += self.dy
self.scale -= self.decay
if self.scale < self.decay:
self.scale = self.decay
self.lifetime -= 1
if self.lifetime <= 0:
self.color_pos += 1
if self.color_pos >= len(self.particle_colors):
self.alive = False
else:
(self.color, self.lifetime) = self.particle_colors[self.color_pos]
class Window(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.set_mouse_visible(True)
arcade.set_background_color(open_color.black)
self.particle_list = arcade.SpriteList()
self.mouse_down = False
self.x = SCREEN_WIDTH // 2
self.y = SCREEN_HEIGHT // 2
def setup(self):
pass
def update(self, delta_time):
self.particle_list.update()
if self.mouse_down:
#generate a new particle
x = self.x + random.uniform(PARTICLE_MIN_X, PARTICLE_MAX_X)
y = self.y
dx = PARTICLE_VELOCITY_X
dy = PARTICLE_VELOCITY_Y
ax = random.uniform(PARTICLE_MIN_AX,PARTICLE_MAX_AX)
ay = random.uniform(PARTICLE_MIN_AY,PARTICLE_MAX_AY)
decay = random.uniform(PARTICLE_MIN_DECAY,PARTICLE_MAX_DECAY)
scale = random.uniform(PARTICLE_MIN_SCALE,PARTICLE_MAX_SCALE)
#Particle(asset, sprite scale, initial position [x], initial position [y], velocity [x], velocity [y], acceleration [x], acceleration [y], scale decay)
particle = Particle('muzzle_03',scale,x,y,dx,dy,ax,ay,decay)
self.particle_list.append(particle)
for p in self.particle_list:
#if the particle is off the edge of the screen, kill it
if p.center_x < -50 or p.center_x > SCREEN_WIDTH + 50 or p.center_y < -50 or p.center_y > SCREEN_HEIGHT + 50:
p.kill()
#if it has reached the end of its life
if not p.alive:
p.kill
def on_draw(self):
arcade.start_render()
self.particle_list.draw()
def on_mouse_press(self, x, y, button, modifiers):
self.x = x
self.y = y
self.mouse_down = True
def on_mouse_release(self, x, y, button, modifiers):
self.x = x
self.y = y
self.mouse_down = False
def on_mouse_motion(self, x, y, dx, dy):
self.x = x
self.y = y
def main():
window = Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()