The mcrfpy
module is your interface to the McRogueFace engine. Import it to access all engine functionality:
import mcrfpy
createScene(name: str) -> None
Creates a new empty scene.
mcrfpy.createScene("game")
setScene(name: str) -> None
Switches to a different scene. The scene must exist.
mcrfpy.setScene("game")
currentScene() -> str
Returns the name of the currently active scene.
scene_name = mcrfpy.currentScene()
sceneUI(name: str) -> UICollection
Returns the UICollection containing all UI elements in the specified scene.
ui = mcrfpy.sceneUI("game")
createSoundBuffer(filename: str) -> int
Loads a sound file and returns its buffer index.
explosion_sound = mcrfpy.createSoundBuffer("assets/explosion.wav")
playSound(buffer_index: int) -> None
Plays a sound effect by its buffer index.
mcrfpy.playSound(explosion_sound)
loadMusic(filename: str) -> None
Loads and plays background music. Music loops by default.
mcrfpy.loadMusic("assets/dungeon_theme.ogg")
setMusicVolume(volume: int) -> None
Sets music volume (0-100).
mcrfpy.setMusicVolume(75)
setSoundVolume(volume: int) -> None
Sets sound effects volume (0-100).
mcrfpy.setSoundVolume(80)
keypressScene(callback: callable) -> None
Sets a function to handle keyboard input for the current scene.
def on_key(key_code):
print(f"Key pressed: {key_code}")
mcrfpy.keypressScene(on_key)
registerPyAction(action: str, callback: callable) -> None
Registers a named action that can be triggered by input.
def jump():
player.pos = (player.pos[0], player.pos[1] - 2)
mcrfpy.registerPyAction("jump", jump)
registerInputAction(input_code: int, action: str) -> None
Maps an input code to a named action.
mcrfpy.registerInputAction(32, "jump") # Spacebar triggers jump
setTimer(name: str, callback: callable, interval_ms: int) -> None
Creates a timer that calls a function repeatedly.
def spawn_enemy():
# Spawn logic here
pass
mcrfpy.setTimer("enemy_spawner", spawn_enemy, 5000) # Every 5 seconds
delTimer(name: str) -> None
Removes a timer.
mcrfpy.delTimer("enemy_spawner")
exit() -> None
Closes the game window and exits.
mcrfpy.exit()
setScale(multiplier: float) -> None
Resizes the game window. Multiplier must be between 0.2 and 4.0.
mcrfpy.setScale(2.0) # Double size
RGBA color representation.
# From separate values
color = mcrfpy.Color(255, 128, 0) # Orange
color = mcrfpy.Color(255, 128, 0, 200) # Semi-transparent orange
# From tuple
color = mcrfpy.Color((255, 128, 0))
color = mcrfpy.Color((255, 128, 0, 200))
r
(int): Red component (0-255)g
(int): Green component (0-255)b
(int): Blue component (0-255)a
(int): Alpha component (0-255, default 255)2D vector for positions and sizes.
# From separate values
vec = mcrfpy.Vector(100, 200)
# From tuple
vec = mcrfpy.Vector((100, 200))
# Default (0, 0)
vec = mcrfpy.Vector()
x
(float): X componenty
(float): Y componentFont for text rendering.
font = mcrfpy.Font("assets/custom_font.ttf")
Sprite sheet with fixed-size sprites.
# Load texture with 16x16 sprites
texture = mcrfpy.Texture("assets/sprites.png", 16, 16)
Tile-based game world with camera controls.
grid = mcrfpy.Grid(
50, 40, # Grid dimensions (tiles)
texture, # Texture for rendering
(0, 0), # Screen position
(800, 600) # Display size (pixels)
)
grid_size
(tuple): Grid dimensions as (width, height)position
(Vector): Screen positionsize
(Vector): Display size in pixelscenter
(tuple): Camera center position in grid coordinateszoom
(float): Camera zoom leveltexture
(Texture): Texture used for renderingchildren
(UIEntityCollection): Entities on this gridclick
(callable): Click handler functionat(index: int) -> GridPoint
: Get GridPoint at linear indexdef on_grid_click(x, y, button, action):
# x, y are grid coordinates (not pixels)
# button: 0=left, 1=right, 2=middle
# action: 0=pressed, 1=released
print(f"Clicked grid at ({x}, {y})")
grid.click = on_grid_click
Individual tile in a Grid.
color
(Color): Base color tintcolor_overlay
(Color): Overlay colorwalkable
(bool): Can entities walk here?transparent
(bool): Can we see through this?tilesprite
(int): Base tile sprite indextile_overlay
(int): Overlay sprite index (-1 for none)uisprite
(int): UI layer sprite index (-1 for none)Game object that exists on a Grid.
entity = mcrfpy.Entity(grid) # Must provide parent grid
pos
(tuple): Grid position as (x, y) integersdraw_pos
(tuple): Smooth position for animation as (x, y) floatssprite_number
(int): Sprite index to displaygridstate
(list): List of GridPointState objects for FOVat(grid_pos: tuple) -> GridPointState
: Get visibility state at positionUI container that can hold other elements.
frame = mcrfpy.Frame(10, 10, 200, 150) # x, y, width, height
x
(float): X positiony
(float): Y positionw
(float): Widthh
(float): Heightoutline
(int): Border thickness in pixelsfill_color
(Color): Background coloroutline_color
(Color): Border colorchildren
(UICollection): Child UI elementsclick
(callable): Click handlerText display element.
# With default font
caption = mcrfpy.Caption("Hello World")
# With custom font
caption = mcrfpy.Caption("Hello World", custom_font)
text
(str): Text to displayx
(float): X positiony
(float): Y positionpos
(Vector): Position as vectorfill_color
(Color): Text coloroutline_color
(Color): Text outline colorclick
(callable): Click handlerSingle sprite display element.
sprite = mcrfpy.Sprite(
texture, # Texture to use
sprite_index, # Which sprite in the texture
(100, 100), # Position
2.0 # Scale (optional, default 1.0)
)
x
(float): X positiony
(float): Y positionscale
(float): Size multipliersprite_number
(int): Sprite index in texturetexture
(Texture): Source textureclick
(callable): Click handlerContainer for UI elements (Frame, Caption, Sprite, Grid).
append(element)
: Add a UI elementremove(element)
: Remove a UI elementfor element in ui_collection:
print(element)
Container for Entity objects on a Grid.
append(entity)
: Add an entityremove(entity)
: Remove an entityfor entity in grid.children:
print(f"Entity at {entity.pos}")
default_font
The default font (JetbrainsMono.ttf) loaded automatically.
caption = mcrfpy.Caption("Text", mcrfpy.default_font)
default_texture
The default texture (kenney_tinydungeon.png) with 16x16 sprites.
grid = mcrfpy.Grid(50, 50, mcrfpy.default_texture, (0, 0), (800, 600))
All visible elements support click handlers:
def handle_click(x, y, button, action):
# x, y: coordinates (screen for UI, grid for Grid)
# button: 0=left, 1=right, 2=middle
# action: 0=pressed, 1=released
pass
element.click = handle_click
# Instant movement
entity.pos = (new_x, new_y)
# Smooth movement (interpolate draw_pos)
entity.draw_pos = (float(new_x), float(new_y))
Elements are drawn in the order they appear in collections:
ui.append(background) # Drawn first
ui.append(midground) # Drawn second
ui.append(foreground) # Drawn last (on top)