Skip to content

Commit

Permalink
Preparation for v1.1.0 release; Migrate to Pyxel 1.7.2; Update depend…
Browse files Browse the repository at this point in the history
…encies; Fix new pylint warnings (#8)
  • Loading branch information
kfurtak1024 authored Aug 14, 2022
1 parent 99a21c8 commit 9adcf8a
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 315 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Krzysztof Furtak
Copyright (c) 2020-2022 Krzysztof Furtak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
441 changes: 211 additions & 230 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bansoko/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Main Bansoko module exposing game globals.
"""
__version__ = "1.0.0"
__version__ = "1.1.0"

GAME_FRAME_RATE = 30
GAME_FRAME_TIME_IN_MS = 1_000 / GAME_FRAME_RATE
Expand Down
4 changes: 2 additions & 2 deletions bansoko/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def main() -> None:
filenames = generate_filenames(bundle_name)
configure_logger(filenames.log_file)
logging.info("Initializing Pyxel window")
pyxel.init(width=SCREEN_WIDTH, height=SCREEN_HEIGHT, caption=GAME_TITLE, fps=GAME_FRAME_RATE,
quit_key=pyxel.KEY_F12)
pyxel.init(width=SCREEN_WIDTH, height=SCREEN_HEIGHT, title=GAME_TITLE, fps=GAME_FRAME_RATE,
quit_key=pyxel.KEY_F12, capture_sec=0)
try:
bundle = load_game_resources(filenames)
logging.info("Bundle name: %s", bundle_name)
Expand Down
23 changes: 12 additions & 11 deletions bansoko/game/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def load_bundle(metadata_filename: str) -> Bundle:
:return: bundle with game resources
"""
try:
with open(metadata_filename) as metadata_file:
with open(metadata_filename, encoding="utf-8") as metadata_file:
metadata = load(metadata_file)
validate(metadata, METADATA_JSON_SCHEMA)
sprites = create_sprites(metadata["sprites"])
Expand Down Expand Up @@ -118,7 +118,8 @@ def create_sprites(json_data: Any) -> Dict[str, Sprite]:
image_bank=data["image_bank"],
uv_rect=Rect.from_list(data["uv_rect"]),
directional=data["directional"],
transparency_color=data["transparency_color"],
transparency_color=data["transparency_color"] if data["transparency_color"] >= 0
else None,
num_layers=data["num_layers"],
num_frames=data["num_frames"])
for name, data in json_data.items()
Expand All @@ -134,7 +135,7 @@ def create_sprite_packs(json_data: Any, sprites: Dict[str, Sprite]) -> Dict[str,
"""
return {
name: SpritePack(
sprites=tuple([sprites[sprite_name] for sprite_name in data]))
sprites=tuple(sprites[sprite_name] for sprite_name in data))
for (name, data) in json_data.items()
}

Expand All @@ -147,15 +148,15 @@ def create_gui_consts(json_data: Any, sprites: Dict[str, Sprite]) -> GuiConsts:
:return: Gui constants
"""
return GuiConsts(
gui_positions=tuple([
gui_positions=tuple(
Point.from_list(json_data["positions"][pos.resource_name]) for pos in list(GuiPosition)
]),
gui_colors=tuple([
),
gui_colors=tuple(
int(json_data["colors"][color.resource_name]) for color in list(GuiColor)
]),
gui_sprites=tuple([
),
gui_sprites=tuple(
sprites[json_data["sprites"][sprite.resource_name]] for sprite in list(GuiSprite)
]))
))


def create_screens(json_data: Any, sprites: Dict[str, Sprite]) -> Dict[str, Screen]:
Expand Down Expand Up @@ -216,12 +217,12 @@ def create_level_templates(json_data: Any, sprite_packs: Dict[str, SpritePack])
:param sprite_packs: collection of available sprite packs
:return: collection of level templates
"""
return tuple([
return tuple(
LevelTemplate.from_level_num(
level_num=level_num,
tileset_index=data["tileset"],
draw_offset=Point.from_list(data["draw_offset"]),
sprite_packs=LevelSpritePacks(
robot_sprite_pack=sprite_packs[data["robot_sprite_pack_ref"]],
crate_sprite_pack=sprite_packs[data["crate_sprite_pack_ref"]]))
for level_num, data in enumerate(json_data)])
for level_num, data in enumerate(json_data))
2 changes: 1 addition & 1 deletion bansoko/game/level_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def from_level_num(cls, level_num: int, tileset_index: int, draw_offset: Point,
tilemap = Tilemap(LEVEL_BASE_TILEMAP, tilemap_uv_rect, LEVEL_NUM_LAYERS)
tileset = Tileset(tileset_index)
layers = tuple(
[Layer(i, opaque=(i == 0), global_offset=draw_offset) for i in range(LEVEL_NUM_LAYERS)])
Layer(i, opaque=(i == 0), global_offset=draw_offset) for i in range(LEVEL_NUM_LAYERS))
return cls(level_num=level_num, tilemap=tilemap, tileset=tileset, layers=layers,
sprite_packs=sprite_packs)

Expand Down
2 changes: 1 addition & 1 deletion bansoko/game/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def time(self) -> str:
seconds = 59
minutes = 59

return "{:d}:{:02d}:{:02d}".format(hours, minutes, seconds)
return f"{hours}:{minutes:02d}:{seconds:02d}"

def merge_with(self, level_score: "LevelScore") -> "LevelScore":
"""Merge this level score with given score.
Expand Down
6 changes: 3 additions & 3 deletions bansoko/game/screens/choose_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def __init__(self, screen_factory: ScreenFactory):
bundle = screen_factory.get_bundle()
screen = bundle.get_screen("choose_level")
item_space = bundle.get_gui_consts().get_position(GuiPosition.LEVEL_ITEM_SPACE)
menu = Menu.with_defaults(tuple([
LevelMenuItem(level_num, screen_factory) for level_num in range(bundle.num_levels)
]), MenuLayout(columns=5, rows=4, position=screen.menu_position,
menu = Menu.with_defaults(tuple(
LevelMenuItem(level_num, screen_factory) for level_num in range(bundle.num_levels)),
MenuLayout(columns=5, rows=4, position=screen.menu_position,
item_space=Size(item_space.x, item_space.y)))
super().__init__(menu=menu, allow_going_back=True, screen=screen)
self.select_and_scroll_to_item(screen_factory.get_player_profile().last_played_level)
Expand Down
12 changes: 6 additions & 6 deletions bansoko/game/screens/level_completed.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def _draw_level_statistics(self) -> None:
pushes_beaten = self.level_score.pushes < self.prev_level_score.pushes
steps_beaten = self.level_score.steps < self.prev_level_score.steps
first_completion = not self.prev_level_score.completed
time_record = new_record if time_beaten or first_completion else ""
pushes_record = new_record if pushes_beaten or first_completion else ""
steps_record = new_record if steps_beaten or first_completion else ""

draw_text(LEVEL_TIME_POS, "#0{:>7s} #8{:s}".format(
self.level_score.time, new_record if time_beaten or first_completion else ""))
draw_text(LEVEL_PUSHES_POS, "#0{:>7d} #8{:s}".format(
self.level_score.pushes, new_record if pushes_beaten or first_completion else ""))
draw_text(LEVEL_STEPS_POS, "#0{:>7d} #8{:s}".format(
self.level_score.steps, new_record if steps_beaten or first_completion else ""))
draw_text(LEVEL_TIME_POS, f"#0{self.level_score.time:>7s} #8{time_record}")
draw_text(LEVEL_PUSHES_POS, f"#0{self.level_score.pushes:>7d} #8{pushes_record}")
draw_text(LEVEL_STEPS_POS, f"#0{self.level_score.steps:>7d} #8{steps_record}")
6 changes: 4 additions & 2 deletions bansoko/game/screens/main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def activate(self) -> None:

def draw(self, draw_as_secondary: bool = False) -> None:
super().draw(draw_as_secondary)
draw_text(Point(79, 240), "(c) 2020 KRZYSZTOF FURTAK", TextStyle(color=7, shadow_color=1))
draw_text(Point(11, 240), f"v{__version__}", TextStyle(color=1))
draw_text(
Point(79, 240), "(c) 2020-2022 KRZYSZTOF FURTAK", TextStyle(color=7, shadow_color=1))
draw_text(
Point(11, 240), f"v{__version__}", TextStyle(color=1))

def update(self, dt_in_ms: float) -> Optional[ScreenController]:
if self.exiting:
Expand Down
6 changes: 3 additions & 3 deletions bansoko/game/screens/playfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def _draw_dynamic_cockpit(self) -> None:

def _draw_level_statistics(self) -> None:
score = self.level.level_score
self._draw_digits(GuiPosition.COCKPIT_LEVEL_NUM_POS, "{:>3d}".format(score.level_num),
self._draw_digits(GuiPosition.COCKPIT_LEVEL_NUM_POS, f"{score.level_num:>3d}",
GuiSprite.LEVEL_DIGITS)
self._draw_digits(GuiPosition.COCKPIT_LEVEL_TIME_POS, score.time, GuiSprite.TIME_DIGITS,
colon_size=4)
self._draw_digits(GuiPosition.COCKPIT_LEVEL_STEPS_POS, "{:>4d}".format(score.steps),
self._draw_digits(GuiPosition.COCKPIT_LEVEL_STEPS_POS, f"{score.steps:>4d}",
GuiSprite.STEPS_DIGITS)
self._draw_digits(GuiPosition.COCKPIT_LEVEL_PUSHES_POS, "{:>4d}".format(score.pushes),
self._draw_digits(GuiPosition.COCKPIT_LEVEL_PUSHES_POS, f"{score.pushes:>4d}",
GuiSprite.PUSHES_DIGITS)

def _get_input_action(self) -> Optional[InputAction]:
Expand Down
Binary file modified bansoko/gamedata/main.pyxres
Binary file not shown.
2 changes: 1 addition & 1 deletion bansoko/graphics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,4 @@ def offset(self) -> Point:
@property
def transparency_color(self) -> int:
"""Transparency color for the layer."""
return -1 if self.opaque else 0
return None if self.opaque else 0
2 changes: 1 addition & 1 deletion bansoko/graphics/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Sprite:
image_bank: int
uv_rect: Rect
directional: bool = False
transparency_color: int = -1
transparency_color: int = None
num_layers: int = 1
num_frames: int = 1

Expand Down
11 changes: 7 additions & 4 deletions bansoko/graphics/tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pyxel

from bansoko.graphics import Rect, Direction, Point, Layer, TILE_SIZE
from bansoko.graphics import Rect, Direction, Point, Layer, TILE_SIZE, TILEMAP_WIDTH


@dataclass(frozen=True)
Expand Down Expand Up @@ -62,9 +62,11 @@ def tile_index_at(self, position: TilePosition) -> int:
:param position: tile position to retrieve tile index at
:return: index of tile at given position
"""
tile_index: int = pyxel.tilemap(self.tilemap_id).get(

tile_x, tile_y = pyxel.tilemap(self.tilemap_id).pget(
self.rect_uv.x + position.tile_x, self.rect_uv.y + position.tile_y)
return tile_index

return tile_x + tile_y * TILEMAP_WIDTH // TILE_SIZE

def tiles_positions(self) -> Generator[TilePosition, None, None]:
"""Generator for iterating over all valid tile positions for this tilemap."""
Expand All @@ -80,5 +82,6 @@ def draw(self, layer: Layer) -> None:
return

pyxel.bltm(layer.offset.x, layer.offset.y, self.tilemap_id + layer.layer_index,
self.rect_uv.x, self.rect_uv.y, self.rect_uv.w, self.rect_uv.h,
self.rect_uv.x * TILE_SIZE, self.rect_uv.y * TILE_SIZE,
self.rect_uv.w * TILE_SIZE, self.rect_uv.h * TILE_SIZE,
colkey=layer.transparency_color)
20 changes: 10 additions & 10 deletions bansoko/gui/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ class InputSystem:
KEY_HOLD_TIME: int = 10
KEY_PERIOD_TIME: int = 2
BUTTONS_MAP: Dict[VirtualButton, List[int]] = {
VirtualButton.UP: [pyxel.KEY_UP, pyxel.KEY_KP_8, pyxel.GAMEPAD_1_UP],
VirtualButton.DOWN: [pyxel.KEY_DOWN, pyxel.KEY_KP_2, pyxel.GAMEPAD_1_DOWN],
VirtualButton.LEFT: [pyxel.KEY_LEFT, pyxel.KEY_KP_4, pyxel.GAMEPAD_1_LEFT],
VirtualButton.RIGHT: [pyxel.KEY_RIGHT, pyxel.KEY_KP_6, pyxel.GAMEPAD_1_RIGHT],
VirtualButton.SELECT: [pyxel.KEY_ENTER, pyxel.KEY_KP_ENTER, pyxel.GAMEPAD_1_A],
VirtualButton.BACK: [pyxel.KEY_ESCAPE, pyxel.GAMEPAD_1_B],
VirtualButton.START: [pyxel.KEY_ESCAPE, pyxel.GAMEPAD_1_START],
VirtualButton.ACTION: [pyxel.KEY_Z, pyxel.KEY_BACKSPACE, pyxel.GAMEPAD_1_B],
VirtualButton.UP: [pyxel.KEY_UP, pyxel.KEY_KP_8, pyxel.GAMEPAD1_BUTTON_DPAD_UP],
VirtualButton.DOWN: [pyxel.KEY_DOWN, pyxel.KEY_KP_2, pyxel.GAMEPAD1_BUTTON_DPAD_DOWN],
VirtualButton.LEFT: [pyxel.KEY_LEFT, pyxel.KEY_KP_4, pyxel.GAMEPAD1_BUTTON_DPAD_LEFT],
VirtualButton.RIGHT: [pyxel.KEY_RIGHT, pyxel.KEY_KP_6, pyxel.GAMEPAD1_BUTTON_DPAD_RIGHT],
VirtualButton.SELECT: [pyxel.KEY_RETURN, pyxel.KEY_KP_ENTER, pyxel.GAMEPAD1_BUTTON_A],
VirtualButton.BACK: [pyxel.KEY_ESCAPE, pyxel.GAMEPAD1_BUTTON_B],
VirtualButton.START: [pyxel.KEY_ESCAPE, pyxel.GAMEPAD1_BUTTON_START],
VirtualButton.ACTION: [pyxel.KEY_Z, pyxel.KEY_BACKSPACE, pyxel.GAMEPAD1_BUTTON_B],
VirtualButton.HOME: [pyxel.KEY_HOME],
VirtualButton.END: [pyxel.KEY_END],
VirtualButton.PAGE_UP: [pyxel.KEY_PAGE_UP],
VirtualButton.PAGE_DOWN: [pyxel.KEY_PAGE_DOWN]
VirtualButton.PAGE_UP: [pyxel.KEY_PAGEUP],
VirtualButton.PAGE_DOWN: [pyxel.KEY_PAGEDOWN]
}
WATCHED_KEYS: Set[int] = set(sum(BUTTONS_MAP.values(), []))

Expand Down
4 changes: 2 additions & 2 deletions resbuilder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ def main() -> None:

logging.info("Processing file '%s'...", files.input_filename)
try:
with open(files.input_filename) as input_file, \
open(files.metadata_filename, "w") as metadata_file:
with open(files.input_filename, encoding="utf-8") as input_file, \
open(files.metadata_filename, "w", encoding="utf-8") as metadata_file:
pyxel.init(width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
input_data = json.load(input_file)
validate(input_data, RESOURCES_JSON_SCHEMA)
Expand Down
38 changes: 19 additions & 19 deletions resbuilder/resources/backgrounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import random
from dataclasses import dataclass
from enum import Enum, unique
from typing import Dict, Any
from typing import Dict, Any, Tuple

import pyxel

Expand All @@ -17,7 +17,7 @@ class TilemapGenerator:
Every tile has relative weight used during randomization.
"""
tiles_weights: Dict[int, int]
tiles_weights: Dict[Tuple[int, int], int]

def generate_tilemap(self, tilemap_id: int, tilemap_rect: Rect, seed: int) -> None:
"""Generate a tilemap using random tiles at given position in Pyxel's mega-tilemap.
Expand All @@ -32,12 +32,12 @@ def generate_tilemap(self, tilemap_id: int, tilemap_rect: Rect, seed: int) -> No
random.seed(seed)
tilemap_points = tilemap_rect.inside_points()
for point in tilemap_points:
pyxel.tilemap(tilemap_id).set(point.x, point.y, self._next_tile())
pyxel.tilemap(tilemap_id).pset(point.x, point.y, self._next_tile())
random.setstate(state)

def _next_tile(self) -> int:
def _next_tile(self) -> Tuple[int, int]:
if not self.tiles_weights:
return 0
return 0, 0
return random.choices(list(self.tiles_weights.keys()),
list(self.tiles_weights.values())).pop()

Expand Down Expand Up @@ -84,7 +84,7 @@ class FrameSlice(Enum):
class NineSlicingFrame:
"""NineSlicingFrame is a frame that can be drawn on a tilemap using collection of 9 slicing
tiles."""
slice_tiles: Dict[FrameSlice, int]
slice_tiles: Dict[FrameSlice, Tuple[int, int]]

def draw_frame(self, tilemap_id: int, rect: Rect) -> None:
"""Draw a frame with nine slicing technique using slice tiles.
Expand All @@ -94,25 +94,25 @@ def draw_frame(self, tilemap_id: int, rect: Rect) -> None:
:param rect: rectangle describing drawn frame
"""
tilemap = pyxel.tilemap(tilemap_id)
tilemap.set(rect.left, rect.top, self._get_tile(FrameSlice.TOP_LEFT_TILE))
tilemap.set(rect.right, rect.top, self._get_tile(FrameSlice.TOP_RIGHT_TILE))
tilemap.pset(rect.left, rect.top, self._get_tile(FrameSlice.TOP_LEFT_TILE))
tilemap.pset(rect.right, rect.top, self._get_tile(FrameSlice.TOP_RIGHT_TILE))
if rect.bottom > rect.top:
tilemap.set(rect.left, rect.bottom, self._get_tile(FrameSlice.BOTTOM_LEFT_TILE))
tilemap.set(rect.right, rect.bottom, self._get_tile(FrameSlice.BOTTOM_RIGHT_TILE))
tilemap.pset(rect.left, rect.bottom, self._get_tile(FrameSlice.BOTTOM_LEFT_TILE))
tilemap.pset(rect.right, rect.bottom, self._get_tile(FrameSlice.BOTTOM_RIGHT_TILE))

for x in range(rect.left + 1, rect.right):
tilemap.set(x, rect.top, self._get_tile(FrameSlice.TOP_TILE))
tilemap.pset(x, rect.top, self._get_tile(FrameSlice.TOP_TILE))
for y in range(rect.top + 1, rect.bottom):
tilemap.set(x, y, self._get_tile(FrameSlice.CENTER_TILE))
tilemap.pset(x, y, self._get_tile(FrameSlice.CENTER_TILE))
if rect.bottom > rect.top:
tilemap.set(x, rect.bottom, self._get_tile(FrameSlice.BOTTOM_TILE))
tilemap.pset(x, rect.bottom, self._get_tile(FrameSlice.BOTTOM_TILE))

for y in range(rect.top + 1, rect.bottom):
tilemap.set(rect.left, y, self._get_tile(FrameSlice.LEFT_TILE))
tilemap.set(rect.right, y, self._get_tile(FrameSlice.RIGHT_TILE))
tilemap.pset(rect.left, y, self._get_tile(FrameSlice.LEFT_TILE))
tilemap.pset(rect.right, y, self._get_tile(FrameSlice.RIGHT_TILE))

def _get_tile(self, frame_slice: FrameSlice) -> int:
return self.slice_tiles.get(frame_slice, 0)
def _get_tile(self, frame_slice: FrameSlice) -> Tuple[int, int]:
return self.slice_tiles.get(frame_slice, (0, 0))


def generate_frame_tilesets(input_data: Any, tile_packer: TilePacker) -> Dict[
Expand All @@ -137,5 +137,5 @@ def generate_frame_tilesets(input_data: Any, tile_packer: TilePacker) -> Dict[
return tilesets


def _pack_tile(tile_filename: str, tile_packer: TilePacker) -> int:
return tile_packer.pack_tile(tile_filename) if tile_filename else 0
def _pack_tile(tile_filename: str, tile_packer: TilePacker) -> Tuple[int, int]:
return tile_packer.pack_tile(tile_filename) if tile_filename else (0, 0)
4 changes: 2 additions & 2 deletions resbuilder/resources/gui_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def process_gui_consts(input_data: Any, sprites: Dict[str, Any]) -> Dict[str, An
sprite_name = sprites_data[gui_sprite.resource_name]
if sprites.get(sprite_name) is None:
raise ResourceError(
f"Gui constants refer to unknown sprite '{sprite_name}'")
f"GUI constants refer to unknown sprite '{sprite_name}'")
gui_sprites[gui_sprite.resource_name] = sprite_name

logging.info("Gui constants processed")
logging.info("GUI constants processed")
return {
"positions": gui_positions,
"colors": gui_colors,
Expand Down
Loading

0 comments on commit 9adcf8a

Please sign in to comment.