0% found this document useful (0 votes)
4 views4 pages

Pygame Gravity Simulation Code

This document contains a Python script using Pygame to simulate the gravitational interaction between two massive objects. The simulation randomly initializes their positions and updates their movement based on gravitational forces calculated in real-time. The objects are represented as red circles on a white background, and the simulation runs until the user closes the window.

Uploaded by

arielkrkr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Pygame Gravity Simulation Code

This document contains a Python script using Pygame to simulate the gravitational interaction between two massive objects. The simulation randomly initializes their positions and updates their movement based on gravitational forces calculated in real-time. The objects are represented as red circles on a white background, and the simulation runs until the user closes the window.

Uploaded by

arielkrkr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import pygame

import random

import time

)([Link]

WIDTH, HEIGHT = 600, 400

screen = [Link].set_mode((WIDTH, HEIGHT))

m0=1000000000#kg

m1=m0

m2=m0

G=10**(-2)

random_x1_s=[Link](0, WIDTH)

x1=random_x1_s

random_y1_s=[Link](0, HEIGHT)

y1=random_y1_s

x1_tm1=random_x1_s

x1_tm2=random_x1_s

y1_tm1 =random_y1_s

y1_tm2=random_y1_s

random_x2_s=[Link](0, WIDTH)

x2=random_x2_s
random_y2_s=[Link](0, HEIGHT)

y2=random_y2_s

x2_tm1=random_x2_s

x2_tm2=random_x2_s

y2_tm1 =random_y2_s

y2_tm2=random_y2_s

radius = 10

color = (255, 0, 0) # red

)(clock = [Link]

)(last_move_time = [Link]

move_interval = 0.1

dt= move_interval

running = True

:while running

[Link]((255, 255, 255))

:)(for event in [Link]

:if [Link] == [Link]

running = False

)(current_time = [Link]

:if current_time - last_move_time >= move_interval


r12=((x1-x2)**2+(y1-y2)**2)**0.5

f12x= -(G*m1*m2)*(x2-x1)/(abs(r12)**3)

f12y= -(G*m1*m2)*(y2-y1)/(abs(r12)**3)

f21x= -(G*m1*m2)*(x1-x2)/(abs(r12)**3)

f21y= -(G*m1*m2)*(y1-y2)/(abs(r12)**3)

ax1=f12x/m1

ay1=f12y/m1

ax2=f21x/m2

ay2=f21y/m2

x1= (ax1*dt**2)+2*x1_tm1-x1_tm2

y1= (ay1*dt**2)+2*y1_tm1-y1_tm2

x2= (ax2*dt**2)+2*x2_tm1-x2_tm2

y2= (ay2*dt**2)+2*y2_tm1-y2_tm2

last_move_time = current_time

[Link](screen, color, (x1, y1), radius)

[Link](screen, color, (x2, y2), radius)

x1_tm2 = x1_tm1

x1_tm1 = x1

y1_tm2 = y1_tm1

y1_tm1 = y1
x2_tm2 = x2_tm1

x2_tm1 = x2

y2_tm2 = y2_tm1

y2_tm1 = y2

)([Link]

[Link](60)

)([Link]

You might also like