diff --git a/__pycache__/asteroid.cpython-313.pyc b/__pycache__/asteroid.cpython-313.pyc new file mode 100644 index 0000000..24e03b6 Binary files /dev/null and b/__pycache__/asteroid.cpython-313.pyc differ diff --git a/__pycache__/asteroid_spawner.cpython-313.pyc b/__pycache__/asteroid_spawner.cpython-313.pyc new file mode 100644 index 0000000..1aefda9 Binary files /dev/null and b/__pycache__/asteroid_spawner.cpython-313.pyc differ diff --git a/__pycache__/spaceship.cpython-313.pyc b/__pycache__/spaceship.cpython-313.pyc new file mode 100644 index 0000000..bde1bc0 Binary files /dev/null and b/__pycache__/spaceship.cpython-313.pyc differ diff --git a/asteroid.py b/asteroid.py index 257354b..a5ce8a5 100644 --- a/asteroid.py +++ b/asteroid.py @@ -2,3 +2,24 @@ # ------------ # By MWC Contributors # This module defines an asteroid agent class. + + +class Asteroid: + character = 'O' + + def __init__(self, position): + self.position = position + + def play_turn(self, game): + width, height = game.board_size + if game.turn_number % 2 == 0: + x, y = self.position + if y == height - 1: + game.remove_agent(self) + else: + ship = game.get_agent_by_name('ship') + new_position = (x, y + 1) + if new_position == ship.position: + game.end() + else: + self.position = new_position \ No newline at end of file diff --git a/asteroid_spawner.py b/asteroid_spawner.py index 92e542d..bcb0562 100644 --- a/asteroid_spawner.py +++ b/asteroid_spawner.py @@ -2,3 +2,19 @@ # ------------------- # By MWC Contributors # This module defines an AsteroidSpawner agent class. + +from random import randint +from asteroid import Asteroid + +class AsteroidSpawner: + display = False + + def play_turn(self, game): + width, height = game.board_size + game.state['score'] += 1 + if self.should_spawn_asteroid(game.turn_number): + asteroid = Asteroid((randint(0, width - 1), 0)) + game.add_agent(asteroid) + + def should_spawn_asteroid(self, turn_number): + return randint(0, 1000) < turn_number \ No newline at end of file diff --git a/nav_game.py b/nav_game.py index 191d83d..e7c8445 100644 --- a/nav_game.py +++ b/nav_game.py @@ -2,3 +2,14 @@ # ------------ # By MWC Contributors # This class implements a simple game where a spaceship avoids asteroids. + + +from retro.game import Game +from spaceship import Spaceship +from asteroid_spawner import AsteroidSpawner + +board_size = (25, 25) +ship = Spaceship(board_size) +spawner = AsteroidSpawner() +game = Game([ship, spawner], {"score": 0}, board_size=board_size) +game.play() \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 3fece54..6ac31c3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.0.0 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.1.4 and should not be changed by hand. [[package]] name = "ansicon" @@ -15,21 +15,23 @@ files = [ [[package]] name = "blessed" -version = "1.20.0" +version = "1.25.0" description = "Easy, practical library for making terminal apps, by providing an elegant, well-documented interface to Colors, Keyboard input, and screen Positioning capabilities." optional = false -python-versions = ">=2.7" +python-versions = ">=3.7" groups = ["main"] files = [ - {file = "blessed-1.20.0-py2.py3-none-any.whl", hash = "sha256:0c542922586a265e699188e52d5f5ac5ec0dd517e5a1041d90d2bbf23f906058"}, - {file = "blessed-1.20.0.tar.gz", hash = "sha256:2cdd67f8746e048f00df47a2880f4d6acbcdb399031b604e34ba8f71d5787680"}, + {file = "blessed-1.25.0-py3-none-any.whl", hash = "sha256:e52b9f778b9e10c30b3f17f6b5f5d2208d1e9b53b270f1d94fc61a243fc4708f"}, + {file = "blessed-1.25.0.tar.gz", hash = "sha256:606aebfea69f85915c7ca6a96eb028e0031d30feccc5688e13fd5cec8277b28d"}, ] [package.dependencies] jinxed = {version = ">=1.1.0", markers = "platform_system == \"Windows\""} -six = ">=1.9.0" wcwidth = ">=0.1.4" +[package.extras] +docs = ["Pillow", "Sphinx (>3)", "sphinx-paramlinks", "sphinx_rtd_theme", "sphinxcontrib-manpage"] + [[package]] name = "jinxed" version = "1.3.0" @@ -61,28 +63,16 @@ files = [ [package.dependencies] blessed = ">=1.20.0,<2.0.0" -[[package]] -name = "six" -version = "1.17.0" -description = "Python 2 and 3 compatibility utilities" -optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" -groups = ["main"] -files = [ - {file = "six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274"}, - {file = "six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81"}, -] - [[package]] name = "wcwidth" -version = "0.2.13" +version = "0.2.14" description = "Measures the displayed width of unicode strings in a terminal" optional = false -python-versions = "*" +python-versions = ">=3.6" groups = ["main"] files = [ - {file = "wcwidth-0.2.13-py2.py3-none-any.whl", hash = "sha256:3da69048e4540d84af32131829ff948f1e022c1c6bdb8d6102117aac784f6859"}, - {file = "wcwidth-0.2.13.tar.gz", hash = "sha256:72ea0c06399eb286d978fdedb6923a9eb47e1c486ce63e9b4e64fc18303972b5"}, + {file = "wcwidth-0.2.14-py2.py3-none-any.whl", hash = "sha256:a7bb560c8aee30f9957e5f9895805edd20602f2d7f720186dfd906e82b4982e1"}, + {file = "wcwidth-0.2.14.tar.gz", hash = "sha256:4d478375d31bc5395a3c55c40ccdf3354688364cd61c4f6adacaa9215d0b3605"}, ] [metadata] diff --git a/spaceship.py b/spaceship.py index 9f3552a..5a45e5f 100644 --- a/spaceship.py +++ b/spaceship.py @@ -2,3 +2,25 @@ # ------------ # By MWC Contributors # This module defines a spaceship agent class. + + +class Spaceship: + name = "ship" + character = '^' + + def __init__(self, board_size): + board_width, board_height = board_size + self.position = (board_width // 2, board_height - 1) + + def handle_keystroke(self, keystroke, game): + x, y = self.position + if keystroke.name in ("KEY_LEFT", "KEY_RIGHT"): + if keystroke.name == "KEY_LEFT": + new_position = (x - 1, y) + else: + new_position = (x + 1, y) + if game.on_board(new_position): + if game.is_empty(new_position): + self.position = new_position + else: + game.end() \ No newline at end of file