diff options
Diffstat (limited to 'src/game.py')
-rw-r--r-- | src/game.py | 140 |
1 files changed, 107 insertions, 33 deletions
diff --git a/src/game.py b/src/game.py index 952006d..d91e378 100644 --- a/src/game.py +++ b/src/game.py @@ -3,11 +3,14 @@ import pygame from blocks import blocks as block_list import audio import zoom as zoom_util +import picker as picker_ui import display import pause import save +import time +import helper -def run(screen, blocks, save_data): +def run(screen, save_data): first = True canvas = pygame.Surface((1280, 720)) running = True @@ -18,75 +21,145 @@ def run(screen, blocks, save_data): need_update_world = True world = pygame.Surface((1280, 720)) paused = False + picker = False screen_blocks = [] + selected_block = "stone" + loaded_chunks = [] - display.opacity = 0 + save.save_world(save_data, loaded_chunks) - #for layer in blocks: - # print("-- Layer:") + display.opacity = 0 - # for height in layer: - # print(" -", height) + last = time.time() - save.save_world(save_data, blocks) + if time.time() - last >= 300: + save.save_world(save_data, loaded_chunks) + last = time.time() while running: - if not first: - pygame.event.wait() - - first = True - if not paused: audio.play_music() for event in pygame.event.get(): if event.type == pygame.MOUSEMOTION: - pygame.mouse.set_visible(paused) + pygame.mouse.set_visible(paused or picker) mouse = pygame.mouse.get_pos() if event.type == pygame.MOUSEBUTTONDOWN: left, middle, right = pygame.mouse.get_pressed() + print(pygame.mouse.get_pos()) if paused: if left: - mouse, screen, paused, keep = pause.click(mouse, screen, paused, True, save_data, blocks) + mouse, screen, paused, keep = pause.click(mouse, screen, paused, True, save_data, loaded_chunks) if not keep: return + elif picker: + if left: + mouse, screen, picker, selected_block = picker_ui.click(mouse, screen, picker, selected_block) else: if left: - block = "stone" - for i in range(len(screen_blocks)): if screen_blocks[i][0] == block_coordinates[0] and screen_blocks[i][1] == block_coordinates[1]: - if block_list[screen_blocks[i][2]]: - audio.play_sfx(block_list[screen_blocks[i][2]]['sounds'][0]) + chunk = None - if len(blocks) - 1 >= screen_blocks[i][3] + 1 and blocks[screen_blocks[i][3] + 1][screen_blocks[i][4]][screen_blocks[i][5]] == "air": - audio.play_sfx(block_list[block]['sounds'][0]) - blocks[screen_blocks[i][3] + 1][screen_blocks[i][4]][screen_blocks[i][5]] = "stone" - need_update_world = True + for chunk_metadata in helper.get_chunk_regions(loaded_chunks): + if chunk_metadata['range'][0] <= screen_blocks[i][4] <= chunk_metadata['range'][2] and chunk_metadata['range'][1] <= screen_blocks[i][5] <= chunk_metadata['range'][3]: + chunk_x = int(chunk_metadata['file'].split(",")[0]) + chunk_y = int(chunk_metadata['file'].split(",")[1].split(".")[0]) + + chunk_is_loaded = False + + if len(loaded_chunks) - 1 >= chunk_x: + if len(loaded_chunks[chunk_x]) - 1 >= chunk_y: + if loaded_chunks[chunk_x][chunk_y] is not None: + chunk_is_loaded = True + + if chunk_is_loaded: + chunk = loaded_chunks[chunk_x][chunk_y] + else: + chunk = save.get_chunk(save_data, (chunk_x, chunk_y)) + + while len(loaded_chunks) - 1 <= chunk_x: + loaded_chunks.append([]) + + while len(loaded_chunks[chunk_x]) - 1 <= chunk_y: + loaded_chunks[chunk_x].append(None) + + loaded_chunks[chunk_x][chunk_y] = chunk + break + + if chunk is not None: + if len(chunk) - 1 < screen_blocks[i][3] + 1 < 65: + chunk.append([["air" for _ in range(16)] for _ in range(16)]) + + if len(chunk) - 1 >= screen_blocks[i][3] + 1 and chunk[screen_blocks[i][3] + 1][screen_blocks[i][7]][screen_blocks[i][6]] == "air": + audio.play_sfx(block_list[selected_block]['sounds'][0]) + chunk[screen_blocks[i][3] + 1][screen_blocks[i][6]][screen_blocks[i][7]] = selected_block + need_update_world = True break elif right: for i in range(len(screen_blocks)): - if screen_blocks[i][0] == block_coordinates[0] and screen_blocks[i][1] == block_coordinates[1]: - if block_list[screen_blocks[i][2]]: - audio.play_sfx(block_list[screen_blocks[i][2]]['sounds'][0]) + if screen_blocks[i][0] == block_coordinates[0] and screen_blocks[i][1] == block_coordinates[1] and screen_blocks[i][2] != "bedrock": + chunk = None + + for chunk_metadata in helper.get_chunk_regions(loaded_chunks): + if chunk_metadata['range'][0] <= screen_blocks[i][4] <= chunk_metadata['range'][2] and chunk_metadata['range'][1] <= screen_blocks[i][5] <= chunk_metadata['range'][3]: + chunk_x = int(chunk_metadata['file'].split(",")[0]) + chunk_y = int(chunk_metadata['file'].split(",")[1].split(".")[0]) + + chunk_is_loaded = False + + if len(loaded_chunks) - 1 >= chunk_x: + if len(loaded_chunks[chunk_x]) - 1 >= chunk_y: + if loaded_chunks[chunk_x][chunk_y] is not None: + chunk_is_loaded = True + + if chunk_is_loaded: + chunk = loaded_chunks[chunk_x][chunk_y] + else: + chunk = save.get_chunk(save_data, (chunk_x, chunk_y)) + + while len(loaded_chunks) - 1 <= chunk_x: + loaded_chunks.append([]) + + while len(loaded_chunks[chunk_x]) - 1 <= chunk_y: + loaded_chunks[chunk_x].append(None) + + loaded_chunks[chunk_x][chunk_y] = chunk + break + + if chunk is not None: + if block_list[screen_blocks[i][2]]: + audio.play_sfx(block_list[screen_blocks[i][2]]['sounds'][1]) + + chunk[screen_blocks[i][3]][screen_blocks[i][6]][screen_blocks[i][7]] = "air" + need_update_world = True - blocks[screen_blocks[i][3]][screen_blocks[i][4]][screen_blocks[i][5]] = "air" - need_update_world = True break if not need_update_world: audio.play_sfx("none") if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: - paused = not paused - if paused: + if picker: + audio.play_sfx("back") + picker = False + else: + paused = not paused + if paused: + audio.play_sfx("menu") + audio.pause_music() + else: + audio.play_sfx("back") + audio.unpause_music() + pygame.mouse.set_visible(paused) + if event.key == pygame.K_e: + picker = not picker + if picker: audio.play_sfx("menu") - audio.pause_music() else: audio.play_sfx("back") - audio.unpause_music() pygame.mouse.set_visible(paused) if event.key == pygame.K_w or event.key == pygame.K_z: offset = zoom_util.offset_up(offset) @@ -110,6 +183,7 @@ def run(screen, blocks, save_data): zoom, offset = zoom_util.zoom_reset() need_update_world = True if event.type == pygame.QUIT: - pause.save_and_quit(screen, save_data, blocks) + pause.save_and_quit(screen, save_data, loaded_chunks) running = False - canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates, paused, screen_blocks = display.draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates, paused, screen_blocks) + + canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data = display.draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data) |