diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/audio.py | 58 | ||||
-rw-r--r-- | src/blocks.py | 10 | ||||
-rw-r--r-- | src/constants.py | 1 | ||||
-rw-r--r-- | src/display.py | 90 | ||||
-rw-r--r-- | src/game.py | 78 | ||||
-rw-r--r-- | src/helper.py | 16 | ||||
-rw-r--r-- | src/loader.py | 12 | ||||
-rw-r--r-- | src/window.py | 6 | ||||
-rw-r--r-- | src/zoom.py | 30 |
9 files changed, 301 insertions, 0 deletions
diff --git a/src/audio.py b/src/audio.py new file mode 100644 index 0000000..ceb7cad --- /dev/null +++ b/src/audio.py @@ -0,0 +1,58 @@ +import pygame +import random + +pygame.mixer.pre_init(48000, -16, 2, 2048) +pygame.mixer.init() +pygame.mixer.set_num_channels(3) + +intro = pygame.mixer.Sound("./assets/sounds/intro.mp3") +bgm = [ + pygame.mixer.Sound("./assets/music/bgm1.mp3"), + pygame.mixer.Sound("./assets/music/bgm2.mp3"), + pygame.mixer.Sound("./assets/music/bgm3.mp3"), + pygame.mixer.Sound("./assets/music/bgm4.mp3"), + pygame.mixer.Sound("./assets/music/bgm5.mp3") +] + +sfx = { + "glass": [ + pygame.mixer.Sound("./assets/sounds/game/block_glass_1.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_glass_2.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_glass_3.ogg") + ], + "grass": [ + pygame.mixer.Sound("./assets/sounds/game/block_grass_1.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_grass_2.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_grass_3.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_grass_4.ogg") + ], + "stone": [ + pygame.mixer.Sound("./assets/sounds/game/block_stone_1.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_stone_2.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_stone_3.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_stone_4.ogg") + ], + "wood": [ + pygame.mixer.Sound("./assets/sounds/game/block_wood_1.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_wood_2.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_wood_3.ogg"), + pygame.mixer.Sound("./assets/sounds/game/block_wood_4.ogg") + ], + "none": [ + pygame.mixer.Sound("./assets/sounds/game/none_1.ogg"), + pygame.mixer.Sound("./assets/sounds/game/none_2.ogg"), + pygame.mixer.Sound("./assets/sounds/game/none_3.ogg"), + pygame.mixer.Sound("./assets/sounds/game/none_4.ogg") + ] +} + +def play_intro(): + pygame.mixer.Channel(0).play(intro) + +def play_music(): + if not pygame.mixer.Channel(1).get_busy(): + pygame.mixer.Channel(1).set_volume(0.5) + pygame.mixer.Channel(1).play(random.choice(bgm)) + +def play_sfx(id): + pygame.mixer.Channel(2).play(random.choice(sfx[id]))
\ No newline at end of file diff --git a/src/blocks.py b/src/blocks.py new file mode 100644 index 0000000..ffdf461 --- /dev/null +++ b/src/blocks.py @@ -0,0 +1,10 @@ +blocks = { + "grass_block": { + "texture": 0, + "sounds": ("grass", "grass") + }, + "stone": { + "texture": 3, + "sounds": ("stone", "stone") + } +}
\ No newline at end of file diff --git a/src/constants.py b/src/constants.py new file mode 100644 index 0000000..5b11ca9 --- /dev/null +++ b/src/constants.py @@ -0,0 +1 @@ +VERSION = "0.1a"
\ No newline at end of file diff --git a/src/display.py b/src/display.py new file mode 100644 index 0000000..23722a5 --- /dev/null +++ b/src/display.py @@ -0,0 +1,90 @@ +import pygame + +import helper + + +def draw(canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates): + 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: + world.fill("gray") + + for block in blocks: + world.blit(helper.draw_texture(helper.get_block_texture(block[2])), (block[0], block[1])) + + need_update_world = False + + world_display.blit(world, (0, 0)) + + if mouse[0] > -1 and mouse[1] > -1 and pygame.mouse.get_focused(): + cursor_x = original_cursor_x = mouse[0] - 42 / 2 + cursor_y = original_cursor_y = mouse[1] - 42 / 2 + cursor_changed = False + + for block in blocks: + if abs(block[0] - mouse[0] + 42 / 2) < 21 and abs(block[1] - mouse[1] + 42 / 2) < 21: + cursor_x = block[0] + cursor_y = block[1] + cursor_changed = True + break + if abs(block[0] - mouse[0] + 42 / 2) < 21 and abs(block[1] - mouse[1] + 42 / 2 + 15) < 21: + cursor_x = block[0] + cursor_y = block[1] + 15 + cursor_changed = True + break + + if cursor_changed: + has_block_below = False + + for i in range(len(blocks) - 1, -1, -1): + if blocks[i][0] == cursor_x and blocks[i][1] == cursor_y: + has_block_below = True + break + + if has_block_below: + world_display.blit(helper.draw_texture(5), (cursor_x, cursor_y)) + else: + world_display.blit(helper.draw_texture(1), (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 + offset[0] + y = 720 / 2 - zoom[1] / 2 + offset[1] + + if x < 0: + x = 0 + offset = (offset[0] - 10, offset[1]) + + if y < 0: + y = 0 + offset = (offset[0], offset[1]) + + if x + zoom[0] > 1280: + x = 0 + offset = (offset[0] - 10, offset[1]) + + if y + zoom[1] > 720: + y = 0 + offset = (offset[0], offset[1]) + + canvas.blit(pygame.transform.scale(world_display.subsurface(x, y, zoom[0], zoom[1]), (1280, 720)), (0, 0)) + + 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() + + return canvas, screen, need_update_world, world, mouse, blocks, zoom, offset, block_coordinates diff --git a/src/game.py b/src/game.py new file mode 100644 index 0000000..209f896 --- /dev/null +++ b/src/game.py @@ -0,0 +1,78 @@ +import pygame + +from blocks import blocks as block_list +import audio +import zoom as zoom_util +import display + +def run(screen): + 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)) + 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() + 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_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) diff --git a/src/helper.py b/src/helper.py new file mode 100644 index 0000000..0504a58 --- /dev/null +++ b/src/helper.py @@ -0,0 +1,16 @@ +from blocks import blocks +import pygame +from math import floor + +texture_map = pygame.image.load('./assets/textures/texture_atlas.png') + +def get_block_texture(block): + return blocks[block]['texture'] + +def draw_texture(id): + if id < 10: + pos = (42 * id, 0) + else: + pos = (42 * (id - (floor(((id + 1) / 10) - 1) * 10)), 42 * (floor(((id + 1) / 10) - 1))) + + return texture_map.subsurface((pos[0], pos[1], 42, 42))
\ No newline at end of file diff --git a/src/loader.py b/src/loader.py new file mode 100644 index 0000000..b6eab51 --- /dev/null +++ b/src/loader.py @@ -0,0 +1,12 @@ +import pygame +from math import floor + +texture_map = pygame.image.load('./assets/textures/intro_atlas.png') +preloaded = [] + +for i in range(8): + for j in range(8): + preloaded.append(texture_map.subsurface((360 * j, 360 * i, 360, 360))) + +def draw_texture(id): + return preloaded[id] diff --git a/src/window.py b/src/window.py new file mode 100644 index 0000000..fd35f39 --- /dev/null +++ b/src/window.py @@ -0,0 +1,6 @@ +import pygame + +def init(): + pygame.display.set_caption("Blocks") + pygame.mouse.set_visible(False) + pygame.mouse.set_pos((0, 0))
\ No newline at end of file diff --git a/src/zoom.py b/src/zoom.py new file mode 100644 index 0000000..2f993a3 --- /dev/null +++ b/src/zoom.py @@ -0,0 +1,30 @@ +def zoom_in(zoom): + if zoom[0] > 128: + zoom = (zoom[0] / 1.5, zoom[1] / 1.5) + return zoom + +def zoom_out(zoom): + if zoom[0] < 1280: + zoom = (zoom[0] * 1.5, zoom[1] * 1.5) + return zoom + +def zoom_reset(zoom, offset): + zoom = (1280.0, 720.0) + offset = (0, 0) + return zoom, offset + +def offset_up(offset): + offset = (offset[0], offset[1] - 10) + return offset + +def offset_down(offset): + offset = (offset[0], offset[1] + 10) + return offset + +def offset_left(offset): + offset = (offset[0] - 10, offset[1]) + return offset + +def offset_right(offset): + offset = (offset[0] + 10, offset[1]) + return offset
\ No newline at end of file |