Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- import sys, os, math
- import pygame
- G = 6.67 * 10 ** -11
- print(G)
- pygame.init()
- screen = pygame.display.set_mode((800,600))
- class Mass:
- def __init__(self,weight,x,y):
- self.mass = weight
- self.x = x
- self.y = y
- self.acceleration = [0,0]
- self.velocity = [0,0]
- def resetAcceleration(self):
- self.acceleration = [0,0]
- def attract(self,mass):
- if self != mass:
- global G
- distance = math.sqrt((self.x + mass.x) ** 2 + (self.y + mass.y) ** 2)
- acceleration = (G * ((self.mass * mass.mass)/distance))/self.mass
- angle = math.atan2(mass.y - self.y,mass.x - self.x)
- x = acceleration * math.cos(angle)
- y = acceleration * math.sin(angle)
- self.acceleration[0] += x
- self.acceleration[1] += y
- def move(self):
- self.x += self.velocity[0]
- self.y += self.velocity[1]
- self.velocity[0] += self.acceleration[0]
- self.velocity[1] += self.acceleration[1]
- def draw(self,screen):
- velocity_scale = 100
- acceleration_scale = 1000000
- pygame.draw.circle(screen,[0,0,0],[int(self.x),int(self.y)],10)
- pygame.draw.line(screen,[255,0,0],[self.x,self.y],[int(self.x + velocity_scale*self.velocity[0]),int(self.y + velocity_scale*self.velocity[1])])
- pygame.draw.line(screen,[0,255,0],[self.x,self.y],[int(self.x + acceleration_scale*self.acceleration[0]),int(self.y + acceleration_scale*self.acceleration[1])])
- #print(str(int(self.x)) + " " + str(int(self.y)))
- masses = list()
- masses.append(Mass(50000,600,100))
- masses.append(Mass(500,600,200))
- masses.append(Mass(50000,400,300))
- timescale = 1000
- while 1:
- #Input
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- #Processing
- for i in range(timescale):
- for mass in masses:
- mass.resetAcceleration()
- for mass in masses:
- for attractionMass in masses:
- mass.attract(attractionMass)
- for mass in masses:
- mass.move()
- #Rendering
- screen.fill([255,255,255])
- for mass in masses:
- mass.draw(screen)
- pygame.display.flip()
Add Comment
Please, Sign In to add comment