summaryrefslogtreecommitdiff
path: root/src/display.py
blob: 4ba3c24172352d87b09f4bc8376c0bcc0d4ac822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pygame

import helper
import pause

opacity = 0

def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates, paused, screen_blocks):
    global opacity
    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:
        screen_blocks = []
        world.fill("gray")

        for layer in range(len(blocks)):
            for height in range(len(blocks[layer]) - 1, -1, -1):
                for width in range(len(blocks[layer][height]) - 1, -1, -1):
                    if blocks[layer][height][width] != "air":
                        x = offset[0] + 500 + (21 * width) - (21 * height)
                        y = offset[1] + 500 - (13 * layer) - (14 * width) - (12 * height)
                        world.blit(helper.draw_texture(helper.get_block_texture(blocks[layer][height][width])), (x, y))

        for layer in range(len(blocks) - 1, -1, -1):
            for height in range(len(blocks[layer]) - 1, -1, -1):
                for width in range(len(blocks[layer][height]) - 1, -1, -1):
                    if blocks[layer][height][width] != "air":
                        x = offset[0] + 500 + (21 * width) - (21 * height)
                        y = offset[1] + 500 - (13 * layer) - (14 * width) - (12 * height)
                        screen_blocks.append((x, y, blocks[layer][height][width], layer, height, width))

        need_update_world = False
        print(offset)

    world_display.blit(world, (0, 0))

    if mouse[0] > -1 and mouse[1] > -1 and pygame.mouse.get_focused() and not paused:
        cursor_x = original_cursor_x = mouse[0] - 42 / 2
        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:
                cursor_x = block[0]
                cursor_y = block[1]
                cursor_changed = True
                break

        if cursor_changed:
            world_display.blit(helper.draw_texture(5), (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
    y = 720 / 2 - zoom[1] / 2

    if x < 0:
        x = 0

    if y < 0:
        y = 0

    if x + zoom[0] > 1280:
        x = 0

    if y + zoom[1] > 720:
        y = 0

    canvas.blit(pygame.transform.scale(world_display.subsurface(x, y, zoom[0], zoom[1]), (1280, 720)), (0, 0))

    if paused:
        canvas.blit(pygame.Surface.convert_alpha(pause.show()), (0, 0))

    scaled_win = pygame.transform.scale(canvas, (width, height))

    if opacity >= 1:
        result = scaled_win
    else:
        result = helper.transparent(scaled_win, opacity)

    screen.blit(result, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2))

    pygame.display.flip()

    if opacity < 1:
        opacity += 1/30

    if opacity >= 1:
        opacity = 1

    return canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates, paused, screen_blocks