Advertisement
mixster

mixster

Sep 18th, 2010
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, pygame, os
  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**1, 10**1), pos, Point(getVelocity(pos.y, 2 * 10**11), getVelocity(pos.x, 2 * 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(-10, 0), Point(0, abs(getVelocity(40, 10**11))), 2, (0, 0, 255)))
  139. planets.append(Body(10**11, Point(10, 0), Point(0, -abs(getVelocity(40, 10**11))), 2, (0, 0, 255)))
  140.  
  141. #planets.append(Body(10**6, Point(1500, 0), Point(0, abs(getVelocity(1500, 10**11))), 2, (255, 0, 0)))
  142. #planets.append(Body(10**-30, Point(1490, 0), Point(0, abs(getVelocity(1500, 10**11)) + abs(getVelocity(10, 10**6))), 2, (0, 255, 0)))
  143.  
  144. for i in range(1, 20):
  145.     x, y = 0, 0
  146.     if random.randint(0, 1) == 0:
  147.         x = random.randint(75, 150)    
  148.     else:
  149.         y = random.randint(50, 100)
  150.  
  151.     if random.randint(0, 1) == 0:
  152.         x = -x
  153.         y = -y
  154.  
  155.     planets.append(getDefaultBody(Point(x, y)))
  156.  
  157. while running:
  158.     getEvents()
  159.     pos[0] += moving.x
  160.     pos[1] += moving.y
  161.     if pos[0] < 0:
  162.         pos[0] = 0
  163.     if pos[0] > universe[0] - size[0]:
  164.         pos[0] = universe[0] - size[0]
  165.     if pos[1] < 0:
  166.         pos[1] = 0
  167.     if pos[1] > universe[1] - size[1]:
  168.         pos[1] = universe[1] - size[1]
  169.     for i in range(0, speed):
  170.         for x in range(0, len(planets)):
  171.             planets[x].resetForce()
  172.         for x in range(0, len(planets)):
  173.             for y in range(x + 1, len(planets)):
  174.                 f = getGravity(planets[x], planets[y])
  175.                 planets[x].applyForce(f)
  176.                 planets[y].applyForce(invertForce(f))
  177.         for x in range(0, len(planets)):
  178.             planets[x].update()
  179.    
  180.     screen.blit(back, (0, 0), pos)
  181.     for planet in planets:
  182.         planet.draw(screen, pos, 2)
  183.  
  184.     clock.tick(50)
  185.     pygame.display.flip()
  186. print('Terminating')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement