-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInflight.py
More file actions
348 lines (284 loc) · 12 KB
/
Copy pathInflight.py
File metadata and controls
348 lines (284 loc) · 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Science Center Project: Inflight Portion
Authors: Callen Reid, Efrain Munoz, Cameron Russell, Samuel Rodriguez, Akshay Yadava
Date: 04/15/20
"""
import turtle
import math
from constants import *
from turtle import *
SCALE = 225.0 / AU
class planet(Turtle):
"""
Class to represent a planet
Attribute xloc: the x location of the planet
Invariant: xloc is a float
Attribute yloc: the y location of the planet
Invariant: yloc is a float
Attribute name: the name of the planet
Invariant: name is a non-empty string
"""
xloc = yloc = 0.0
name=''
mass = 0.0
def getX(self):
return self.xloc
def getY(self):
return self.yloc
class spaceship(planet):
"""
Class to represent a spaceship
"""
# Attribute xloc: the x location of the spaceship
# Invariant: xloc is a float >= 0.0 & <= 800.0
#
# Attribute yloc: the y location of the spaceship
# Invariant: yloc is a float >= 0.0 & <= 800.0
#
# Attribute xVel: the magnitude of the x-component of velocity of the spaceship
# Invariant: xVel is a float >= 0.0
#
# Attribute yVel: the magnitude of the y-component of velocity of the spaceship
# Invariant: yVel is a float >= 0.0
#
# Attribute fuel: the remaining fuel in the spaceship
# Invariant: fuel is a float >= 0.0
def getX(self):
"""
Returns the x location of the spaceship object
"""
return self.xloc
def setX(self, x):
"""
Sets the x-location of the spaceship object
Parameter x: The x-location of the spaceship object
Precondition: x is a float >= 0.0 & <= 800.0
"""
assert isinstance(x, float), 'Invalid type for x, x must be a float'
self.xloc = x
def getY(self):
"""
Returns the y location of the spaceship object
"""
return self.yloc
def setY(self, y):
"""
Sets the y location of the spaceship object
Parameter y: The y-location of the spaceship object
Precondition: y is a float >= 0.0 & <= 800.0
"""
assert isinstance(y, float), 'Invalid type for y, y must be a float'
# assert (y >= 0.0 and y <= 800.0), 'Invalid value for y, y must be between 0 and 800 inclusive'
self.yloc = y
def getXVel(self):
"""
Returns the x-velocity of the spaceship object
"""
return self.xVel
def setXVel(self, vx):
"""
Sets the x-velocity of the spaceship object
Parameter vx: The x-velocity of the spaceship object
Precondtion: vx is a float >=0.0
"""
assert isinstance(vx, float), 'Invalid type for vx, vx must be a float'
# assert (vx >= 0.0), 'Invalid value for vx, vx must be greater than 0.0'
self.xVel = vx
def getYVel(self):
"""
Returns the y-velocity of the spaceship object
"""
return self.yVel
def setYVel(self, vy):
"""
Sets the y-velocity of the spaceship object
Parameter vy: The y-velocity of the spaceship object
Precondtion: vy is a float >=0.0
"""
assert isinstance(vy, float), 'Invalid type for vy, vy must be a float'
# assert (vy >= 0.0), 'Invalid value for vy, vy must be greater than 0.0'
self.yVel = vy
def getFuel(self):
"""
Returns the amount of remaining fuel in the spaceship object
"""
return self.fuel
def setFuel(self, f):
"""
Sets the amount of fuel in the spaeship object
Parameter f: The amount of fuel in the spaceship object
Precondition: f is a float >=0.0
"""
assert isinstance(f, float), 'Invalid type for f, f must be a float'
# assert (vy >= 0.0), 'Invalid value for f, f must be greater than 0.0'
self.fuel = f
def __init__(self, xCoord, yCoord, alt, ang, vel, fuel = float(MASS_FUEL)):
"""
Intiales a space spaceship
Parameter alt: The initial altitude of the rocket w.r.t Earth
Precondition: alt is a float > 0.0
Parameter vel: The magnitude of the inital velocity of the rocket
Precondition: vel is a float > 0.0
Parameter ang: The angle of launch with respect to the positive x-axis
Precondition: ang is a float in the range [0.0..360.0]
Parameter fuel: The amount of remaining fuel in the rocket
Preconditoin: Fuel is a float >= 0.0
"""
assert isinstance(alt,float), 'Invalid type for alt, alt must be a float'
assert alt > 0.0, 'Invalid value for alt, alt must be greater than 0.0'
assert isinstance(ang,float), 'Invalid type for alt, alt must be a float'
assert (ang >= 0.0 and ang <= 360.0), 'Invalid value for alt, alt must be in the range [0.0..360.0]'
assert isinstance(fuel,float), 'Invalid type for fuel, fuel must be a float'
assert fuel > 0.0, 'Invalid value for fuel, fuel must be greater than 0.0'
self.setX(xCoord + math.cos(ang)*alt)
self.setY(yCoord + math.sin(ang)*alt)
self.setFuel(fuel)
self.xVel = vel*math.cos(ang)
self.yVel = vel*math.sin(ang)
self.mass = MASS_ROCKET+MASS_FUEL
self.alt = 0.0
self.ang = 0.0
planet.__init__(self)
def thrust(self):
self.vx=AU * -0.02 / 86400
def attraction(self, other):
rx = other.xloc - self.xloc
ry = other.yloc-self.yloc
r = math.sqrt((rx**2+ry**2))
f = GRAVITATIONAL_CONSTANT*(self.mass*other.mass)/r**2
theta = math.atan2(ry,rx)
fx = math.cos(theta)*f
fy = math.sin(theta)*f
return fx,fy
def loop(system, saturnV, mars):
timestep = 1*24*3600
elapsedRuns = 0
while enroute(saturnV, mars) and not offScreen(saturnV) and elapsedRuns < 2000: #stop the simulation if the spaceship reaches Mars, if the spaceship goes too far off screen, or if the simulation takes too long
saturnV.goto(saturnV.xloc*SCALE, saturnV.yloc*SCALE)
saturnV.pendown()
total_fx = total_fy = 0.0
for body in system:
body.goto(body.xloc*SCALE, body.yloc*SCALE)
fx, fy = saturnV.attraction(body)
total_fx += fx
total_fy += fy
saturnV.setXVel(saturnV.getXVel()+((total_fx*timestep)/saturnV.mass))
saturnV.setYVel(saturnV.getYVel()+((total_fy*timestep)/saturnV.mass))
saturnV.xloc += saturnV.getXVel()*timestep
saturnV.yloc += saturnV.getYVel()*timestep
saturnV.goto(saturnV.xloc*SCALE, saturnV.yloc*SCALE)
elapsedRuns += 1
def enroute(saturnV, mars):
#Checks if the rocket has reached mars yet
#Return False if the rocket has reached mars, and return true otherwise
spaceshipCoord = (saturnV.getX(),saturnV.getY())
endingCoord = (mars.getX(), mars.getY())
rx = spaceshipCoord[0] - endingCoord[0]
ry = spaceshipCoord[1] - endingCoord[1]
r = math.sqrt(rx**2+ry**2)*SCALE
if (r < 40):
return False
return True
def offScreen(saturnV):
#check if the rocket is offscreen
#return true if the rocket is too far offscreen, and return false otherwise
spaceshipCoord = (saturnV.getX(),saturnV.getY())
if (abs(spaceshipCoord[0])>4*10**11 or abs(spaceshipCoord[1])>4*10**11):
return True
else:
return False
def askUser():
#asks the user for the escape angle and escape velocity
#escape angle must be an integer. Escape angle is the angle at which the rocket is leaving the Earth
#escape velocity must be an integer greater than 4000. Escape velocity is the velocity the rocket leaves the Earth at
askingUser = True
while (askingUser): #ask the user for the angle at which the rocket will escape the Earth
spaceshipAngleInput = input(
"At what angle (in degrees) do you want the rocket to leave Earth? (Directly to the right is 0, and the angles increase counterclockwise from there): ")
try: #if the input can't be converted into an integer, ask the user again
spaceshipAngle = int(spaceshipAngleInput)
except:
print("I couldn't understand your input. Please be sure to input a number.")
else:
spaceshipAngle = spaceshipAngle*math.pi/180
askingUser = False
askingUser = True
while (askingUser): #ask the user for the velocity the rocket will escape the Earth at (must be at least 4000 m/s)
spaceshipVelocityInput = input(
"At what velocity do you want the rocket to leave Earth? (Hint: a velocity of at least 4000 m/s is rquired to escape Earth) ")
try: #try to convert the user input into an integer. If it can't be converted, ask again.
spaceshipVelocity = int(spaceshipVelocityInput)
except:
print("I couldn't understand your input. Please be sure to input a number.")
else: #if the value CAN be converted into an integer, check to make sure it's greater than 4000 m/s. Else, ask the user again.
if (spaceshipVelocity < 4000):
print("The velocity wasn't fast enough, please try inputting a faster velocity.")
else:
askingUser = False
return (spaceshipAngle,spaceshipVelocity)
def main():
(spaceshipAngle, spaceshipVelocity) = askUser() #ask the user for an angle to escape the Earth at, and a velocity to escape the Earth with.
turtle.setup(800, 800) # Set the window size to 800 by 800 pixels
turtle.bgcolor("black") # Set up the window with a black background
sun = planet()
sun.name = 'Sun'
sun.mass = 1.98892 * 10**30
sun.penup()
sun.color('yellow')
sun.shape('circle')
sun.diameter = 1.3914 * 10**6
sun.shapesize(5.0,5.0,1)
mercury = planet()
mercury.name = 'Mercury'
mercury.mass = 3.302 * 10**23
mercury.penup()
mercury.color('gray')
mercury.shape('circle')
mercury.shapesize(0.5,0.5,1)
mercury.diameter = 2440*2
mercury.yloc = (1 * AU) * -0.4608453269808703
mercury.xloc = (1 * AU) * -0.06333487572394930
mercury.vy = AU * -0.002399853089908365 / 86400
mercury.vx = AU * 0.02222816779156590 / 86400
venus = planet()
venus.name = 'Venus'
venus.mass = 48.685 * 10**23
venus.penup()
venus.color('Khaki')
venus.shape('circle')
venus.shapesize(1.5,1.5,1)
venus.diameter = 6051.893*2
venus.yloc = (0.7262658 * AU) * 0.0525483
venus.xloc = (0.7262658 * AU) * 0.7232002
venus.vy = AU * 0.0200813 / 86400
earth = planet()
earth.name = 'Earth'
earth.mass = MASS_EARTH
earth.penup()
earth.color('green')
earth.shape('circle')
earth.shapesize(1.5,1.5,1)
earth.diameter = 12742
earth.yloc = (1 * AU) * 0.96756
earth.xloc = (1 * AU) * -0.17522
mars = planet()
mars.name = 'Mars'
mars.mass = MASS_MARS
mars.penup()
mars.color('red')
mars.shape('circle')
mars.shapesize(1.25,1.25,1)
mars.diameter = 3389.92*2
mars.yloc = (1 * AU) * -0.857574644771996
mars.xloc = (1 * AU) * -1.320107604952232
saturnV = spaceship(earth.xloc, earth.yloc, 200.0, float(spaceshipAngle), float(spaceshipVelocity)) #(x location, y location, altitude, angle, velocity, fuel)
saturnV.name = "Saturn V"
saturnV.penup()
saturnV.shape('classic')
saturnV.color('pink')
saturnV.shapesize(0.5,0.5,1)
saturnV.yloc = (1.01 * AU) * 0.96756
saturnV.xloc = (1.01 * AU) * -0.17522
loop([sun, earth, mars, venus, mercury],saturnV,mars) #(solar system, spaceship, target)
if __name__ == '__main__': # The code starts here
main() # Goes to the function called main (line 274)