From 5c5883ec33c466c8dab9f4582dc29036d1e99b11 Mon Sep 17 00:00:00 2001 From: kated Date: Tue, 12 May 2026 10:59:35 -0400 Subject: [PATCH] wrote code in asteroid.py, asteroid_spawner.py, spaceship.py, and nav_game.py in order to create an interactive game where the spaceship (player) must avoid incoming asteroids --- .envrc | 1 + __pycache__/asteroid.cpython-311.pyc | Bin 0 -> 1298 bytes __pycache__/asteroid_spawner.cpython-311.pyc | Bin 0 -> 1392 bytes __pycache__/spaceship.cpython-311.pyc | Bin 0 -> 1361 bytes asteroid.py | 20 +++++ asteroid_spawner.py | 16 ++++ nav_game.py | 10 +++ poetry.lock | 81 +++++++++++++++++++ spaceship.py | 21 +++++ 9 files changed, 149 insertions(+) create mode 100644 .envrc create mode 100644 __pycache__/asteroid.cpython-311.pyc create mode 100644 __pycache__/asteroid_spawner.cpython-311.pyc create mode 100644 __pycache__/spaceship.cpython-311.pyc create mode 100644 poetry.lock diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4a96c22 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +source .venv/bin/activate \ No newline at end of file diff --git a/__pycache__/asteroid.cpython-311.pyc b/__pycache__/asteroid.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..24582d54db122a32fbf7b343a4856ae0cc2806d0 GIT binary patch literal 1298 zcmZux%}*0i5Pxq!N}+A26e$Tt+5}^IfI^gbQ4a-uBMCi56um+eAA+T@?obX<={d^C`L05|aj?0#3Bo>ND2FM^NfhT~ zD!oEEh04HHs(`2Jh371raZ{%rp3e7x!BfaGJQu@0jDsCSKi}-*`iry*)`2(R5J?Md1kHIW+hwJ)^wvlHg&s5@+M{3@@9S} zTQzk1PPU{i6V7aIX0;%-smhiYCPas0LTcB#8BcXp{UTxv*bZ_>Qw`-!tO4r+6zI~G z#C7~DjX;g|k%L!-iBB&e;ks0pLD#JxRJ#Uuq0*XP7G81CsxJu#-(IP!j_Sx%u0baQ ztE7WlBE}VR&>h}7Q2URXx*F%ots1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,23 @@ # ------------ # By MWC Contributors # This module defines an asteroid agent class. + +class Asteroid: + character = "0" + + 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..5e576bd 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..2225687 100644 --- a/nav_game.py +++ b/nav_game.py @@ -2,3 +2,13 @@ # ------------ # 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 new file mode 100644 index 0000000..afc5112 --- /dev/null +++ b/poetry.lock @@ -0,0 +1,81 @@ +# This file is automatically @generated by Poetry 2.3.1 and should not be changed by hand. + +[[package]] +name = "ansicon" +version = "1.89.0" +description = "Python wrapper for loading Jason Hood's ANSICON" +optional = false +python-versions = "*" +groups = ["main"] +markers = "platform_system == \"Windows\"" +files = [ + {file = "ansicon-1.89.0-py2.py3-none-any.whl", hash = "sha256:f1def52d17f65c2c9682cf8370c03f541f410c1752d6a14029f97318e4b9dfec"}, + {file = "ansicon-1.89.0.tar.gz", hash = "sha256:e4d039def5768a47e4afec8e89e83ec3ae5a26bf00ad851f914d1240b444d2b1"}, +] + +[[package]] +name = "blessed" +version = "1.27.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 = ">=3.7" +groups = ["main"] +files = [ + {file = "blessed-1.27.0-py3-none-any.whl", hash = "sha256:1c599969acc993bb5842bf3f638b0691e335277a9d9058cd079463a346988714"}, + {file = "blessed-1.27.0.tar.gz", hash = "sha256:e3064559388bd532ab6460d9b6c7d6dd699c4e0cf54d28ed6e2cab12feda13bb"}, +] + +[package.dependencies] +jinxed = {version = ">=1.1.0", markers = "platform_system == \"Windows\""} +wcwidth = ">=0.2.14" + +[package.extras] +docs = ["Pillow", "Sphinx (>3)", "sphinx-paramlinks", "sphinx_rtd_theme", "sphinxcontrib-manpage"] + +[[package]] +name = "jinxed" +version = "1.3.0" +description = "Jinxed Terminal Library" +optional = false +python-versions = "*" +groups = ["main"] +markers = "platform_system == \"Windows\"" +files = [ + {file = "jinxed-1.3.0-py2.py3-none-any.whl", hash = "sha256:b993189f39dc2d7504d802152671535b06d380b26d78070559551cbf92df4fc5"}, + {file = "jinxed-1.3.0.tar.gz", hash = "sha256:1593124b18a41b7a3da3b078471442e51dbad3d77b4d4f2b0c26ab6f7d660dbf"}, +] + +[package.dependencies] +ansicon = {version = "*", markers = "platform_system == \"Windows\""} + +[[package]] +name = "retro-games" +version = "1.1.3" +description = "A simple framework for Terminal-based games" +optional = false +python-versions = "<4.0,>=3.10" +groups = ["main"] +files = [ + {file = "retro_games-1.1.3-py3-none-any.whl", hash = "sha256:4bdd27241b5cb3ee72e69a042d301ff58df2a2ade7e3c29400a538fa54e30148"}, + {file = "retro_games-1.1.3.tar.gz", hash = "sha256:4f91ff725e551820aa4e30c12c0264e2da41967ed34252122b7136bc2a8ed311"}, +] + +[package.dependencies] +blessed = ">=1.20.0,<2.0.0" + +[[package]] +name = "wcwidth" +version = "0.3.5" +description = "Measures the displayed width of unicode strings in a terminal" +optional = false +python-versions = ">=3.8" +groups = ["main"] +files = [ + {file = "wcwidth-0.3.5-py3-none-any.whl", hash = "sha256:b0a0245130566939a24ab8432e625b38272fbc62ecbe5aecbdcb50b8f02ce993"}, + {file = "wcwidth-0.3.5.tar.gz", hash = "sha256:7c3463f312540cf21ddd527ea34f3ae95c057fa191aa7a9e043898d20d636e59"}, +] + +[metadata] +lock-version = "2.1" +python-versions = ">=3.10,<4.0" +content-hash = "03cc38c17964eb2c920ecf014cbfcf966c0c719418a127947b33382f086a0a6e" diff --git a/spaceship.py b/spaceship.py index 9f3552a..1b334c7 100644 --- a/spaceship.py +++ b/spaceship.py @@ -2,3 +2,24 @@ # ------------ # 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()