118 lines
3.0 KiB
Python
Executable File
118 lines
3.0 KiB
Python
Executable File
import pygame
|
|
import pygame_gui as pgg
|
|
import numpy as np
|
|
import Tiles
|
|
import logging as log
|
|
|
|
screen_size = np.array([16 / 9 / 2, 1]) * 1440
|
|
|
|
origin = np.array([screen_size[0] / 2, Tiles.tile_size])
|
|
|
|
|
|
def load_map(map_file="map.npz"):
|
|
print("started loading tiles")
|
|
data = np.load(map_file)
|
|
us = data["us"]
|
|
vs = data["vs"]
|
|
es = data["es"]
|
|
ts = data["ts"]
|
|
|
|
map_tiles = pygame.sprite.RenderPlain()
|
|
for u, v, e, t in zip(us, vs, es, ts):
|
|
tile = getattr(Tiles, t)(u, v, e, origin)
|
|
map_tiles.add(tile)
|
|
|
|
print(f"loaded {len(map_tiles)} tiles")
|
|
|
|
return map_tiles
|
|
|
|
|
|
def save_map(map_tiles: pygame.sprite.RenderPlain, map_file: str = "map.npz"):
|
|
us = np.array([])
|
|
vs = np.array([])
|
|
es = np.array([])
|
|
ts = np.array([])
|
|
for tile in map_tiles.sprites():
|
|
us = np.append(us, tile.u)
|
|
vs = np.append(vs, tile.v)
|
|
es = np.append(es, tile.elevation)
|
|
ts = np.append(ts, tile.__class__.__name__)
|
|
|
|
np.savez(file=map_file, us=us, vs=vs, es=es, ts=ts)
|
|
|
|
|
|
def main():
|
|
pygame.init()
|
|
# pygame.font.init()
|
|
# font = pygame.font.SysFont("Raleway", 12)
|
|
screen = pygame.display.set_mode(screen_size)
|
|
bg = pygame.Surface(screen_size)
|
|
bg.fill(pygame.Color("#cccccc"))
|
|
clock = pygame.time.Clock()
|
|
|
|
gui_manager = pgg.UIManager(screen_size)
|
|
save_btn = pgg.elements.UIButton(
|
|
relative_rect=pygame.Rect((0, 0), (100, 20)),
|
|
text="Save",
|
|
manager=gui_manager,
|
|
)
|
|
load_btn = pgg.elements.UIButton(
|
|
relative_rect=pygame.Rect((0, 20), (100, 20)),
|
|
text="Load",
|
|
manager=gui_manager,
|
|
)
|
|
|
|
# mouse_prev_x, mouse_prev_y = pygame.mouse.get_pos()
|
|
|
|
# map_text = font.render("test", True, (0, 0, 0))
|
|
map_tiles = pygame.sprite.RenderPlain()
|
|
# for xix in range(10):
|
|
# for yix in range(10):
|
|
# tile = Tiles.IsoMapTile(xix, yix, 0, origin)
|
|
# map_tiles.add(tile)
|
|
|
|
gameit = 0
|
|
is_running = True
|
|
while is_running:
|
|
time_delta = clock.tick(60) / 1000
|
|
|
|
# events
|
|
# mouse_x, mouse_y = pygame.mouse.get_pos()
|
|
|
|
# if pygame.mouse.get_pressed()[1]:
|
|
# map_tiles.update(mouse_prev_x - mouse_x, mouse_prev_y - mouse_y)
|
|
|
|
# mouse_prev_x = mouse_x
|
|
# mouse_prev_y = mouse_y
|
|
for evt in pygame.event.get():
|
|
if evt.type == pygame.QUIT:
|
|
is_running = False
|
|
if evt.type == pygame.KEYDOWN:
|
|
if evt.key == pygame.K_ESCAPE:
|
|
is_running = False
|
|
|
|
if evt.type == pgg.UI_BUTTON_PRESSED:
|
|
if evt.ui_element == save_btn:
|
|
save_map(map_tiles)
|
|
if evt.ui_element == load_btn:
|
|
map_tiles = load_map()
|
|
gui_manager.process_events(evt)
|
|
|
|
gui_manager.update(time_delta)
|
|
|
|
# Drawing
|
|
screen.blit(bg, (0, 0))
|
|
map_tiles.draw(screen)
|
|
|
|
gui_manager.draw_ui(screen)
|
|
pygame.display.flip()
|
|
|
|
gameit += 1
|
|
|
|
pygame.quit()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|