diff options
author | RaindropsSys <contact@minteck.org> | 2023-09-02 15:32:03 +0200 |
---|---|---|
committer | RaindropsSys <contact@minteck.org> | 2023-09-02 15:32:03 +0200 |
commit | 6a20ab31ef4d9f6bb0e008b9f1b8d3fbeb6bd956 (patch) | |
tree | d425cdef328ed268fb149d6e2b139c9b2c26dbd2 | |
parent | c2f1ed6a85c3895483d36af0f64e2829c3fa3263 (diff) | |
download | blocks-6a20ab31ef4d9f6bb0e008b9f1b8d3fbeb6bd956.tar.gz blocks-6a20ab31ef4d9f6bb0e008b9f1b8d3fbeb6bd956.tar.bz2 blocks-6a20ab31ef4d9f6bb0e008b9f1b8d3fbeb6bd956.zip |
Updated 13 files and added 2 files (automated)
-rw-r--r-- | assets/source/texture_atlas.psd | bin | 194607 -> 106829 bytes | |||
-rw-r--r-- | assets/textures/controls.png | bin | 0 -> 7946 bytes | |||
-rw-r--r-- | assets/textures/texture_atlas.png | bin | 10552 -> 9128 bytes | |||
-rw-r--r-- | main.py | 5 | ||||
-rw-r--r-- | src/audio.py | 28 | ||||
-rw-r--r-- | src/blocks.py | 18 | ||||
-rw-r--r-- | src/constants.py | 2 | ||||
-rw-r--r-- | src/display.py | 160 | ||||
-rw-r--r-- | src/game.py | 140 | ||||
-rw-r--r-- | src/helper.py | 25 | ||||
-rw-r--r-- | src/loader.py | 4 | ||||
-rw-r--r-- | src/menu.py | 13 | ||||
-rw-r--r-- | src/picker.py | 60 | ||||
-rw-r--r-- | src/save.py | 107 | ||||
-rw-r--r-- | src/zoom.py | 26 |
15 files changed, 479 insertions, 109 deletions
diff --git a/assets/source/texture_atlas.psd b/assets/source/texture_atlas.psd Binary files differindex e630156..790f1f9 100644 --- a/assets/source/texture_atlas.psd +++ b/assets/source/texture_atlas.psd diff --git a/assets/textures/controls.png b/assets/textures/controls.png Binary files differnew file mode 100644 index 0000000..7a2b281 --- /dev/null +++ b/assets/textures/controls.png diff --git a/assets/textures/texture_atlas.png b/assets/textures/texture_atlas.png Binary files differindex 156526e..47b2383 100644 --- a/assets/textures/texture_atlas.png +++ b/assets/textures/texture_atlas.png @@ -1,5 +1,5 @@ -from os import environ -environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1' +import os +os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1' import traceback import datetime @@ -53,6 +53,7 @@ while running: clock.tick(25) else: + loader.unload_intro() menu.show(screen) break except Exception as e: diff --git a/src/audio.py b/src/audio.py index baba6da..1d8f725 100644 --- a/src/audio.py +++ b/src/audio.py @@ -1,6 +1,8 @@ import pygame import random +enable_audio = False + pygame.mixer.pre_init(48000, -16, 2, 2048) pygame.mixer.init() pygame.mixer.set_num_channels(3) @@ -55,38 +57,46 @@ sfx = { } def play_intro(): - return + if not enable_audio: + return pygame.mixer.Channel(0).play(intro) def play_music(): - return + if not enable_audio: + return if not pygame.mixer.Channel(1).get_busy(): pygame.mixer.Channel(1).set_volume(0.5) pygame.mixer.Channel(1).play(random.choice(bgm)) def play_menu(force=False): - return + if not enable_audio: + return if not pygame.mixer.Channel(1).get_busy() or force: pygame.mixer.Channel(1).set_volume(0.5) pygame.mixer.Channel(1).play(menu) def stop(channel=1): - return + if not enable_audio: + return pygame.mixer.Channel(channel).stop() def pause_music(): - return + if not enable_audio: + return pygame.mixer.Channel(1).pause() def unpause_music(): - return + if not enable_audio: + return pygame.mixer.Channel(1).unpause() def play_sfx(id): - return + if not enable_audio: + return pygame.mixer.Channel(2).play(random.choice(sfx[id])) def wait_for_sfx(): - return + if not enable_audio: + return while pygame.mixer.Channel(2).get_busy(): - pass
\ No newline at end of file + pass diff --git a/src/blocks.py b/src/blocks.py index 769418b..b534e6b 100644 --- a/src/blocks.py +++ b/src/blocks.py @@ -1,14 +1,26 @@ blocks = { "air": { "texture": 4, - "sounds": ("stone", "stone") + "sounds": ("stone", "stone"), + "placeable": False, + "name": "Air" }, "grass_block": { "texture": 1, - "sounds": ("grass", "grass") + "sounds": ("grass", "grass"), + "placeable": True, + "name": "Grass Block" }, "stone": { "texture": 3, - "sounds": ("stone", "stone") + "sounds": ("stone", "stone"), + "placeable": True, + "name": "Stone" + }, + "bedrock": { + "texture": 7, + "sounds": ("stone", "stone"), + "placeable": False, + "name": "Bedrock" } }
\ No newline at end of file diff --git a/src/constants.py b/src/constants.py index dc7a533..c377593 100644 --- a/src/constants.py +++ b/src/constants.py @@ -1 +1 @@ -VERSION = "0.2a"
\ No newline at end of file +VERSION = "0.3a" diff --git a/src/display.py b/src/display.py index 4ba3c24..536b2f3 100644 --- a/src/display.py +++ b/src/display.py @@ -2,10 +2,22 @@ import pygame import helper import pause +import picker as picker_ui +import time +import save opacity = 0 -def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates, paused, screen_blocks): +def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data): + global v1 + global v2 + global last_v1 + global last_v2 + + blocks = [] # TODO: Figure out what chunks to get and how to get them + + start = time.time() + timings = {} global opacity canvas.fill("red") @@ -17,33 +29,108 @@ def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, width = height * (16/9) world_display = pygame.Surface((1280, 720)) + needed_update_world = need_update_world if need_update_world: screen_blocks = [] world.fill("gray") - for layer in range(len(blocks)): - for height in range(len(blocks[layer]) - 1, -1, -1): - for width in range(len(blocks[layer][height]) - 1, -1, -1): - if blocks[layer][height][width] != "air": - x = offset[0] + 500 + (21 * width) - (21 * height) - y = offset[1] + 500 - (13 * layer) - (14 * width) - (12 * height) - world.blit(helper.draw_texture(helper.get_block_texture(blocks[layer][height][width])), (x, y)) - - for layer in range(len(blocks) - 1, -1, -1): - for height in range(len(blocks[layer]) - 1, -1, -1): - for width in range(len(blocks[layer][height]) - 1, -1, -1): - if blocks[layer][height][width] != "air": - x = offset[0] + 500 + (21 * width) - (21 * height) - y = offset[1] + 500 - (13 * layer) - (14 * width) - (12 * height) - screen_blocks.append((x, y, blocks[layer][height][width], layer, height, width)) - + tstart = time.time() + + chunks_to_display = [] + print(chunks_to_display) + + for x in range(len(loaded_chunks)): + for y in range(len(loaded_chunks[x])): + if loaded_chunks[x][y] is not None: + if (x, y) not in chunks_to_display: + save.save_chunk(save_data, str(x) + "," + str(y) + ".bcr", loaded_chunks[x][y]) + loaded_chunks[x][y] = None + + chunk_x = 0 + + for _ in range(8): + chunk_y = 0 + display_status = [] + + for _ in range(8): + displayed = False + coords = [] + + for layer in range(1): + for height in range(15, -1, -1): + for width in range(15, -1, -1): + coords.append((offset[0] + (chunk_x * 336) - (chunk_y * 333) + 500 + (21 * width) - (21 * height), offset[1] + (chunk_x * 193) + (chunk_y * 222) + 500 - (13 * layer) - (14 * width) - (12 * height))) + + for coord in coords: + if 0 < coord[0] < 1280 and 0 < coord[1] < 720: + displayed = True + + display_status.append(displayed) + if displayed: + chunks_to_display.append((chunk_x, chunk_y)) + chunk_y += 1 + chunk_x += 1 + + print(chunks_to_display) + + for chunk_coords in chunks_to_display: + chunk_x = chunk_coords[0] + chunk_y = chunk_coords[1] + + 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: + blocks = loaded_chunks[chunk_x][chunk_y] + else: + blocks = 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] = blocks + + for layer in range(len(blocks)): + for height in range(len(blocks[layer]) - 1, -1, -1): + if len(blocks[layer][height]) == len(list(filter(lambda i: i == "air", blocks[layer][height]))): + continue + + for width in range(len(blocks[layer][height]) - 1, -1, -1): + if blocks[layer][height][width] != "air": + x = offset[0] + (chunk_x * 336) - (chunk_y * 333) + 500 + (21 * width) - (21 * height) + y = offset[1] + (chunk_x * 193) + (chunk_y * 222) + 500 - (13 * layer) - (14 * width) - (12 * height) + world.blit(helper.draw_texture(helper.get_block_texture(blocks[layer][height][width])), (x, y)) + + for layer in range(len(blocks) - 1, -1, -1): + for height in range(len(blocks[layer]) - 1, -1, -1): + if len(blocks[layer][height]) == len(list(filter(lambda i: i == "air", blocks[layer][height]))): + continue + + for width in range(len(blocks[layer][height]) - 1, -1, -1): + if blocks[layer][height][width] != "air": + x = offset[0] + (chunk_x * 336) - (chunk_y * 333) + 500 + (21 * width) - (21 * height) + y = offset[1] + (chunk_x * 193) + (chunk_y * 222) + 500 - (13 * layer) - (14 * width) - (12 * height) + screen_blocks.append((x, y, blocks[layer][height][width], layer, height + (chunk_x * 16), width + (chunk_y * 16), height, width)) + + timings['draw_map'] = (time.time() - tstart) * 1000 + tstart = time.time() need_update_world = False - print(offset) + tstart = time.time() world_display.blit(world, (0, 0)) + timings['screen_display_world'] = (time.time() - tstart) * 1000 - if mouse[0] > -1 and mouse[1] > -1 and pygame.mouse.get_focused() and not paused: + tstart = time.time() + + if mouse[0] > -1 and mouse[1] > -1 and pygame.mouse.get_focused() and not paused and not picker: cursor_x = original_cursor_x = mouse[0] - 42 / 2 cursor_y = original_cursor_y = mouse[1] - 42 / 2 cursor_changed = False @@ -63,6 +150,9 @@ def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, world_display.blit(helper.draw_texture(2), (original_cursor_x + 42 / 2, original_cursor_y + 42 / 2)) + timings['process_mouse'] = (time.time() - tstart) * 1000 + tstart = time.time() + x = 1280 / 2 - zoom[0] / 2 y = 720 / 2 - zoom[1] / 2 @@ -79,9 +169,29 @@ def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, y = 0 canvas.blit(pygame.transform.scale(world_display.subsurface(x, y, zoom[0], zoom[1]), (1280, 720)), (0, 0)) + timings['screen_display_world_zoomed'] = (time.time() - tstart) * 1000 + tstart = time.time() + + canvas.blit(pygame.transform.scale(helper.draw_control(79), (34, 34)), (10, 10)) + canvas.blit(helper.text("Pause", 20, (255, 255, 255)), (54, 20)) + canvas.blit(pygame.transform.scale(pygame.Surface.convert_alpha(helper.draw_texture(0)), (84, 84)), (1191, 631)) + timings['hud_static'] = (time.time() - tstart) * 1000 + tstart = time.time() + + canvas.blit(pygame.Surface.convert_alpha(helper.draw_texture(helper.get_block_texture(selected_block))), (1228, 10)) + canvas.blit(pygame.transform.scale(helper.draw_control(4), (34, 34)), (1184, 15)) + + timings['hud_picker'] = (time.time() - tstart) * 1000 + tstart = time.time() if paused: - canvas.blit(pygame.Surface.convert_alpha(pause.show()), (0, 0)) + canvas.blit(pause.show(), (0, 0)) + + if picker: + canvas.blit(picker_ui.show(mouse), (0, 0)) + + timings['hud_gui'] = (time.time() - tstart) * 1000 + tstart = time.time() scaled_win = pygame.transform.scale(canvas, (width, height)) @@ -91,8 +201,11 @@ def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, result = helper.transparent(scaled_win, opacity) screen.blit(result, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) + timings['screen_display_final'] = (time.time() - tstart) * 1000 + tstart = time.time() pygame.display.flip() + timings['screen_display_flip'] = (time.time() - tstart) * 1000 if opacity < 1: opacity += 1/30 @@ -100,4 +213,9 @@ def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, if opacity >= 1: opacity = 1 - return canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates, paused, screen_blocks + if needed_update_world: + print((time.time() - start) * 1000) + print(timings) + print("-----------------------") + + return canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data 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) diff --git a/src/helper.py b/src/helper.py index e391212..46f831a 100644 --- a/src/helper.py +++ b/src/helper.py @@ -6,6 +6,7 @@ from pathlib import Path import os texture_map = pygame.image.load('./assets/textures/texture_atlas.png') +controls_map = pygame.image.load('./assets/textures/controls.png') def get_data_path(): path = str(Path.home()) + "/.blocks" @@ -38,7 +39,7 @@ def transparent(surface, percentage): return screen def text(text, size, color): - return pygame.font.Font("./assets/font/main.ttf", size).render(text, False, color) + return pygame.Surface.convert_alpha(pygame.font.Font("./assets/font/main.ttf", size).render(text, False, color)) def get_block_texture(block): return blocks[block]['texture'] @@ -49,4 +50,24 @@ def draw_texture(id): else: pos = (42 * (id - (floor(((id + 1) / 10) - 1) * 10)), 42 * (floor(((id + 1) / 10) - 1))) - return texture_map.subsurface((pos[0], pos[1], 42, 42))
\ No newline at end of file + return texture_map.subsurface((pos[0], pos[1], 42, 42)) + +def draw_control(id): + if id < 10: + pos = (17 * id, 0) + else: + pos = (17 * (id - (floor(((id + 1) / 10) - 1) * 10)), 17 * (floor(((id + 1) / 10) - 1))) + + return pygame.Surface.convert_alpha(controls_map.subsurface((pos[0], pos[1], 17, 17))) + +def get_chunk_regions(chunks): + data = [] + + for x in range(len(chunks)): + for y in range(len(chunks)): + data.append({ + "file": str(x) + "," + str(y) + ".bcr", + "range": (16 * x, 16 * y, 15 + 16 * x, 15 + 16 * y) + }) + + return data
\ No newline at end of file diff --git a/src/loader.py b/src/loader.py index b6eab51..02da9e0 100644 --- a/src/loader.py +++ b/src/loader.py @@ -10,3 +10,7 @@ for i in range(8): def draw_texture(id): return preloaded[id] + +def unload_intro(): + global texture_map + del texture_map
\ No newline at end of file diff --git a/src/menu.py b/src/menu.py index 95147df..1b877ac 100644 --- a/src/menu.py +++ b/src/menu.py @@ -51,6 +51,8 @@ def show(screen): if event.key == pygame.K_ESCAPE: run = False elif event.key == pygame.K_RETURN: + text = text.strip() + for i in ["con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9"]: @@ -90,15 +92,10 @@ def show(screen): screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) pygame.display.flip() - world_map = [[["grass_block" for _ in range(20)] for _ in range(20)]] - - for _ in range(9): - world_map.append([["air" for _ in range(20)] for _ in range(20)]) - audio.wait_for_sfx() import game - game.run(screen, world_map, helper.get_data_path() + "/saves/" + text) + game.run(screen, helper.get_data_path() + "/saves/" + text) return else: @@ -159,6 +156,8 @@ def show(screen): canvas.blit(helper.text("Notice:", 20, (255, 255, 255)), (50, 75 + (25 * (last_i + 2)))) canvas.blit(helper.text("Worlds from Blocks 0.1a are not compatible and", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 3)))) canvas.blit(helper.text("do not show up here.", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 4)))) + canvas.blit(helper.text("Worlds from Blocks 0.2a are not compatible and", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 5)))) + canvas.blit(helper.text("will crash the game.", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 6)))) scaled_win = pygame.transform.scale(canvas, (width, height)) screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) @@ -184,7 +183,7 @@ def show(screen): audio.wait_for_sfx() import game - game.run(screen, loaded['blocks'], helper.get_data_path() + "/saves/" + world_name) + game.run(screen, helper.get_data_path() + "/saves/" + world_name) return else: diff --git a/src/picker.py b/src/picker.py new file mode 100644 index 0000000..d23b8c3 --- /dev/null +++ b/src/picker.py @@ -0,0 +1,60 @@ +import pygame + +import helper +from blocks import blocks +import audio + +def show(mouse): + surface = pygame.Surface((1280, 720)) + surface.fill((0, 0, 0, 128)) + + surface.blit(helper.text("Select a block:", 20, (255, 255, 255)), (50, 50)) + + x = 50 + y = 85 + + for block in list(filter(lambda i: blocks[i]['placeable'], blocks.keys())): + if x <= mouse[0] <= x + 42 and y <= mouse[1] <= y + 42: + surface.fill((255, 255, 255), (x - 5, y - 5, 52, 52)) + + surface.blit(helper.draw_texture(blocks[block]['texture']), (x, y)) + x += 52 + + if x >= 1040: + x = 50 + y += 52 + + + x = 50 + y = 85 + + for block in list(filter(lambda i: blocks[i]['placeable'], blocks.keys())): + if x <= mouse[0] <= x + 42 and y <= mouse[1] <= y + 42: + text = pygame.font.Font("./assets/font/main.ttf", 20).render(blocks[block]['name'], False, (0, 0, 0), (255, 255, 255)) + surface.blit(text, (x - (text.get_width() / 2) + 21, y - text.get_height() - 5)) + + x += 52 + + if x >= 1040: + x = 50 + y += 52 + + return surface + +def click(mouse, screen, picker, selected_block): + x = 50 + y = 85 + + for block in list(filter(lambda i: blocks[i]['placeable'], blocks.keys())): + if x <= mouse[0] <= x + 42 and y <= mouse[1] <= y + 42: + audio.play_sfx("action") + selected_block = block + picker = False + + x += 52 + + if x >= 1040: + x = 50 + y += 52 + + return mouse, screen, picker, selected_block
\ No newline at end of file diff --git a/src/save.py b/src/save.py index 0e8e3d2..fccd4b7 100644 --- a/src/save.py +++ b/src/save.py @@ -4,12 +4,19 @@ import json import constants -def save_world(path, regions): +def save_world(path, chunks): checksums = {} region_list = [] if not os.path.exists(path): os.mkdir(path) + else: + loaded = load_world(path) + + if 'regions' in loaded: + region_list = loaded['regions'] + if 'checksums' in loaded: + checksums = loaded['checksums'] if not os.path.exists(path + "/region"): os.mkdir(path + "/region") @@ -17,11 +24,29 @@ def save_world(path, regions): if not os.path.exists(path + "/playerdata"): os.mkdir(path + "/playerdata") - for i in range(len(regions)): - with open(path + "/region/" + str(i) + ".bcr", "wb") as f: - checksums[str(i) + ".bcr"] = hex(zlib.crc32(str.encode(json.dumps(regions[i])))) - region_list.append(str(i) + ".bcr") - f.write(zlib.compress(str.encode(json.dumps(regions[i])))) + for x in range(len(chunks)): + for y in range(len(chunks[x])): + if y is None: + continue + + print(str(x) + "," + str(y) + ".bcr") + + with open(path + "/region/" + str(x) + "," + str(y) + ".bcr", "wb") as f: + checksums[str(x) + "," + str(y) + ".bcr"] = hex(zlib.crc32(str.encode(json.dumps(chunks[x][y])))) + + chunk_exists = False + + for region in region_list: + if region["file"] == str(x) + "," + str(y) + ".bcr": + chunk_exists = True + + if not chunk_exists: + region_list.append({ + "file": str(x) + "," + str(y) + ".bcr", + "range": (16 * x, 16 * y, 15 + 16 * x, 15 + 16 * y) + }) + + f.write(zlib.compress(str.encode(json.dumps(chunks[x][y])))) with open(path + "/level.dat", "wb") as f: f.write(zlib.compress(str.encode(json.dumps({ @@ -31,24 +56,70 @@ def save_world(path, regions): })))) def load_world(path): - blocks = [] - with open(path + "/level.dat", "rb") as f: data = json.loads(zlib.decompress(f.read())) print(data) - for region in data['regions']: - with open(path + "/region/" + region, "rb") as f: - raw = zlib.decompress(f.read()) - - if hex(zlib.crc32(raw)) != data['checksums'][region]: - raise Exception("Region file " + region + " is corrupted.") - - blocks.append(json.loads(raw)) - + regions = data['regions'] return { "version": data['version'], - "blocks": blocks + "regions": regions } + +def get_chunk(path, coords): + if os.path.exists(path + "/region/" + str(coords[0]) + "," + str(coords[1]) + ".bcr"): + with open(path + "/region/" + str(coords[0]) + "," + str(coords[1]) + ".bcr", "rb") as f: + data = json.loads(zlib.decompress(f.read())) + + if data is None: + return [[["bedrock" for _ in range(16)] for _ in range(16)], [["grass_block" for _ in range(16)] for _ in range(16)]] + else: + return data + else: + return [[["bedrock" for _ in range(16)] for _ in range(16)], [["grass_block" for _ in range(16)] for _ in range(16)]] + +def save_chunk(path, file, data): + checksums = {} + region_list = [] + + if not os.path.exists(path): + os.mkdir(path) + else: + loaded = load_world(path) + + if 'regions' in loaded: + region_list = loaded['regions'] + if 'checksums' in loaded: + checksums = loaded['checksums'] + + if not os.path.exists(path + "/region"): + os.mkdir(path + "/region") + + if not os.path.exists(path + "/playerdata"): + os.mkdir(path + "/playerdata") + + with open(path + "/region/" + file, "wb") as f: + checksums[file] = hex(zlib.crc32(str.encode(json.dumps(data)))) + + chunk_exists = False + + for region in region_list: + if region["file"] == file: + chunk_exists = True + + if not chunk_exists: + region_list.append({ + "file": file, + "range": (16 * int(file.split(",")[0]), 16 * int(file.split(",")[1].split(".")[0]), 15 + 16 * int(file.split(",")[0]), 15 + 16 * int(file.split(",")[1].split(".")[0])) + }) + + f.write(zlib.compress(str.encode(json.dumps(data)))) + + with open(path + "/level.dat", "wb") as f: + f.write(zlib.compress(str.encode(json.dumps({ + "version": constants.VERSION, + "checksums": checksums, + "regions": region_list + })))) diff --git a/src/zoom.py b/src/zoom.py index 1e57d56..d3092e0 100644 --- a/src/zoom.py +++ b/src/zoom.py @@ -1,4 +1,4 @@ -offset_limit = 200 +offset_limit = 600 def zoom_in(zoom): if zoom[0] > 128: @@ -15,22 +15,22 @@ def zoom_reset(): offset = (0, 0) return zoom, offset -def offset_up(offset): - if offset[1] - 10 > -offset_limit: - offset = (offset[0], offset[1] - 10) - return offset - def offset_down(offset): - if offset[1] + 10 < offset_limit: - offset = (offset[0], offset[1] + 10) + if offset[1] - 20 > -offset_limit: + offset = (offset[0], offset[1] - 20) return offset -def offset_left(offset): - if offset[0] - 10 > -(offset_limit * 16/9): - offset = (offset[0] - 10, offset[1]) +def offset_up(offset): + if offset[1] + 20 < offset_limit: + offset = (offset[0], offset[1] + 20) return offset def offset_right(offset): - if offset[0] + 10 < offset_limit * 16/9: - offset = (offset[0] + 10, offset[1]) + if offset[0] - 20 > -(offset_limit * 16/9): + offset = (offset[0] - 20, offset[1]) + return offset + +def offset_left(offset): + if offset[0] + 20 < offset_limit * 16/9: + offset = (offset[0] + 20, offset[1]) return offset
\ No newline at end of file |