-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
119 lines (92 loc) · 3.15 KB
/
main.py
File metadata and controls
119 lines (92 loc) · 3.15 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
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
import random
import pygame
from simulator.rocket import *
from simulator.config import *
from simulator.spacecalc import *
CRASH_DIST = 5
OUT_DIST = 10000
def main():
cfg = Config()
offset_x = cfg.getInitialX()
offset_y = cfg.getInitialY()
zoom = cfg.getInitialZoom()
landing_speed = cfg.getLandingMaxSpeed()
#PyGame init
pygame.init()
screen = pygame.display.set_mode(cfg.getDisplay())
pygame.display.set_caption("RedTroll space program")
#Space init
bg = Surface(cfg.getDisplay())
bg.fill(Color(cfg.getSpaceColor()))
#Draw fixed stars
for i in range(cfg.getStarNumber()):
draw.circle(bg, Color(random.sample(cfg.getStarColors(), 1)[0]),
(random.randrange(cfg.getWidth()),
random.randrange(cfg.getHeight())),
0)
#Timer init
timer = pygame.time.Clock()
#Solar system init
system = cfg.getSystem()
#Focus screen on
focused = cfg.getInitialFocus()
calc = SpaceCalculator(CRASH_DIST, OUT_DIST, landing_speed, cfg)
done = False
paused = False
save_screen = False
frame_no = 0
frame_saved = 0
while not done:
timer.tick(30)
for e in pygame.event.get():
if e.type == QUIT:
done = True
break
if e.type == KEYDOWN:
if e.key == K_q or e.key == K_ESCAPE:
done = True
break
if e.key == K_p or e.key == K_SPACE:
paused = not paused
if e.key == K_f:
pygame.display.toggle_fullscreen()
if e.key == K_LEFT:
offset_x += 10
if e.key == K_RIGHT:
offset_x -= 10
if e.key == K_UP:
offset_y += 10
if e.key == K_DOWN:
offset_y -= 10
if e.key == K_z:
zoom *= 2
if e.key == K_x:
zoom /= 2
if e.key == K_s:
save_screen = not save_screen
if e.key == K_TAB:
focused += 1
if focused >= len(system):
focused = 0
print('View focused on: ' + system[focused].getName())
if not paused:
calc.newPosition(system)
#Put space to screen
screen.blit(bg, (0, 0))
calc.drawSystem(system, screen, zoom, offset_x, offset_y, focused)
#update screen
pygame.display.update()
frame_no += 1
if save_screen and (frame_no - frame_saved > 1):
file_to_save = 'screens/screen%05d.tga' % (frame_saved, )
image.save(screen, file_to_save)
frame_saved = frame_no
if calc.collisionDetected():
print("Collision detected! Exiting... ")
break
if calc.outOfSystemDetected():
print("Out of system! Exiting...")
break
print(":-)")
if __name__ == "__main__":
main()