import streamlit as st import os import pygame from PIL import Image import requests import io from streamlit_autorefresh import st_autorefresh import base64 st.set_page_config(layout="wide") os.environ["SDL_VIDEODRIVER"] = "dummy" pygame.init() pygame.display.set_mode((1, 1)) # Initialize session state for game variables if 'page' not in st.session_state: st.session_state.page = 'menu' # Helper to load and convert image to base64 data URL def img_to_data_url(path): img = Image.open(path) buf = io.BytesIO() img.save(buf, format="PNG") b64 = base64.b64encode(buf.getvalue()).decode() return f"data:image/png;base64,{b64}" # Helper to load and convert audio to base64 data URL def audio_to_data_url(path, mime_type): with open(path, "rb") as f: b64 = base64.b64encode(f.read()).decode() return f"data:{mime_type};base64,{b64}" # Player image plane_data_url = img_to_data_url('images/player.png') # Audio base64 URLs shoot_audio_url = audio_to_data_url('game_sounds/shooting/shoot.mp3', 'audio/mpeg') explosion_audio_url = audio_to_data_url('game_sounds/explosions/explosion1.wav', 'audio/wav') gameover_audio_url = audio_to_data_url('game_sounds/gameover.mp3', 'audio/mpeg') bgm_audio_url = audio_to_data_url('game_sounds/background_music.mp3', 'audio/mpeg') # Enemy images enemy_imgs = [ img_to_data_url('images/enemy/enemy1_1.png'), img_to_data_url('images/enemy/enemy1_2.png'), img_to_data_url('images/enemy/enemy1_3.png'), img_to_data_url('images/enemy/enemy2_1.png'), img_to_data_url('images/enemy/enemy2_2.png'), ] # Boss images boss_imgs = [ img_to_data_url('images/boss/boss1.png'), img_to_data_url('images/boss/boss2.png'), img_to_data_url('images/boss/boss2_1.png'), img_to_data_url('images/boss/boss3.png'), ] # Bullet images bullet_imgs = [ img_to_data_url('images/bullets/bullet1.png'), img_to_data_url('images/bullets/bullet2.png'), img_to_data_url('images/bullets/bullet3.png'), img_to_data_url('images/bullets/bullet4.png'), ] # Explosion images (first 5 frames for animation) explosion_imgs = [ img_to_data_url('images/explosion/explosion0.png'), img_to_data_url('images/explosion/explosion1.png'), img_to_data_url('images/explosion/explosion2.png'), img_to_data_url('images/explosion/explosion3.png'), img_to_data_url('images/explosion/explosion4.png'), ] # Meteor images meteor_imgs = [ img_to_data_url('images/meteors/meteor_1.png'), img_to_data_url('images/meteors/meteor_2.png'), img_to_data_url('images/meteors/meteor_3.png'), img_to_data_url('images/meteors/meteor_4.png'), img_to_data_url('images/meteors/meteor2_1.png'), img_to_data_url('images/meteors/meteor2_2.png'), img_to_data_url('images/meteors/meteor2_3.png'), img_to_data_url('images/meteors/meteor2_4.png'), ] # Refill images refill_imgs = [ img_to_data_url('images/refill/bullet_refill.png'), img_to_data_url('images/refill/double_refill.png'), img_to_data_url('images/refill/health_refill.png'), ] # Hole images hole_imgs = [ img_to_data_url('images/hole/black_hole.png'), img_to_data_url('images/hole/black_hole2.png'), ] # Score images score_imgs = [ img_to_data_url('images/score/score_coin.png'), ] # Menu page if st.session_state.page == 'menu': st.title('Cosmic Heat') menu_img = Image.open('images/mainmenu.jpg') st.image(menu_img, use_container_width=True) if st.button('Play'): st.session_state.page = 'game' st.rerun() if st.button('Exit'): st.stop() # Game page if st.session_state.page == 'game': st.title('Cosmic Heat - Game') st_autorefresh(interval=100, key="game_refresh") # Pass image arrays as JSON to JS import json enemy_imgs_js = json.dumps(enemy_imgs) boss_imgs_js = json.dumps(boss_imgs) bullet_imgs_js = json.dumps(bullet_imgs) explosion_imgs_js = json.dumps(explosion_imgs) meteor_imgs_js = json.dumps(meteor_imgs) refill_imgs_js = json.dumps(refill_imgs) hole_imgs_js = json.dumps(hole_imgs) score_imgs_js = json.dumps(score_imgs) game_data = st.components.v1.html(f"""
""", height=600).value if game_data and isinstance(game_data, str): try: player_x, player_y, frame_data, bullet_count = game_data.split(',') player_x, player_y = map(int, (player_x, player_y)) bullet_count = int(bullet_count) # Render the frame using the data URL response = requests.get(frame_data) frame = Image.open(io.BytesIO(response.content)) st.image(frame, caption='Cosmic Heat Frame', use_container_width=True) st.write(f"Bullets: {bullet_count}") except ValueError as e: st.write("Error processing frame:", e) st.image(Image.open('images/bg/background.jpg'), caption='Cosmic Heat Frame', use_container_width=True) st.write("Bullets: 200") if st.button('Back to Menu'): st.session_state.page = 'menu' st.rerun()