summaryrefslogtreecommitdiff
path: root/src/game.py
blob: be1659eecf00099c1267438dd67437ac7aac8c7f (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
import pygame

from blocks import blocks as block_list
import audio
import zoom as zoom_util
import display

def run(screen, blocks):
    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))

    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_ESCAPE:
                    import menu
                    menu.show(screen)
                    return
                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)