def testDebugDraw(self): s = p.Space() b1 = p.Body(1, 3) s1 = p.Circle(b1, 5) s.add(b1, s1) s.step(1) o = p.SpaceDebugDrawOptions() if sys.version_info >= (3, 0): new_out = io.StringIO() else: new_out = io.BytesIO() sys.stdout = new_out try: s.debug_draw(o) finally: sys.stdout = sys.__stdout__ if sys.version_info >= (3, 0): msg = ("draw_circle (Vec2d(0.0, 0.0), 0.0, 5.0, " "SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), " "SpaceDebugColor(r=52.0, g=152.0, b=219.0, a=255.0))\n") else: msg = ("('draw_circle', (Vec2d(0.0, 0.0), 0.0, 5.0, " "SpaceDebugColor(r=44.0, g=62.0, b=80.0, a=255.0), " "SpaceDebugColor(r=52.0, g=152.0, b=219.0, a=255.0)))\n") self.assertEqual(msg, new_out.getvalue())
def testDebugDrawZeroLengthSpring(self): if sys.version_info < (3, 0): return s = p.Space() b1 = p.Body(1, 3) c = p.DampedSpring(b1, s.static_body, (0, 0), (0, 0), 0, 10, 1) s.add(b1, c) s.step(1) o = p.SpaceDebugDrawOptions() new_out = io.StringIO() sys.stdout = new_out try: s.debug_draw(o) finally: sys.stdout = sys.__stdout__ expected = ( "draw_dot (5.0, Vec2d(0.0, 0.0), SpaceDebugColor(r=142.0, g=68.0, b=173.0, a=255.0))\n" "draw_dot (5.0, Vec2d(0.0, 0.0), SpaceDebugColor(r=142.0, g=68.0, b=173.0, a=255.0)) \n" "draw_segment (Vec2d(0.0, 0.0), Vec2d(0.0, 0.0), SpaceDebugColor(r=142.0, g=68.0, b=173.0, a=255.0))\n" "draw_segment (Vec2d(0.0, 0.0), Vec2d(0.0, 0.0), SpaceDebugColor(r=142.0, g=68.0, b=173.0, a=255.0))\n" ) actual = new_out.getvalue() try: self.assertEqual(expected, actual) except: print("\nExpected", expected) print("\nActual", actual) raise
def main(): space = pymunk.Space() fill_space(space) options = pymunk.SpaceDebugDrawOptions() space.step(1) space.step(2) space.debug_draw(options)
def draw(self): px.cls(7) self.player.draw(self.offsetX, self.offsetY) # draw ground px.rect(0, px.height + self.offsetY, px.width, 150, 0) for b in self.blocks: # if block is queried by the player, color it a different color col = 1 fill = 13 if self.player.state['nc']['emitter']: emitter = self.player.state['nc']['emitter'] if emitter['pointingAt'] and emitter[ 'pointingAt'].shape == b.shape: col = 2 fill = 15 if self.player.state['persistent']['holding'] and self.player.state[ 'persistent']['holding'].shape == b.shape: fill = False b.draw(col=col, fill=fill) for p in self.platforms: p.draw(px, self.offsetX, self.offsetY) for e in self.enemies: e.draw(self.offsetX, self.offsetY) # draw crosshair px.line(px.mouse_x - 1, px.mouse_y, px.mouse_x + 1, px.mouse_y, 1) px.line(px.mouse_x, px.mouse_y - 1, px.mouse_x, px.mouse_y + 1, 1) px.rectb(10, 5, self.player.maxHealth + 2, 6, 0) px.rect(11, 6, self.player.health, 4, 14) px.text(px.width - 70, 5, "field emitter", 10) if debug: options = pymunk.SpaceDebugDrawOptions() _screen.fill((255, 255, 255)) self.space.debug_draw(_draw_options) pygame.display.flip()
def debug_draw_options(): """ Debug draw function. Keep in mind that this has a huge impact on performance. Use it only for debugging and testing :example: space = pymunk.Space() def draw(): pyxel.cls(0) debug_draw(space) :param space: :return: """ def debug_draw_circle(pos, angle, radius, outline_color, fill_color, data=None): pyxel.circ(*pos, r=radius - 1, col=fill_color) pyxel.circb(*pos, r=radius, col=outline_color) def debug_draw_dot(size, pos, color): pyxel.pset(*pos, color) def debug_draw_segment(a, b, color): pyxel.line(*a, *b, col=color) def debug_draw_polygon(vs, radius, outline_color, fill_color): poly(*vs, col=outline_color) def _c(color): return sum([color.r, color.g, color.b]) % 15 options = pymunk.SpaceDebugDrawOptions() options.draw_circle = debug_draw_circle options.draw_dot = debug_draw_dot options.draw_segment = debug_draw_segment options._c = _c options.draw_polygon = debug_draw_polygon return options
x = 500 y = 200 boxSize = (50, 50) moment = pymunk.moment_for_box(mass, boxSize) #moment = pymunk.moment_for_box(mass, (boxSize[0], boxSize[1])) body = pymunk.Body(mass, moment) body.position = pymunk.Vec2d(x, y) shape = pymunk.Poly.create_box(body, boxSize, boxSize[0] // 2) shape.elasticity = 0.7 shape.friction = 0.1 space.add(body, shape) print('body.position = ', [(int(a[0]), int(a[1])) for a in shape.get_vertices()]) #space.add(shape) # Add both body and shape to the simulation print_options = pymunk.SpaceDebugDrawOptions() # For easy printing def draw(gameDisplay, color, pos, size): #pygame.draw.rect(gameDisplay, color, [pos[0], pos[1], size[0], size[1]] ) pygame.draw.polygon(gameDisplay, color, [[int(a[0]), int(a[1])] for a in shape.get_vertices()]) pygame.draw.line(gameDisplay, color, [line[0], line[1]], [line[2], line[3]]) def gameLoop(): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() gameDisplay.fill(gray) gameExit = False
def update_game(self): self.world.update() print_options = pymunk.SpaceDebugDrawOptions() self.space.step(0.02)