This commit is contained in:
Aron Petau 2025-10-12 13:17:29 +02:00
parent b8b6abf48c
commit 5ceecdb0f3
2 changed files with 97 additions and 36 deletions

View file

@ -1,8 +1,8 @@
pygame pygame>=2.6.1
rasterio
numpy numpy
openeo opencv-python
rasterio
geopandas geopandas
leafmap matplotlib
plotly python-vlc
python-vlc moviepy

View file

@ -13,24 +13,85 @@ DEBUG = False
MAX_AMMO = 5 MAX_AMMO = 5
import vlc def play_video(win, video_path):
import pygame """Play video with audio - extracts audio and plays with pygame mixer"""
import cv2
import os
import tempfile
def play_intro_video(win, video_path): try:
instance = vlc.Instance() # Extract audio from video to temporary file
player = instance.media_player_new() audio_path = None
media = instance.media_new(video_path) try:
player.set_media(media) from moviepy import VideoFileClip
player.set_xwindow(win.get_window_id()) # Für Linux, für Windows/Mac ggf. anpassen print("Extracting audio from video...")
player.play() video = VideoFileClip(video_path)
# Warte bis das Video fertig ist oder eine Taste gedrückt wird if video.audio:
playing = True temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
while playing: audio_path = temp_audio.name
for event in pygame.event.get(): temp_audio.close()
if event.type == pygame.KEYDOWN or event.type == pygame.QUIT: video.audio.write_audiofile(audio_path, logger=None)
player.stop() print(f"Audio extracted to: {audio_path}")
playing = False
pygame.time.wait(100) # 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') trinkbrunnen_positions = np.load('trinkbrunnen_pixel_positions.npy')
# Nach dem Laden der Positionen: # Nach dem Laden der Positionen:
@ -72,7 +133,7 @@ MINIMAP_WIDTH = 200
MINIMAP_HEIGHT = int(MAP_H / MAP_W * MINIMAP_WIDTH) 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)) mini = pygame.transform.smoothscale(map_surface, (MINIMAP_WIDTH, MINIMAP_HEIGHT))
win.blit(mini, (WIDTH - MINIMAP_WIDTH - MINIMAP_MARGIN, MINIMAP_MARGIN)) 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) 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) ex, ey = map2mini(enemy.x + PLAYER_SIZE // 4, enemy.y + PLAYER_SIZE // 4)
pygame.draw.circle(win, color, (ex, ey), 5) pygame.draw.circle(win, color, (ex, ey), 5)
for bottle in bottles: for wasser in kranwasser:
pygame.draw.circle(win, (0, 180, 255), map2mini(bottle.x, bottle.y), 3) pygame.draw.circle(win, (0, 180, 255), map2mini(wasser.x, wasser.y), 3)
def show_win_screen(win): def show_win_screen(win):
@ -119,11 +180,11 @@ def main():
enemies = [ enemies = [
Enemy(random.randint(0, MAP_W - PLAYER_SIZE), random.randint(0, MAP_H - PLAYER_SIZE), enemy_img, human_img, 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)] i == 0) for i in range(20)]
bottles = [] kranwasser = []
minimap_visible = True minimap_visible = True
clock = pygame.time.Clock() clock = pygame.time.Clock()
play_intro_video(WIN, "videos/test.mov") play_video(WIN, "videos/test.mov")
running = True running = True
while running: while running:
clock.tick(60) clock.tick(60)
@ -134,7 +195,7 @@ def main():
if event.key == pygame.K_SPACE: if event.key == pygame.K_SPACE:
if player.ammo > 0: 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 player.ammo -= 1
else: else:
print("Keine Munition!") print("Keine Munition!")
@ -146,13 +207,13 @@ def main():
for enemy in enemies: for enemy in enemies:
enemy.move(keys, player, MAP_W, MAP_H) enemy.move(keys, player, MAP_W, MAP_H)
enemy.check_kill_player(player) enemy.check_kill_player(player)
for bottle in bottles[:]: for wasser in kranwasser[:]:
for enemy in enemies: for enemy in enemies:
if enemy.rect.colliderect(bottle.rect): if enemy.rect.colliderect(wasser.rect):
enemy.take_damage(20) enemy.take_damage(20)
bottles.remove(bottle) kranwasser.remove(wasser)
break 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)) 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)) oy = max(0, min(player.y + PLAYER_SIZE // 2 - HEIGHT // 2, MAP_H - HEIGHT))
if scaled_map: if scaled_map:
@ -163,12 +224,12 @@ def main():
for px, py in trinkbrunnen_positions: for px, py in trinkbrunnen_positions:
pygame.draw.circle(WIN, (0, 0, 0), (px - ox, py - oy), 4) pygame.draw.circle(WIN, (0, 0, 0), (px - ox, py - oy), 4)
player.draw(WIN, ox, oy) player.draw(WIN, ox, oy)
for bottle in bottles: for wasser in kranwasser:
bottle.draw(WIN, ox, oy) wasser.draw(WIN, ox, oy)
for enemy in enemies: for enemy in enemies:
enemy.draw(WIN, ox, oy) enemy.draw(WIN, ox, oy)
if minimap_visible: 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) player_rect = pygame.Rect(player.x, player.y, PLAYER_SIZE, PLAYER_SIZE)