<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lola on Medium]]></title>
        <description><![CDATA[Stories by Lola on Medium]]></description>
        <link>https://medium.com/@lolaklyceva06?source=rss-6df9deb14993------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SUq9R1XazgIB8wPo</url>
            <title>Stories by Lola on Medium</title>
            <link>https://medium.com/@lolaklyceva06?source=rss-6df9deb14993------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:31:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lolaklyceva06/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a 2D Solar System Model with Python is no longer a theory]]></title>
            <link>https://medium.com/@lolaklyceva06/building-a-2d-solar-system-model-with-python-is-no-longer-a-theory-20163481fc85?source=rss-6df9deb14993------2</link>
            <guid isPermaLink="false">https://medium.com/p/20163481fc85</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[planets]]></category>
            <category><![CDATA[cosmos]]></category>
            <dc:creator><![CDATA[Lola]]></dc:creator>
            <pubDate>Sat, 12 Apr 2025 19:59:29 GMT</pubDate>
            <atom:updated>2025-04-12T19:59:29.553Z</atom:updated>
            <content:encoded><![CDATA[<p>Even Python Beginners Can Create a Planet Simulation — Here’s How:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*gLhFHfNXvaOLJWqv-J_OsA.jpeg" /></figure><p>Hi, I am Lola.</p><p>Recently, I was able to create a Planet Simulation using:</p><ul><li><a href="https://youtu.be/WTLPmUHTPqo"><em>Youtube tutorial</em></a></li><li><em>AI tools(Claude.ai and ChatGPT)</em></li><li><em>Basic programming knowledge</em></li></ul><p>I was a complete beginner in this field until I decided to just dive in and make it happen, no matter what.</p><p>In this article I’ll walk you through how I built the simulation using <em>real astronomical values</em> and <em>basic physics principles</em>. I’ll explain each step(10) in detail and share some very important lessons and insights I learned along the way that you must know and tell you <em>why you should give it a shot .</em></p><h4>Let’s dive in →</h4><h3>1. Setting Up the Environment</h3><blockquote>First we need to choose where we will work and install<strong> Pygame:<br></strong><em> </em><strong>Vs Code</strong> <em>or</em> <strong>Pycharm</strong><em>(recommended)</em></blockquote><p>I chose to download <strong>PyCharm. </strong>There were two versions of it on the official website (<a href="https://www.jetbrains.com/pycharm/">https://www.jetbrains.com/pycharm/</a>): <strong>“Community Edition”</strong> (free) or <strong>“Professional”</strong> (paid). I opted for the Community Edition because it is suitable for projects like this, and it supports the <strong>Pygame library</strong> that we’ll be using for the simulation.</p><p>Once you have PyCharm downloaded you can install Pygame. Python has<strong> many other libraries</strong> besides Pygame, depending on what you want to do. We’re using Pygame because it makes it easy to create <strong>2D graphics and simulations</strong> in Python.</p><p>The Youtube tutorial provides <em>several methods</em> for installing Pygame, so you shouldn’t have any trouble. However, if you do run into issues, feel free to ask <strong>AI for help</strong>. After the installion , in Pycharm, we import<em> pygame and math libraries</em>(<strong>math</strong> <strong>library</strong> is a <strong>built-in </strong>Python module, so you just need to import it<strong>)</strong></p><p>Now we can <strong>initialize</strong> our simulation with<strong> </strong>pygame.init() It is a function that initializes all the modules of <strong>Pygame</strong> that your program needs</p><pre>import pygame<br>import math<br><br>pygame.init()</pre><h3>2. Setting Up the Window</h3><p>After initializing our program, we need to create a <strong>Pygame window</strong> where everything will run and give it a name.</p><pre>WIDTH, HEIGHT = 800, 800<br>WIN = pygame.display.set_mode((WIDTH, HEIGHT))<br>pygame.display.set_caption(&quot;Planet Simulation&quot;)</pre><blockquote><strong>WIDTH<em> and </em>HEIGHT<em> </em></strong>→ define the<strong> size of the simulation window </strong>(800x800 pixels).</blockquote><blockquote><strong><em>pygame.display.set_mode((WIDTH, HEIGHT))</em></strong><em> </em>→ creates a <strong>window</strong> where the simulation will be displayed.</blockquote><blockquote><strong><em>pygame.display.set_caption(“Planet Simulation”) </em></strong>→ sets the <strong>window title.</strong></blockquote><h3>3. Defining Colors(constants)</h3><pre>WHITE = (255, 255, 255)<br>YELLOW = (255, 255, 0)<br>BLUE = (100, 149, 237)<br>RED = (188, 39, 50)<br>DARK_GREY = (80, 78, 81)</pre><p>As you see we’re already setting colors, because they are defined as <strong>constants</strong> (values that don’t change).</p><p>In programming, it’s common practice to define constants<strong> at the top</strong> so that they are available to <strong>all parts of the code</strong>.</p><p>If they were inside a function, we wouldn’t be able to use them <strong>outside of that function</strong> unless we specifically passed them.</p><p>Next we<strong> </strong>define <strong>text font</strong> for Display:</p><pre>FONT = pygame.font.SysFont(&quot;comicsans&quot;, 16)</pre><h3>4. Creating the Planet Class</h3><p>In this step, we create a <strong>class</strong> called Planet to represent each planet in our simulation.</p><p><strong>Why Do We Use a Class?</strong></p><p>A <strong>class</strong> is a blueprint for creating objects. Since we’re simualting <strong>4 planets</strong> (Earth, Mars, Mercury, Venus), we need an easy way to create and manage them. Instead of writing separate code for <strong>each planet</strong>, we define a class that holds <strong>all the properties and behaviors of a planet</strong>.</p><p>That’s why at the <strong>top</strong> of the Class, we define some important<strong> planetary</strong> <strong>constants</strong> for our simulation(just like we did earlier by setting colors — constants at the top)</p><pre>class Planet:<br>    AU = 149.6e6 * 1000  <br>    G = 6.67428e-11  <br>    SCALE = 250 / AU  <br>    TIMESTEP = 3600 * 24</pre><blockquote><strong><em>AU</em></strong><em>:</em> 1 Astronomical Unit (the distance between Earth and the Sun) in meters.</blockquote><blockquote><strong><em>G</em></strong>: The gravitational constant, used to calculate the gravitational force.</blockquote><blockquote><strong><em>SCALE</em></strong><em>:</em> Converts real-world distances into pixels so planets fit on the screen.</blockquote><blockquote><strong><em>TIMESTEP</em></strong>: Each simulation step represents 1 day in real-time.</blockquote><p>💡 <strong>In simple terms: The class acts like a blueprint, and each planet is an object that follows this blueprint.</strong></p><h3>5. Initializing Each Planet’s Properties</h3><p>In this step, we define the <strong>constructor</strong> <strong>method<em>(</em></strong><em>__init__</em><strong><em>)</em></strong>, which runs when we create a planet:</p><p>This method is inside the class because <strong>each planet</strong> has unique properties:</p><ul><li>Position (x, y)</li><li>Size (radius)</li><li>Mass (mass)</li><li>Color (color)</li></ul><p>📌 <strong>Why inside the class?</strong><br> Because when we create a planet, it <strong>automatically gets</strong> these properties. Without this, we’d have to manually store this data for each planet in separate variables, which is inefficient.</p><pre>def __init__(self, x, y, radius, color, mass):<br>    self.x = x<br>    self.y = y<br>    self.radius = radius<br>    self.color = color<br>    self.mass = mass<br><br>    self.orbit = []  <br>    self.sun = False  <br>    self.distance_to_sun = 0<br><br>    self.x_vel = 0  # Initial X velocity<br>    self.y_vel = 0  # Initial Y velocity</pre><p>📌 <strong>What does this do?</strong></p><blockquote><strong><em>x, y</em></strong> → Position of the planet in space.</blockquote><blockquote><strong><em>radius</em></strong> → Size of the planet on screen.</blockquote><blockquote><strong><em>color</em></strong><em> </em>→ Planet’s color (predefined at the top of the code).</blockquote><blockquote><strong><em>mass</em></strong><em> </em>→ Determines gravitational pull.</blockquote><blockquote><strong><em>orbit</em></strong><em> </em>→ Stores the planet’s past positions for orbit visualization.</blockquote><blockquote><strong><em>sun</em></strong><em> </em>→ Set to True if the planet is the Sun.</blockquote><blockquote><strong><em>distance_to_sun</em></strong> → Stores how far the planet is from the Sun.</blockquote><blockquote><strong><em>x_vel, y_vel</em></strong> → Initial velocity (how fast the planet moves in x and y directions).</blockquote><p>🚀 <strong>Why are x_vel and y_vel set to 0?</strong><br> Because planets <strong>start at rest</strong>, and their velocities will be calculated based on gravity.</p><h3>6. Drawing a Planet</h3><p>This method also belongs inside the class because <strong>each planet should know how to draw itself :)</strong></p><p>📌 <strong>Why inside the class?</strong></p><ul><li>The planet knows its own <strong>position, size, and color</strong>, so it can draw itself.</li><li>If this function were <strong>outside the class</strong>, we would have to <strong>pass all these values manually</strong> every time we wanted to draw a planet.</li></ul><p>Instead of:</p><pre>draw_planet(planet.x, planet.y, planet.color, planet.radius)</pre><p>We can just call:</p><pre>planet.draw(WIN) # WIN is a display window where everything in your simulation is drawn</pre><p>Which is <strong>cleaner and easier</strong> to manage.</p><h3>6.1 Converting Real Coordinates to Pygame Coordinates</h3><pre>x = self.x * self.SCALE + WIDTH / 2<br>y = self.y * self.SCALE + HEIGHT / 2</pre><p>This part is converting the planet’s position<strong> in space </strong>into a position on the <strong>screen</strong>.</p><h3>Why we do this:</h3><p>In space, the Sun is at (0, 0), and planets have positions like millions of kilometers away. But your screen can’t handle those huge values directly — it’s only 800x800 pixels!</p><h3>So, what this line does:</h3><ol><li>self.x * self.SCALE</li></ol><ul><li><em>Shrinks the real space distance into screen-friendly size (pixels).</em></li><li><em>For example, 1 AU becomes ~100 pixels.</em></li></ul><p>2. + WIDTH / 2 (and HEIGHT / 2)</p><ul><li><em>Moves the center of the simulation to the center of the screen.</em></li><li><em>Without this, the Sun would be in the top-left corner instead of the middle.</em></li></ul><h4>Simple analogy:</h4><p>Imagine you’re drawing the Solar System on paper — you shrink the distances to fit the paper and then center it nicely so<strong> everything fits</strong>.</p><h3>6.2 Drawing the Orbit Path</h3><p>Here we’re drawing the <strong>planet’s orbit path</strong> using stored coordinates:</p><pre> if len(self.orbit) &gt; 2:<br>            updated_points = []<br>            for point in self.orbit:<br>                x, y = point<br>                x = x * self.SCALE + WIDTH / 2<br>                y = y * self.SCALE + HEIGHT / 2<br>                updated_points.append((x, y))<br><br>            pygame.draw.lines(win, self.color, False, updated_points, 2)<br>pygame.draw.circle(win, self.color, (x, y), self.radius)</pre><p>if len(self.orbit) &gt; 2:</p><ul><li><em>This checks if the planet already has </em><strong><em>at least 3 points</em></strong><em> in its orbit.</em></li><li><em>We need more than 2 points to draw a visible path</em></li></ul><p>updated_points = [ ]</p><ul><li><em>This will store the converted orbit points in screen coordinates (pixels)</em>.</li></ul><p>for point in self.orbit:</p><ul><li><em>Loops through all saved positions of the planet in its orbit.</em></li></ul><p><strong>Inside the loop:</strong></p><p>x = x * self.SCALE + WIDTH / 2<br> y = y * self.SCALE + HEIGHT / 2</p><ul><li><em>Converts each point from real space coordinates to screen coordinates, just like we do when drawing the planet.</em></li></ul><p>updated_points.append((x, y))</p><ul><li><em>Saves the converted point so we can draw the line</em></li></ul><p>pygame.draw.lines(win, self.color, False, updated_points, 2)</p><ul><li><em>Draws a smooth, connected line between all the orbit points.</em></li></ul><p>what each part means:</p><blockquote><strong><em>self.color</em></strong> → is the color of the orbit</blockquote><blockquote><strong><em>False</em></strong> → Tells Pygame <strong>not</strong> to connect the last point back to the first one (so it’s not a closed shape)</blockquote><blockquote><strong><em>updated_points</em></strong> → A list of positions that shows the path the planet has taken (its orbit)</blockquote><blockquote><strong>2</strong> → is the line thickness</blockquote><p>Now, we’re drawing the planet itself:</p><p>pygame.draw.circle(win, self.color, (x, y), self.radius)).</p><p>what each part means:</p><blockquote><strong>win</strong> → the screen where we draw (that’s WIN from earlier).</blockquote><blockquote><strong>self.color</strong> → the color of the planet (like yellow for the Sun, blue for Earth, etc.).</blockquote><blockquote><strong>(x, y)</strong> → the planet’s position on the screen (already converted to pixel coordinates).</blockquote><blockquote><strong>self.radius</strong> → how big the planet looks on screen (in pixels, not real size).</blockquote><p>🪐 So this line simply <strong>draws a circle to represent the planet</strong> at its current position</p><p>To make the simulation more <strong>informative</strong>, we display the distance of each planet from the Sun in kilometers. This feature is only applied <strong>to planets</strong> — not the Sun itself — which is why we begin with a <strong>check </strong>to<em> exclude the Sun</em>.</p><p>So this code checks if the object is a planet (not the Sun), and if so, it <strong>renders</strong> the distance <strong>to the Sun</strong> in kilometers <strong>as text</strong> and <strong>displays it centered</strong> on the planet in white font:</p><pre>  if not self.sun:<br>      distance_text = FONT.render(f&quot;{round(self.distance_to_sun / 1000, 1)}km&quot;, 1, WHITE)<br>      win.blit(distance_text, (x - distance_text.get_width() / 2, y - distance_text.get_height() / 2))</pre><blockquote>distance_text = FONT.render(...)</blockquote><blockquote>Converts the distance (in meters) to kilometers by dividing by 1000.<br>Rounds it to <strong>1 decimal place</strong>.<br>Renders the text in white using the chosen font</blockquote><blockquote>win.blit(...)</blockquote><blockquote>Draws the distance text <strong>centered over the planet</strong>.<br>blit just means “draw this text/image onto the screen”</blockquote><h3><em>7. Attraction and Calculating Gravitational Forces</em></h3><p><strong>Before</strong> simulating gravitational force, we need to calculate the<strong> distance</strong> `between` two objects in space. This is essential because gravitational force depends directly on <strong>how far apart </strong>the objects are.</p><p><strong>Each</strong> object in our simulation (like a planet or the sun) has an x and y coordinate. So we start by extracting the x and y coordinates of the other object (<em>another planet or the Sun</em>), then find the <em>difference</em> between those coordinates and the current object’s position. <br>Finally , using the Pythagorean theorem, we calculate the <strong>straight-line distance</strong> between them:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*YmsTI52cLckN8gP6_Q8YQA.png" /><figcaption><strong>Distance formula</strong> derived from the Pythagorean theorem</figcaption></figure><p>If this looks a bit confusing, you can undestand this part, by watching that <a href="https://youtu.be/WTLPmUHTPqo"><strong>Youtube tutorial</strong> </a>or doing a bit of research on the use of Pythagorean theorem in space.</p><p>In code:</p><pre><br>def attraction(self, other):<br>    other_x, other_y = other.x, other.y<br>    distance_x = other_x - self.x<br>    distance_y = other_y - self.y<br>    distance = math.sqrt(distance_x ** 2 + distance_y ** 2)</pre><p><strong>Inputs</strong>: the coordinates of the <strong>two objects</strong><br><strong>Output</strong>: distance — the <strong>straight-line distance</strong> between them (we’ll call it r in the next formula)</p><blockquote><strong><em>other_x, other_y</em></strong> → These represent the <strong>x and y coordinates</strong> of another planet.</blockquote><blockquote><strong><em>distance_x, distance_y </em></strong>→ These represent how far apart two planets are in the <strong>x-direction</strong> and <strong>y-direction</strong>.</blockquote><pre>if other.sun:<br>       self.distance_to_sun = distance</pre><p>Here, we check <strong>whether</strong> the object we’re calculating against is the Sun, if so we SAVE the distance in self.distance_to_sun</p><p>Later, we use this distance to <strong>display the label</strong> like “150.2 million km” next to each plane — just for<em> visual reference </em>in the simulation.</p><p><strong>Now </strong>that we have the <strong>distance</strong>, we can finally calculate the<strong> gravitational force</strong> between the two objects.</p><blockquote><strong>Why do we need it anyways ?</strong></blockquote><blockquote><strong>Because,</strong> we want to simulate <strong>Real Motion</strong> — In real life, planets follow ELLIPTICAL orbits because the Sun’s gravity constantly pulls them <strong>inward</strong> while their velocity moves them <strong>forward.</strong> By calculating the gravitational force, we can make our planets <em>behave similarly</em>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/197/1*l5tT5jLEE1DTZXquoo5oGw.png" /><figcaption>Newton’s Law of Universal Gravitation</figcaption></figure><p><strong>Inputs</strong>:</p><ul><li><strong>self.mass</strong>: the mass of the current object (e.g., a planet),</li><li><strong>other.mass:</strong> the mass of the other object (e.g., the sun),</li><li><strong>distance</strong>: the value from step 1</li><li><strong>G</strong> (gravitational constant):</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*zLEBNEFQ2bUsdxj8TlGPwQ.png" /></figure><p><strong>Output</strong>: The TOTAL gravitational force <strong>between</strong> them</p><p>This formula lets us determine how much the gravity of the Sun (or other planets) pulls on each planet.</p><p>In code:</p><pre>  force = self.G * self.mass * other.mass / distance**2</pre><blockquote><strong><em>self.mass </em></strong>→<em> </em>is the mass of the current planet<em>.<br></em><strong><em> other.mas</em></strong><em>s </em>→ is the mass of the planet it’s interacting with.<em><br></em><strong><em> distance**2 </em></strong>→<strong> </strong>ensures that as planets get farther apart, the force decreases.</blockquote><p>When two objects attract each other through gravity, the <strong>force has a direction</strong> — it doesn’t just pull randomly, it pulls <em>toward</em> the other object.</p><p>To move the planet in the <strong>right direction</strong>, we need to know the angle between the two objects. That’s where this line comes in:</p><pre>angle = math.atan2(distance_y, distance_x)</pre><p>This finds the<strong> exact angle</strong> from the current planet to the other object (like the sun).<br>We then use that angle to <strong>split the gravitational force</strong> into x and y directions (<strong>horizontal</strong> and <strong>vertical</strong> components), so the planet moves DIAGONALLY— not just straight up, down, left, or right:</p><pre> <br>  force_x = math.cos(theta) * force  # Horizontal part of the force<br>  force_y = math.sin(theta) * force  # Vertical part of the force<br>   return force_x, force_y</pre><p>By multiplying the TOTAL force by cos(angle) and sin(angle), we’re figuring out <strong>how much </strong>of the force `pulls`<strong> left-right</strong> and <strong>how much `</strong>pulls`<strong> up-down.</strong></p><p>In other words, this allows us to update the <strong>velocity</strong> in both directions separately.</p><blockquote><strong><em>math.cos() </em></strong><em>and</em><strong><em> math.sin()</em></strong><em> split the force into</em><strong><em> x </em></strong><em>and</em><strong><em> y</em></strong><em> directions.</em></blockquote><p><em>This allows each planet to move realistically under the influence of gravity!</em></p><p><strong>Why do we return </strong>force_x, force_y?<br>Because each planet affects others differently in the <strong>horizontal (x)</strong> and <strong>vertical (y)</strong> directions.</p><h3><strong><em>8. Updating Position Based on Gravity</em></strong><em> (update_position)</em></h3><p>In Pygame, it’s important to update elements like the <strong>planets</strong> or the <strong>ball’s position</strong> using specific code. Without this, the simulation won’t work.</p><pre>def update_position(self, planets):</pre><p>This <em>update method</em> updates the planet’s movement <strong>by applying</strong> gravitational forces.</p><p><strong>1.Initialize Total Forces</strong></p><p>We start by setting the total force in the x-direction (total_fx) and y-direction (total_fy) to <strong>0</strong> because we will calculate them later:</p><pre><br>  total_fx = total_fy = 0</pre><p><strong>2.Loop Through All Planets</strong></p><p>We check each planet in the simulation to see how much gravitational pull it has on the current planet.</p><pre>for planet in planets:<br>    if self == planet:<br>        continue</pre><blockquote><strong><em>if self == planet:</em></strong> → <em>This makes sure a planet doesn’t try to apply force on </em><strong><em>itself</em></strong><em> (because that wouldn’t make sense).</em></blockquote><h4>Second, we <strong>calculate the Force from Each Planet</strong></h4><p>We use the attraction method (which we wrote earlier) to find the force acting on the current planet.</p><pre>fx, fy = self.attraction(planet)</pre><p>This gives us <em>two things</em>:</p><ul><li><strong>fx:</strong> The gravitational force in the x-direction.</li><li><strong>fy</strong>: The gravitational force in the y-direction.</li></ul><p>We <strong>add</strong> these forces to our <strong>total_fx </strong>and <strong>total_fy:</strong></p><pre>total_fx += fx<br>total_fy += fy</pre><p><strong>Third: Turning Force into Acceleration and Updating Velocity</strong></p><p>Now that we know the<strong> total force</strong> acting on the planet, we can update its <strong>velocity</strong>.</p><p>We need a formula that is based on Newton’s Second Law: <strong>F = ma</strong> (Force = mass × acceleration).</p><pre><br>self.x_vel += total_fx / self.mass * self.TIMESTEP<br>self.y_vel += total_fy / self.mass * self.TIMESTEP</pre><p>We divide force by mass to get <strong>acceleration (a= F/m) </strong>and multiply by <strong>TIMESTEP</strong> to simulate `movement` over time.</p><p>Finally, we UPDATE the <strong>x</strong> and <strong>y</strong> positions of the planet using its NEW velocity:</p><pre><br>self.x += self.x_vel * self.TIMESTEP<br>self.y += self.y_vel * self.TIMESTEP</pre><p>This moves the planet to its <strong>New Position</strong>.</p><p>Then, we have to Store the Orbit Path:</p><pre>self.orbit.append((self.x, self.y))</pre><ul><li>We add the new position to the `ORBIT LIST` to track the planet’s path.</li></ul><p><strong>Why do we store the orbit?</strong><br>So we can <strong>draw the planet’s path</strong>, making the simulation more realistic.</p><h3><strong><em>9. Setting Up the Simulation and Creating Planets</em></strong></h3><p>In this step, we’re going to create a loop that keeps our simulation <strong>running</strong> and bring our planets to life by adding them to the scene.</p><p>We need a loop because our simulation needs to <strong>continuously update</strong> and <strong>redraw</strong> the planets on the screen. Without it, the program would just draw everything once and then close immediately</p><h3>Why only in this step?</h3><p>Before this step, we were just <strong>defining</strong> everything — creating the Planet class, setting up colors, initializing variables, and defining functions. But now, we need to <strong>put everything into action</strong>. The loop is where the simulation actually comes to life.</p><h4><strong>1. Setting Up the Simulation</strong></h4><pre>def main():<br>    run = True<br>    clock = pygame.time.Clock()</pre><p>This variable <em>controls</em> whether our simulation keeps running. If it ever becomes<strong> False</strong>, the program will STOP.</p><blockquote><strong><em>clock = pygame.time.Clock() </em></strong>→ This helps control the <strong>speed</strong> of the simulation so that it runs at a steady pace (e.g., 60 updates per second).</blockquote><h4><strong>2. Creating the Planets</strong></h4><p>Now, we define our Sun and planets:</p><pre>sun = Planet(0, 0, 30, YELLOW, 1.98892 * 10**30)<br>sun.sun = True<br><br>earth = Planet(-1 * Planet.AU, 0, 16, BLUE, 5.9742 * 10**24)<br>earth.y_vel = 29.783 * 1000 <br><br>mars = Planet(-1.524 * Planet.AU, 0, 12, RED, 6.39 * 10**23)<br>mars.y_vel = 24.077 * 1000<br><br>mercury = Planet(0.387 * Planet.AU, 0, 8, DARK_GREY, 3.30 * 10**23)<br>mercury.y_vel = -47.4 * 1000<br><br>venus = Planet(0.723 * Planet.AU, 0, 14, WHITE, 4.8685 * 10**24)<br>venus.y_vel = -35.02 * 1000</pre><p><strong>Here’s what’s happening:</strong></p><p>We create the <strong>Sun</strong> at (0,0), give it a radius of 30, and set its color to yellow.</p><p>We create <strong>planets</strong> at different positions with their <strong>real mass and velocity</strong> values.</p><p><strong>y_vel</strong> (initial velocity) makes planets start moving, simulating their orbit.</p><p>We then <strong>store all planets in a list:</strong></p><pre>planets = [sun, earth, mars, mercury, venus]</pre><h3>10.Running the simulation (The Game Loop)</h3><ol><li>The game loop is the core of the simulation. It keeps running while run = True and updates everything.</li></ol><pre>while run:<br>    clock.tick(60)  # Limits the loop to 60 times per second<br>    WIN.fill((0, 0, 0))  # Clears the screen by filling it with black</pre><blockquote><strong><em>clock.tick(60) </em></strong>→ Ensures the game runs <strong>at 60 FPS</strong>. Without this, it could run too fast or inconsistently.</blockquote><blockquote><strong><em>WIN.fill((0, 0, 0))</em></strong> → This <strong>clears the screen</strong> every frame, preventing messy trails.</blockquote><h4><strong>2.Handling Events (Like Closing the Window</strong>)</h4><pre>for event in pygame.event.get():<br>    if event.type == pygame.QUIT:<br>        run = False  # If the user clicks the close button, we stop the loop.</pre><p>This allows the user to <strong>exit the program</strong> by closing the window.</p><h4>3. Updating and Drawing Planets</h4><pre>for planet in planets:<br>    planet.update_position(planets)  # Moves planets based on gravity<br>    planet.draw(WIN)  # Draws planets and their orbits</pre><blockquote><strong><em>planet.update_position(planets)</em></strong><em> </em>→ Calculates <strong>gravity effects</strong> and moves planets accordingly.</blockquote><blockquote><strong><em>planet.draw(WIN)</em></strong> → Draws the planet and its orbit on the screen.</blockquote><p><strong>Finally, we update the display to show the new positions:</strong></p><pre>pygame.display.update()    </pre><p>Without this, nothing would appear on the screen!</p><h4><strong>4.Quitting the Game Properly</strong></h4><pre>pygame.quit() # Closes the game</pre><p>When the loop ends ( if the user closes the window), we <strong>shut down Pygame properly</strong>.</p><blockquote>Ta-da! We are done</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*1vTERQR2-3V7fHuNso6LpA.gif" /></figure><blockquote><strong>Lessons and insights</strong>:</blockquote><h4><strong>1. Use AI and Take Notes</strong></h4><p>I credit at least 70% of my understanding of the code to <strong>AI tools</strong> and <strong>persistence.</strong> AI, especially tools like ChatGPT, can explain things from <em>scratch,</em> which is incredibly helpful.</p><p>However, the amount of information can be `<strong>overwhelming</strong>`, especially for beginners. Without creating <em>patterns </em>or <em>analyzing</em> the content, it’s easy to forget what you’ve learned.</p><p><strong>To avoid this,</strong> I recommend using a <strong>note-taking system</strong>. Personally, I created a private Telegram channel where I would <strong>jot down key takeaways or new insights</strong> while watching tutorial or <strong>discussing the code with ChatGPT.</strong></p><p>This way, I had a dedicated space to <strong>review </strong>and <strong>organize</strong> everything, making the learning process smoother and more manageable.</p><h4><strong>2. Don’t try to understand everything</strong></h4><p>I struggled with<em> perfectionism </em>and often spent HOURS trying to understand <strong>every line of code</strong>. This became unproductive and <strong>prevented</strong> me from completing the project for a LONG time.</p><p>Eventually, I decided to focus on grasping only the <strong>fundamentals</strong> and understanding why certain parts of the code were <strong>necessary.</strong> I paid attention to the <em>role each section or line played</em>. As a result, I reached a 70–80% understanding of the code.</p><p>Later, when I researched it, I found that this level of understanding is actually<em> sufficient</em> for beginners:)</p><h4>3. <strong>What is important is learning in the process</strong></h4><p>If you encounter a problem, focus on solving it first. Research the issue, ask ChatGPT for help, and take the time to learn.</p><p><strong>This approach </strong>not only makes the process more interesting but also helps you stay <em>engaged with your work.</em></p><blockquote><strong>Why you should give it a shot?</strong></blockquote><p>When I started this project, I was completely new to coding and had no experience with Python, libraries, or even basic physics. But as I worked through it step by step, I picked up so much — how different functions work, how to use libraries, and even a little bit of physics.</p><p><strong>The best part?</strong> I learned everything in the process. It wasn’t just about coding; it was about problem-solving, researching, and figuring things out as I went.And when I finally saw the planets moving, I was very happy. It was a small win, but a big confidence boost.</p><p>This project made me realize how<strong> fun </strong>and <strong>rewarding</strong> programming can be. I know, I still have a lot to learn, but now I actually want to keep going, to build more things, to challenge myself.</p><p>If you’re even a little bit curious about coding, I’d say GO FOR IT. You’ll learn way more than you expect, you can <em>boost your programming skill</em> and trust me — <strong>It’s worth it.</strong></p><blockquote>The complete code is given in the<a href="https://youtu.be/WTLPmUHTPqo"><strong> video</strong></a><strong> </strong>description.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20163481fc85" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>