TINI-8/core/lua_api.py

65 lines
1.8 KiB
Python
Raw Permalink Normal View History

# engine/lua_api.py
import pygame
class LuaAPI:
def __init__(self, renderer, input_handler):
self.renderer = renderer
self.input_handler = input_handler
def get_api(self, lua):
# Expose functions to Lua
lua.globals()['cls'] = self.cls
lua.globals()['px'] = self.px
lua.globals()['spr'] = self.spr
lua.globals()['btn'] = self.btn
def cls(self, color=0):
self.renderer.clear(color)
def px(self, x, y, color=1):
self.renderer.draw_pixel(x, y, color)
def spr(self, x, y, sprite, color=1):
"""
Draws a sprite at position (x, y).
:param x: X-coordinate
:param y: Y-coordinate
:param sprite: 2D list representing the sprite
:param color: Color index
"""
# Convert Lua table to Python list if necessary
if hasattr(sprite, 'keys'):
# If sprite is a Lua table, convert it to a Python list
sprite = [sprite[i + 1] for i in range(len(sprite))]
self.renderer.draw_sprite(x, y, sprite, color)
def btn(self, button):
"""
Checks if a specific button is pressed.
Button Mapping:
0: Left Arrow
1: Right Arrow
2: Up Arrow
3: Down Arrow
4: Z (Button A)
5: X (Button B)
:param button: Button index
:return: Boolean indicating if the button is pressed
"""
# Map button indices to Pygame keys
key_map = {
0: pygame.K_LEFT,
1: pygame.K_RIGHT,
2: pygame.K_UP,
3: pygame.K_DOWN,
4: pygame.K_z, # Button A
5: pygame.K_x, # Button B
}
key = key_map.get(button)
if key:
return self.input_handler.is_key_pressed(key)
return False