Setup for basic Editor for map, started gameplay screen

This commit is contained in:
2026-05-20 15:58:06 +02:00
parent cc0a92060b
commit 6283fb0d5a
13 changed files with 383 additions and 59 deletions
+62 -30
View File
@@ -1,40 +1,72 @@
import pygame as pg
import pygame
import pygame_gui
from constants import Config
class Game():
def __init__(self, screen, clock, manager):
self.screen = screen
self.clock = clock
self.manager = manager
self.fps_label = pygame_gui.elements.UILabel(
relative_rect=pg.Rect((10, 10), (120, 40)),
text="FPS: 0",
manager=self.manager
from game.util import *
from game.tilemap import Tilemap
from game.screens.main_menu import MainMenu
from game.screens.game_play import GamePlay
FPS=60
WINDOW_SIZE=(960,540)
TILE=16
class Game:
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode(WINDOW_SIZE)
self.clock = pygame.time.Clock()
self.ui_manager = pygame_gui.UIManager(
WINDOW_SIZE,
"theme.json"
)
self.tilemap = Tilemap(self)
self.assets={
"base": load_images('/tiles/test')
}
self.screens = {
"menu": MainMenu(self),
"game": GamePlay(self)
}
self.current_screen = self.screens["menu"]
def change_screen(self, name):
self.current_screen.hide()
self.current_screen = self.screens[name]
self.current_screen.show()
def run(self):
self.running = True
while self.running:
self.clock.tick(Config.FPS)
self.update()
self.draw()
return self.running
def update(self):
fps = str(int(self.clock.get_fps()))
self.fps_label.set_text('fps: ' + fps)
running = True
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
while running:
self.manager.process_events(event)
dt = self.clock.tick(FPS) / 1000.0
time_delta = self.clock.tick(Config.FPS)/1000.0
self.manager.update(time_delta)
for event in pygame.event.get():
def draw(self):
self.screen.fill(self.manager.get_theme().get_colour('normal_bg'))
self.manager.draw_ui(self.screen)
pg.display.update()
if event.type == pygame.QUIT:
running = False
self.ui_manager.process_events(event)
self.current_screen.process_event(event)
self.ui_manager.update(dt)
self.window.fill((20, 20, 20))
self.current_screen.draw(self.window)
self.ui_manager.draw_ui(self.window)
pygame.display.update()
pygame.quit()
View File
+23
View File
@@ -0,0 +1,23 @@
class BaseScreen:
def __init__(self, game):
self.game = game
self.elements = []
def process_event(self, event):
pass
def update(self, dt):
pass
def draw(self, surface):
pass
def show(self):
for element in self.elements:
element.show()
def hide(self):
for element in self.elements:
element.hide()
+35
View File
@@ -0,0 +1,35 @@
from game.screens.base_screen import BaseScreen
import pygame as pg
import pygame_gui as pgi
class GamePlay(BaseScreen):
def __init__(self, game):
super().__init__(game)
self.tile_multiplicator = 2.0
self.width, self.height = pg.display.get_surface().get_size()
self.map_screen = pg.surface.Surface((int(self.width/4*3),int(self.height/6*5)))
self.scroll = [0, 0]
self.map = 'test'
self.path = 'assets/maps/' + str(self.map) + '.json'
try:
self.game.tilemap.load(self.path)
except FileNotFoundError:
print('File not Found')
def process_event(self, event):
pass
def update(self, dt):
pass
def draw(self, surface):
self.map_screen.fill((40, 40, 40))
render_scroll = (int(self.scroll[0]), int(self.scroll[1]))
self.game.tilemap.render(self.map_screen, offset=render_scroll)
self.game.window.blit(self.map_screen, (0, self.height/6))
+79
View File
@@ -0,0 +1,79 @@
from game.screens.base_screen import BaseScreen
import pygame as pg
import pygame_gui as pgi
class MainMenu(BaseScreen):
def __init__(self, game):
super().__init__(game)
manager = game.ui_manager
width, height = pg.display.get_surface().get_size()
# Größen
title_width = 400
title_height = 80
button_width = 250
button_height = 60
center_x = width // 2
self.title = pgi.elements.UILabel(
relative_rect=pg.Rect(
center_x - title_width // 2,
int(height * 0.2),
title_width,
title_height
),
text="MAIN MENU",
manager=manager
)
self.play_button = pgi.elements.UIButton(
relative_rect=pg.Rect(
center_x - button_width // 2,
int(height * 0.35),
button_width,
button_height
),
text="Play",
manager=manager
)
self.quit_button = pgi.elements.UIButton(
relative_rect=pg.Rect(
center_x - button_width // 2,
int(height * 0.60),
button_width,
button_height
),
text="Quit",
manager=manager
)
self.elements = [
self.title,
self.play_button,
self.quit_button
]
def process_event(self, event):
if event.type == pgi.UI_BUTTON_PRESSED:
if event.ui_element == self.play_button:
# Zum Gameplay wechseln
self.game.change_screen("game")
if event.ui_element == self.quit_button:
pg.quit()
exit()
+29
View File
@@ -0,0 +1,29 @@
import pygame, json
class Tilemap:
def __init__(self, game, tile=16):
self.game = game
self.tile_size = tile
self.tilemap = {}
def save(self, path):
f = open(path, 'w')
json.dump({'tilemap': self.tilemap, 'tile_size': self.tile_size}, f)
f.close()
def load(self, path):
f = open(path, 'r')
map_data = json.load(f)
f.close()
self.tilemap = map_data['tilemap']
self.tile_size = map_data['tile_size']
def render(self, surf, offset=(0, 0)):
for x in range(offset[0] // self.tile_size, (offset[0] + surf.get_width()) // self.tile_size + 1):
for y in range(offset[1] // self.tile_size, (offset[1] + surf.get_height()) // self.tile_size + 1):
loc = str(x) + ';' + str(y)
if loc in self.tilemap:
tile = self.tilemap[loc]
surf.blit(self.game.assets[tile['type']][tile['variant']], (tile['pos'][0] * self.tile_size - offset[0], tile['pos'][1] * self.tile_size - offset[1]))
+16
View File
@@ -0,0 +1,16 @@
import pygame as pg
import os
img_path='assets/images'
WINDOW_SIZE=(800,600)
def load_image(path, colorkey=(0,0,0)):
img = pg.image.load(img_path + path).convert()
img.set_colorkey(colorkey)
return img
def load_images(dir_path):
images = []
for img_name in os.listdir(img_path+dir_path):
images.append(load_image(dir_path + '/' + img_name))
return images