import pygame from blocks import blocks as block_list import audio import zoom as zoom_util import display import pause import save def run(screen, blocks, save_data): first = True canvas = pygame.Surface((1280, 720)) running = True zoom = (1280.0, 720.0) offset = (0, 0) mouse = (-1, -1) block_coordinates = (-1, -1) need_update_world = True world = pygame.Surface((1280, 720)) paused = False screen_blocks = [] display.opacity = 0 #for layer in blocks: # print("-- Layer:") # for height in layer: # print(" -", height) save.save_world(save_data, blocks) 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) mouse = pygame.mouse.get_pos() if event.type == pygame.MOUSEBUTTONDOWN: left, middle, right = pygame.mouse.get_pressed() if paused: if left: mouse, screen, paused, keep = pause.click(mouse, screen, paused, True, save_data, blocks) if not keep: return 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]) 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 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]) 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: 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) 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 if event.type == pygame.QUIT: pause.save_and_quit(screen, save_data, blocks) 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)