โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ gameObj API Contents โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ๐ฆ gameObj โโ ๐ฅ Import โโ ๐ Overview โโ ๐งฑ Creating a game object โโ ๐ Accepted sprite sources โโ โ๏ธ Core methods โโ ๐๏ธ Animation helpers โโ ๐ฅ Collision helpers โโ ๐ Movement helpers โโ ๐ Clamp helpers โโ ๐ Useful properties โโ ๐งต Sprite buffer access โโ ๐ Sprite streaming helpers โโ โถ๏ธ Example โโ ๐ Notes
๐ฆ API Reference: gameObj
The gameObj class is a sprite-backed object for gameplay code.
It combines:
- sprite-based drawing
- simple animation playback
- position, velocity, and acceleration
- rectangle collision helpers
- clamp helpers
- basic move-towards helpers
- raw sprite buffer access
- TileBus-oriented sprite streaming helpers
๐ฅ Import
from retroPy import gameObj
๐ Overview
gameObj is a lightweight engine object for moving, drawing, and animating sprite-backed entities.
For most projects it is useful for:
- players and enemies
- simple physics-style movement
- sprite animation playback
- rectangle-based collision checks
๐งฑ Creating a game object
The constructor is keyword-driven in the C implementation.
Preferred constructor style:
go = gameObj(
buffer=sprite("player.rs8"),
pos_x=40,
pos_y=20,
change=0,
speed_x=0,
speed_y=0,
animation_mode=0,
camera_relative=0,
frame=0,
flip=0,
)
Compatibility aliases still supported:
mode-> alias ofanimation_modecam_mode-> alias ofcamera_relativecurrNdx-> alias offrame
๐ Accepted sprite sources
The constructor can load its sprite source from:
- a file path string
- a bytes / bytearray object
- a
sprite(...)object - a file-like object with
.read()
For most retroPy projects, passing a sprite(...) object or file path is the clearest option.
โ๏ธ Core methods
gameObj.draw()
Draws the current frame.
The draw path applies world_to_screen(...), so the objectโs position is camera-relative when camera mode is enabled.
gameObj.update()
Advances the objectโs position using its current speed and acceleration.
This is a lightweight built-in motion step. Many games will still wrap it with their own state logic.
gameObj.pos(x, y)
Sets position directly.
go.pos(80, 32)
gameObj.speed(vx, vy)
Sets velocity.
go.speed(40, 0)
gameObj.acc(ax, ay)
Sets acceleration.
go.acc(0, 120)
gameObj.limit_speed(max_speed=None)
Clamps velocity to a maximum.
If a value is passed, that value is used. Otherwise the objectโs speed_max property is used.
๐๏ธ Animation helpers
gameObj.frame()
gameObj.frame(index)
gameObj.frame(index, flip)
Gets or sets the current frame index, with optional flip update.
Compatibility alias:
gameObj.currNdx(...)
gameObj.flip()
gameObj.flip(value)
Gets or sets flip mode.
Current flip values follow the sprite draw path:
0-> normal1-> horizontal flip2-> vertical flip3-> both
gameObj.animation_mode()
gameObj.animation_mode(value)
Gets or sets animation mode.
In the current implementation, animation_mode > 0 behaves like a one-shot range that stops after the end frame, while animation_mode == 0 loops back to the start frame.
Compatibility alias:
gameObj.mode(...)
gameObj.sprite(source, change_ms)
Replaces the attached sprite and animation timing.
source can be raw sprite bytes or a sprite(...) object.
gameObj.frame_duration_ms()
gameObj.frame_duration_ms(ms)
Gets or sets the frame-change duration in milliseconds.
Compatibility alias:
gameObj.flipDuration(...)
gameObj.frame_range(start, end)
Sets the active animation range.
go.frame_range(0, 3)
Compatibility alias:
gameObj.ndxFromTo(...)
๐ฅ Collision helpers
gameObj.dist(other)
gameObj.dist(x, y)
Returns distance from this object to another object or point.
gameObj.collider(other)
Simple rectangle overlap test using each objectโs collider box.
Returns 1 for overlap, otherwise 0.
gameObj.collider_xy(other, dx, dy)
Tests collision after applying an offset to this objectโs collider.
This is useful for โwould I collide if I moved?โ checks.
gameObj.colliderPt(x, y)
Returns whether a point falls inside the objectโs collider box.
gameObj.colliderPtEx(x, y)
Point collision helper that also reports hit side information.
gameObj.colliderEx(other)
Extended rectangle collision helper that returns a directional hit code instead of only true/false.
gameObj.resizeCollider(cx, cy, cw, ch)
Changes the collider rectangle relative to the sprite position.
go.resizeCollider(2, 2, 12, 12)
gameObj.drawCollider(color)
Draws the collider rectangle as an outline.
Useful for debugging collisions.
๐ Movement helpers
gameObj.updateTowards(tx, ty, speed)
Moves the object toward a target point using delta time.
Returns the remaining distance.
gameObj.updateMove2(tx, ty, speed)
Another move-towards helper with similar behavior.
๐ Clamp helpers
gameObj.clamp_x(min_x, max_x)
gameObj.clamp_y(min_y, max_y)
gameObj.clamp_xy(min_x, max_x, min_y, max_y)
Keeps the object inside bounds.
go.clamp_xy(0, 239, 0, 239)
๐ Useful properties
The class exposes many direct properties through attr. The most useful ones to document for normal game code are:
Position and size
x,ypos_x,pos_ypos_ix,pos_iywidth,heightmid_x,mid_ybot_x,bot_y
Velocity and acceleration
speed_x,speed_yacc_x,acc_yspeed_max
Frame / animation state
camera_relative
Compatibility alias:
cam_mode
Other runtime animation state still follows the underlying implementation and may also be visible through direct attributes.
Collider box
cx,cy,cw,ch
Misc
idtypeval
๐งต Sprite buffer access
gameObj.get_buffer()
Returns a bytearray by reference to the underlying sprite buffer.
Because this is by reference, editing the returned data affects the object directly.
๐ Sprite streaming helpers
The current implementation also includes TileBus-oriented sprite handoff helpers such as spawning or reconstructing objects from streamed sprite buffers.
These are more advanced engine helpers and are best treated as specialized APIs unless you are working on multi-tile object migration.
Example
from retroPy import *
player_sprite = sprite("hero.rs8")
player = gameObj(
buffer=player_sprite,
pos_x=20,
pos_y=20,
change=120,
speed_x=0,
speed_y=0,
animation_mode=0,
camera_relative=1,
frame=0,
flip=0,
)
player.frame_range(0, 3)
player.frame_duration_ms(120)
def Update():
if kb.left_down():
player.speed(-60, 0)
elif kb.right_down():
player.speed(60, 0)
if kb.A_down():
loop.exit()
player.update()
player.clamp_xy(0, 239 - player.width, 0, 239 - player.height)
def Draw():
draw.clear(0)
player.draw()
loop = gameloop(Update, Draw)
loop.run()
Notes
gameObjis a lightweight engine object, not a full entity framework.- The draw path is camera-aware when camera mode is enabled.
- The animation helpers are frame-based and tied to the sprite data attached to the object.
- The current implementation also contains lower-level or specialized helpers related to sprite ownership and multi-tile streaming; those are intentionally not the focus of the normal public API.