# main.py import pygame from core.lua_api import LuaAPI from core.renderer import Renderer from core.input_handler import InputHandler from lupa import LuaRuntime import os import sys # Constants for 8-bit style SCREEN_WIDTH = 160 SCREEN_HEIGHT = 144 SCALE_FACTOR = 5 # Scale up for visibility FPS = 60 def main(): pygame.init() pygame.display.set_caption("8-Bit Python Engine") window = pygame.display.set_mode((SCREEN_WIDTH * SCALE_FACTOR, SCREEN_HEIGHT * SCALE_FACTOR)) clock = pygame.time.Clock() # Create a surface for the 8-bit display display_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT)) # Initialize components renderer = Renderer(display_surface) input_handler = InputHandler() lua = LuaRuntime(unpack_returned_tuples=True) api = LuaAPI(renderer, input_handler) api.get_api(lua) # Expose API to Lua # Load Lua game script script_path = os.path.join("scripts", "game.lua") if not os.path.exists(script_path): print(f"Error: Lua script '{script_path}' not found.") pygame.quit() sys.exit(1) with open(script_path, "r") as f: lua_script = f.read() try: lua.execute(lua_script) except Exception as e: print(f"Error executing Lua script: {e}") pygame.quit() sys.exit(1) # Retrieve update and draw functions from Lua lua_globals = lua.globals() if not hasattr(lua_globals, 'update') or not hasattr(lua_globals, 'draw'): print("Error: Lua script must define 'update' and 'draw' functions.") pygame.quit() sys.exit(1) update = lua_globals.update draw = lua_globals.draw running = True while running: delta_time = clock.tick(FPS) / 1000.0 # Seconds since last frame for event in pygame.event.get(): if event.type == pygame.QUIT: running = False input_handler.process_event(event) try: # Update game logic via Lua update(delta_time) # Clear the display renderer.clear() # Draw game elements via Lua draw() except Exception as e: print(f"Error during Lua execution: {e}") running = False # Scale and blit to the window scaled_surface = pygame.transform.scale(display_surface, (SCREEN_WIDTH * SCALE_FACTOR, SCREEN_HEIGHT * SCALE_FACTOR)) window.blit(scaled_surface, (0, 0)) pygame.display.flip() pygame.quit() if __name__ == "__main__": main()