DEV Community

masoomjethwa
masoomjethwa

Posted on

Moon Landing Animation using python

A simple example of how you can create a basic animation of a lander's soft landing on the moon using the Matplotlib library.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML

# Set up the figure and axis
fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_aspect('equal')

# Define the moon's surface as a line
moon_surface, = ax.plot([0, 10], [2, 2], color='gray')

# Define the lander as a circle
lander_radius = 0.3
lander = plt.Circle((5, 9), lander_radius, fc='blue')
ax.add_patch(lander)

# Set initial velocity and position
velocity = -0.1
position = 9

def init():
    return lander,  # Return the lander as the artist to be re-drawn

def update(frame):
    global velocity, position

    # Update position based on velocity
    position += velocity

    # Check if lander has landed
    if position - lander_radius <= 2:
        velocity = 0

    # Update the position of the lander
    lander.center = (5, position)

    return lander,  # Return the lander as the artist to be re-drawn

# Add labels to the X and Y axes
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')

# Create the animation
ani = animation.FuncAnimation(fig, update, init_func=init, frames=range(100), blit=True)

# Convert the animation to HTML
ani_html = HTML(ani.to_jshtml())

# Display the animation in the notebook
ani_html
Enter fullscreen mode Exit fullscreen mode

The animation is displayed directly within the notebook.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay