-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslide1.py
More file actions
executable file
·117 lines (98 loc) · 3.57 KB
/
slide1.py
File metadata and controls
executable file
·117 lines (98 loc) · 3.57 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
import sys
import random
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk
import math
def to_pygame(p):
return int(p.x), int(-p.y+600)
def add_ball(space):
mass = 1
radius = 14
inertia = pymunk.moment_for_circle(mass, 0, radius)
body = pymunk.Body(mass, inertia)
x = random.randint(120, 380)
body.position = x, 550
shape = pymunk.Circle(body, radius)
space.add(body, shape)
return shape
def draw_ball(screen, ball):
p = int(ball.body.position.x), 600 - int(ball.body.position.y)
pygame.draw.circle(screen, THECOLORS["tomato2"], p, int(ball.radius), 0)
def add_L(space):
rotation_center_body = pymunk.Body()
rotation_center_body.position = (300, 300)
rotation_limit_body = pymunk.Body()
rotation_limit_body.position = (200, 300)
body = pymunk.Body(10, 10000)
body.position = (300, 300)
l1 = pymunk.Segment(body, (-150, 0), (255.0, 0.0), 5.0)
l2 = pymunk.Segment(body, (-150.0, 0), (-150.0, 50.0), 5.0)
rotation_center_joint = pymunk.PinJoint(body,
rotation_center_body,
(0, 0),
(0, 0))
joint_limit = 25
rotation_limit_joint = pymunk.SlideJoint(body,
rotation_limit_body,
(-100, 0),
(0, 0),
0,
joint_limit)
space.add(l1, l2, body, rotation_center_joint, rotation_limit_joint)
return l1, l2
def draw_lines(screen, lines):
for line in lines:
body = line.body
pv1 = body.position + line.a.rotated(body.angle)
pv2 = body.position + line.b.rotated(body.angle)
p1 = to_pygame(pv1)
p2 = to_pygame(pv2)
pygame.draw.lines(screen, THECOLORS["black"], False, [p1, p2])
def add_static_L(space):
body = pymunk.Body()
body.position = (300, 300)
l1 = pymunk.Segment(body, (-150, 0), (255.0, 0.0), 5.0)
l2 = pymunk.Segment(body, (-150.0, 0), (-150.0, 50.0), 5.0)
space.add_static(l1, l2)
return l1, l2
def main():
pygame.init()
screen = pygame.display.set_mode((600, 600), pygame.RESIZABLE)
pygame.display.set_caption("Nanho Game - Pyweek May 2012")
clock = pygame.time.Clock()
running = True
space = pymunk.Space()
space.gravity = (0.0, -900.0)
lines = add_L(space)
balls = []
ticks_to_next_ball = 10
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 25
ball_shape = add_ball(space)
balls.append(ball_shape)
screen.fill(THECOLORS["lightblue"])
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 150:
balls_to_remove.append(ball)
draw_ball(screen, ball)
for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)
draw_lines(screen, lines)
pygame.draw.circle(screen, THECOLORS["red"], (300, 300), 5)
pygame.draw.circle(screen, THECOLORS["green"], (200, 300), 25, 2)
space.step(1/50.0)
pygame.display.flip()
clock.tick(50)
if __name__ == '__main__':
sys.exit(main())