import pygame from blocks import blocks as block_list import audio import zoom as zoom_util import display def run(screen): 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)) blocks = [] for i in range(20): for j in range(20): blocks.append((802 + j * 21 - i * 42, 202 + j * 13 + i, "grass_block")) while running: audio.play_music() screen.fill("black") for event in pygame.event.get(): if event.type == pygame.MOUSEMOTION: pygame.mouse.set_visible(False) mouse = pygame.mouse.get_pos() if event.type == pygame.MOUSEBUTTONDOWN: left, middle, right = pygame.mouse.get_pressed() if left: if block_coordinates[0] > -1: block = "stone" can_place = True for i in range(len(blocks) - 1, -1, -1): if blocks[i][0] == block_coordinates[0] and blocks[i][1] == block_coordinates[1]: can_place = False break if can_place: audio.play_sfx(block_list[block]['sounds'][0]) blocks.append((block_coordinates[0], block_coordinates[1], block)) need_update_world = True elif right: for i in range(len(blocks) - 1, -1, -1): if blocks[i][0] == block_coordinates[0] and blocks[i][1] == block_coordinates[1]: if block_list[blocks[i][2]]: audio.play_sfx(block_list[blocks[i][2]]['sounds'][0]) blocks.pop(i) need_update_world = True break if not need_update_world: audio.play_sfx("none") if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_z: offset = zoom_util.offset_up(offset) if event.key == pygame.K_s: offset = zoom_util.offset_down(offset) if event.key == pygame.K_q or event.key == pygame.K_a: offset = zoom_util.offset_left(offset) if event.key == pygame.K_d: offset = zoom_util.offset_right(offset) if event.key == pygame.K_o: zoom = zoom_util.zoom_in(zoom) if event.key == pygame.K_l: zoom = zoom_util.zoom_out(zoom) if event.key == pygame.K_p: zoom, offset = zoom_util.zoom_reset(zoom, offset) if event.type == pygame.QUIT: running = False canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates = display.draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates)