import time import framebuf from lib.display import Display from lib.userinput import UserInput from apps.Boo.menu import menu # Use absolute import from apps.Boo.stats import stats # Use absolute import # Initialize display and input (only once) display = Display() input = UserInput() # Movement parameters x_pos = 0 y_pos = 0 x_vel = 2 y_vel = 2 # Blinking parameters blink_interval = 3 # Blink every 3 seconds blink_duration = 0.2 # Blink lasts 0.2 seconds last_blink_time = time.ticks_ms() # Initialize the blink time def load_image(filename): with open(f'/apps/Boo/{filename}.raw', 'rb') as f: return f.read() def draw_framebuffer(framebuffer, x_offset, y_offset): width = 32 height = 32 for y in range(height): for x in range(width): if framebuffer.pixel(x, y): display.pixel(x + x_offset, y + y_offset, 0xFFFF) def draw_text(x, y, text, color): """Draw text on the display with a simulated larger size.""" CHAR_WIDTH = 8 cursor_x = x for char in text: if char == ' ': cursor_x += CHAR_WIDTH else: display.text(text=char, x=cursor_x, y=y, color=color) cursor_x += CHAR_WIDTH def main_loop(): global x_pos, y_pos, x_vel, y_vel, last_blink_time while True: keys = input.get_new_keys() if "2" in keys: stats(display, input) elif "1" in keys: menu(display, input) current_time = time.ticks_ms() # Load images only when needed ghost_data = load_image('ghost') blink_data = load_image('ghost_blink') ghost_fb = framebuf.FrameBuffer(bytearray(ghost_data), 32, 32, framebuf.MONO_HLSB) blink_fb = framebuf.FrameBuffer(bytearray(blink_data), 32, 32, framebuf.MONO_HLSB) # Determine which image to display based on blinking timing if time.ticks_diff(current_time, last_blink_time) % (blink_interval * 1000) < blink_duration * 1000: framebuffer = blink_fb else: framebuffer = ghost_fb display.fill(0) draw_framebuffer(framebuffer, x_pos, y_pos) # Display menu at the top draw_text(x=5, y=0, text='1: Menu 2: Stats', color=0xFFFF) display.show() # Update positions x_pos += x_vel y_pos += y_vel # Handle bouncing off edges if x_pos <= 0 or x_pos + 32 >= 240: x_vel = -x_vel if y_pos <= 0 or y_pos + 32 >= 135: y_vel = -y_vel # Free up memory after use del ghost_data, blink_data, ghost_fb, blink_fb time.sleep(0.1) # Run the main loop main_loop()