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
|
# physics
import pippy
import pygame
import sys
import math
from pygame.locals import *
from pippy import physics
# initialize pygame first thing
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# set up the physics world (instance of Elements)
world = physics.Elements(screen.get_size())
world.renderer.set_surface(screen)
# set up initial physics objects
world.add.ground()
world.add.ball((600, 0), 50)
world.add.rect((500, 0), 25, 300, dynamic=True, density=1.0, restitution=0.16, friction=0.5)
# add 20 more balls
balls = 0
while(balls < 20):
world.add.ball(((balls * 5) + 200, balls * 5), 50)
balls += 1
# begin physics simulation
world.run_physics = True
while pippy.pygame.next_frame() and world.run_physics:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYDOWN:
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
bodylist = world.get_bodies_at_pos(event.pos, include_static=False)
if bodylist and len(bodylist) > 0:
world.add.mouseJoint(bodylist[0], event.pos)
elif event.type == MOUSEBUTTONUP and event.button == 1:
world.add.remove_mouseJoint()
elif event.type == MOUSEMOTION and event.buttons[0]:
world.mouse_move(event.pos)
# clear display with a color
screen.fill((80, 160, 240))
# update & draw physics world
world.update()
world.draw()
# update the display
pygame.display.flip()
|