gravity-spiral: f06eec1ce2331c9569acd9794461a1da3617789c

     1: #!/usr/bin/env python
     2: import pygame
     3: from pygame.locals import *
     4: import random, math, sys
     5: 
     6: pygame.init()
     7: 
     8: while True:
     9: 	ParticleNumber = raw_input("Enter how many particles to draw: ")
    10: 	try:
    11: 		ParticleNumber = int(ParticleNumber)
    12: 		break
    13: 	except:
    14: 		print "\nWell, now.  Let's try that again."
    15: 
    16: Surface = pygame.display.set_mode((800,600))
    17: 
    18: Particles = []
    19: class Particle:
    20: 	def __init__(self):
    21: 		self.x = random.randint(200,600)
    22: 		self.y = random.randint(200,400)
    23: 		self.speedx = 0.0
    24: 		self.speedy = 0.0
    25: 		self.mass = 1
    26: 		self.radius = math.sqrt(self.mass)
    27: for x in range(ParticleNumber):
    28: 	Particles.append(Particle())
    29: 	Particles[-1].colour = [255, 0, 0]
    30: 	Particles[-1].mode = 'red'
    31: def Move():
    32: 	for P in Particles:
    33: 		for P2 in Particles:
    34: 			if P != P2:
    35: 				XDiff = P.x - P2.x
    36: 				YDiff = P.y - P2.y
    37: 				Distance = math.sqrt((XDiff**2)+(YDiff**2))
    38: 				if Distance < 10: Distance = 10
    39: 				#F = (G*M*M)/(R**2)
    40: 				Force = 0.125*(P.mass*P2.mass)/(Distance**2)
    41: 				#F = M*A  ->  A = F/M
    42: 				Acceleration = Force / P.mass
    43: 				XComponent = XDiff/Distance
    44: 				YComponent = YDiff/Distance
    45: 				P.speedx -= Acceleration * XComponent
    46: 				P.speedy -= Acceleration * YComponent
    47: 	for P in Particles:
    48: 		P.x += P.speedx
    49: 		P.y += P.speedy
    50: def CollisionDetect():
    51: 	for P in Particles:
    52: 		if P.x > 800-P.radius:   P.x = 800-P.radius;  P.speedx *= -1
    53: 		if P.x < 0+P.radius:	 P.x = 0+P.radius;	P.speedx *= -1
    54: 		if P.y > 600-P.radius:   P.y = 600-P.radius;  P.speedy *= -1
    55: 		if P.y < 0+P.radius:	 P.y = 0+P.radius;	P.speedy *= -1
    56: 		for P2 in Particles:
    57: 			if P != P2:
    58: 				Distance = math.sqrt(  ((P.x-P2.x)**2)  +  ((P.y-P2.y)**2)  )
    59: 				if Distance < (P.radius+P2.radius):
    60: 					P.speedx = ((P.mass*P.speedx)+(P2.mass*P2.speedx))/(P.mass+P2.mass)
    61: 					P.speedy = ((P.mass*P.speedy)+(P2.mass*P2.speedy))/(P.mass+P2.mass)
    62: 					P.x = ((P.mass*P.x)+(P2.mass*P2.x))/(P.mass+P2.mass)
    63: 					P.y = ((P.mass*P.y)+(P2.mass*P2.y))/(P.mass+P2.mass)
    64: 					P.mass += P2.mass
    65: 					P.radius = math.sqrt(P.mass)
    66: 					Particles.remove(P2)
    67: def Draw():
    68: 	##Surface.fill((25,0,0))
    69: 	for P in Particles:
    70: 		pygame.draw.circle(Surface, P.colour, (int(P.x),int(600-P.y)), int(round(P.radius)))
    71: 		if P.mode == 'red':
    72: 			if P.colour[1] < 255:
    73: 				P.colour[1] += 1
    74: 			else:
    75: 				P.mode = 'yellow'
    76: 		elif P.mode == 'yellow':
    77: 			if P.colour[0] > 0:
    78: 				P.colour[0] -= 1
    79: 			else:
    80: 				P.mode = 'green'
    81: 		elif P.mode == 'green':
    82: 			if P.colour[2] < 255:
    83: 				P.colour[2] += 1
    84: 			else:
    85: 				P.mode = 'turquoise'
    86: 		elif P.mode == 'turquoise':
    87: 			if P.colour[1] > 0:
    88: 				P.colour[1] -= 1
    89: 			else:
    90: 				P.mode = 'blue'
    91: 		elif P.mode == 'blue':
    92: 			if P.colour[0] < 255:
    93: 				P.colour[0] += 1
    94: 			else:
    95: 				P.mode = 'magenta'
    96: 		elif P.mode == 'magenta':
    97: 			if P.colour[2] > 0:
    98: 				P.colour[2] -= 1
    99: 			else:
   100: 				P.mode = 'red'
   101: 		##Surface.set_at((int(P.x),int(600-P.y)),(255,255,255))
   102: 	pygame.display.flip()
   103: def GetInput():
   104: 	keystate = pygame.key.get_pressed()
   105: 	for event in pygame.event.get():
   106: 		if event.type == QUIT or keystate[K_ESCAPE]:
   107: 			pygame.quit(); sys.exit()
   108: def main():
   109: 	count = 0
   110: 	while True:
   111: 		GetInput()
   112: 		Move()
   113: 		CollisionDetect()
   114: 		if count > 5:
   115: 			Draw()
   116: 			count = 0
   117: 		count += 1
   118: if __name__ == '__main__': main()

Generated by git2html.