-
Notifications
You must be signed in to change notification settings - Fork 0
/
18092022.py
70 lines (57 loc) · 1.55 KB
/
18092022.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
import pygame
import pymunk
import pymunk.pygame_util
import math
'''
https://www.youtube.com/watch?v=tLsi2DeUsak
'''
pygame.init()
wid, hei = 800, 700
window = pygame.display.set_mode((wid, hei))
def draw(space, window, draw_options):
window.fill("white")
space.debug_draw(draw_options)
pygame.display.update()
def wall(space, wid, hei):
rects = [
[(wid/2, hei - 10), (wid, 20)],
[(wid/2, 10), (wid, 20)]
]
for pos, size in rects:
body = pymunk.Body(body_type=pymunk.Body.STATIC)
body.position = pos
shape = pymunk.Poly.create_box(body, size)
shape.elasticity = 0.4
shape.friction = 0.5
space.add(body, shape)
def create_ball(space, radius, mass):
body = pymunk.Body()
body.position = (300, 300)
shape = pymunk.Circle(body, radius)
shape.mass = mass
shape.color = (55, 0, 0, 100)
space.add(body, shape)
shape.elasticity = 0.9
shape.friction = 0.4
return shape
def run(window, wid, hei):
run = True
clock = pygame.time.Clock()
fps = 60
dt = 1 / fps
space = pymunk.Space()
space.gravity = (0, 981)
ball = create_ball(space, 30, 10)
wall(space, wid, hei)
draw_options = pymunk.pygame_util.DrawOptions(window)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
draw(space, window, draw_options)
space.step(dt)
clock.tick(fps)
pygame.quit()
if __name__ == "__main__":
run(window, wid, hei)