-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravedad_mouse.py
More file actions
131 lines (104 loc) · 3.54 KB
/
gravedad_mouse.py
File metadata and controls
131 lines (104 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python2
##Here is a better example, an yes, the variable names suck. thats me\.::
import pygame
import math
import sys
import random
from pygame.time import Clock
from pygame import Color, mouse
pygame.init()
circles = set()
tspeed = 1
speed = tspeed
count = 0
n_launchers = 10
def random_color(count):
color = Color("white")
color.hsva = (count%360, 90, 80, 60)
return color
class World(object):
def __init__(self, x, y, x_accel=0, y_accel=0):
self.x = x
self.y = y
self.x_accel = x_accel
self.y_accel = y_accel
self.center_x = self.x / 2
self.center_y = self.y / 2
def update(self):
self._calculate_gravity()
def _calculate_gravity(self):
x_mouse, y_mouse = pygame.mouse.get_pos()
self.x_accel = (x_mouse - self.center_x) / float(self.x)
self.y_accel = (y_mouse - self.center_y) / float(self.y)
class Circle(object):
def __init__(self, size, world, x=0, y=0, angle=0, speed=1, color = (0,0,0)):
self.world = world
self.color = color
self.size = size/2
self.speed_x = math.sin(angle) * speed
self.speed_y = math.cos(angle) * speed
self.accel_x = 0
self.accel_y = 1
self.x = x
self.y = y #posicion inicial
def update(self, screen):
global circles
x_size, y_size = screen.get_size()
if not ( (0 < self.x and self.x < x_size) and (0 < self.y and self.y < y_size)):
circles.remove(self)
self.speed_x += self.world.x_accel
self.speed_y += self.world.y_accel
self.x += self.speed_x
self.y += self.speed_y
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), int(self.size) )
def __repr__(self):
return "<Circle (%d, %d) %d>" % (self.x, self.y, self.angle)
class Launcher(object):
def __init__(self, x, y, world, queue):
self.x = x
self.y = y
self.world = world
self.queue = queue
self.cicles = 0
self.rot_speed = 0
self.mov_speed = 0
self.q = 0
self.rotation = 0
self.counter = random.randint(0, 0xFFFFFF)
self.color_change_speed = random.randint(1,3)
def explode(self):
for n in xrange(self.q):
angle = ((n*360.0/self.q) + self.rotation) % 360
color = random_color(self.counter)
self.counter += self.color_change_speed
self.queue.add(Circle(4, self.world, x=self.x, y=self.y, angle=angle, speed=self.mov_speed, color=color))
def change(self):
if not self.cicles:
self.rot_speed = random.uniform(-1, 1)
self.mov_speed = random.uniform(1, 10)
self.cicles = random.randint(500, 1000)
self.q = 1 # random.randint(1, 6)
self.rotation += (self.rot_speed % 360)
self.cicles -= 1
x , y = 1024, 768
screen = pygame.display.set_mode((x, y))
clock = Clock()
world = World(x, y, 0, 0.1)
launchers = []
for n in xrange(1, n_launchers + 1 ):
launchers.append( Launcher(n * x/ (n_launchers+1), y/2, world, circles ))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
screen.fill((0,0,0))
circle_list = list(circles)
world.update()
for c in circle_list:
c.update(screen)
for launcher in launchers:
launcher.change()
if len(circles) < 5000:
for launcher in launchers:
launcher.explode()
pygame.display.update()
clock.tick(50)