Entity

class pypunk.core.Entity
Main game Entity class, updated by World.
Parameters:
  • x – X position to place the Entity.
  • y – Y position to place the Entity.
  • graphic – Graphic to assign to the Entity.
  • mask – Mask to assign to the Entity.

Note

Make sure to call super().__init__() if overriding to ensure World instance is set up correctly.

visible = True

If the Entity should render.

collideable = True

If the Entity should respond to collision checks.

x
y

X/Y position of the Entity in the World

width
height

Width/Height of the Entity’s hitbox

origin_x
origin_y

X/Y origin of the Entity’s hitbox

render_target = None

The Screen object to draw the entity onto. Leave as None to render to the primary window.

added()
removed()

Override these, called when the Entity is added/removed to/from a world.

update()

Override this, called every frame by the current World as part of the main game loop.

render()

Renders the Entity’s Graphic. If you override this to implement additional behaviour, remember to call super().render() to ensure the Entity is drawn.

collide(t, x, y)

Checks for a collision between the Entity, positioned at (x, y), and an Entity of type t. Returns the first Entity collided with, or None if there was no collision.

collide_types(types, x, y)

Same as collide(), but checks against a list of Entity types types.

collide_with(e, x, y)

Same as collide(), but checks against a single Entity instance, e.

collide_rect(x, y, r_x, r_y, r_width, r_height)

Returns whether the Entity, positioned at (x, y), overlaps the specified rectangle at (r_x, r_y) with dimensions r_width x r_height.

collide_point(x, y, p_x, p_y)

Returns whether this Entity, positioned at (x, y), overlaps the specified position (p_x, p_y)

Todo

  • collide_into
  • collide_types_into
  • on_camera
world

Read only. The World object this Entity has been added to.

center_x
center_y

The center x/y position of the Entity’s hitbox area.

left
right
top
bottom

The left/right/top/bottom-most position of the Entity’s hitbox.

layer

The rendering layer of this entity. Higher layers are rendered first.

type

The collision type, used for collision checks.

Todo

Mask support.

graphic

Graphic object to render to the screen during the render loop.

add_graphic(g)

Adds the Graphic g to the Entity via a Graphicslist

set_hitbox(width, height, origin_x, origin_y)

Sets the Entity’s hitbox properties.

set_hitbox_to(o)

Sets the Entity’s hitbox to math that of the provided object o.

set_origin(x=0, y=0)

Sets the origin of the Entity to (x, y).

center_origin()

Sets the Entity’s origin to (width/2, height/2).

Todo

  • distance_from
  • distance_to_point
  • distance_to_rect
  • move_by
  • move_to
  • move_towards
  • move_collide_x
  • move_collide_y
clamp_horizontal(left, right, padding=0)
clamp_vertical(top, bottom, padding=0)

Clamps the Entity’s hitbox on the x/y axis, between (left, right) /(top, bottom), with optional additional padding.

Project Versions

Previous topic

World

This Page