def resize(cls, resolution): """Redraw the window with a new resolution. width - the new window width height - the new window height """ Resolution.set_primary(resolution) # Clear the cached surfaces, which will need to be # recreated for the new window size. cls.clear_cache() cls.draw()
def update(self, location): # TODO other arguments for animation, setting correct image etc # TODO scaling ??? scale = ( Resolution.get_primary().block_height ) # TODO only valid while display is square self.rect.x = ( location.col % Resolution.get_primary().blocks_across ) * scale self.rect.y = ( location.row % Resolution.get_primary().blocks_across ) * scale # XXX should be height
def _create_view(cls, view): """Creates and returns an appropriately sized surface for a view. view - the purpose of the surface """ if view == ModeType.MAP: width, height, x, y = Resolution.get_primary().map_area elif view == ModeType.PUZZLE: width, height, x, y = Resolution.get_primary().puzzle_area elif view == ModeType.JOURNAL: width, height, x, y = Resolution.get_primary().journal_area elif view == ModeType.MENU: width, height, x, y = Resolution.get_primary().menu_area return View(view, pygame.Surface((width, height)), (x, y))
def load_texture(cls, block, variant=0): """Load a surface containing the texture for a block, scaled to window. block - the BlockType or Decoration variant - the optional variant of the BlockType, default 0 """ try: return cls.cache[(block, variant)] except KeyError: blockset = loader.load_texture("blockset") texture = pygame.Surface((TEXTURE_RES, TEXTURE_RES), pygame.SRCALPHA) if block == BlockType.AIR and variant == BlockType.AIR: texture.fill(Color.SKY) else: texture.blit( blockset, (0, 0), BlockTexture.texture_location(block, variant), ) bh = Resolution.get_primary().block_height texture = pygame.transform.scale(texture, (bh, bh)) cls.cache[(block, variant)] = texture return texture
def _draw_text(cls, row, text, text_attributes): style = text_attributes.style align = text_attributes.align color = text_attributes.color font = text_attributes._font antialias = True text_surface = font.render(text, antialias, color) item_width, item_height = Resolution.get_primary().menu_item_dim if align == FontAlign.LEFT: x = cls.horizontal_margin elif align == FontAlign.RIGHT: text_width, text_height = text_surface.get_size() x = item_width - 2 * cls.horizontal_margin - text_width elif align == FontAlign.CENTER: text_width, text_height = text_surface.get_size() x = (item_width - text_width) // 2 else: raise ValueError(f"Unknown font align: {align}") y = row * item_height # Draw text cls.view.surface.blit(text_surface, dest=(x, y))
def _draw_shadow(cls): """Return a shadowed version of whole window.""" if cls.shadow is None: width, height, x, y = Resolution.get_primary().map_area tint = pygame.Surface((width, height)) tint.fill(Color.BLACK) tint.set_alpha(200) cls.shadow = pygame.display.get_surface().copy() cls.shadow.blit(tint, (x, y))
def _draw_item_background(cls, row, color=Color.WOOD_LIGHT): item_width, item_height = Resolution.get_primary().menu_item_dim left = cls.horizontal_margin top = row * item_height + cls.vertical_margin width = item_width - 2 * cls.horizontal_margin height = item_height - cls.vertical_margin bg_rect = pygame.Rect(left, top, width, height) pygame.draw.rect(cls.view.surface, color, bg_rect)
def _draw_highlight(cls, row): item_width, item_height = Resolution.get_primary().menu_item_dim top = row * item_height + cls.vertical_margin width = cls.horizontal_margin height = item_height - cls.vertical_margin # Draw background for left in [0, item_width - cls.horizontal_margin]: bg_rect = pygame.Rect(left, top, width, height) pygame.draw.rect(cls.view.surface, (255, 215, 0), bg_rect)
def draw(cls): """Draw the screen from scratch.""" screen = pygame.display.set_mode(Resolution.get_primary().resolution) # Write the caption to the screen. pygame.display.set_caption(constants.TITLE) # Fill the screen with the frame color. screen.fill(Color.WOOD) # Render to the screen. cls.update()
def _load_sprite(self, index): row, col = index # TODO issue with sprite sheet? these should probably be the same as TEXTURE_RES? sprite_width = TEXTURE_RES_SPRITE sprite_height = TEXTURE_RES_SPRITE rect = pygame.Rect( col * sprite_width, row * sprite_height, sprite_width, sprite_height ) image = pygame.Surface(rect.size, pygame.SRCALPHA) image.blit(self.sheet, (0, 0), rect) bh = Resolution.get_primary().block_height image = pygame.transform.scale(image, (bh, bh)) return image
def load_texture(cls, texture): """Load a sprite Utilizes map blocks's height and width (16x16)""" try: return cls.cache[texture] except KeyError: spritesheet = loader.load_texture("sprite") miner = pygame.Surface( (TEXTURE_RES, TEXTURE_RES), pygame.SRCALPHA ) miner.blit( spritesheet, (0, 0), SpriteTexture.texture_location(texture), ) bh = Resolution.get_primary().block_height miner = pygame.transform.scale(miner, (bh, bh)) cls.cache[(texture, variant)] = miner return miner
def draw(cls, fullscreen=False): """Draw the screen from scratch.""" # From https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode # pygame.FULLSCREEN create a fullscreen display # pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL # pygame.HWSURFACE hardware accelerated, only in FULLSCREEN # pygame.OPENGL create an OpenGL-renderable display # pygame.RESIZABLE display window should be sizeable # pygame.NOFRAME display window will have no border or controls # pygame.SCALED resolution depends on desktop size and scale graphics flags = pygame.DOUBLEBUF | pygame.SCALED if fullscreen: flags |= pygame.FULLSCREEN screen = pygame.display.set_mode(Resolution.get_primary().resolution, flags) # Write the caption to the screen. pygame.display.set_caption(constants.TITLE) # Fill the screen with the frame color. screen.fill(Color.WOOD) # Render to the screen. cls.update_view()
def of(cls, col, resolution=None): if resolution is None: resolution = Resolution.get_primary() return Section(math.ceil(col / resolution.blocks_across), resolution)
def __init__(self, section, resolution=None): if resolution is None: resolution = Resolution.get_primary() self.res = resolution self.start_row = (section - 1) * self.res.blocks_across
class FontAttributeDefaults: menu_item_width, menu_item_height = Resolution.get_primary().menu_item_dim MENU = FontAttributes( fontface=Fontfaces.DECORATIVE, size=menu_item_height, )