lunar_lander.html


001| ####################################################################################################
002| import math
003| import random
004| import time
005| import turtle
006| ####################################################################################################
007| 
008| ##================================================================================================##
009| def main ():
010|     # Set up the game window
011|     window = turtle.Screen()
012|     window.tracer(0)
013|     window.setup(0.6, 0.6)
014|     window.title("The Python Lunar Landing Game")
015|     window.bgcolor("black")
016| 
017|     width = window.window_width()
018|     height = window.window_height()
019| 
020|     # Game parameters
021|     n_of_stars = 100
022|     # Lunar module design parameters
023|     branch_size = width / 16
024|     n_of_discs = 5
025|     disc_colour = "light gray"
026|     centre_colour = "gold"
027|     landing_gear_colour = "red"
028|     # Lunar module movement parameters
029|     rotation_step = 0.2
030|     speed_step = 0.1
031|     # Landing parameters
032|     landing_pad_position = 0, -height / 2.1
033|     module_landing_position = (
034|         landing_pad_position[0],
035|         landing_pad_position[1] + branch_size,
036|     )
037|     landing_pos_tolerance_x = 20
038|     landing_pos_tolerance_y = 5
039|     landing_orientation = 270  # vertically downwards
040|     landing_orientation_tolerance = 15
041| 
042|     gravity = 0.03
043| 
044|     # Create stars and moon
045|     stars = turtle.Turtle()
046|     stars.hideturtle()
047|     stars.penup()
048|     stars.color("white")
049|     for _ in range(n_of_stars):
050|         x_pos = random.randint(-width // 2, width // 2)
051|         y_pos = random.randint(-height // 2, height // 2)
052|         stars.setposition(x_pos, y_pos)
053|         stars.dot(random.randint(2, 6))
054| 
055|     moon = turtle.Turtle()
056|     moon.penup()
057|     moon.color("slate gray")
058|     moon.sety(-height * 2.8)
059|     moon.dot(height * 5)
060| 
061|     # Create landing pad
062|     landing_pad = turtle.Turtle()
063|     landing_pad.hideturtle()
064|     landing_pad.penup()
065|     landing_pad.setposition(landing_pad_position)
066|     landing_pad.pendown()
067|     landing_pad.pensize(10)
068|     landing_pad.forward(branch_size / 2)
069|     landing_pad.forward(-branch_size)
070|     landing_pad.forward(branch_size / 2)
071| 
072|     # Create the lunar module
073|     lunar_module = turtle.Turtle()
074|     lunar_module.penup()
075|     lunar_module.hideturtle()
076|     lunar_module.setposition(-width / 3, height / 3)
077|     lunar_module.rotation = random.randint(-9, 9)
078|     lunar_module.clockwise_thruster = False
079|     lunar_module.anticlockwise_thruster = False
080|     lunar_module.travel_speed = random.randint(1, 3)
081|     lunar_module.travel_direction = random.randint(-45, 0)
082| 
083|     def draw_lunar_module():
084|         lunar_module.clear()
085|         # "save" the starting position and orientation
086|         position = lunar_module.position()
087|         heading = lunar_module.heading()
088| 
089|         lunar_module.pendown()
090|         lunar_module.pensize(5)
091|         # Landing gear
092|         lunar_module.color(landing_gear_colour)
093|         lunar_module.forward(branch_size)
094|         lunar_module.left(90)
095|         lunar_module.forward(branch_size / 2)
096|         lunar_module.forward(-branch_size)
097|         lunar_module.forward(branch_size / 2)
098|         lunar_module.right(90)
099|         lunar_module.forward(-branch_size)
100|         lunar_module.pensize(1)
101|         # Pods around the edge of the module
102|         lunar_module.color(disc_colour)
103|         for _ in range(n_of_discs - 1):
104|             lunar_module.right(360 / n_of_discs)
105|             lunar_module.forward(branch_size)
106|             lunar_module.dot(branch_size / 2)
107|             lunar_module.forward(-branch_size)
108|         # Centre part of the lunar module
109|         lunar_module.color(centre_colour)
110|         lunar_module.dot(branch_size)
111|         lunar_module.penup()
112| 
113|         # reset the turtle to initial position and orientation
114|         lunar_module.setposition(position)
115|         lunar_module.setheading(heading)
116| 
117|     # Create burning fuel
118|     burning_fuel = turtle.Turtle()
119|     burning_fuel.penup()
120|     burning_fuel.hideturtle()
121| 
122|     def draw_burning_fuel(thruster):
123|         # Place turtle in the correct location
124|         # depending on which thruster is on
125|         if thruster == "clockwise":
126|             direction = 1
127|         elif thruster == "anticlockwise":
128|             direction = -1
129|         burning_fuel.penup()
130|         burning_fuel.setposition(lunar_module.position())
131|         burning_fuel.setheading(lunar_module.heading())
132|         burning_fuel.right(direction * 360 / n_of_discs)
133|         burning_fuel.forward(branch_size)
134|         burning_fuel.left(direction * 360 / n_of_discs)
135| 
136|         # Draw burning fuel
137|         burning_fuel.pendown()
138|         burning_fuel.pensize(8)
139|         burning_fuel.color("yellow")
140|         burning_fuel.forward(branch_size)
141|         burning_fuel.backward(branch_size)
142|         burning_fuel.left(5)
143|         burning_fuel.color("red")
144|         burning_fuel.pensize(5)
145|         for _ in range(2):
146|             burning_fuel.forward(branch_size)
147|             burning_fuel.backward(branch_size)
148|             burning_fuel.right(10)
149| 
150|     def turn_on_clockwise_thruster():
151|         lunar_module.clockwise_thruster = True
152| 
153|     def turn_on_anticlockwise_thruster():
154|         lunar_module.anticlockwise_thruster = True
155| 
156|     def turn_off_clockwise_thruster():
157|         lunar_module.clockwise_thruster = False
158| 
159|     def turn_off_anticlockwise_thruster():
160|         lunar_module.anticlockwise_thruster = False
161| 
162|     window.onkeypress(turn_on_clockwise_thruster, "Right")
163|     window.onkeypress(turn_on_anticlockwise_thruster, "Left")
164|     window.onkeyrelease(turn_off_clockwise_thruster, "Right")
165|     window.onkeyrelease(turn_off_anticlockwise_thruster, "Left")
166|     window.listen()
167| 
168|     # Applying forces to translate the lunar module
169|     def apply_force(mode):
170|         # Initial components of lunar module velocity
171|         tangential = lunar_module.travel_speed
172|         normal = 0
173| 
174|         if mode == "gravity":
175|             force_direction = -90
176|             step = gravity
177|         elif mode == "thrusters":
178|             force_direction = lunar_module.heading() + 180
179|             step = speed_step
180| 
181|         angle = math.radians(
182|             force_direction - lunar_module.travel_direction
183|         )
184| 
185|         # New components of lunar module velocity
186|         tangential += step * math.cos(angle)
187|         normal += step * math.sin(angle)
188| 
189|         direction_change = math.degrees(
190|             math.atan2(normal, tangential)
191|         )
192|         lunar_module.travel_direction += direction_change
193| 
194|         lunar_module.travel_speed = math.sqrt(
195|             normal ** 2 + tangential ** 2
196|         )
197| 
198|     # Check for successful landing
199|     def check_landing():
200|         if (
201|             abs(lunar_module.xcor() - module_landing_position[0])
202|             < landing_pos_tolerance_x
203|             and abs(lunar_module.ycor() - module_landing_position[1])
204|             < landing_pos_tolerance_y
205|         ):
206|             if (
207|                 abs(lunar_module.heading() - landing_orientation)
208|                 < landing_orientation_tolerance
209|             ):
210|                 lunar_module.setposition(module_landing_position)
211|                 lunar_module.setheading(landing_orientation)
212|                 draw_lunar_module()
213|                 burning_fuel.clear()
214|                 return True
215|             else:
216|                 burning_fuel.clear()
217|                 return False  # Crash on landing pad - wrong angle
218|         if lunar_module.ycor() < -height / 2:
219|             burning_fuel.clear()
220|             return False  # Crash below moon surface
221|         return None  # No successful or unsuccessful landing yet
222| 
223|     while True:
224|         burning_fuel.clear()
225|         # Apply thrust if both thrusters are on
226|         if (
227|             lunar_module.clockwise_thruster
228|             and lunar_module.anticlockwise_thruster
229|         ):
230|             apply_force("thrusters")
231|         # Change rotational speed of lunar module
232|         if lunar_module.clockwise_thruster:
233|             draw_burning_fuel("clockwise")
234|             lunar_module.rotation -= rotation_step
235|         if lunar_module.anticlockwise_thruster:
236|             draw_burning_fuel("anticlockwise")
237|             lunar_module.rotation += rotation_step
238|         # Apply effect of gravity
239|         apply_force("gravity")
240|         # Rotate lunar module
241|         lunar_module.left(lunar_module.rotation)
242| 
243|         # Translate lunar module
244|         x = lunar_module.travel_speed * math.cos(
245|             math.radians(lunar_module.travel_direction)
246|         )
247|         y = lunar_module.travel_speed * math.sin(
248|             math.radians(lunar_module.travel_direction)
249|         )
250|         lunar_module.setx(lunar_module.xcor() + x)
251|         lunar_module.sety(lunar_module.ycor() + y)
252| 
253|         # Refresh image of lunar module
254|         draw_lunar_module()
255| 
256|         # Check for successful or unsuccessful landing
257|         successful_landing = check_landing()
258|         if successful_landing is not None:
259|             if successful_landing:
260|                 window.title("Well Done! You've landed successfully")
261|             else:
262|                 window.bgcolor("red")
263|                 window.title("The lunar module crashed")
264|             break
265| 
266|         time.sleep(0.05)
267|         window.update()
268| 
269|     turtle.done()
270| 
271| ####################################################################################################
272| if __name__ == '__main__':
273|     print()
274|     main()
275|     print()
276| ####################################################################################################
277| '''eof'''
278|