Guest User

Untitled

a guest
Jan 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. #!/usr/bin/python2
  2. import sys, os, math
  3. import pygame
  4.  
  5. G = 6.67 * 10 ** -11
  6. print(G)
  7.  
  8. pygame.init()
  9.  
  10. screen = pygame.display.set_mode((800,600))
  11.  
  12. class Mass:
  13.     def __init__(self,weight,x,y):
  14.         self.mass = weight
  15.         self.x = x
  16.         self.y = y
  17.         self.acceleration = [0,0]
  18.         self.velocity = [0,0]
  19.     def resetAcceleration(self):
  20.         self.acceleration = [0,0]
  21.     def attract(self,mass):
  22.         if self != mass:
  23.             global G
  24.             distance = math.sqrt((self.x + mass.x) ** 2 + (self.y + mass.y) ** 2)
  25.             acceleration = (G * ((self.mass * mass.mass)/distance))/self.mass
  26.             angle = math.atan2(mass.y - self.y,mass.x - self.x)
  27.             x = acceleration * math.cos(angle)
  28.             y = acceleration * math.sin(angle)
  29.             self.acceleration[0] += x
  30.             self.acceleration[1] += y
  31.     def move(self):
  32.         self.x += self.velocity[0]
  33.         self.y += self.velocity[1]
  34.         self.velocity[0] += self.acceleration[0]
  35.         self.velocity[1] += self.acceleration[1]
  36.     def draw(self,screen):
  37.         velocity_scale = 100
  38.         acceleration_scale = 1000000
  39.         pygame.draw.circle(screen,[0,0,0],[int(self.x),int(self.y)],10)
  40.         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])])
  41.         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])])
  42.         #print(str(int(self.x)) + " " + str(int(self.y)))
  43.  
  44. masses = list()
  45. masses.append(Mass(50000,600,100))
  46. masses.append(Mass(500,600,200))
  47. masses.append(Mass(50000,400,300))
  48.  
  49. timescale = 1000
  50.  
  51. while 1:
  52.     #Input
  53.     for event in pygame.event.get():
  54.         if event.type == pygame.QUIT:
  55.             sys.exit()
  56.     #Processing
  57.     for i in range(timescale):
  58.         for mass in masses:
  59.             mass.resetAcceleration()
  60.         for mass in masses:
  61.             for attractionMass in masses:
  62.                 mass.attract(attractionMass)
  63.         for mass in masses:
  64.             mass.move()
  65.     #Rendering
  66.     screen.fill([255,255,255])
  67.     for mass in masses:
  68.         mass.draw(screen)
  69.     pygame.display.flip()
Add Comment
Please, Sign In to add comment