mixster

mixster

Apr 7th, 2010
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 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.         tmp = self.pos
  22.         self.pos.x += dx
  23.         self.pos.y += dy
  24.         pygame.draw.line(back, self.color, (tmp.x, tmp.y), (self.pos.x, self.pos.y))
  25.  
  26.     def resetForce(self):
  27.         self.acc = Point(0, 0)
  28.  
  29.     def applyForce(self, force):
  30.         self.acc.x += force.x / self.mass
  31.         self.acc.y += force.y / self.mass
  32.  
  33.     def update(self):
  34.         self.vel.x += self.acc.x
  35.         self.vel.y += self.acc.y
  36.         self.move(self.vel.x, self.vel.y)
  37.  
  38.     def draw(self, screen, width=1):
  39.         pygame.draw.circle(screen, self.color, (int(self.pos.x), int(self.pos.y)), self.radius, width)
  40.  
  41. running = True
  42. speed = 1
  43.  
  44. def getEvents():
  45.     for event in pygame.event.get():
  46.         if event.type == KEYDOWN:
  47.             global speed
  48.             if event.key == K_UP:
  49.                 if speed < 64:
  50.                     speed *= 2
  51.                 else:
  52.                     print('There''s a cap at x64!')
  53.             if event.key == K_DOWN:
  54.                 if speed > 2:
  55.                     speed /= 2
  56.                 else:
  57.                     print('No pausing or going backwards!')
  58.             if event.key == ord('q'):
  59.                 running = False
  60.                 sys.exit()
  61.         if event.type == pygame.QUIT:
  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)), 5, randomCol())
  100.  
  101. pygame.init()
  102. screen = pygame.display.set_mode((600, 400))
  103. back = pygame.Surface((600, 400))
  104. back.fill((0, 0, 0))
  105. pygame.display.set_caption('Gravity simulator')
  106. pygame.mouse.set_visible(0)
  107.  
  108. clock = pygame.time.Clock()
  109. random.seed()
  110.  
  111. planets = []
  112. planets.append(Body(10**11, Point(300, 200), Point(0, 0), 2, (0, 0, 255)))
  113.  
  114. for i in range(1, 20):
  115.     x, y = 0, 0
  116.     if random.randint(0, 1) == 0:
  117.         x = random.randint(25, 200)    
  118.     else:
  119.         y = random.randint(25, 200)
  120.  
  121.     if random.randint(0, 1) == 0:
  122.         x = -x
  123.         y = -y
  124.  
  125.     planets.append(getDefaultBody(Point(300 + x, 200 + y)))
  126.  
  127. while running:
  128.     getEvents()
  129.     for i in range(0, speed):
  130.         for planet in planets:
  131.             planet.resetForce()
  132.             for otherplanet in planets:
  133.                 planet.applyForce(getGravity(planet, otherplanet))
  134.             planet.update()
  135.    
  136.     #screen.fill((0, 0, 0))
  137.     screen.blit(back, (0, 0))
  138.     #pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(0, 0, 600, 400))
  139.     for planet in planets:
  140.         planet.draw(screen, 2)
  141.  
  142.     clock.tick(30)
  143.     pygame.display.flip()
  144. print('Terminating')
Add Comment
Please, Sign In to add comment