-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (57 loc) · 2.12 KB
/
Copy pathmain.py
File metadata and controls
71 lines (57 loc) · 2.12 KB
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
import pygame
from physics_space import setup_space
from racer import Racer
from obstacle import Obstacle
from utilities import get_unique_colors
from button import Button
def main():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Physics Racing Simulation")
space = setup_space()
colors = get_unique_colors(50)
racers = [Racer(space, color, (600, 0)) for color in colors]
obstacles = [Obstacle(space) for _ in range(10)]
restart_button = Button(10, 10, 100, 50, 'Restart')
reset_obstacles_button = Button(10, 70, 100, 50, 'Reset All')
def restart_simulation():
nonlocal racers
# Remove existing racers from the space
for racer in racers:
racer.remove_from_space(space)
racers = [Racer(space, color, (600, 0)) for color in colors]
def reset_all_simulation():
nonlocal racers, obstacles
# Remove existing obstacles from the space
for obstacle in obstacles:
space.remove(obstacle.body, obstacle.shape)
# Remove existing racers from the space
for racer in racers:
racer.remove_from_space(space)
racers = [Racer(space, color, (600, 0)) for color in colors]
obstacles = [Obstacle(space) for _ in range(10)]
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if restart_button.is_clicked(event):
restart_simulation()
elif reset_obstacles_button.is_clicked(event):
reset_all_simulation()
space.step(1 / 60)
screen.fill((0, 0, 0))
for racer in racers:
racer.update()
racer.draw(screen)
for obstacle in obstacles:
obstacle.draw(screen)
restart_button.draw(screen)
reset_obstacles_button.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()