예제 #1
0
    def __init__(self):
        self._items = [
            Block(BlockType.dirt),
            Block(BlockType.grass),
            Block(BlockType.stone),
            Block(BlockType.cobblestone),
            Block(BlockType.gravel),
            Block(BlockType.sand),
            Block(BlockType.sandstone),
            Block(BlockType.oak_log),
            Block(BlockType.oak_planks),
        ]

        self._selected_index = 0

        widget_surf = pg.image.load(
            os.path.join(TEXTURES_PATH, "gui", "widgets.png"))

        self._orig_pix_size = HOTBAR_ORIG_PIX_SIZE
        self._empty_surf = pg.Surface(self._orig_pix_size, flags=pg.SRCALPHA)
        self._empty_surf.blit(widget_surf, PIX_ORIGIN + 1,
                              (PIX_ORIGIN, self._orig_pix_size - 2))
        self._filled_surf = self._empty_surf.copy()
        self._selected_filled_surf = self._filled_surf.copy()

        self._pix_size = HOTBAR_PIX_SIZE
        self._surf = pg.Surface(self._pix_size, flags=pg.SRCALPHA)

        self._item_selector_pix_size = PixVec(24, 24)
        self._item_selector_surf = pg.Surface(self._item_selector_pix_size,
                                              flags=pg.SRCALPHA)
        self._item_selector_surf.blit(widget_surf, -PixVec(0, 22))

        self._draw_filled_surf()
        self._draw_surf()
예제 #2
0
 def _mouse_w_pos(self):
     mouse_pix_shift = PixVec(pg.mouse.get_pos())
     mouse_w_shift = pix_to_w_shift(
         mouse_pix_shift,
         PixVec(),
         self._pix_size,
         dest_pivot=self._pix_size * PLAYER_S_POS,
         scale=self._scale,
     )
     mouse_w_pos = mouse_w_shift + self._pos
     return mouse_w_pos
예제 #3
0
def pix_to_w_shift(pix_shift: PixVec,
                   source_surf_pix_size: PixVec,
                   dest_surf_pix_size: PixVec,
                   source_pivot: PixVec = PixVec(),
                   dest_pivot: PixVec = PixVec(),
                   *,
                   scale=BLOCK_PIX_SIZE.x):
    return WVec(
        (pix_shift.x + source_pivot.x - dest_pivot.x) / scale,
        (-pix_shift.y + dest_surf_pix_size.y - source_surf_pix_size.y +
         source_pivot.y - dest_pivot.y) / scale,
    )
예제 #4
0
def w_to_pix_shift(w_shift: WVec,
                   source_surf_pix_size: PixVec,
                   dest_surf_pix_size: PixVec,
                   source_pivot: PixVec = PixVec(),
                   dest_pivot: PixVec = PixVec(),
                   *,
                   scale=BLOCK_PIX_SIZE.x):
    return PixVec(
        w_shift.x * scale - source_pivot.x + dest_pivot.x,
        dest_surf_pix_size.y - w_shift.y * scale - source_surf_pix_size.y +
        source_pivot.y - dest_pivot.y,
    )
예제 #5
0
    def __init__(self):
        self._pos = WVec()
        self._req_pos = WVec(self._pos)

        self._vel = WVec()
        self._req_vel = WVec(self._vel)

        self._zoom_vel = 1.0
        self._req_zoom_vel = 1.0

        self._scale = CAM_DEFAULT_SCALE

        if FULLSCREEN:
            self._screen = pg.display.set_mode((0, 0), pg.FULLSCREEN)
        else:
            self._screen = pg.display.set_mode((1280, 720))
        self._pix_size = PixVec(self._screen.get_size())

        self.selected_block_w_pos = WVec(self._pos)
        self.selected_space_w_pos = WVec(self._pos)
        self._block_selector_surf = pg.image.load(
            os.path.join(GUI_PATH, "block_selector.png")).convert()
        self._block_selector_space_only_surf = pg.image.load(
            os.path.join(GUI_PATH, "block_selector_space_only.png")).convert()

        # Surfs to reuse
        self._world_max_surf_scaled = pg.Surface((0, 0))
        self._player_surf_scaled = pg.Surface((0, 0))
        self._player_surf_scaled.set_colorkey(C_KEY)
        self._block_selector_surf_scaled = pg.Surface((0, 0))
        self._block_selector_surf_scaled.set_colorkey(C_KEY)

        self._clock = pg.time.Clock()
        self._font = pg.font.SysFont(pg.font.get_default_font(), 24)
예제 #6
0
    def __init__(self, dir_path, w_height, neutrals=(), frame_rate=30):
        self.neutrals = neutrals

        self._images = []
        self._images_flipped = []
        images_path = os.path.join(dir_path, "images")
        masks_path = os.path.join(dir_path, "masks")
        for image_file, mask_file in zip(
                sorted(os.scandir(images_path), key=lambda x: x.name),
                sorted(os.scandir(masks_path), key=lambda x: x.name)):
            image = pg.image.load(image_file.path).convert()
            mask = pg.image.load(mask_file.path).convert()
            self._images.append((image, mask))
            self._images_flipped.append((pg.transform.flip(image, True, False),
                                         pg.transform.flip(mask, True, False)))

        self.pix_size = PixVec(*self._images[0][0].get_size())
        pix_to_w_factor = w_height / self.pix_size.y
        self.w_size = WVec(x=self.pix_size.x * pix_to_w_factor, y=w_height)

        self.action = AnimAction.pause
        self._frame = 0
        self._frame_rate = frame_rate
        self.is_flipped = False

        self.surf = pg.Surface(self.pix_size)
        self._light_surf = pg.Surface(self.pix_size)
예제 #7
0
    def draw_block_selector(self, action_w_pos: WVec, world):
        selection: BlockSelection
        selection = self._select_block(action_w_pos, world)
        if selection.block_w_pos is None:
            self.selected_block_w_pos = None
            self.selected_space_w_pos = None
            return

        self.selected_block_w_pos = selection.block_w_pos
        self.selected_space_w_pos = self.selected_block_w_pos + selection.space_w_pos_shift

        surf_pix_size = floor(PixVec(self._scale, self._scale))
        if not selection.space_only:
            surf = self._block_selector_surf
        else:
            surf = self._block_selector_space_only_surf

        if self._block_selector_surf_scaled.get_size() != surf_pix_size:
            self._block_selector_surf_scaled = pg.transform.scale(
                self._block_selector_surf_scaled, surf_pix_size)
        self._block_selector_surf_scaled = pg.transform.scale(
            surf, surf_pix_size, self._block_selector_surf_scaled)
        self._block_selector_surf_scaled = pg.transform.rotate(
            self._block_selector_surf_scaled,
            DIR_TO_ANGLE[selection.space_w_pos_shift])

        w_shift = floor(self.selected_block_w_pos) - self._pos

        if selection.space_only:
            self.selected_block_w_pos = None

        pix_shift = w_to_pix_shift(w_shift,
                                   surf_pix_size,
                                   self._pix_size,
                                   source_pivot=PixVec(-1, 1),
                                   dest_pivot=self._pix_size * PLAYER_S_POS,
                                   scale=self._scale)

        self._screen.blit(self._block_selector_surf_scaled, pix_shift)
예제 #8
0
    def draw_world(self, max_surf, max_view_pos: WVec):
        max_surf_scaled_pix_size = floor(
            PixVec(max_surf.get_size()) * (self._scale / BLOCK_PIX_SIZE))
        if self._world_max_surf_scaled.get_size() != max_surf_scaled_pix_size:
            self._world_max_surf_scaled = pg.transform.scale(
                self._world_max_surf_scaled, max_surf_scaled_pix_size)
        self._world_max_surf_scaled = pg.transform.scale(
            max_surf, max_surf_scaled_pix_size, self._world_max_surf_scaled)

        w_shift = max_view_pos - self._pos
        pix_shift = w_to_pix_shift(w_shift,
                                   max_surf_scaled_pix_size,
                                   self._pix_size,
                                   dest_pivot=self._pix_size * PLAYER_S_POS,
                                   scale=self._scale)

        self._screen.blit(self._world_max_surf_scaled, pix_shift)
예제 #9
0
    def draw_player(self, anim_surf, player_pos: WVec, sky_light):
        anim_surf.draw_and_tick(sky_light)

        surf_scaled_pix_size = floor(anim_surf.w_size * self._scale)
        if self._player_surf_scaled.get_size() != surf_scaled_pix_size:
            self._player_surf_scaled = pg.transform.scale(
                self._player_surf_scaled, surf_scaled_pix_size)
        self._player_surf_scaled = pg.transform.scale(anim_surf.surf,
                                                      surf_scaled_pix_size,
                                                      self._player_surf_scaled)

        w_shift = player_pos - self._pos
        pix_shift = w_to_pix_shift(w_shift,
                                   surf_scaled_pix_size,
                                   self._pix_size,
                                   source_pivot=surf_scaled_pix_size *
                                   PixVec(0.5, 0.0),
                                   dest_pivot=self._pix_size * PLAYER_S_POS,
                                   scale=self._scale)

        self._screen.blit(self._player_surf_scaled, pix_shift)
예제 #10
0
import os

import pygame as pg

from core.classes import PixVec, WVec, WBounds, Dir

# ==== TECHNICAL DIMENSIONS ====
PLAYER_S_POS = PixVec(0.5, 0.333)
HOTBAR_S_POS = PixVec(0.5, 0.1)
HOTBAR_ORIG_PIX_SIZE = PixVec(184, 24)
HOTBAR_PIX_SIZE = HOTBAR_ORIG_PIX_SIZE * 4
BLOCK_PIX_SIZE = PixVec(
    16, 16)  # Should stay equal to block texture size resolution. That is, 16.
CHUNK_W_SIZE = WVec(8, 8)
CHUNK_PIX_SIZE = BLOCK_PIX_SIZE * CHUNK_W_SIZE
WORLD_HEIGHT_BOUNDS = WVec(0, 2**8)
BLOCK_BOUND_SHIFTS = WBounds(WVec(0, 0), WVec(1, 1))

# ==== COLORS ====
C_KEY = pg.Color(255, 0, 0)

C_BLACK = pg.Color(0, 0, 0)
C_WHITE = pg.Color(255, 255, 255)
C_SKY = pg.Color(120, 190, 225)

# ==== CAM ====
CAM_FPS = 60
CAM_DEFAULT_SCALE = 64.0
CAM_SCALE_BOUNDS = (16.0, 128.0)

# ==== GAME DYNAMICS ====
예제 #11
0
 def _index_to_pix_shift(index):
     return PixVec(index * (4 + BLOCK_PIX_SIZE.x), 0)
예제 #12
0
 def _chunk_w_pos_to_pix_shift(self, chunk_w_pos: WVec):
     max_view_w_shift = chunk_w_pos - self._max_view.min
     return w_to_pix_shift(max_view_w_shift, CHUNK_PIX_SIZE,
                           PixVec(self._max_surf.get_size()))