From c9b37ea99525cedfd171085a8a7e6758a7970d07 Mon Sep 17 00:00:00 2001 From: kathrynoh23 <126970932+kathrynoh23@users.noreply.github.com> Date: Mon, 27 May 2024 18:00:48 -0400 Subject: [PATCH] Kathryn Odell-Hamilton May27, 2024 unit3/lab_retro The lab_retro was straight forward with the instructions and code. I made a few typos while coding that threw errors, but that was good for me to go back and review the code. You also found an error with the file name and too many of one file. That was good to see. I did realize that the Terminal window has to be larger so that the game can be viewed and played, otherwise you'll get an error, "TerminalTooSmall." I understand the concept of setting up class' and agents for the game. I've already watched your "Beast" game video to help each of us to get started with creating our Retro game. --- .DS_Store | Bin 0 -> 6148 bytes __pycache__/asteroid.cpython-311.pyc | Bin 0 -> 1306 bytes __pycache__/asteroid_spawner.cpython-311.pyc | Bin 0 -> 1574 bytes __pycache__/spaceship.cpython-311.pyc | Bin 0 -> 1361 bytes asteroid.py | 19 +++++ asteroid_spawner.py | 19 +++++ game.py | 2 +- nav_game.py | 11 +++ nav_game_initial_code.py | 11 +++ poetry.lock | 71 +++++++++++-------- spaceship.py | 21 ++++++ 11 files changed, 123 insertions(+), 31 deletions(-) create mode 100644 .DS_Store 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 nav_game_initial_code.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..38734ca2de71d90578b12a191d5ff30a57f26d5c GIT binary patch literal 6148 zcmeHKJ8Hu~5S@u#2;8`IxmU;y7U7)02atb(6aoc8igc=cE+5TrJ{W}TCXgn)ftj~E znx|!7q0xwlw%_Mhk+q04a6`FRn43K}pV>=h6bQ#VPI7>M$h&m2>c}_IdtuEgkdX>d0V;4;z`hR!Zden?K>u_g_y_=8 zBJGB?&l13531Cee1CfDgP=P_!95FQL$d|0EiDO{UMRWMjJXv!>Q9m8;FJ3NM0~x6R z6__fpi0#_?{~P?t{68geM+KA!1r*gxx&q`b_#;GW1zQV hY^)t`yeR65t?|4jj)6``-swR8445u7D)83|+yF)|6_)@2 literal 0 HcmV?d00001 diff --git a/__pycache__/asteroid.cpython-311.pyc b/__pycache__/asteroid.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d24fbe624e4db664479ef0b8d0f7c837af037aa4 GIT binary patch literal 1306 zcmZux&1(}u6rb7Mq-m3q)D)vuYzmQ*LrsGgFMd!cSWE3iyaZv`W~Ry7WH+7NSQ9qT zgNFznq7>SLhX~SE`Um)Lw4u;tPlBi3BK71!e6yQGN_~@kJM%t%@6CH}vp+gIf`DH9 zSzh}lA@oBW`lR$W=SkS?APGG|6um_h?}6nZJU-S z7^Y@vhMtyO#bl*59t0Cpcqfy;7c(zR#?8#SY881+&nz%=-7+efvbwJ61+uAGMUpot z%ak|s*-TZ}th|aR!IlluvO(c(W~WE z#=Ta%BddnWDKWJ=b0eIUjn|B*1*Ti>Sbc!#5EA^x#WgqL zgA|!oC|W!!)DfL+lKym=!XxPu7yWjJD>|7kH=G;XzD1Ma$@7Bv8hgM zs+~HHO`pW3kNUswiHXz1OeZmOuy&l7J4wv76el^=&bBu`&+XP*5hp&`zW3!}Cq8== zpLNEs?JRy={IImW)biH%)NE^E`&kQCH-RFzAtNsS3s#`MFz2FBgt#Mwlnq)fiG75S zjjCGeX@>Jfm8*I9Zuu~{@f*OtA`il=4Hu*5F);_`ePCOI<;3D!PrC{ZOm)$K1tWoM z7hPsPrhtQQ-G`kN*dkjm)bG#(KgmZt2{JJ%Uc9c1G476B!2o-i{S>la|513re*yHI BEl~gf literal 0 HcmV?d00001 diff --git a/__pycache__/asteroid_spawner.cpython-311.pyc b/__pycache__/asteroid_spawner.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..41922fa6d5c62fe523e96f4ac7a5cd99e61e4072 GIT binary patch literal 1574 zcmZuxL2DaF6rP#gmDiGNC6q)?L~GgD4QdmM5QuXyp@!7yp@@L#MYIfS&Dz>n+O=m^ zbu8Azha8*`XnL}bzBEn9ALy|^K?(~I3xPl`%}v3DLQZ`%s}+%x(d?TyZ{B?K=6!GU z=fp%BLA&(dkL$k^gx<Sm#=xLb@&=Sc6yUos#*N{7 ztqydj|BKQmF)R=3{H8<@?2G4Nv}kPsCnFeC!^vr(9#%~e9O0^bE59G8Z1sa zO*n`$ev7-*Z8g_y&Sj8Xvdm@p(YVDJwKi<1VvWm}6X_dP(~it!w~)Dog5ZT&_lCuwB0p}@vLhWdYRm-%>2vD{4oo^zTV9& zhMC1aBIEO~^V5IipPhYnE4;k)*LL^vcioxgaAx`MtDWNa-TX?JUpe@pmp$`aydvm$j>e35r+&u|NFv zCcoIf-PegR1F1`dsT+{Vp~zxmp^rWl@evZeQZk~H{8#jvagE8h7nhtmyeOxV6&272 yb1A;fO+E`YNrw0ZL|?-g_tOX$I_ToZy@$T+jP|}hi6N#>#d~S{_cIIX`2PX#OK9T& literal 0 HcmV?d00001 diff --git a/__pycache__/spaceship.cpython-311.pyc b/__pycache__/spaceship.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f7f748c80d2146a8009ddb1bf5a530d224d78cc3 GIT binary patch literal 1361 zcmZuw&2JM&6rb6Tu>Pt8j?{pPa3HF*M6y9haA<;r6iNa#hXxK?ZSyx1zFNE(q%JNh<|)29GnHF zk0`2O3socwS7b}7D3<&bRa8p>I%cWhJ;qbZO~a&a)oyYnr1+7U!~hV;E<6XN5Yfjg zVC_g9*$31%@ID~7;Wew0j|kj}mW4!aOZx(9Uu30Yt~DF3?b!`SS0EN$U*+lh4TD+4 zwcC_uB7W1hyeiK|LY3NURnHX_DZ=`+Sib90=9X)QS7j}yJWt)4*JzgOM$LBC$foU8 ziP^AdxxQ&$EN?ircd5K?+$W5BtWkF3mzJ6>t`TB`osjm4qynW_mx6%q_dvE#FkKpI zQ{7B&B{(_#3J*`}uV(+68qWN%H#yr?wsT$B2XGUaqEhins`e2yedMDDu?2~)-abOi{EtSegaz1pbim)g3_JO(L+%aP!Ly z6Ne)=Q@;9jC&4$K%EV7FJ8*Ur4Td>gPL8fD=@Qp&UB5?eU;ptAPYLel#TyHE*a>i9 z8W3G&qSRb*j5_67!y#ecxn{eB)|+07%ha*-F($qio~qH73yrK%uB-_M{Fvhw&pLFI z#G~bo!A&tM#UlGgIE_~g$6BW(@ok^_x8Gmea^T_q4g~t1FKl<7DP7#X7-S~4ukM|` zyq6KbAV0bNWN_-A{LEf{CMZq~uKl479=>e%vc2rvFGWE3wew5o<;pOB{!RY;2r0QU zLE+o}-1E7?cf&&IO`$YG^6@jl^o8F1i=TS1-%d>S7oIQtcJsx}UbsgEl)VB$M`>^l z5!g7Sx1=8v11zdW2u~AIZ&(}fNW!y(JlrtWBMX}l6-b#?gBhkr8OwvrP6K(3LY%Nf zOpHZHRAAg$AX~`}GRL>(gYm-F;z*Iyxe-c;mQf+2qa*?rm`gg2u+NGBBJVKXCv7%L g_bNLFIx(W|6(A!SV?0V1Fu)!q?}hHe{}@8x&nZkrZU6uP literal 0 HcmV?d00001 diff --git a/asteroid.py b/asteroid.py index 257354b..6249197 100644 --- a/asteroid.py +++ b/asteroid.py @@ -2,3 +2,22 @@ # ------------ # 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): + if game.turn_number % 2 == 0: + x, y = self.position + board_height = game.board_size[1] + if y == board_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 diff --git a/asteroid_spawner.py b/asteroid_spawner.py index 92e542d..033fd41 100644 --- a/asteroid_spawner.py +++ b/asteroid_spawner.py @@ -2,3 +2,22 @@ # ------------------- # By MWC Contributors # This module defines an AsteroidSpawner agent class. +from random import randint +from asteroid import Asteroid + +class AsteroidSpawner: + display = False + + def __init__(self, board_size): + width, height = board_size + self.board_width = width + + def play_turn(self, game): + game.state['score'] += 1 + if self.should_spawn_asteroid(game.turn_number): + asteroid = Asteroid((randint(0, self.board_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/game.py b/game.py index 22b5b62..cea0af5 100644 --- a/game.py +++ b/game.py @@ -5,4 +5,4 @@ agent = ArrowKeyAgent() state = {} game = Game([agent], state) game.play() -debug=True \ No newline at end of file + diff --git a/nav_game.py b/nav_game.py index 191d83d..e6b4085 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 import Asteroid +from asteroid_spawner import AsteroidSpawner + +board_size = (25, 25) +ship = Spaceship(board_size) +spawner = AsteroidSpawner(board_size) +game = Game([ship, spawner], {"score": 0}, board_size=board_size) +game.play() + diff --git a/nav_game_initial_code.py b/nav_game_initial_code.py new file mode 100644 index 0000000..ae79633 --- /dev/null +++ b/nav_game_initial_code.py @@ -0,0 +1,11 @@ +# nav_game.py +# ------------ +# By MWC Contributors +# This class implements a simple game where a spaceship avoids asteroids. +from retro.game import Game +from spaceship import Spaceship + +board_size = (25, 25) +ship = Spaceship(board_size) +game = Game([ship], {"score" : 0}, board_size=board_size) +game.play() diff --git a/poetry.lock b/poetry.lock index 098cc0b..d671a52 100644 --- a/poetry.lock +++ b/poetry.lock @@ -24,13 +24,13 @@ files = [ [[package]] name = "babel" -version = "2.14.0" +version = "2.15.0" description = "Internationalization utilities" optional = false -python-versions = ">=3.7" +python-versions = ">=3.8" files = [ - {file = "Babel-2.14.0-py3-none-any.whl", hash = "sha256:efb1a25b7118e67ce3a259bed20545c29cb68be8ad2c784c83689981b7a57287"}, - {file = "Babel-2.14.0.tar.gz", hash = "sha256:6919867db036398ba21eb5c7a0f6b28ab8cbc3ae7a73a44ebe34ae74a4e7d363"}, + {file = "Babel-2.15.0-py3-none-any.whl", hash = "sha256:08706bdad8d0a3413266ab61bd6c34d0c28d6e1e7badf40a2cebe67644e2e1fb"}, + {file = "babel-2.15.0.tar.gz", hash = "sha256:8daf0e265d05768bc6c7a314cf1321e9a123afc328cc635c18622a2f30a04413"}, ] [package.extras] @@ -196,13 +196,13 @@ files = [ [[package]] name = "docutils" -version = "0.20.1" +version = "0.21.2" description = "Docutils -- Python Documentation Utilities" optional = false -python-versions = ">=3.7" +python-versions = ">=3.9" files = [ - {file = "docutils-0.20.1-py3-none-any.whl", hash = "sha256:96f387a2c5562db4476f09f13bbab2192e764cac08ebbf3a34a95d9b1e4a59d6"}, - {file = "docutils-0.20.1.tar.gz", hash = "sha256:f08a4e276c3a1583a86dce3e34aba3fe04d02bba2dd51ed16106244e8a923e3b"}, + {file = "docutils-0.21.2-py3-none-any.whl", hash = "sha256:dafca5b9e384f0e419294eb4d2ff9fa826435bf15f15b7bd45723e8ad76811b2"}, + {file = "docutils-0.21.2.tar.gz", hash = "sha256:3a6b18732edf182daa3cd12775bbb338cf5691468f91eeeb109deff6ebfa986f"}, ] [[package]] @@ -224,13 +224,13 @@ sphinx-basic-ng = "*" [[package]] name = "idna" -version = "3.6" +version = "3.7" description = "Internationalized Domain Names in Applications (IDNA)" optional = false python-versions = ">=3.5" files = [ - {file = "idna-3.6-py3-none-any.whl", hash = "sha256:c05567e9c24a6b9faaa835c4821bad0590fbb9d5779e7caa6e1cc4978e7eb24f"}, - {file = "idna-3.6.tar.gz", hash = "sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca"}, + {file = "idna-3.7-py3-none-any.whl", hash = "sha256:82fee1fc78add43492d3a1898bfa6d8a904cc97d8427f683ed8e798d07761aa0"}, + {file = "idna-3.7.tar.gz", hash = "sha256:028ff3aadf0609c1fd278d8ea3089299412a7a8b9bd005dd08b9f8285bcb5cfc"}, ] [[package]] @@ -246,13 +246,13 @@ files = [ [[package]] name = "jinja2" -version = "3.1.3" +version = "3.1.4" description = "A very fast and expressive template engine." optional = false python-versions = ">=3.7" files = [ - {file = "Jinja2-3.1.3-py3-none-any.whl", hash = "sha256:7d6d50dd97d52cbc355597bd845fabfbac3f551e1f99619e39a35ce8c370b5fa"}, - {file = "Jinja2-3.1.3.tar.gz", hash = "sha256:ac8bd6544d4bb2c9792bf3a159e80bba8fda7f07e81bc3aed565432d5925ba90"}, + {file = "jinja2-3.1.4-py3-none-any.whl", hash = "sha256:bc5dd2abb727a5319567b7a813e6a2e7318c39f4f487cfe6c89c6f9c7d25197d"}, + {file = "jinja2-3.1.4.tar.gz", hash = "sha256:4a3aee7acbbe7303aede8e9648d13b8bf88a429282aa6122a993f0ac800cb369"}, ] [package.dependencies] @@ -357,28 +357,27 @@ files = [ [[package]] name = "pygments" -version = "2.17.2" +version = "2.18.0" description = "Pygments is a syntax highlighting package written in Python." optional = false -python-versions = ">=3.7" +python-versions = ">=3.8" files = [ - {file = "pygments-2.17.2-py3-none-any.whl", hash = "sha256:b27c2826c47d0f3219f29554824c30c5e8945175d888647acd804ddd04af846c"}, - {file = "pygments-2.17.2.tar.gz", hash = "sha256:da46cec9fd2de5be3a8a784f434e4c4ab670b4ff54d605c4c2717e9d49c4c367"}, + {file = "pygments-2.18.0-py3-none-any.whl", hash = "sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a"}, + {file = "pygments-2.18.0.tar.gz", hash = "sha256:786ff802f32e91311bff3889f6e9a86e81505fe99f2735bb6d60ae0c5004f199"}, ] [package.extras] -plugins = ["importlib-metadata"] windows-terminal = ["colorama (>=0.4.6)"] [[package]] name = "requests" -version = "2.31.0" +version = "2.32.2" description = "Python HTTP for Humans." optional = false -python-versions = ">=3.7" +python-versions = ">=3.8" files = [ - {file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"}, - {file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"}, + {file = "requests-2.32.2-py3-none-any.whl", hash = "sha256:fc06670dd0ed212426dfeb94fc1b983d917c4f9847c863f313c9dfaaffb7c23c"}, + {file = "requests-2.32.2.tar.gz", hash = "sha256:dd951ff5ecf3e3b3aa26b40703ba77495dab41da839ae72ef3c8e5d8e2433289"}, ] [package.dependencies] @@ -442,20 +441,20 @@ files = [ [[package]] name = "sphinx" -version = "7.2.6" +version = "7.3.7" description = "Python documentation generator" optional = false python-versions = ">=3.9" files = [ - {file = "sphinx-7.2.6-py3-none-any.whl", hash = "sha256:1e09160a40b956dc623c910118fa636da93bd3ca0b9876a7b3df90f07d691560"}, - {file = "sphinx-7.2.6.tar.gz", hash = "sha256:9a5160e1ea90688d5963ba09a2dcd8bdd526620edbb65c328728f1b2228d5ab5"}, + {file = "sphinx-7.3.7-py3-none-any.whl", hash = "sha256:413f75440be4cacf328f580b4274ada4565fb2187d696a84970c23f77b64d8c3"}, + {file = "sphinx-7.3.7.tar.gz", hash = "sha256:a4a7db75ed37531c05002d56ed6948d4c42f473a36f46e1382b0bd76ca9627bc"}, ] [package.dependencies] -alabaster = ">=0.7,<0.8" +alabaster = ">=0.7.14,<0.8.0" babel = ">=2.9" colorama = {version = ">=0.4.5", markers = "sys_platform == \"win32\""} -docutils = ">=0.18.1,<0.21" +docutils = ">=0.18.1,<0.22" imagesize = ">=1.3" Jinja2 = ">=3.0" packaging = ">=21.0" @@ -468,11 +467,12 @@ sphinxcontrib-htmlhelp = ">=2.0.0" sphinxcontrib-jsmath = "*" sphinxcontrib-qthelp = "*" sphinxcontrib-serializinghtml = ">=1.1.9" +tomli = {version = ">=2", markers = "python_version < \"3.11\""} [package.extras] docs = ["sphinxcontrib-websupport"] -lint = ["docutils-stubs", "flake8 (>=3.5.0)", "flake8-simplify", "isort", "mypy (>=0.990)", "ruff", "sphinx-lint", "types-requests"] -test = ["cython (>=3.0)", "filelock", "html5lib", "pytest (>=4.6)", "setuptools (>=67.0)"] +lint = ["flake8 (>=3.5.0)", "importlib_metadata", "mypy (==1.9.0)", "pytest (>=6.0)", "ruff (==0.3.7)", "sphinx-lint", "tomli", "types-docutils", "types-requests"] +test = ["cython (>=3.0)", "defusedxml (>=0.7.1)", "pytest (>=6.0)", "setuptools (>=67.0)"] [[package]] name = "sphinx-basic-ng" @@ -585,6 +585,17 @@ lint = ["docutils-stubs", "flake8", "mypy"] standalone = ["Sphinx (>=5)"] test = ["pytest"] +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + [[package]] name = "urllib3" version = "2.2.1" diff --git a/spaceship.py b/spaceship.py index 9f3552a..5f35715 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 = 'v' + + 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() +