diff --git a/requirements.txt b/requirements.txt index dc6a7a3..a5cdcc7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -pygame -rasterio +pygame>=2.6.1 numpy -openeo +opencv-python +rasterio geopandas -leafmap -plotly -python-vlc \ No newline at end of file +matplotlib +python-vlc +moviepy \ No newline at end of file diff --git a/water-game.py b/water-game.py index 4b8a783..f82c56a 100644 --- a/water-game.py +++ b/water-game.py @@ -13,24 +13,85 @@ DEBUG = False MAX_AMMO = 5 -import vlc -import pygame +def play_video(win, video_path): + """Play video with audio - extracts audio and plays with pygame mixer""" + import cv2 + import os + import tempfile -def play_intro_video(win, video_path): - instance = vlc.Instance() - player = instance.media_player_new() - media = instance.media_new(video_path) - player.set_media(media) - player.set_xwindow(win.get_window_id()) # Für Linux, für Windows/Mac ggf. anpassen - player.play() - # Warte bis das Video fertig ist oder eine Taste gedrückt wird - playing = True - while playing: - for event in pygame.event.get(): - if event.type == pygame.KEYDOWN or event.type == pygame.QUIT: - player.stop() - playing = False - pygame.time.wait(100) + try: + # Extract audio from video to temporary file + audio_path = None + try: + from moviepy import VideoFileClip + print("Extracting audio from video...") + video = VideoFileClip(video_path) + if video.audio: + temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') + audio_path = temp_audio.name + temp_audio.close() + video.audio.write_audiofile(audio_path, logger=None) + print(f"Audio extracted to: {audio_path}") + + # Play audio + pygame.mixer.init() + pygame.mixer.music.load(audio_path) + pygame.mixer.music.play() + print("Audio playback started") + else: + print("No audio track found in video") + video.close() + except Exception as e: + print(f"Audio extraction warning: {e}") # Play video + cap = cv2.VideoCapture(video_path) + + if not cap.isOpened(): + print(f"Error: Could not open video file: {video_path}") + return + + clock = pygame.time.Clock() + running = True + win_size = win.get_size() + frame_count = 0 + + while running and cap.isOpened(): + ret, frame = cap.read() + if not ret: + break + + frame_count += 1 + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + # Transpose for pygame (width, height) format + frame = frame.swapaxes(0, 1) + frame_surface = pygame.surfarray.make_surface(frame) + frame_surface = pygame.transform.smoothscale(frame_surface, win_size) + win.blit(frame_surface, (0, 0)) + pygame.display.update() + + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN or event.type == pygame.QUIT: + running = False + if audio_path: + pygame.mixer.music.stop() + + clock.tick(30) + + cap.release() + if audio_path: + pygame.mixer.music.stop() + + print(f"Played {frame_count} video frames") + + # Clean up temp audio file + if audio_path and os.path.exists(audio_path): + try: + os.unlink(audio_path) + except: + pass + + except Exception as e: + print(f"Video playback error: {e}") + print("Skipping video...") trinkbrunnen_positions = np.load('trinkbrunnen_pixel_positions.npy') # Nach dem Laden der Positionen: @@ -72,7 +133,7 @@ MINIMAP_WIDTH = 200 MINIMAP_HEIGHT = int(MAP_H / MAP_W * MINIMAP_WIDTH) -def draw_minimap(win, map_surface, player, enemies, bottles): +def draw_minimap(win, map_surface, player, enemies, kranwasser): mini = pygame.transform.smoothscale(map_surface, (MINIMAP_WIDTH, MINIMAP_HEIGHT)) win.blit(mini, (WIDTH - MINIMAP_WIDTH - MINIMAP_MARGIN, MINIMAP_MARGIN)) @@ -87,8 +148,8 @@ def draw_minimap(win, map_surface, player, enemies, bottles): color = (0, 128, 255) if enemy.is_human else (255, 0, 0) if enemy.controlled else (200, 200, 0) ex, ey = map2mini(enemy.x + PLAYER_SIZE // 4, enemy.y + PLAYER_SIZE // 4) pygame.draw.circle(win, color, (ex, ey), 5) - for bottle in bottles: - pygame.draw.circle(win, (0, 180, 255), map2mini(bottle.x, bottle.y), 3) + for wasser in kranwasser: + pygame.draw.circle(win, (0, 180, 255), map2mini(wasser.x, wasser.y), 3) def show_win_screen(win): @@ -119,11 +180,11 @@ def main(): enemies = [ Enemy(random.randint(0, MAP_W - PLAYER_SIZE), random.randint(0, MAP_H - PLAYER_SIZE), enemy_img, human_img, i == 0) for i in range(20)] - bottles = [] + kranwasser = [] minimap_visible = True clock = pygame.time.Clock() - play_intro_video(WIN, "videos/test.mov") + play_video(WIN, "videos/test.mov") running = True while running: clock.tick(60) @@ -134,7 +195,7 @@ def main(): if event.key == pygame.K_SPACE: if player.ammo > 0: - bottles.append(TapWater(player.x, player.y, player.dir)) + kranwasser.append(TapWater(player.x, player.y, player.dir)) player.ammo -= 1 else: print("Keine Munition!") @@ -146,13 +207,13 @@ def main(): for enemy in enemies: enemy.move(keys, player, MAP_W, MAP_H) enemy.check_kill_player(player) - for bottle in bottles[:]: + for wasser in kranwasser[:]: for enemy in enemies: - if enemy.rect.colliderect(bottle.rect): + if enemy.rect.colliderect(wasser.rect): enemy.take_damage(20) - bottles.remove(bottle) + kranwasser.remove(wasser) break - bottles = [b for b in bottles if 0 <= b.x <= MAP_W and 0 <= b.y <= MAP_H] + kranwasser = [w for w in kranwasser if 0 <= w.x <= MAP_W and 0 <= w.y <= MAP_H] ox = max(0, min(player.x + PLAYER_SIZE // 2 - WIDTH // 2, MAP_W - WIDTH)) oy = max(0, min(player.y + PLAYER_SIZE // 2 - HEIGHT // 2, MAP_H - HEIGHT)) if scaled_map: @@ -163,12 +224,12 @@ def main(): for px, py in trinkbrunnen_positions: pygame.draw.circle(WIN, (0, 0, 0), (px - ox, py - oy), 4) player.draw(WIN, ox, oy) - for bottle in bottles: - bottle.draw(WIN, ox, oy) + for wasser in kranwasser: + wasser.draw(WIN, ox, oy) for enemy in enemies: enemy.draw(WIN, ox, oy) if minimap_visible: - draw_minimap(WIN, map_surface, player, enemies, bottles) + draw_minimap(WIN, map_surface, player, enemies, kranwasser) player_rect = pygame.Rect(player.x, player.y, PLAYER_SIZE, PLAYER_SIZE)