mixster

mixster

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