mixster

mixster

Apr 6th, 2010
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, pygame
  3. from pygame.locals import *
  4. import math, random
  5.  
  6. class Point(object):
  7.     def __init__(self, x=0, y=0):
  8.         self.x = x
  9.         self.y = y
  10.  
  11. class Body(object):
  12.     def __init__(self, mass=1, pos=(0, 0), vel=Point(0, 0), radius=0, color=(0, 0, 0)):
  13.         self.pos = pos
  14.         self.radius = radius
  15.         self.acc = Point(0, 0)
  16.         self.vel = vel
  17.         self.mass = mass
  18.         self.color = color
  19.  
  20.     def move(self, dx, dy):
  21.         self.pos.x += dx
  22.         self.pos.y += dy
  23.  
  24.     def resetForce(self):
  25.         self.acc = Point(0, 0)
  26.  
  27.     def applyForce(self, force):
  28.         self.acc.x += force.x / self.mass
  29.         self.acc.y += force.y / self.mass
  30.  
  31.     def update(self):
  32.         self.vel.x += self.acc.x
  33.         self.vel.y += self.acc.y
  34.         self.move(self.vel.x, self.vel.y)
  35.  
  36.     def draw(self, screen, width=1):
  37.         pygame.draw.circle(screen, self.color, (int(self.pos.x), int(self.pos.y)), self.radius, width)
  38.  
  39. running = True
  40. speed = 1
  41.  
  42. def getEvents():
  43.     for event in pygame.event.get():
  44.         if event.type == KEYDOWN:
  45.             global speed
  46.             if event.key == K_UP:
  47.                 if speed < 64:
  48.                     speed *= 2
  49.                 else:
  50.                     print('There''s a cap at x64!')
  51.             if event.key == K_DOWN:
  52.                 if speed > 2:
  53.                     speed /= 2
  54.                 else:
  55.                     print('No pausing or going backwards!')
  56.             if event.key == ord('q'):
  57.                 #global running
  58.                 running = False
  59.                 sys.exit()
  60.         if event.type == pygame.QUIT:
  61.             #global running
  62.             running = False
  63.             sys.exit()
  64.  
  65. def getGravity(bodyA, bodyB):
  66.     dx = bodyA.pos.x - bodyB.pos.x
  67.     dy = bodyA.pos.y - bodyB.pos.y
  68.     d = dx**2 + dy**2
  69.  
  70.     if d == 0:
  71.         return Point(0, 0)
  72.     else:
  73.         f = ((6.673*(10**-11)) * bodyA.mass * bodyB.mass) / d
  74.         a = math.atan2(dy, dx)
  75.         fx = f * -math.cos(a)
  76.         fy = f * -math.sin(a)
  77.  
  78.         return Point(fx, fy)
  79.  
  80. def randomCol():
  81.     x = random.randint(0, 255)
  82.     y = random.randint(0, 255)
  83.     z = random.randint(0, 255)
  84.     if x + y + z >= 100:
  85.         return (x, y, z)
  86.     else:
  87.         return randomCol()
  88.  
  89. def invertForce(force):
  90.     return Point(-force.x, -force.y)
  91.  
  92. def getVelocity(r, mass):
  93.     if r != 0:
  94.         return abs((6.673*(10**-11)*mass)/r)**0.5*(random.randrange(-1, 2, 2))
  95.     else:
  96.         return 0
  97.  
  98. def getDefaultBody(pos):
  99.     return Body(1, pos, Point(getVelocity(200 - pos.y, 10**11), getVelocity(300 - pos.x, 10**11)), 2, randomCol())
  100.  
  101. pygame.init()
  102. screen = pygame.display.set_mode((600, 400))
  103. pygame.display.set_caption('Gravity simulator')
  104. pygame.mouse.set_visible(0)
  105.  
  106. clock = pygame.time.Clock()
  107. random.seed()
  108.  
  109. planets = []
  110. planets.append(Body(10**11, Point(300, 200), Point(0, 0), 2, (0, 0, 255)))
  111.  
  112. for i in range(1, 20):
  113.     x, y = 0, 0
  114.     if random.randint(0, 1) == 0:
  115.         x = random.randint(25, 200)    
  116.     else:
  117.         y = random.randint(25, 200)
  118.  
  119.     if random.randint(0, 1) == 0:
  120.         x = -x
  121.         y = -y
  122.  
  123.     planets.append(getDefaultBody(Point(300 + x, 200 + y)))
  124.  
  125. while running:
  126.     getEvents()
  127.     for i in range(0, speed):
  128.         for planet in planets:
  129.             planet.resetForce()
  130.             for otherplanet in planets:
  131.                 planet.applyForce(getGravity(planet, otherplanet))
  132.             planet.update()
  133.    
  134.     pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(0, 0, 600, 400))
  135.     for planet in planets:
  136.         planet.draw(screen, 2)
  137.  
  138.     clock.tick(30)
  139.     pygame.display.flip()
  140. print('Terminating')
Add Comment
Please, Sign In to add comment