-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanetAnimation.py
192 lines (163 loc) · 7.14 KB
/
PlanetAnimation.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
# this animates a single planet and its moons
import pygame
from src.Planet import *
from src.Moon import *
from src.menu import *
from src.PlanetExplorer import PlanetExplorer
from src import main
from src import menu
# from OpenGL.GL import *
# TODO: zooooooomiiiin
# some imports and globals
# define display size
display_width = 800
display_height = 600
ico = pygame.image.load('../imgs/favicon.ico')
pygame.init()
pygame.font.init() # for text
# globals for controlling the animation
sim = True
pause = False
class PlanetAnimation:
def __init__(self, planet):
self.planet = planet
self.CreateMoons() # init the moons
def setSimulation(self, simulation):
self.exp = PlanetExplorer(simulation)
def CreateMoons(self):
for nbMoon in range(self.planet.nrMoons):
if(len(str(self.planet.mass)) <= 8):
mass = random.randint(round(self.planet.mass / 10000), round(self.planet.mass / 100)) # for now its hardcoded, may think
else: # about change later
mass = random.randint(round(self.planet.mass / 1000000), round(self.planet.mass / 1000))
if mass == 0:
mass = 1
highBorderDistance = int(round((mass / 4.8 * math.pi) ** (1. / 3)) ** 3)
lowBorderDistance = int(round((mass / 97.2 * math.pi) ** (1. / 3)) ** 3)
if lowBorderDistance == 0:
lowBorderDistance = 1
radius = random.randint(lowBorderDistance, highBorderDistance)
moon = Moon(radius, mass, self.planet)
self.planet.moons.append(moon)
def unpause(self):
global pause
pause = False
# pause function
def paused(self, DISPLAY):
global pause
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.unpause()
# return to menu from pause
if event.key == pygame.K_e:
self.exp.pe_main()
if event.key == pygame.K_m: # if m then stop and go to menu
# self.planet.moons = []
menu.mainer(DISPLAY)
if event.key == pygame.K_a:
main.apploop(DISPLAY)
def text_object(self, msg, size):
myfont = pygame.font.SysFont("../imgs/Ubuntu-R.ttf", size)
textsurface = myfont.render(msg, False, WHITE)
return textsurface
def multiline_txt(self, DISPLAY, text, pos, font):
words = [word.split(' ') for word in text.splitlines()] # 2D array where each row is a list of words.
space = font.size(' ')[0] # The width of a space.
max_width, max_height = DISPLAY.get_size()
x, y = pos
for line in words:
for word in line:
word_surface = font.render(word, 0, WHITE)
word_width, word_height = word_surface.get_size()
if x + word_width >= max_width:
x = pos[0] # Reset the x.
y += word_height # Start on new row.
DISPLAY.blit(word_surface, (x, y))
x += word_width + space
x = pos[0] # Reset the x.
y += word_height # Start on new row.
def printPlanetInfo(self, DISPLAY, w, h):
myfont = pygame.font.Font("../imgs/Ubuntu-R.ttf", 15)
info = self.planet.printPlanet()
self.multiline_txt(DISPLAY, info, (0.05*w+20, 0.05*h+20), myfont)
pygame.display.update()
def planet_events(self, DISPLAY, w, h, x, y, scale):
#DISPLAY.fill(RICHBLUE)
self.planet.drawBigPlanet(DISPLAY, int(h*0.1), scale)
maintxt = "Planet Information"
textSurf = self.text_object(maintxt, 20)
DISPLAY.blit(textSurf, (0.05 * w, 0.05 * h))
for m in self.planet.moons:
m.drawOrbit(DISPLAY, x, y, scale)
self.printPlanetInfo(DISPLAY, w, h)
#pygame.display.update()
def planet_anim(self, screen, w, h, x, y, scale):
self.planet_events(screen, w, h, x, y, scale)
for m in self.planet.moons:
m.animate(screen, x, y, h, scale)
def check_scale(self, scale):
new_scale = scale
if scale <= 0.2:
new_scale = 0.2
elif scale >= 50:
new_scale = 50
return new_scale
# animates the moons
def animateMoons(self, screen, w, h, scale):
global sim, pause
#screen = pygame.display.set_mode((display_width, display_height), pygame.RESIZABLE)
pygame.display.set_caption('Planetbox')
pygame.display.set_icon(ico)
screen.fill(RICHBLUE)
pygame.display.update()
planet_pos_x = int(w / 2)
planet_pos_y = int(h / 2)
clock = pygame.time.Clock()
sim = True
while sim:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.VIDEORESIZE:
screen = pygame.display.set_mode(event.dict['size'],
pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
screen.fill(RICHBLUE)
sim = False
scale = 1
self.animateMoons(screen, event.dict['w'], event.dict['h'], scale)
# KEY EVENTS
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: # if space pressed then pause
pause = True
self.paused(screen)
if event.key == pygame.K_e: # back to explorer
self.exp.pe_main()
if event.key == pygame.K_m: # if m then stop and go to menu
menu.mainer(screen)
if event.key == pygame.K_a:
main.apploop(screen)
if event.key == pygame.K_r: # resets zoom
scale = 1
screen.fill(RICHBLUE)
self.animateMoons(screen, display_width, display_height, scale)
if event.key == pygame.K_UP: # zoom up
scale += 0.1
scale = self.check_scale(scale)
screen.fill(RICHBLUE)
self.animateMoons(screen, display_width, display_height, scale)
if event.key == pygame.K_DOWN: # zoom down
scale -= 0.1
scale = self.check_scale(scale)
screen.fill(RICHBLUE)
self.animateMoons(screen, display_width, display_height, scale)
screen.fill(RICHBLUE)
# draw the planet and the planet's info
self.planet_anim(screen, w, h, planet_pos_x, planet_pos_y, scale)
pygame.display.flip()
clock.tick(100)