From f8606a776c05f87fcaa7d3ab0db3d22ba56f9630 Mon Sep 17 00:00:00 2001 From: Rebecca Hankey Date: Sun, 15 Dec 2024 10:50:29 -0500 Subject: [PATCH] I finished the codes for the retro games. When I work through this format of coding it seems much more linear and easier to troubleshoot. I think back to my drawing project and every part of the process was housed in the one page of code without the use of classes at all. If I had understood classes, I could have broken up the pieces of the books that iterate into a class. The I could have used that class to complie the image itself. It makes the creation of the code and program cleaner as well. Everything is in the same spot on each page. So, when there is an error, or something does not look quite right, it is simple to find the thing that needs to be fixed and troubleshoot it. Without that the process would be hunting for the location of the problem then trying to decipher the fix. When thinking about students interacting with programs and the process of learning computer science, organization like this makes things much more accessible. Problems are easy to find and work through. It also makes grading and feedback easier to give (as a techer) since the code itself can be graded on coherence as well as effectivness. --- __pycache__/asteroid.cpython-312.pyc | Bin 0 -> 1158 bytes __pycache__/asteroid_spawner.cpython-312.pyc | Bin 0 -> 1384 bytes __pycache__/spaceship.cpython-312.pyc | Bin 0 -> 1316 bytes asteroid.py | 19 ++++++++++++++ asteroid_spawner.py | 19 ++++++++++++++ nav_game.py | 13 ++++++++++ spaceship.py | 26 +++++++++++++++++++ 7 files changed, 77 insertions(+) create mode 100644 __pycache__/asteroid.cpython-312.pyc create mode 100644 __pycache__/asteroid_spawner.cpython-312.pyc create mode 100644 __pycache__/spaceship.cpython-312.pyc diff --git a/__pycache__/asteroid.cpython-312.pyc b/__pycache__/asteroid.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3503a75c331d9bf5a15e2d3220d5cd650660828b GIT binary patch literal 1158 zcmZuwK}#D!6rS1L#Asqoj3rV9BUtDjG=}P}DG1V9p^+YX358+Z9g{W5CeChZ6E@U? z2U{q_gG73Yrz#%$Lwbs(81^Le&{N-RHX@-9_U(J~zW3(M`(}QOj*cLZ%Eo&3 ztANmN?({|SjP4{DO(dX~NJINb!{>nHl0j8L7S*29)7W#m(_l0aMH#H2jG*C+s0sTh zBWWUNUsaNQPGH5fC@bljgEv&Kq?^ud5x(xYTg1jTB5r`IDw1(p#!jGIGIdKY8M5F= zCM|3+KSWKQ8;}1@C*GQrnF&TWsj4bD#mLiYVwIYCt5i-DmAr0b$*ykYh+5KUqPVLj z6BR?Zo+b*)CSlZKrG(L6H<%xyI$xo4fB1u+C!bm_<{6gvIZcRxRn z*}o;QKhWt*U-N@rbJR5t0iQX&Ec>0HRbd7(D#cC8TzZ`eMvJBQlqgwhSY)$G45dgN z$<>i8wFUwrH8gpM4Y@_L9O}`V5c({ZoD{tbGZKb zV<$Xu8J62&`E>5Pd?x=2FI~l^E@SiU*!=0%&)9N9>cnST$=1%v@^P&Z>O^N+OW#r_ zsf*}hXJWFscDQ!5el@q)SUq}u^ALp|GyaOxFfq8Nd=`W_BZL%7TBX445FtAirQk(| z)ttf<72Y1>)U!vR&X8NeJu%}K)m83`ih#F|I^pQvt6K^CX1o5NFWEzLAiTw1!VJ&i ieYi#b2A=h{f6n5t#%VSAv~NU=@t;rxCvOobH{l-~Bm{#1 literal 0 HcmV?d00001 diff --git a/__pycache__/asteroid_spawner.cpython-312.pyc b/__pycache__/asteroid_spawner.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..17457d27386e044eb723e9b4e4f54471f6fd035e GIT binary patch literal 1384 zcmZuxzi$&+5Pt9NZnD@;4B|%uIfRJ_l0}FQAq9%)bVv|R6mHO6VMTh{_`TRm?A_$O z^#xmwlp;ma09{g2P=NRcDCudi0*bZbbj3AChhm*hqPm%9FS=Na|2&&3CoCybE4@Ffv@VDx?jV~+?TXpPiqiBhy#jn%D^MF}JKiLkbbuwMfLO37|I z`H|0PzM}D{@&e%pQJ%)`9UVy(`l5_yiO_-3n+9W#NK&GLl$f9;OR#M~L-dj(Y|tZ! z9A^`phil$OAk|`dP}v}%ZNabigTZUlCGc#rO{>k?gqsjlO_HZYXezxNdP?vOUqscs z9ot&gR^n9Osr{CW9di_qRHa|3MjC0!fqea~@T-qfp+?9GY3fBsC zZ`BVf02@_#ITW%`-zeWGGy*@mS*Ur-T**j<1#dtI*ZrH!tuX z0fJR#3-Hz^k1%&g03%tC|DC)Zj8e@Uc ztaoOAKDd16?Tz=jdj}JXJHK=@C!ZE~i~F}fX0Cj3NanV}D#wP8momf8fw4!T)_7nEZD7?;oE+fYLj~g;ubYmGFfCa7066A6PH|ojMWcpxhV%7?R~wv+3ST3kDhp`p0xIm| z6)`Ag7z#JfD4e9m+S{Z%etPTvk!)CJG4I49_%9XK`#XSNL4PF- tSPoy2SX1DuyU_3MCp8T|e literal 0 HcmV?d00001 diff --git a/__pycache__/spaceship.cpython-312.pyc b/__pycache__/spaceship.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c0c317ba38dad988ad78e812670da538af3361c1 GIT binary patch literal 1316 zcmaJ=&rcgi6rR~1X6-dHiPHq4v@sEtx}mZgMM@3@q)Jdsni8ociejOTmfZm_w%5+= z5^8H5`H%w~D&P<~M|w-eski6p+AMwUrNt7`5cTcvXPCesD?`zBU>s-@W>LHMYjAB*~*?2XP1e#nl*Yj0*)1jW5hk@BnL(pQB=Y<0!CbtfmIQVCDm2{6M~H=zi*fp^~z3z zD*{jP4_0r#hzU-P#M3)zfEOX!mfCUvoGq{e;BCQ8m!$xas6;>$8S?z0AUYH$g7a#P zy65;#-8B^0iB^|+YN>8An|Mx(ay_=!9osMSbnKL=vr_gwp;Zpn>7&A9k20^o=n}Oo zvuwH*+AK^{uj1Dmg_>D$+!eC!_+?_%ZCa?UTXza;uH)Y=RLv#AsL$#JuYcQoqscWw z9EcOrx_qWWzW*%)+j_SEIw%~=?`xwQ`u1WtGWG`Vj~H(zejD8%|MKnd#D?YCrO~cci4;Rsy^#gu1Y7{2Z~;f5fE9X9zJ3w3mhUfwC;MRd*or zfhYC@X;r;IDq`whZ${W{$QbzF3@uRq*;S}W{<~tY_X$Ja#m`XY4T)>t%seFzXTDzG zNnw3AH(Oj_A3-Y90SuL;;l&jf=9p`BmqcCXn&S~#YxqqrQ`a^UY*1t+q17HVutK@A zA`0-295;E|rR$_WSY84$#iZnN?2;%Nf)TN+l$_PJuAle$Vk`?4o(};1&SbaRo9#_y z18>|3^($Lbn^SK;zPGR63p2x8FE(H7UjHLA9_B`Ozxr9-efGMwlRh|iw$r`(*Ng8m zx5MnU?&Qwo?u~cZd^q;S_VlZ753US%i#x?v4~~*(X#7Y=>HAFVK5q<&kra9m!c&CQ z>h>BOpYSvx&(_Rp9AVeQ-I7+>WTxd)#x8@;asa=eh$p%{6JkUl#P+TNyhNdXsWTl8 zWjk}nilk1SXu5hcE)m)DhRRdX-y|<2W@3nWqBtY^#?>ct(+kCz^-~C(?{un7#0Q;X l1_Z=#d7l6r$r$6m(g@!;Na~$LsHHmUdnJo+9wUIL{sTB?I=27- literal 0 HcmV?d00001 diff --git a/asteroid.py b/asteroid.py index 257354b..5c07c07 100644 --- a/asteroid.py +++ b/asteroid.py @@ -2,3 +2,22 @@ # ------------ # 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, height): + if game.turn_number % 2 == 0: + x, y = self.position + if y == height - 1: + game.remove_agent_by_name(self.name) + 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..d1df41d 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 diff --git a/nav_game.py b/nav_game.py index 191d83d..b9b7bd9 100644 --- a/nav_game.py +++ b/nav_game.py @@ -2,3 +2,16 @@ # ------------ # 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 + +HEIGHT = 25 +WIDTH = 25 + +board_size = (WIDTH, HEIGHT) +ship = Spaceship(board_size) +asteroid = Asteroid((WIDTH // 2, 0)) +game = Game([ship, asteroid], {"score": 0}, board_size=board_size) +game.play() \ No newline at end of file diff --git a/spaceship.py b/spaceship.py index 9f3552a..e4dc859 100644 --- a/spaceship.py +++ b/spaceship.py @@ -2,3 +2,29 @@ # ------------ # By MWC Contributors # This module defines a spaceship agent class. + +from retro.game import Game + +HEIGHT = 25 +WIDTH = 25 + +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