refactor to hatch

This commit is contained in:
Waylon Walker 2023-01-03 20:33:45 -06:00
parent 37583a4c5f
commit 00a57bc786
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
33 changed files with 312 additions and 18 deletions

View file

@ -16,7 +16,7 @@ jobs:
with:
python-version: '3.10'
- run: pip install -r requirements.txt
- run: pyinstaller --noconsole creeper.py
- run: pyinstaller --noconsole creeper_adventure/creeper.py
- run: cp -r assets dist/creeper
- name: Step 3 - Use the Upload Artifact GitHub Action
uses: actions/upload-artifact@v2

9
LICENSE.txt Normal file
View file

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023-present Waylon S. Walker <waylon@waylonwalker.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

21
README.md Normal file
View file

@ -0,0 +1,21 @@
# creeper-adventure
[![PyPI - Version](https://img.shields.io/pypi/v/creeper-adventure.svg)](https://pypi.org/project/creeper-adventure)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/creeper-adventure.svg)](https://pypi.org/project/creeper-adventure)
-----
**Table of Contents**
- [Installation](#installation)
- [License](#license)
## Installation
```console
pip install creeper-adventure
```
## License
`creeper-adventure` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

View file

@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT
__version__ = '0.0.1'

View file

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT

View file

@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT
import sys
if __name__ == '__main__':
from .cli import creeper_adventure
sys.exit(creeper_adventure())

View file

Before

Width:  |  Height:  |  Size: 630 B

After

Width:  |  Height:  |  Size: 630 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 530 B

After

Width:  |  Height:  |  Size: 530 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 810 B

After

Width:  |  Height:  |  Size: 810 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 769 B

After

Width:  |  Height:  |  Size: 769 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 769 B

After

Width:  |  Height:  |  Size: 769 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 769 B

After

Width:  |  Height:  |  Size: 769 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 769 B

After

Width:  |  Height:  |  Size: 769 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 776 B

After

Width:  |  Height:  |  Size: 776 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 629 B

After

Width:  |  Height:  |  Size: 629 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 599 B

After

Width:  |  Height:  |  Size: 599 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 813 B

After

Width:  |  Height:  |  Size: 813 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 2 KiB

After

Width:  |  Height:  |  Size: 2 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 979 B

After

Width:  |  Height:  |  Size: 979 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before After
Before After

View file

@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT
import click
from ..__about__ import __version__
from ..creeper import main
@click.group(
context_settings={"help_option_names": ["-h", "--help"]},
invoke_without_command=True,
)
@click.version_option(version=__version__, prog_name="creeper-adventure")
@click.pass_context
@click.option("--debug", is_flag=True, help="start with the debug menu open")
def creeper_adventure(ctx: click.Context, debug):
print(debug)
main(debug)

View file

@ -6,7 +6,9 @@ from pathlib import Path
import pygame
from more_itertools import flatten
from game import Game
from creeper_adventure.game import Game
ASSETS = Path(__file__).parent / "assets"
class MouseSprite:
@ -29,7 +31,7 @@ class MouseSprite:
if self.hotbar.selected.type is None:
pygame.draw.rect(self.surf, (255, 0, 0), self.rect)
else:
self.img = pygame.image.load(f"assets/{self.hotbar.selected.type}.png")
self.img = pygame.image.load(ASSETS / f"{self.hotbar.selected.type}.png")
self.surf.blit(
pygame.transform.scale(self.img, (16, 16)),
self.get_nearest_block_pos(),
@ -169,7 +171,7 @@ class HotBarItem:
self.surf.fill((185, 185, 205, 60))
if self.type:
self.img = pygame.image.load(f"assets/{self.type}.png")
self.img = pygame.image.load(ASSETS / f"{self.type}.png")
self.ui.blit(
pygame.transform.scale(
self.img,
@ -269,7 +271,7 @@ class LightSource:
self.img = img
self.center = center
self.sx, self.sy = center
self.spot = pygame.image.load("assets/spotlight.png")
self.spot = pygame.image.load(ASSETS / "spotlight.png")
class Leaf:
@ -279,7 +281,7 @@ class Leaf:
self.center = center
self.sx, self.sy = center
self.img = pygame.transform.scale(
pygame.image.load("assets/leaf.png"), (4, 4)
pygame.image.load(ASSETS / "leaf.png"), (4, 4)
).convert_alpha()
self.lifespan = lifespan
self.r = random.randint(0, 360)
@ -314,7 +316,7 @@ class Leaf:
class Bee:
def __init__(self):
self.bee = pygame.image.load("assets/bee/idle/1.png")
self.bee = pygame.image.load(ASSETS / "bee/idle/1.png")
self.x = 0
self.y = 0
@ -331,7 +333,7 @@ class Bee:
class Creeper(Game):
def __init__(self):
def __init__(self, debug=False):
super().__init__()
self.inventory = {}
self.day_len = 1000 * 60
@ -339,29 +341,29 @@ class Creeper(Game):
self.foreground = pygame.Surface(self.screen.get_size())
self.build = pygame.Surface(self.screen.get_size())
self.darkness = pygame.Surface(self.screen.get_size()).convert_alpha()
self.axe_sound = pygame.mixer.Sound("assets/sounds/axe.mp3")
self.walking_sound = pygame.mixer.Sound("assets/sounds/walking.mp3")
self.axe_sound = pygame.mixer.Sound(ASSETS / "sounds/axe.mp3")
self.walking_sound = pygame.mixer.Sound(ASSETS / "sounds/walking.mp3")
self.walking_sound_is_playing = False
self.background.fill((0, 255, 247))
self.x, self.y = [i / 2 for i in self.screen.get_size()]
self.spot = pygame.image.load("assets/spotlight.png")
self.spot = pygame.image.load(ASSETS / "spotlight.png")
self.light_power = 1.1
self.leaf = pygame.transform.scale(
pygame.image.load("assets/leaf.png"), (4, 4)
pygame.image.load(ASSETS / "leaf.png"), (4, 4)
).convert_alpha()
self.creepers = cycle(
flatten(
[
repeat(pygame.image.load(img), 5)
for img in Path("assets/stev/idle/").glob("*.png")
for img in Path(ASSETS / "stev/idle/").glob("*.png")
]
)
)
self.tree_imgs = [
pygame.image.load(img) for img in Path("assets/oak_trees/").glob("*.png")
pygame.image.load(img) for img in Path(ASSETS / "oak_trees/").glob("*.png")
]
# self.creeper = pygame.image.load("assets/creeper/idle/1.png")
# self.creeper = pygame.image.load(ASSETS/"creeper/idle/1.png")
self.creeper = next(self.creepers)
self.bee = Bee()
x = 0
@ -402,7 +404,7 @@ class Creeper(Game):
self.main_open_debounce = 1
self.inventory_menu = Menu(self, title="inventory")
self.debug_menu = DebugMenu(self)
self.debug_menu.is_open = True
self.debug_menu.is_open = debug
self.main_menu = Menu(self, title="main menu")
def attack(self):
@ -619,6 +621,10 @@ class Creeper(Game):
self.normal_keys()
if __name__ == "__main__":
creeper = Creeper()
def main(debug=False):
creeper = Creeper(debug=debug)
creeper.run()
if __name__ == "__main__":
main()

View file

@ -8,6 +8,8 @@ class Game:
pygame.display.set_caption(__file__)
self.screen_size = (854, 480)
self.screen_size = (1280, 800)
self.screen_size = (1920, 1080)
self.screen = pygame.display.set_mode(self.screen_size)
self.clock = pygame.time.Clock()

View file

@ -0,0 +1,149 @@
import pygame
pygame.init()
# This is a simple class that will help us print to the screen.
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 25)
def tprint(self, screen, text):
text_bitmap = self.font.render(text, True, (0, 0, 0))
screen.blit(text_bitmap, (self.x, self.y))
self.y += self.line_height
def reset(self):
self.x = 10
self.y = 10
self.line_height = 15
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
def main():
# Set the width and height of the screen (width, height), and name the window.
screen = pygame.display.set_mode((500, 700))
pygame.display.set_caption("Joystick example")
# Used to manage how fast the screen updates.
clock = pygame.time.Clock()
# Get ready to print.
text_print = TextPrint()
# This dict can be left as-is, since pygame will generate a
# pygame.JOYDEVICEADDED event for every joystick connected
# at the start of the program.
joysticks = {}
done = False
while not done:
# Event processing step.
# Possible joystick events: JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN,
# JOYBUTTONUP, JOYHATMOTION, JOYDEVICEADDED, JOYDEVICEREMOVED
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True # Flag that we are done so we exit this loop.
if event.type == pygame.JOYBUTTONDOWN:
print("Joystick button pressed.")
if event.button == 0:
joystick = joysticks[event.instance_id]
if joystick.rumble(0, 0.7, 500):
print(f"Rumble effect played on joystick {event.instance_id}")
if event.type == pygame.JOYBUTTONUP:
print("Joystick button released.")
# Handle hotplugging
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
joysticks[joy.get_instance_id()] = joy
print(f"Joystick {joy.get_instance_id()} connencted")
if event.type == pygame.JOYDEVICEREMOVED:
del joysticks[event.instance_id]
print(f"Joystick {event.instance_id} disconnected")
# Drawing step
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill((255, 255, 255))
text_print.reset()
# Get count of joysticks.
joystick_count = pygame.joystick.get_count()
text_print.tprint(screen, f"Number of joysticks: {joystick_count}")
text_print.indent()
# For each joystick:
for joystick in joysticks.values():
jid = joystick.get_instance_id()
text_print.tprint(screen, f"Joystick {jid}")
text_print.indent()
# Get the name from the OS for the controller/joystick.
name = joystick.get_name()
text_print.tprint(screen, f"Joystick name: {name}")
guid = joystick.get_guid()
text_print.tprint(screen, f"GUID: {guid}")
power_level = joystick.get_power_level()
text_print.tprint(screen, f"Joystick's power level: {power_level}")
# Usually axis run in pairs, up/down for one, and left/right for
# the other. Triggers count as axes.
axes = joystick.get_numaxes()
text_print.tprint(screen, f"Number of axes: {axes}")
text_print.indent()
for i in range(axes):
axis = joystick.get_axis(i)
text_print.tprint(screen, f"Axis {i} value: {axis:>6.3f}")
text_print.unindent()
buttons = joystick.get_numbuttons()
text_print.tprint(screen, f"Number of buttons: {buttons}")
text_print.indent()
for i in range(buttons):
button = joystick.get_button(i)
text_print.tprint(screen, f"Button {i:>2} value: {button}")
text_print.unindent()
hats = joystick.get_numhats()
text_print.tprint(screen, f"Number of hats: {hats}")
text_print.indent()
# Hat position. All or nothing for direction, not a float like
# get_axis(). Position is a tuple of int values (x, y).
for i in range(hats):
hat = joystick.get_hat(i)
text_print.tprint(screen, f"Hat {i} value: {str(hat)}")
text_print.unindent()
text_print.unindent()
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 30 frames per second.
clock.tick(30)
if __name__ == "__main__":
main()
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()

69
pyproject.toml Normal file
View file

@ -0,0 +1,69 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "creeper-adventure"
description = 'a top down 2d adventure game'
readme = "README.md"
requires-python = ">=3.7"
license = "MIT"
keywords = []
authors = [
{ name = "Waylon S. Walker", email = "waylon@waylonwalker.com" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"click",
"pygame",
"more_itertools",
]
dynamic = ["version"]
[project.urls]
Documentation = "https://github.com/unknown/creeper-adventure#readme"
Issues = "https://github.com/unknown/creeper-adventure/issues"
Source = "https://github.com/unknown/creeper-adventure"
[project.scripts]
creeper-adventure = "creeper_adventure.cli:creeper_adventure"
joytest = "creeper_adventure.joytest:main"
[tool.hatch.version]
path = "creeper_adventure/__about__.py"
[tool.hatch.envs.default]
dependencies = [
"pytest",
"pytest-cov",
]
[tool.hatch.envs.default.scripts]
cov = "pytest --cov-report=term-missing --cov-config=pyproject.toml --cov=creeper_adventure --cov=tests {args}"
no-cov = "cov --no-cov {args}"
[[tool.hatch.envs.test.matrix]]
python = ["37", "38", "39", "310", "311"]
[tool.coverage.run]
branch = true
parallel = true
omit = [
"creeper_adventure/__about__.py",
]
[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]

3
tests/__init__.py Normal file
View file

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT