Created
November 24, 2013 21:07
-
-
Save Sulter/7632402 to your computer and use it in GitHub Desktop.
micro "physics" simulation? Just testing pygame.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import sys | |
import math | |
import random | |
#vector stuff http://stackoverflow.com/a/4114962 | |
def magnitude(v): | |
return math.sqrt(sum(v[i]*v[i] for i in range(len(v)))) | |
def add(u, v): | |
return [ u[i]+v[i] for i in range(len(u)) ] | |
def normalize(v): | |
vmag = magnitude(v) | |
return [ v[i]/vmag for i in range(len(v)) ] | |
class star(): | |
def __init__(self, x,y,size): | |
self.x=x | |
self.y=y | |
self.x_center=x+size/2 | |
self.y_center=y+size/2 | |
self.size = size | |
self.mass = size*size*math.pi | |
self.surface = pygame.Surface((size, size)) | |
self.v_x = 0 | |
self.v_y = 0 | |
pygame.draw.circle(self.surface, (75,2,10), (size/2, size/2), size/2) | |
def update(self): | |
self.x=self.new_x | |
self.y=self.new_y | |
self.x_center=self.x+self.size/2 | |
self.y_center=self.y+self.size/2 | |
def draw(self, screen): | |
screen.blit(self.surface, (self.x, self.y), None, (pygame.BLEND_ADD)) | |
#physics stuff, should probably be a seperate engine | |
def physics_step(self, star_list): | |
force_list = [] | |
for star in star_list: | |
if star is not self: | |
dist = self.distance((self.x_center, self.y_center),(star.x_center, star.y_center)) | |
power = dist*dist | |
force = star.mass-(power*0.01) | |
vect = [star.x_center - self.x_center, star.y_center - self.y_center] | |
vect = normalize(vect) | |
vect[0]*=force | |
vect[1]*=force | |
force_list.append(vect) | |
final_force = [0,0] | |
for force in force_list: | |
final_force[0]+=force[0] | |
final_force[1]+=force[1] | |
#print final_force | |
scaling = 0.000005 | |
self.v_x+=final_force[0]*scaling | |
self.v_y+=final_force[1]*scaling | |
#damping | |
self.v_x*=0.99 | |
self.v_y*=0.99 | |
self.new_x=self.x+self.v_x | |
self.new_y=self.y+self.v_y | |
def distance(self, p0, p1): | |
return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2) | |
######################################################################################################################################################## | |
#main | |
DISPLAY = pygame.display.set_mode((1024, 768), (pygame.DOUBLEBUF), 32) | |
FPS = 60 | |
fpsClock = pygame.time.Clock() | |
star_list = [] | |
nr_stars = 100 | |
for i in range(nr_stars): | |
new_star = star(random.randint(0,1024),random.randint(0,728), random.randint(10,75)) | |
star_list.append(new_star) | |
while True: # the main game loop | |
#events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
sys.exit() | |
#logic | |
for star in star_list: | |
star.physics_step(star_list) | |
for star in star_list: | |
star.update() | |
#drawing | |
for star in star_list: | |
star.draw(DISPLAY) | |
fpsClock.tick(FPS) | |
pygame.display.update() | |
DISPLAY.fill((0,0,0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment