mixster

mixster

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