diff options
Diffstat (limited to 'src/display.py')
-rw-r--r-- | src/display.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/display.py b/src/display.py new file mode 100644 index 0000000..23722a5 --- /dev/null +++ b/src/display.py @@ -0,0 +1,90 @@ +import pygame + +import helper + + +def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates): + canvas.fill("red") + + width = screen.get_size()[0] + height = width / (16/9) + + if width > screen.get_size()[0] or height > screen.get_size()[1]: + height = screen.get_size()[1] + width = height * (16/9) + + world_display = pygame.Surface((1280, 720)) + + if need_update_world: + world.fill("gray") + + for block in blocks: + world.blit(helper.draw_texture(helper.get_block_texture(block[2])), (block[0], block[1])) + + need_update_world = False + + world_display.blit(world, (0, 0)) + + if mouse[0] > -1 and mouse[1] > -1 and pygame.mouse.get_focused(): + cursor_x = original_cursor_x = mouse[0] - 42 / 2 + cursor_y = original_cursor_y = mouse[1] - 42 / 2 + cursor_changed = False + + for block in blocks: + if abs(block[0] - mouse[0] + 42 / 2) < 21 and abs(block[1] - mouse[1] + 42 / 2) < 21: + cursor_x = block[0] + cursor_y = block[1] + cursor_changed = True + break + if abs(block[0] - mouse[0] + 42 / 2) < 21 and abs(block[1] - mouse[1] + 42 / 2 + 15) < 21: + cursor_x = block[0] + cursor_y = block[1] + 15 + cursor_changed = True + break + + if cursor_changed: + has_block_below = False + + for i in range(len(blocks) - 1, -1, -1): + if blocks[i][0] == cursor_x and blocks[i][1] == cursor_y: + has_block_below = True + break + + if has_block_below: + world_display.blit(helper.draw_texture(5), (cursor_x, cursor_y)) + else: + world_display.blit(helper.draw_texture(1), (cursor_x, cursor_y)) + + block_coordinates = (cursor_x, cursor_y) + else: + block_coordinates = (-1, -1) + + world_display.blit(helper.draw_texture(2), (original_cursor_x + 42 / 2, original_cursor_y + 42 / 2)) + + x = 1280 / 2 - zoom[0] / 2 + offset[0] + y = 720 / 2 - zoom[1] / 2 + offset[1] + + if x < 0: + x = 0 + offset = (offset[0] - 10, offset[1]) + + if y < 0: + y = 0 + offset = (offset[0], offset[1]) + + if x + zoom[0] > 1280: + x = 0 + offset = (offset[0] - 10, offset[1]) + + if y + zoom[1] > 720: + y = 0 + offset = (offset[0], offset[1]) + + canvas.blit(pygame.transform.scale(world_display.subsurface(x, y, zoom[0], zoom[1]), (1280, 720)), (0, 0)) + + 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)) + + pygame.display.flip() + + return canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates |