#!/usr/bin/env python3 # pygame_to_flask.py - PyGame renders, Flask serves import pygame,os import numpy as np import time import threading from flask import Flask, Response # Setup os.environ['SDL_VIDEODRIVER'] = 'dummy' WIDTH, HEIGHT = 400, 300 app = Flask(__name__) # Simple shared state current_frame = None frame_lock = threading.Lock() @app.route('/') def index(): return ''' ''' @app.route('/pygame_frame') def pygame_frame(): with frame_lock: if current_frame: return Response(current_frame, mimetype='application/octet-stream') # Fallback: black frame black = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8).tobytes() return Response(black, mimetype='application/octet-stream') def pygame_render(): """PyGame rendering thread""" print("🎬 Starting PyGame...") try: pygame.init() surface = pygame.Surface((WIDTH, HEIGHT)) print("✅ PyGame initialized") except Exception as e: print(f"❌ PyGame failed: {e}") return # Simple animation x, y = 200, 150 dx, dy = 3, 2 while True: # Clear surface.fill((20, 20, 40)) # Update x += dx y += dy if x < 20 or x > 380: dx = -dx if y < 20 or y > 280: dy = -dy # Draw pygame.draw.circle(surface, (255, 100, 100), (int(x), int(y)), 20) # Convert to bytes pixels = pygame.surfarray.pixels3d(surface) with frame_lock: global current_frame current_frame = pixels.tobytes() time.sleep(1/30) # Start PyGame thread threading.Thread(target=pygame_render, daemon=True).start() # Start Flask print("🌐 Starting Flask...") app.run(host='0.0.0.0', port=7860, threaded=True)