From 2bfaa54f1d70545a925aac74a0759b333fd5346f Mon Sep 17 00:00:00 2001 From: tsmith37 Date: Mon, 1 Dec 2025 16:15:27 -0500 Subject: [PATCH] finished the game The program in unit 1, the turtle would draw whatever you made when it starts, but for the nav_game the objects are already made, and you can move things around after it starts --- __pycache__/asteroid.cpython-313.pyc | Bin 0 -> 1196 bytes __pycache__/asteroid_spawner.cpython-313.pyc | Bin 0 -> 1263 bytes __pycache__/spaceship.cpython-313.pyc | Bin 0 -> 1286 bytes asteroid.py | 21 ++++++++++++ asteroid_spawner.py | 16 +++++++++ nav_game.py | 11 ++++++ poetry.lock | 34 +++++++------------ spaceship.py | 22 ++++++++++++ 8 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 __pycache__/asteroid.cpython-313.pyc create mode 100644 __pycache__/asteroid_spawner.cpython-313.pyc create mode 100644 __pycache__/spaceship.cpython-313.pyc diff --git a/__pycache__/asteroid.cpython-313.pyc b/__pycache__/asteroid.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..24e03b618d03f111828cb3e94b8f8b55cc267bda GIT binary patch literal 1196 zcmZuxL2DCH5PmP&q-m3wv{e#X(NwD?hc+!51hGP)f(l0LQpiDgY?9YxG1*PuzO<$n zJ&8qx_7GZ6){}bizmy_f-bwJ3n;68aGn-AR)qxE2X1+J`y_wnc;_(pzvZ2kHUjl@D z$HPE0-{_^m=#VtILUb}gbeabwiXhcMHdyW-`kMNO-ZU5;@{|aw6A{p<2A?vi zLb?VztcS!6kolcFUB-^eX<=b5ydxyELBA=k1@c_UXhN=$Gm z?403KnQOLHgfSDsjH%H9!?}SAyXGL>m&8qH-o|@ilD~K(XOH(7^rS;p=?X&5NDDYN zuR`qfi+zkvXTyZ(f!jqwj5R`5$;x0Nr?Pk%?iF=CI18LS>=aAs7wCDmw^%f87OF0&1y|5`drbrfG!agv-l<(Y>$Imlkr91gw=4fZnPP4P-7=M zaN>&t#MuIcOK|ah;$31p{yOAF6UWiX!|3F`aTvXPuy7QecE?kDi@S^4%deN*L~{Sh z?!z}P+~oP=R|e7a^_oV@;Ehnn40~xc9hERXl`ceVEsdWe`_b^j$i(`_+jyA zeBM2u-n+khzw_|h^nB;hj|(JrO&|w>vo2&KxUtv**w_)q8g`{sM?1#YddsN$kpyFF zrf_84w76wMc(`0Mgi(f1P~Z&3B~a=DV~#YWS!RZm!Ys8UcNqIj`t`6k0+-uUSlxjo zAg_p<9Dn)Xr$$3lzrwN5LLZI5LKgl3@WORe9%-+^kvn~wcf||@a1ERUs2@Q}>F;x6 Lbm1ofSe~6XAHSJ5GrNQ7=>kLA zGpgaT#@JtU$jBHAgR3ZPGncviKD)<(J0xrOwR<|~4m-_0XRiJUbB$|=KmSQsK&4mPr6{=r_rh5g!YKG78)5Ap`C*&|D6>tARn-V7dnKoNGe9m8;~H zogukhZzBrea0+98XpRdQ0_KtH_#ga@Y@0RsgQ5B;OEIGR1={`}Id*aRZSA&Z;>pz( zrWupE)Gp$w!5)xuf)FD&p~B;}@#QX!P2Ls8R;bn?trRoK)f*aS!C_=gboH;bEIKj} z9V?imhLL$VtcAadQCFsH#tZpCf8;?a5 zlN2HmrJdCPfG!EN1Zv9jec`nN9HOZV8MGfMqvdrFZ6oxPHI-Wn!q!@%tPDs}tV5)} ztuVy^GVvOP1Zks?l=2adu#fs*|$p13R{KeOZ(Q@o%NpO;Im(}4~p}>;`|H$ z;KE|>!s0=3saITjGk5;w+^gfSzJI;Edw2h{#ozg!b7Oz*=F=bhrDMpSIr`}~W`J$qr#T7WZX*k$#Kr7R&LoSYD< zAVeqj(>BQ^A=Xo`J+|0FJPJW3?Jx?W7|nS(89nyz|a5&H48;+^mkg$)}FXm7|YE+!?MN%%XC%g+rX9 n*vh&!D*qcOqaU?gQh9H6&iVTi<7fY5pZ;qUdG#GbHcayYeNY(l literal 0 HcmV?d00001 diff --git a/__pycache__/spaceship.cpython-313.pyc b/__pycache__/spaceship.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bde1bc043d8b5a8635c1aedcd8f4768e84b6f552 GIT binary patch literal 1286 zcmZuxO>YxP5bd6iWE?v`&pMk}JQJ;|Zlv{*_xKd@0Nfc3*YpS~HRZYJtO=oy`h=80j z^8S!c$p7f{#fXhg7K|3jlC#7iqr{;zS}!6W;WWGwmsP*9I|xW+psDDiQrxOr7G99xzcT@{Y)UR#mF!RJmWNh7t+Mfv7(UuH-@%LeUHbe_FW8<#JeW6joMC7Yfb54=)yK z-Vzf$6!n7a-z?u)Qx;=B#2Hh+{J3$xf0l&$G6!m%MC19kHMWtAPL98$?UVMq+5g7c zlUF`uXE%&j!yU*WC8VM+Kd7>ATrVBQ%7+Vv^G_Iy_>ng?3Orfbf`n}K_Q;^hIM6>->ySmtXIhPup^Q9%IxN6|Hv3ivAPZ%1pMruNNN2X5Z$95NHfWSQwl%*w|KaqdkI73>I=l5^^Tp2S$Mj^B z8{7HwUvuZl+h^OU&->2nRE&Q6@KbuCot}s?C*I6$&+VN0l*zX<`DpyN*6q*7vTqi) z7g{&>5@cj@FG*6riwv|D_)S=}6S%EN3vBBUV=MJ?6K=s&im@k6uNFtrj6L>+3~PSC zgF0j41b|hdRP}^c3b_zRv0@Gnk25AiFZ4^y3q#>AHA5~Ldrx{zkt>0F+*P<3@>ftV zNt8Ugel;4&tl#)zXy)8kD`}qTK`?vKhHnd^aI^L+DQmX(!t=t00B)Z|ZufLb>9->p LdgcoO)dhS9?rS63 literal 0 HcmV?d00001 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