summaryrefslogtreecommitdiff
path: root/src/game.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.py')
-rw-r--r--src/game.py102
1 files changed, 70 insertions, 32 deletions
diff --git a/src/game.py b/src/game.py
index be1659e..952006d 100644
--- a/src/game.py
+++ b/src/game.py
@@ -4,8 +4,11 @@ from blocks import blocks as block_list
import audio
import zoom as zoom_util
import display
+import pause
+import save
-def run(screen, blocks):
+def run(screen, blocks, save_data):
+ first = True
canvas = pygame.Surface((1280, 720))
running = True
zoom = (1280.0, 720.0)
@@ -14,64 +17,99 @@ def run(screen, blocks):
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:
- audio.play_music()
- screen.fill("black")
+ 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(False)
+ pygame.mouse.set_visible(paused)
mouse = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
left, middle, right = pygame.mouse.get_pressed()
- if left:
- if block_coordinates[0] > -1:
+ 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"
- 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
+ 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])
- 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")
+ 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:
- import menu
- menu.show(screen)
- return
+ 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(zoom, offset)
+ 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 = display.draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates)
+ 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)