diff options
-rw-r--r-- | src/display.py | 8 | ||||
-rw-r--r-- | src/game.py | 88 | ||||
-rw-r--r-- | src/zoom.py | 14 |
3 files changed, 66 insertions, 44 deletions
diff --git a/src/display.py b/src/display.py index ad4c48b..9cf0392 100644 --- a/src/display.py +++ b/src/display.py @@ -124,6 +124,8 @@ def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, o world_display.blit(world, (0, 0)) timings['screen_display_world'] = (time.time() - tstart) * 1000 + screen_blocks = list(sorted(screen_blocks, key=lambda k: -k[3])) + tstart = time.time() if mouse[0] > -1 and mouse[1] > -1 and pygame.mouse.get_focused() and not paused and not picker: @@ -131,8 +133,10 @@ def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, o cursor_y = original_cursor_y = mouse[1] - 42 / 2 cursor_changed = False - for block in screen_blocks: - if block[0] <= mouse[0] <= block[0] + 42 and block[1] <= mouse[1] <= block[1] + 42: + for i in range(len(screen_blocks)): + block = screen_blocks[i] + + if block[0] <= mouse[0] <= block[0] + 42 and block[1] <= mouse[1] <= block[1] + 42 and block[2] != "bedrock" and block[2] != "air": cursor_x = block[0] cursor_y = block[1] cursor_changed = True diff --git a/src/game.py b/src/game.py index 611d5b1..1eb9581 100644 --- a/src/game.py +++ b/src/game.py @@ -30,6 +30,8 @@ def run(screen, save_data): screen_blocks = [] selected_block = "stone" loaded_chunks = [] + pressed_key = None + last_key_repeat = 5 save.save_world(save_data, loaded_chunks) @@ -102,19 +104,29 @@ def run(screen, save_data): 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" and chunk[screen_blocks[i][3] + 1][screen_blocks[i][6]][screen_blocks[i][7]] != selected_block: + target_x = screen_blocks[i][6] + target_y = screen_blocks[i][3] + 1 + target_z = screen_blocks[i][7] + target_x_changed = False + target_z_changed = False + + if abs(mouse[0] - block_coordinates[0]) <= 12: + target_x = screen_blocks[i][6] + target_y = screen_blocks[i][3] + target_z = screen_blocks[i][7] - 1 + target_z_changed = True + elif abs(mouse[0] - block_coordinates[0]) >= 30: + target_x = screen_blocks[i][6] - 1 + target_y = screen_blocks[i][3] + target_z = screen_blocks[i][7] + target_x_changed = True + + if len(chunk) - 1 >= target_y and len(chunk[target_y]) - 1 >= target_x and len(chunk[target_y][target_x]) - 1 >= target_z and chunk[target_y][target_x][target_z] == "air" and chunk[target_y][target_x][target_z] != selected_block: 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 + chunk[target_y][target_x][target_z] = selected_block need_update_world = True else: - print("Cannot place block at " + str(screen_blocks[i][6]) + ", " + str(screen_blocks[i][3] + 1) + ", " + str(screen_blocks[i][7])) - - if not (len(chunk) - 1 >= screen_blocks[i][3] + 1): - print(" => No layer available") - elif not (chunk[screen_blocks[i][3] + 1][screen_blocks[i][7]][screen_blocks[i][6]] == "air"): - print(" => Above layer obstructed, has " + chunk[screen_blocks[i][3] + 1][screen_blocks[i][7]][screen_blocks[i][6]]) - elif not (chunk[screen_blocks[i][3] + 1][screen_blocks[i][6]][screen_blocks[i][7]] != selected_block): - print(" => No change to make") + print("Cannot place block at " + str(target_x) + ", " + str(target_y) + ", " + str(target_z)) break elif right: @@ -176,38 +188,50 @@ def run(screen, save_data): audio.play_sfx("back") audio.unpause_music() pygame.mouse.set_visible(paused) - if event.key == pygame.K_e: + elif pressed_key == pygame.K_e: picker = not picker if picker: audio.play_sfx("menu") else: audio.play_sfx("back") pygame.mouse.set_visible(paused) - if event.key == pygame.K_w or event.key == pygame.K_z: - offset = zoom_util.offset_up(offset) - need_update_world = True - if event.key == pygame.K_s: - offset = zoom_util.offset_down(offset) - need_update_world = True - if event.key == pygame.K_q or event.key == pygame.K_a: - offset = zoom_util.offset_left(offset) - need_update_world = True - if event.key == pygame.K_d: - offset = zoom_util.offset_right(offset) - need_update_world = True - if event.key == pygame.K_o: - zoom = zoom_util.zoom_in(zoom) - need_update_world = True - if event.key == pygame.K_l: - zoom = zoom_util.zoom_out(zoom) - need_update_world = True - if event.key == pygame.K_p: - zoom, offset = zoom_util.zoom_reset() - need_update_world = True + else: + pressed_key = event.key + if event.type == pygame.KEYUP: + pressed_key = None + last_key_repeat = 5 if event.type == pygame.QUIT: pause.save_and_quit(screen, save_data, loaded_chunks, False) running = False + if pressed_key is not None and last_key_repeat >= 5: + last_key_repeat = 0 + + if pressed_key == pygame.K_w or pressed_key == pygame.K_z: + offset = zoom_util.offset_up(offset) + need_update_world = True + if pressed_key == pygame.K_s: + offset = zoom_util.offset_down(offset) + need_update_world = True + if pressed_key == pygame.K_q or pressed_key == pygame.K_a: + offset = zoom_util.offset_left(offset) + need_update_world = True + if pressed_key == pygame.K_d: + offset = zoom_util.offset_right(offset) + need_update_world = True + if pressed_key == pygame.K_o: + zoom = zoom_util.zoom_in(zoom) + need_update_world = True + if pressed_key == pygame.K_l: + zoom = zoom_util.zoom_out(zoom) + need_update_world = True + if pressed_key == pygame.K_p: + zoom, offset = zoom_util.zoom_reset() + need_update_world = True + + if pressed_key is not None: + last_key_repeat += 1 + canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data, focused, clock = display.draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data, focused, clock) clock.tick(60) diff --git a/src/zoom.py b/src/zoom.py index d3092e0..78b133b 100644 --- a/src/zoom.py +++ b/src/zoom.py @@ -1,5 +1,3 @@ -offset_limit = 600 - def zoom_in(zoom): if zoom[0] > 128: zoom = (zoom[0] / 1.5, zoom[1] / 1.5) @@ -16,21 +14,17 @@ def zoom_reset(): return zoom, offset def offset_down(offset): - if offset[1] - 20 > -offset_limit: - offset = (offset[0], offset[1] - 20) + offset = (offset[0], offset[1] - 20) return offset def offset_up(offset): - if offset[1] + 20 < offset_limit: - offset = (offset[0], offset[1] + 20) + offset = (offset[0], offset[1] + 20) return offset def offset_right(offset): - if offset[0] - 20 > -(offset_limit * 16/9): - offset = (offset[0] - 20, offset[1]) + 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]) + offset = (offset[0] + 20, offset[1]) return offset
\ No newline at end of file |