summaryrefslogtreecommitdiff
path: root/src/game.py
blob: 952006d06821cd66aea56fc575936fd15908bc8a (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
104
105
106
107
108
109
110
111
112
113
114
115
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)