import pygame import numpy as np from flask import Flask, Response, render_template_string, request import time import os # Initialize Pygame headlessly os.environ['SDL_VIDEODRIVER'] = 'dummy' pygame.init() pygame.mixer.init(frequency=44100, size=-16, channels=2) app = Flask(__name__) class ShaderRenderer: def __init__(self, width=640, height=480): self.width = width self.height = height self.mouse_x = width // 2 self.mouse_y = height // 2 self.start_time = time.time() self.surface = pygame.Surface((width, height)) # Sound sources self.sound_source = 'none' # 'none', 'pygame', 'browser' self.pygame_sound = None self.pygame_playing = False self.sound_amp = 0.0 # Load pygame sound if available if os.path.exists('sound.mp3'): try: self.pygame_sound = pygame.mixer.Sound('sound.mp3') print("āœ… Pygame sound loaded") except: print("āš ļø Could not load sound.mp3") def set_mouse(self, x, y): self.mouse_x = max(0, min(self.width, x)) self.mouse_y = max(0, min(self.height, y)) def set_sound_source(self, source): """Change sound source: none, pygame, browser""" self.sound_source = source # Handle pygame sound if source == 'pygame': if self.pygame_sound and not self.pygame_playing: self.pygame_sound.play(loops=-1) self.pygame_playing = True self.sound_amp = 0.5 else: if self.pygame_playing: pygame.mixer.stop() self.pygame_playing = False self.sound_amp = 0.0 def render_frame(self): """Render a simple pattern that shader will transform""" t = time.time() - self.start_time # Clear self.surface.fill((20, 20, 30)) # Draw TOP marker (should be at top of canvas) pygame.draw.rect(self.surface, (255, 100, 100), (10, 10, 100, 30)) font = pygame.font.Font(None, 24) text = font.render("TOP", True, (255, 255, 255)) self.surface.blit(text, (20, 15)) # Draw BOTTOM marker pygame.draw.rect(self.surface, (100, 255, 100), (10, self.height-40, 100, 30)) text = font.render("BOTTOM", True, (0, 0, 0)) self.surface.blit(text, (20, self.height-35)) # Draw moving circle circle_size = 30 + int(20 * np.sin(t * 2)) # Color code based on sound source if self.sound_source == 'pygame': color = (100, 255, 100) # Green for pygame sound elif self.sound_source == 'browser': color = (100, 100, 255) # Blue for browser sound else: color = (255, 100, 100) # Red for no sound pygame.draw.circle(self.surface, color, (self.mouse_x, self.mouse_y), circle_size) # Draw grid for x in range(0, self.width, 50): alpha = int(40 + 20 * np.sin(x * 0.1 + t)) pygame.draw.line(self.surface, (alpha, alpha, 50), (x, 0), (x, self.height)) for y in range(0, self.height, 50): alpha = int(40 + 20 * np.cos(y * 0.1 + t)) pygame.draw.line(self.surface, (alpha, alpha, 50), (0, y), (self.width, y)) # Sound meter meter_width = int(200 * self.sound_amp) pygame.draw.rect(self.surface, (60, 60, 60), (self.width-220, 10, 200, 20)) pygame.draw.rect(self.surface, (100, 255, 100), (self.width-220, 10, meter_width, 20)) # Shader indicator shader_text = font.render("SHADER ON", True, (255, 255, 0)) self.surface.blit(shader_text, (self.width-150, self.height-30)) return pygame.image.tostring(self.surface, 'RGB') def get_frame(self): return self.render_frame() renderer = ShaderRenderer() @app.route('/') def index(): return render_template_string(''' Pygame + WebGL Shader + Sound

šŸŽ® Pygame + WebGL Shader + Sound

EFFECTS ACTIVE

šŸ”Š Sound Source

Source: None Shader: ON
No sound Pygame sound Browser sound | šŸ”“ TOP marker should be at top | 🟢 BOTTOM at bottom
''') @app.route('/frame') def frame(): """Return raw RGB bytes""" return Response(renderer.get_frame(), mimetype='application/octet-stream') @app.route('/mouse', methods=['POST']) def mouse(): data = request.json renderer.set_mouse(data['x'], data['y']) return 'OK' @app.route('/sound/source', methods=['POST']) def sound_source(): data = request.json renderer.set_sound_source(data['source']) return 'OK' @app.route('/sound/amp') def sound_amp(): return {'amp': renderer.sound_amp} # Serve static sound file @app.route('/static/sound.mp3') def serve_sound(): if os.path.exists('sound.mp3'): with open('sound.mp3', 'rb') as f: return Response(f.read(), mimetype='audio/mpeg') return 'Sound not found', 404 if __name__ == '__main__': print("\n" + "="*70) print("šŸŽ® Pygame + WebGL Shader + Sound") print("="*70) print("🌐 http://localhost:5000") print("\nšŸ”® Shader Toggle: See pure Pygame vs. effects") print("šŸ”Š Sound Sources:") print(" • None - No sound") print(" • Pygame - sound.mp3 plays in Pygame (streamed)") print(" • Browser - sound.mp3 plays in browser") print("\nšŸŽÆ Orientation fixed: TOP at top, BOTTOM at bottom") print(" Circle color indicates sound source") print(" Sound meter shows activity") print("="*70 + "\n") app.run(host='0.0.0.0', port=5000, debug=False)