Tinkering

This commit is contained in:
Kayne Ruse 2025-03-13 19:50:34 +11:00
parent 831e7a64c9
commit 4a9806a1b4
11 changed files with 360 additions and 0 deletions

177
.gitignore vendored Normal file
View File

@ -0,0 +1,177 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Ruff stuff:
.ruff_cache/
# PyPI configuration file
.pypirc
#custom
bin/

View File

@ -1,2 +1,12 @@
# Stepwise
## Concept
The "water meter" is always visible, and ticks down each time you take an action. When it hits zero, you die.
## Link
I'm working from this:
https://rogueliketutorials.com/tutorials/tcod/v2/part-2/

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

5
pyvenv.cfg Normal file
View File

@ -0,0 +1,5 @@
home = /usr/bin
include-system-site-packages = false
version = 3.11.2
executable = /usr/bin/python3.11
command = /usr/bin/python -m venv /home/kruse/Repo/Stepwise

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
tcod>=11.13
numpy>=1.18

14
source/actions.py Normal file
View File

@ -0,0 +1,14 @@
class Action: #lawsuit?
pass
class EscapeAction(Action):
pass
class MovementAction(Action):
def __init__(self, dx: int, dy: int):
super().__init__()
self.dx = dx
self.dy = dy

35
source/engine.py Normal file
View File

@ -0,0 +1,35 @@
from typing import Set, Iterable, Any
from tcod.context import Context
from tcod.console import Console
from actions import EscapeAction, MovementAction
from input_events import EventHandler
from entity import Entity
class Engine:
def __init__(self, entities: Set[Entity], event_handler: EventHandler, player: Entity):
self.entities = entities
self.event_handler = event_handler
self.player = player
def handle_events(self, events: Iterable[Any]) -> None:
for event in events:
action = self.event_handler.dispatch(event)
if action is None:
continue
elif isinstance(action, EscapeAction):
raise SystemExit()
elif isinstance(action, MovementAction):
self.player.move(action.dx, action.dy)
def render(self, console: Console, context: Context) -> None:
for entity in self.entities:
console.print(entity.x, entity.y, entity.char, fg=entity.color)
context.present(console)
console.clear()

17
source/entity.py Normal file
View File

@ -0,0 +1,17 @@
from typing import Tuple
class Entity:
"""
A generic object to represent players, enemies, items, etc.
"""
def __init__(self, x: int, y: int, char: str, color: Tuple[int, int, int]):
self.x = x
self.y = y
self.char = char
self.color = color
def move(self, dx: int, dy: int) -> None:
# Move the entity by a given amount
self.x += dx
self.y += dy

31
source/input_events.py Normal file
View File

@ -0,0 +1,31 @@
from typing import Optional
import tcod.event
from actions import Action, EscapeAction, MovementAction
class EventHandler(tcod.event.EventDispatch[Action]):
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit()
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
action: Optional[Action] = None
key = event.sym
# parse input
if key == tcod.event.KeySym.UP:
action = MovementAction(dx=0, dy=-1)
elif key == tcod.event.KeySym.DOWN:
action = MovementAction(dx=0, dy=1)
elif key == tcod.event.KeySym.LEFT:
action = MovementAction(dx=-1, dy=0)
elif key == tcod.event.KeySym.RIGHT:
action = MovementAction(dx=1, dy=0)
elif key == tcod.event.KeySym.ESCAPE:
action = EscapeAction()
# No valid key was pressed
return action

40
source/main.py Executable file
View File

@ -0,0 +1,40 @@
#!./bin/python
import tcod
from engine import Engine
from entity import Entity
from input_events import EventHandler
def main() -> None:
#Initial values
screen_width = 80
screen_height = 50
tileset = tcod.tileset.load_tilesheet("assets/dejavu10x10_gs_tc.png", 32, 8, tcod.tileset.CHARMAP_TCOD)
event_handler = EventHandler()
#entities
player = Entity(screen_width // 2, screen_height // 2, "@", (255, 255, 255))
shopkeeper = Entity(screen_width // 2 - 5, screen_height // 2, "@", (255, 255, 0))
entities = {player, shopkeeper}
engine = Engine(entities, event_handler, player)
with tcod.context.new_terminal(
screen_width,
screen_height,
tileset=tileset,
title="Stepwise Tutorial",
vsync=True,
) as context:
root_console = tcod.console.Console(screen_width, screen_height, order="F")
while True:
engine.render(console = root_console, context = context)
events = tcod.event.wait()
engine.handle_events(events)
if __name__ == "__main__":
main()

29
source/tile_types.py Normal file
View File

@ -0,0 +1,29 @@
from typing import Tuple
import numpy as np #libtcod is built on this
#data types
graphic_dt = np.dtype(
[
("ch", np.int32),
("fg", "3B"),
("bg", "3B")
]
)
tile_dt = np.dtype(
[
("walkable", np.bool), # True if this tile can be walked over.
("transparent", np.bool), # True if this tile doesn't block FOV.
("dark", graphic_dt), # Graphics for when this tile is not in FOV.
]
)
def new_tile(*, walkable: int, transparent: int, dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]], ) -> np.ndarray:
"""Helper function for defining individual tile types """
return np.array((walkable, transparent, dark), dtype=tile_dt)
floor = new_tile(walkable=True, transparent=True, dark=(ord(" "), (255, 255, 255), (50, 50, 150)))
wall = new_tile(walkable=False, transparent=False, dark=(ord(" "), (255, 255, 255), (0, 0, 100)))