summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/audio.py10
-rw-r--r--src/game.py11
-rw-r--r--src/helper.py3
-rw-r--r--src/menu.py80
4 files changed, 98 insertions, 6 deletions
diff --git a/src/audio.py b/src/audio.py
index ceb7cad..17ae5ef 100644
--- a/src/audio.py
+++ b/src/audio.py
@@ -6,6 +6,8 @@ pygame.mixer.init()
pygame.mixer.set_num_channels(3)
intro = pygame.mixer.Sound("./assets/sounds/intro.mp3")
+menu = pygame.mixer.Sound("./assets/music/menu.mp3")
+
bgm = [
pygame.mixer.Sound("./assets/music/bgm1.mp3"),
pygame.mixer.Sound("./assets/music/bgm2.mp3"),
@@ -54,5 +56,13 @@ def play_music():
pygame.mixer.Channel(1).set_volume(0.5)
pygame.mixer.Channel(1).play(random.choice(bgm))
+def play_menu(force=False):
+ if not pygame.mixer.Channel(1).get_busy() or force:
+ pygame.mixer.Channel(1).set_volume(0.5)
+ pygame.mixer.Channel(1).play(menu)
+
+def stop(channel=1):
+ pygame.mixer.Channel(channel).stop()
+
def play_sfx(id):
pygame.mixer.Channel(2).play(random.choice(sfx[id])) \ No newline at end of file
diff --git a/src/game.py b/src/game.py
index 209f896..be1659e 100644
--- a/src/game.py
+++ b/src/game.py
@@ -5,7 +5,7 @@ import audio
import zoom as zoom_util
import display
-def run(screen):
+def run(screen, blocks):
canvas = pygame.Surface((1280, 720))
running = True
zoom = (1280.0, 720.0)
@@ -14,11 +14,6 @@ def run(screen):
block_coordinates = (-1, -1)
need_update_world = True
world = pygame.Surface((1280, 720))
- blocks = []
-
- for i in range(20):
- for j in range(20):
- blocks.append((802 + j * 21 - i * 42, 202 + j * 13 + i, "grass_block"))
while running:
audio.play_music()
@@ -58,6 +53,10 @@ def run(screen):
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:
diff --git a/src/helper.py b/src/helper.py
index 0504a58..669ea1b 100644
--- a/src/helper.py
+++ b/src/helper.py
@@ -4,6 +4,9 @@ from math import floor
texture_map = pygame.image.load('./assets/textures/texture_atlas.png')
+def text(text, size, color):
+ return pygame.font.Font("./assets/font/main.ttf", size).render(text, False, color)
+
def get_block_texture(block):
return blocks[block]['texture']
diff --git a/src/menu.py b/src/menu.py
new file mode 100644
index 0000000..d066f51
--- /dev/null
+++ b/src/menu.py
@@ -0,0 +1,80 @@
+import pygame
+import helper
+import audio
+
+def show(screen):
+ running = True
+ audio.play_menu(True)
+
+ while running:
+ for event in pygame.event.get():
+ if event.type == pygame.MOUSEMOTION:
+ pygame.mouse.set_visible(True)
+ if event.type == pygame.MOUSEBUTTONDOWN:
+ left, middle, right = pygame.mouse.get_pressed()
+ mouse = pygame.mouse.get_pos()
+ print(mouse)
+
+ if left:
+ if 45 < mouse[0] < 195 and 40 < mouse[1] < 70:
+ print("New game")
+ screen.fill("black")
+ canvas = pygame.Surface((1280, 720))
+ canvas.fill("black")
+
+ 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)
+
+ canvas.blit(helper.text("Generating world...", 20, (255, 255, 255)), (50, 50))
+
+ 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()
+
+ blocks = []
+
+ for i in range(20):
+ for j in range(20):
+ blocks.append((802 + j * 21 - i * 42, 202 + j * 13 + i, "grass_block"))
+
+ import game
+ audio.stop(1)
+ game.run(screen, blocks)
+ return
+ elif 45 < mouse[0] < 205 and 40+25 < mouse[1] < 70+25:
+ print("Load game")
+ elif 45 < mouse[0] < 125 and 40+25*2 < mouse[1] < 70+25*2:
+ print("Settings")
+ elif 45 < mouse[0] < 90 and 40+25*3 < mouse[1] < 70+25*3:
+ print("Exit")
+ running = False
+
+ if event.type == pygame.QUIT:
+ running = False
+
+ screen.fill("black")
+ canvas = pygame.Surface((1280, 720))
+ 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)
+
+ canvas.blit(helper.text("Start new game", 20, (255, 255, 255)), (50, 50))
+ canvas.blit(helper.text("Load saved game", 20, (255, 255, 255)), (50, 75))
+ canvas.blit(helper.text("Settings", 20, (255, 255, 255)), (50, 100))
+ canvas.blit(helper.text("Exit", 20, (255, 255, 255)), (50, 125))
+
+ 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))
+
+ audio.play_menu()
+ pygame.display.flip() \ No newline at end of file