cdec/water-game.py

132 lines
5.1 KiB
Python
Raw Normal View History

2025-10-12 08:54:30 +02:00
import sys
import random
import pygame
import numpy as np
import rasterio
from player import Player
from enemy import Enemy
from tapwater import TapWater
from settings import PLAYER_SIZE, MAP_PATH, WIDTH, HEIGHT, MINIMAP_MARGIN
DEBUG = False
2025-10-11 14:12:58 +02:00
pygame.init()
2025-10-12 08:54:30 +02:00
# logo_img = pygame.image.load("images/logo.png")
# pygame.display.set_icon(logo_img)
2025-10-11 14:12:58 +02:00
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
2025-10-12 10:39:47 +02:00
pygame.display.set_caption("Wasser der Regierung")
logo_img = pygame.image.load("images/Wasser_der_Regierung.PNG")
pygame.display.set_icon(logo_img)
2025-10-11 14:12:58 +02:00
2025-10-11 16:26:27 +02:00
def load_map(path):
2025-10-11 14:12:58 +02:00
try:
2025-10-11 16:26:27 +02:00
arr = rasterio.open(path).read(1)
arr = np.nan_to_num(arr - 272.15, nan=0)
2025-10-11 14:12:58 +02:00
arr = np.clip(arr, np.percentile(arr, 5), np.percentile(arr, 95))
2025-10-11 16:26:27 +02:00
arr = ((arr - arr.min()) / (arr.max() - arr.min()) * 255).astype(np.uint8)
arr = np.stack([arr, arr, arr], -1)
2025-10-11 14:12:58 +02:00
surf = pygame.surfarray.make_surface(arr)
2025-10-11 16:26:27 +02:00
return surf, arr.shape[1], arr.shape[0]
2025-10-11 14:12:58 +02:00
except Exception as e:
2025-10-12 08:54:30 +02:00
print(f"Map load error: {e}")
return None, WIDTH, HEIGHT
2025-10-11 17:30:00 +02:00
2025-10-12 08:54:30 +02:00
map_surface, MAP_W, MAP_H = load_map(MAP_PATH)
MINIMAP_WIDTH = 200
MINIMAP_HEIGHT = int(MAP_H / MAP_W * MINIMAP_WIDTH)
2025-10-11 17:30:00 +02:00
2025-10-12 08:54:30 +02:00
def draw_minimap(win, map_surface, player, enemies, bottles):
mini = pygame.transform.smoothscale(map_surface, (MINIMAP_WIDTH, MINIMAP_HEIGHT))
win.blit(mini, (WIDTH - MINIMAP_WIDTH - MINIMAP_MARGIN, MINIMAP_MARGIN))
2025-10-11 14:12:58 +02:00
2025-10-12 08:54:30 +02:00
def map2mini(x, y):
mx = int(x / MAP_W * MINIMAP_WIDTH)
my = int(y / MAP_H * MINIMAP_HEIGHT)
return WIDTH - MINIMAP_WIDTH - MINIMAP_MARGIN + mx, MINIMAP_MARGIN + my
2025-10-11 16:26:27 +02:00
2025-10-12 08:54:30 +02:00
pygame.draw.circle(win, (0, 255, 0), map2mini(player.x + PLAYER_SIZE // 2, player.y + PLAYER_SIZE // 2), 5)
2025-10-11 17:30:00 +02:00
for enemy in enemies:
2025-10-12 08:54:30 +02:00
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)
2025-10-11 17:30:00 +02:00
pygame.draw.circle(win, color, (ex, ey), 5)
2025-10-12 08:54:30 +02:00
for bottle in bottles:
pygame.draw.circle(win, (0, 180, 255), map2mini(bottle.x, bottle.y), 3)
2025-10-11 14:12:58 +02:00
2025-10-12 10:39:47 +02:00
def show_win_screen(win):
win.fill((40, 180, 80))
font = pygame.font.SysFont(None, 72)
text = font.render("Gewonnen!", True, (255, 255, 255))
win.blit(text, (win.get_width() // 2 - text.get_width() // 2, win.get_height() // 2 - text.get_height() // 2))
pygame.display.update()
pygame.time.wait(5000)
def show_lose_screen(win):
win.fill((180, 40, 40))
font = pygame.font.SysFont(None, 72)
text = font.render("Verloren!", True, (255, 255, 255))
win.blit(text, (win.get_width() // 2 - text.get_width() // 2, win.get_height() // 2 - text.get_height() // 2))
pygame.display.update()
pygame.time.wait(5000)
2025-10-11 14:12:58 +02:00
def main():
2025-10-12 08:54:30 +02:00
from settings import PLAYER_IMG_PATH, ENEMY_IMG_PATH, HUMAN_IMG_PATH, PLAYER_SIZE, load_img
player_img = load_img(PLAYER_IMG_PATH, (PLAYER_SIZE, PLAYER_SIZE))
enemy_img = load_img(ENEMY_IMG_PATH, (PLAYER_SIZE, PLAYER_SIZE))
human_img = load_img(HUMAN_IMG_PATH, (PLAYER_SIZE, PLAYER_SIZE))
player = Player(MAP_W // 2, MAP_H // 2, player_img)
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)]
2025-10-11 16:26:27 +02:00
bottles = []
minimap_visible = True
clock = pygame.time.Clock()
2025-10-11 14:12:58 +02:00
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
2025-10-12 08:54:30 +02:00
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bottles.append(TapWater(player.x, player.y, player.dir))
if event.key == pygame.K_m:
minimap_visible = not minimap_visible
2025-10-11 14:12:58 +02:00
keys = pygame.key.get_pressed()
2025-10-12 08:54:30 +02:00
player.move(keys, MAP_W, MAP_H)
2025-10-11 17:30:00 +02:00
for enemy in enemies:
2025-10-12 08:54:30 +02:00
enemy.move(keys, player, MAP_W, MAP_H)
2025-10-11 17:30:00 +02:00
enemy.check_kill_player(player)
2025-10-12 08:54:30 +02:00
for bottle in bottles[:]:
2025-10-11 17:30:00 +02:00
for enemy in enemies:
2025-10-12 08:54:30 +02:00
if enemy.rect.colliderect(bottle.rect):
enemy.take_damage(20)
bottles.remove(bottle)
break
bottles = [b for b in bottles if 0 <= b.x <= MAP_W and 0 <= b.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 map_surface:
WIN.blit(map_surface, (0, 0), area=pygame.Rect(ox, oy, WIDTH, HEIGHT))
else:
WIN.fill((255, 255, 255))
2025-10-11 16:26:27 +02:00
player.draw(WIN, ox, oy)
2025-10-12 08:54:30 +02:00
for bottle in bottles:
bottle.draw(WIN, ox, oy)
for enemy in enemies:
enemy.draw(WIN, ox, oy)
2025-10-11 17:30:00 +02:00
if minimap_visible:
draw_minimap(WIN, map_surface, player, enemies, bottles)
2025-10-11 14:12:58 +02:00
pygame.display.update()
2025-10-12 10:39:47 +02:00
if not player.alive:
show_lose_screen(WIN)
running = False
elif all(not e.alive or e.health <= 0 for e in enemies):
show_win_screen(WIN)
running = False
2025-10-12 08:54:30 +02:00
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()