-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
265 lines (197 loc) · 8.9 KB
/
main.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
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
#!/usr/bin/python
# importing required packages
from os import lseek
import pygame
import pygame.freetype
import pygame.font
import random
import math
# setting initial variables for the window
RED = (255, 0, 0)
running = True
(window_width, window_height) = (900, 600)
(game_width, game_height) = (window_width - 200, window_height)
selected_particle = None
background_color = (120, 160, 250)
pygame.display.set_caption('Ball game')
particle_count = 35
random_momentum = True
gravity_on = False
gravity = (math.pi, 0.02)
mass_of_air = 0.01
elasticity = 0.5
particle_max_size = 30
margin = 5
# Initialize the screen object
screen = pygame.display.set_mode((window_width, window_height))
# Initialize pygame.font library
pygame.font.init()
font = pygame.font.Font(None, 20)
# Initialize empty particle array
my_particles = []
class Particle:
# Constructor function that assigns parameters to internal values of the Particle object
def __init__(self, x, y, size, mass=1):
self.x = x
self.y = y
self.size = size
self.mass = mass
# TEMPORARY: set random colour and thickness
self.colour = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.thickness = 50
# set default speed and angle, can be changed when creating Particle object
self.speed = 0.01
self.angle = math.pi / 2
self.drag = (self.mass / (self.mass + mass_of_air)) ** self.size
# Call pygame package to draw circle, pass values stored in the current (self) Particle object as parameters
def display(self):
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
# Update the x and y position based on the angle and speed of the Particle object
def move(self):
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
if gravity_on:
# Gravity function that adds a pre-defined gravity vector to the existing movement vector of the particle
(self.angle, self.speed) = add_vectors(self.angle, self.speed, *gravity)
# Multiply particle's speed by the drag to introduce air drag
self.speed *= self.drag
# if statements that detect and respond to collisions. Each if statement handles 1 out of 4 sides of the window.
def bounce(self):
if self.x > game_width - self.size:
self.x = 2 * (game_width - self.size) - self.x
self.angle = - self.angle
self.speed *= elasticity
elif self.x < self.size:
self.x = 2 * self.size - self.x
self.angle = - self.angle
self.speed *= elasticity
if self.y > game_height - self.size:
self.y = 2 * (game_height - self.size) - self.y
self.angle = math.pi - self.angle
self.speed *= elasticity
elif self.y < self.size:
self.y = 2 * self.size - self.y
self.angle = math.pi - self.angle
self.speed *= elasticity
class Player(Particle):
pass
# Function that adds two vectors together - used to handle gravity and collisions
def add_vectors(angle1, length1, angle2, length2):
x = math.sin(angle1) * length1 + math.sin(angle2) * length2
y = math.cos(angle1) * length1 + math.cos(angle2) * length2
length = math.hypot(x, y)
angle = 0.5 * math.pi - math.atan2(y, x)
return angle, length
def find_particle(particles, x, y):
for p in particles:
if math.hypot(p.x - x, p.y - y) <= p.size:
return p
return None
# Collision detection function
def collide(p1, p2):
col_dx = p1.x - p2.x
col_dy = p1.y - p2.y
distance = math.hypot(col_dx, col_dy)
# Collision handling requires refactoring as it doesn't currently take into consideration the angle at which both
# particles collide
if distance < p1.size + p2.size:
angle = math.atan2(col_dy, col_dx) + 0.5 * math.pi
total_mass = p1.mass + p2.mass
(p1.angle, p1.speed) = add_vectors(*(p1.angle, p1.speed * (p1.mass - p2.mass) / total_mass),
*(angle, 2 * p2.speed * p2.mass / total_mass))
(p2.angle, p2.speed) = add_vectors(*(p1.angle, p2.speed * (p2.mass - p1.mass) / total_mass),
*(angle + math.pi, 2 * p1.speed * p1.mass / total_mass))
p1.speed *= elasticity
p2.speed *= elasticity
overlap = 0.5 * (p1.size + p2.size - distance + 1)
p1.x += math.sin(angle) * overlap
p1.y -= math.cos(angle) * overlap
p2.x -= math.sin(angle) * overlap
p2.y += math.cos(angle) * overlap
def init_random_particles():
# Create a number of Particle objects using random values to populate the screen
for n in range(particle_count):
particle_size = random.randint(10, particle_max_size)
particle_density = 1.00
particle_x = random.randint(particle_size, game_width - particle_size)
particle_y = random.randint(particle_size, game_height - particle_size)
new_particle = Particle(particle_x, particle_y, particle_size, particle_density * particle_size ** 2)
# particle.colour = (200 - particle_density * 10, 200 - particle_density * 10, 255)
new_particle.speed = random.random()
if random_momentum == True:
new_particle.speed = random.randint(1, 3)
new_particle.angle = random.uniform(0, math.pi * 2)
else:
new_particle.speed = 0
new_particle.angle = random.uniform(0, math.pi * 2)
my_particles.append(new_particle)
#append player particle
my_particles.append(Player(0,0, 50, 1 * 20 ** 2))
def init_test_particles():
test_particle1 = Particle(game_width / 2, 100, 30, 1.00 * 50 ** 2)
test_particle2 = Particle(game_width / 2, 200, 30, 1.00 * 10 ** 2)
test_particle1.speed = 0.0
test_particle1.angle = random.uniform(0, math.pi * 2)
test_particle2.speed = 0.0
test_particle2.angle = random.uniform(0, math.pi * 2)
my_particles.append(test_particle1)
my_particles.append(test_particle2)
def write(text, location, color=(0, 0, 0)):
screen.blit(font.render(text, True, color), location)
def draw_game():
# Reset the scene
screen.fill(background_color)
pygame.draw.line(screen, (0, 0, 0), (game_width, 0), (game_width, game_height))
state = ''
moving_particles_count = 0
# Iterate through all particle objects
for i, particle in enumerate(my_particles):
if particle != selected_particle:
particle.move()
particle.bounce()
for particle2 in my_particles[i + 1:]:
collide(particle, particle2)
particle.display()
if selected_particle:
(mouseX, mouseY) = pygame.mouse.get_pos()
dx = mouseX - selected_particle.x
dy = mouseY - selected_particle.y
selected_particle.angle = math.atan2(dy, dx) - 0.5 * math.pi
selected_particle.speed = math.hypot(dx, dy) * 0.1
# Draw line between mouse pointer and the particle when selected
pygame.draw.line(screen, (0, 0, 0), (mouseX, mouseY), (selected_particle.x, selected_particle.y))
pygame.draw.line(screen, (255, 255, 255), (selected_particle.x, selected_particle.y),
(selected_particle.x + (selected_particle.x - mouseX), selected_particle.y +
(selected_particle.y - mouseY)))
pygame.draw.circle(screen, (0, 0, 0), (mouseX, mouseY), 5, 5)
write('V = ' + str(round(math.hypot(dx, dy), 2)), (mouseX + 20, mouseY + 10))
if particle.speed > 0.01:
moving_particles_count += 1
else:
particle.speed = 0
if moving_particles_count == 0:
state = 'Still'
else:
state = 'Moving'
# Draw text
ball_count = len(my_particles)
write('Ball count: ' + str(ball_count), (game_width + margin, margin))
write('Current state: ' + state, (game_width + margin, margin * 4))
write('Balls moving: ' + str(moving_particles_count), (game_width + margin, margin * 7))
# Swap the frame buffer
pygame.display.flip()
init_random_particles()
# init_test_particles()
# Main loop of the program
while running:
# Listen for Quit message from the X button on the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Detect mouse click, if the user clicked on a particle
if event.type == pygame.MOUSEBUTTONDOWN:
(sel_mouseX, sel_mouseY) = pygame.mouse.get_pos()
selected_particle = find_particle(my_particles, sel_mouseX, sel_mouseY)
elif event.type == pygame.MOUSEBUTTONUP:
selected_particle = None
draw_game()