Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
running = True
### Physics stuff
space = pm.Space()
space.gravity = Vec2d(0.0, -900.0)
draw_options = pymunk.pygame_util.DrawOptions(screen)
## Balls
balls = []
### walls
static_lines = [pm.Segment(space.static_body, Vec2d(111.0, 280.0), Vec2d(407.0, 246.0), 1.0)
,pm.Segment(space.static_body, Vec2d(407.0, 246.0), Vec2d(407.0, 343.0), 1.0)
]
space.add(static_lines)
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
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "point_query.png")
ticks_to_next_ball -= 1
def loop(self):
for event in pygame.event.get():
if event.type == QUIT:
self.running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
self.running = False
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(self.screen, "playground.png")
elif event.type == MOUSEBUTTONDOWN and event.button == 1: # LMB
if pygame.key.get_mods() & KMOD_SHIFT:
p = self.flipyv(Vec2d(event.pos))
self.polys.append(self.create_box(pos = p))
else:
#t = -10000
p = self.flipyv(Vec2d(event.pos))
self.balls.append(self.create_ball(p))
elif event.type == MOUSEBUTTONDOWN and event.button == 3: #RMB
if pygame.key.get_mods() & KMOD_SHIFT:
pass
elif pygame.key.get_mods() & KMOD_CTRL:
p = self.flipyv(Vec2d(event.pos))
self.wall_points.append(p)
elif self.shape_to_remove is not None:
self.balls = list(filter(lambda a: a != self.shape_to_remove, self.balls))
self.walls = list(filter(lambda a: a != self.shape_to_remove, self.walls))
self.polys = list(filter(lambda a: a != self.shape_to_remove, self.polys))
self.space.remove(self.shape_to_remove.body, self.shape_to_remove)
def create_wall_segments(self, points):
"""Create a number of wall segments connecting the points"""
if len(points) < 2:
return []
points = map(Vec2d, points)
for i in range(len(points)-1):
v1 = Vec2d(points[i].x, points[i].y)
v2 = Vec2d(points[i+1].x, points[i+1].y)
wall_body = pm.Body()
wall_shape = pm.Segment(wall_body, v1, v2, .0)
wall_shape.friction = 1.0
wall_shape.collision_type = COLLTYPE_DEFAULT
self.space.add(wall_shape)
self.walls.append(wall_shape)
def on_update(self, delta_time):
start_time = timeit.default_timer()
self.ticks_to_next_ball -= 1
if self.ticks_to_next_ball <= 0:
self.ticks_to_next_ball = 20
mass = 0.5
radius = 15
inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
body = pymunk.Body(mass, inertia)
x = random.randint(0, SCREEN_WIDTH)
y = SCREEN_HEIGHT
body.position = x, y
shape = pymunk.Circle(body, radius, pymunk.Vec2d(0, 0))
shape.friction = 0.3
self.space.add(body, shape)
sprite = CircleSprite(":resources:images/items/coinGold.png", shape)
self.ball_list.append(sprite)
# Check for balls that fall off the screen
for ball in self.ball_list:
if ball.pymunk_shape.body.position.y < 0:
# Remove balls from physics space
self.space.remove(ball.pymunk_shape, ball.pymunk_shape.body)
# Remove balls from physics list
ball.remove_from_sprite_lists()
# Update physics
# Use a constant time step, don't use delta_time
def make_circle(self, x, y):
size = 20
mass = 12.0
moment = pymunk.moment_for_circle(mass, 0, size, (0, 0))
body = pymunk.Body(mass, moment)
body.position = pymunk.Vec2d(x, y)
shape = pymunk.Circle(body, size, pymunk.Vec2d(0, 0))
shape.friction = 0.3
self.space.add(body, shape)
sprite = CircleSprite(shape, ":resources:images/items/coinGold.png")
self.sprite_list.append(sprite)
self.lose()
else:
self.win()
self.equipped_gun = data['equipped_gun']
# claw
self.claw_in_motion = data['claw_in_motion']
self.sprite_claw.visible = data['claw_visible']
in_claw_pins = data['claw_pins']
self.claw_attached = data['claw_attached']
if self.claw_in_motion:
in_body = data['claw']['body']
# create claw
body = pymunk.Body(mass=5, moment=1000000)
body.position = Vec2d(in_body['position'])
body.angle = in_body['angle']
body.velocity = Vec2d(in_body['velocity'])
self.claw = pymunk.Circle(body, self.claw_radius)
self.claw.friction = 1
self.claw.elasticity = 0
self.claw.collision_type = Collision.Claw
self.claw_joint = pymunk.SlideJoint(self.claw.body, self.man.body, Vec2d(0, 0), Vec2d(0, 0), 0, data['claw_joint']['max'])
self.claw_joint.max_bias = max_bias
self.space.add(body, self.claw, self.claw_joint)
if in_claw_pins is None:
self.claw_pins = None
else:
self.claw_pins = []
for in_joint in in_claw_pins:
joint = pymunk.PinJoint(self.claw.body, self.ceiling.body, Vec2d(0, 0), Vec2d(0, 0))
joint.max_bias = max_bias
return v
pre_v = self.sphere.data['pre_velocity']
v = self.sphere.body.velocity
change = (v - pre_v).normalized()
position = self.sphere.body.position
target = self.sphere.target
attraction = target_force(position, target, repulse=False)
chaser_repulsion = target_force(position, self.chaser.body.position)
obstacle_repulsion = sum([target_force(position, obs.data['position']) for obs in self.obstacles])
# avoid walls
wall_repulsion = 50.0 * (Vec2d(1, 0) / position[0] + Vec2d(-1, 0) / (self.width - position[0]) + Vec2d(0, 1) / position[1] + Vec2d(0, -1) / (self.height - position[1]))
# the base weights will be adjusted with aggressiveness
base_weights = {
'target': 1.0,
'chaser': 8.0,
'obstacle': 9.0,
'wall': 5.0,
}
w_wall = base_weights['wall']
w_obstacle = base_weights['obstacle']
bw_target = base_weights['target']
bw_chaser = base_weights['chaser']
# the aggressiveness represents a trade off between
# avoiding the chaser and approaching the target
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
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "point_query.png")
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 100
mass = 10
radius = 25
inertia = pm.moment_for_circle(mass, 0, radius, Vec2d(0,0))
body = pm.Body(mass, inertia)
x = random.randint(115,350)
body.position = x, 400
shape = pm.Circle(body, radius, Vec2d(0,0))
shape.color = THECOLORS["lightgrey"]
space.add(body, shape)
balls.append(shape)
### Clear screen
screen.fill(THECOLORS["white"])
### Draw stuff
pymunk.pygame_util.draw(screen, space)
balls_to_remove = []
for ball in balls:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "point_query.png")
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 100
mass = 10
radius = 25
inertia = pm.moment_for_circle(mass, 0, radius, Vec2d(0,0))
body = pm.Body(mass, inertia)
x = random.randint(115,350)
body.position = x, 400
shape = pm.Circle(body, radius, Vec2d(0,0))
shape.color = THECOLORS["lightgrey"]
space.add(body, shape)
balls.append(shape)
### Clear screen
screen.fill(THECOLORS["white"])
### Draw stuff
space.debug_draw(draw_options)
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 200: balls_to_remove.append(ball)
for ball in balls_to_remove:
space.remove(ball, ball.body)