-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
96 lines (68 loc) · 1.74 KB
/
template.py
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
#!/usr/bin/env python2
import pygame
import sys
##############################
# CLASSES
##############################
class Particle(object):
def __init__(self, pos, vel=Vector2D(0,0)):
self.pos = pos
self.vel = vel
self.acc = Vector2D(0, 0)
self.color = COLOR_WHITE
self.r = 2
def apply_force(self, force):
self.vel.add(force)
def draw(self):
p = map(int, self.pos.as_tuple())
pygame.draw.circle(screen, self.color, p, self.r, 0)
def update(self):
self.apply_force(gravity)
self.pos.add(self.vel)
def alive(self):
return self.pos.x < 0 or self.pos.x > width or self.pos.y < 0 or self.pos.y > height
##############################
# GLOBALS
##############################
COLOR_BLACK = (0,0,0)
COLOR_WHITE = (255,255,255)
width = 800
height = 600
screen = None
clock = None
elapsed = 0
gravity = Vector2D(0, 0.2)
objects = list()
##############################
# FUNCTIONS
##############################
def setup():
pygame.init()
global clock
clock = pygame.time.Clock()
global screen
screen = pygame.display.set_mode((width, height))
def loop():
global objects
global elapsed
keep_running = True
elapsed = clock.tick(60)
screen.fill(COLOR_BLACK)
for o in objects:
o.update()
o.draw()
# remove deleteable particles
objects = filter(lambda x: not x.alive(), objects)
print len(objects)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_running = False
return keep_running
def main():
setup()
while(loop()):
pass
print "exiting cleanly"
if __name__ == "__main__":
main()