diff options
Diffstat (limited to 'src/game.py')
-rw-r--r-- | src/game.py | 88 |
1 files changed, 56 insertions, 32 deletions
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) |