-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlander.py
More file actions
80 lines (63 loc) · 2.08 KB
/
lander.py
File metadata and controls
80 lines (63 loc) · 2.08 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
'''
File name: lander.py
Author username(s): denneyen, jaltarnr
Date: 11/28/2017
lander.py
A module for writing a Lunar Lander game
Authors: Andy Exley, Erin Denney, Nidhi Jaltare
'''
import interfaces
import graphics
class LunarLander:
'''Fill in this class!
'''
def __init__(self, alt, vel, fuel):
'''Constructor for a LunarLander object'''
self.altitude = alt
self.velocity = vel
self.fuel = fuel
def get_altitude(self):
'''Returns this LunarLander's altitude'''
return self.altitude
def get_velocity(self):
'''Returns this LunarLander's velocity'''
return self.velocity
def get_fuel(self):
'''Returns this LunarLander's fuel'''
return self.fuel
def update(self, thrustamount):
'''Update LunarLander attributes after some fuel is spent to fire
the engine.'''
# 1. Expend thrustamount units of fuel, or what is left
if thrustamount > self.fuel:
thrustamount = self.fuel
self.fuel = self.fuel - thrustamount
# 2. For each unit of fuel burnt, increase ship velocity by 4
self.velocity = self.velocity + 4 * thrustamount
# 3. Gravity
self.velocity = self.velocity - 2
# 4. Calculate new altitude
self.altitude = self.altitude + self.velocity
class LanderGame:
'''This class represents a Lunar Lander Game
'''
def __init__(self):
'''Constructor for the game'''
self.interface = interfaces.GraphicLanderInterface()
self.lander = LunarLander(200, 0, 30)
def play(self):
'''This is the method that plays the game'''
while self.lander.get_altitude() > 0:
self.interface.show_info(self.lander)
amt = self.interface.get_thrust()
self.lander.update(amt)
if self.lander.get_velocity() < -10:
self.interface.show_crash()
else:
self.interface.show_landing()
self.interface.close()
def main():
game = LanderGame()
game.play()
if __name__ == '__main__':
main()