From 45f45a428ff17fe1032797029e19631180c14ee2 Mon Sep 17 00:00:00 2001 From: jwberent Date: Sat, 22 Nov 2025 19:29:20 -0500 Subject: [PATCH] I added the retro lab code, made 4 python files, and gave the player the ability to move left, right, up, and down. A recent success that I am proud of is that I was able to give the player the ability to move up, down, left, and right. When implementing the ability to move up and down it seemed that the code was opposite of what I expected it to be. I was able to figure out how to implement it the correct way. --- __pycache__/path.cpython-312.pyc | Bin 0 -> 1349 bytes __pycache__/person.cpython-312.pyc | Bin 0 -> 1429 bytes __pycache__/spawner.cpython-312.pyc | Bin 0 -> 1203 bytes new.py | 9 ++++++ path.py | 41 ++++++++++++++++++++++++++++ person.py | 24 ++++++++++++++++ spawner.py | 15 ++++++++++ 7 files changed, 89 insertions(+) create mode 100644 __pycache__/path.cpython-312.pyc create mode 100644 __pycache__/person.cpython-312.pyc create mode 100644 __pycache__/spawner.cpython-312.pyc create mode 100644 new.py create mode 100644 path.py create mode 100644 person.py create mode 100644 spawner.py diff --git a/__pycache__/path.cpython-312.pyc b/__pycache__/path.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5ac257319a0b642a40e3307beae1248dae14fbb5 GIT binary patch literal 1349 zcmZux&1)M+6rcT))|P};mO%6@EvCM%!}kb%maj>^2w zl?9zYKxI)EKuR4^l|p&LaSdXdddU4$nT2G>sV=}XSzmL(k^(pS~~F?1oX?X>S?Cu-sxpdu3Wx+&0}BuGqR!Y_uz-V#_k! zhs7qbcZ`aQs~VhWYHsa*vlGe~o8ZOROU=`~KR40FI13=&pdg#?iN7Q!utC>qpcqzz z2Q3*u)4IIR?NHbk>u1QgD|AKhUhTmQrSl$?r3PCFY7j)5s zfim{i+2{B98(;)n20A8G_$RP~EWV0n$Z72epc-8=_g zqjPFKr{Hs1Jus$bHbbdy?ir364Hvg;jp*1hJtK^{En;D-)d1=+PK9w|H0(VCYgM39 zn5r7?RK;5zYyoLP$#FH;2nEB^)dfPE9*UGrBt=!JZR&0uOAQ6H%E{DSbx8Mntl+h6h?)k=ly5Fa(3|Y@a3@m zVR`MiFkZe3?;w*Kc!%C-ZJfDttes|xG&%W5{%!F>K-mWu5p=86fBNf>7daHaMdCSXhSg$7cp+wdaF+LIL5mztv>TCRB9U0D)8$dl)B^_5Pu>Tf;ElM&yEQ?X@`#B z0Q!@D^Wvk(NrlNBMJi3v6bQeP>49(QDVhfh@=1O&z5YA03N{ug3Ce}Qaoqp>ZLahY IfnWzd16s>18UO$Q literal 0 HcmV?d00001 diff --git a/__pycache__/person.cpython-312.pyc b/__pycache__/person.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7d365307573b7048d354e8792d6e7549de066185 GIT binary patch literal 1429 zcmaJ=&rcgi6rR~1W{pivV%j)qD@;VCY>04B)l&f}5@|?DovKY#)j}OPy8~W>cg^fl z*Va1azyS^wL~7(5dT?&3IQG*2p_hPCX*H)xJ>`btLnTgqv+E^swa5z>w4LsZpEr|Mc5#wYi+v8~kZ zVsLT%8Q#CBKb!u2Y=7d@=eg-E<)@(@L?J`WC0@sKT7vs_kdLY{a^e}zPHrncu8Pr| zS|qL2ET-)%OHx}sBUU*P3pL&vtH!ZDXd`-$XXZ`35eu2a9;B)m z+^O}Wy4P1CyXs5Tfm1MS{nrtiufC}rU?l&`*y|r*P?z}W14Yo1ws7-Xa_8n}_jpox zU){bne~%}Gd;iM_oV)u?k(~!J(}c+iGa}5pV04vL26`Yc6;{uhYS0ug&>pLdAR+sd+8uE5#-0ZH=e59 zwI{9JbPT+Ikh%81K<*%;3t+0d_VkmJj5yT^Agumm@kQopkiF2Ec{J1g;6-*a7{9(d z_xOvyMsl6`NAr(AKTM+GiNh>P-@$Ac`Y!4N;xiNDB!mwVQgh68xIN)%Le|#Hs}aK9 z6QfBQl`<8do~K@R}|(6XUD{66&j%?S8z5_kUF<97Yt`NZog6_b*86f)X|6{ qqKi6?SmS~!Itnu#Y12_7v+N@{3)6{`hcd?aAL#-feTBe;gnt3_8ccKm literal 0 HcmV?d00001 diff --git a/__pycache__/spawner.cpython-312.pyc b/__pycache__/spawner.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..88fd063d02e4b12b4a8047390b193156e547b394 GIT binary patch literal 1203 zcmZuw%}*0S6rb6Tw)BIF0tF>f5=ydBT8#;iNK7zM^wJBr#$>Z}mUc^bx6E!WZA!v{ z1DxOnp5)*G!o{QigBO}=Xg1)*i`)o3m~isVwyT0p(>HJCz4v=>-tX<_NF<10_1_-T zni!$)?BFe-tF#}1vWo~JxQKL|!Ik< zia|8Pmar3;vuu@`8p(DXI}-A`t33kSE>ckj6O`cy&hUiWKpBCdXT+)?iB8l*SSTqK zL!~dX-JC9hA0!X|_BR))jtlPfb@2uw*0?nutbfOLod{m#R=Hs~5v~?ND9dNLSvb{E zLA;K`PJrS2_x7m!BKpwo@vL^p(+FSZ02})5^@|8goENhu04C(Ux$1)Ffl(EHl9+Zx zD{E5qoMofzDd&t!$?pUgO@$KK(rPOB*ov(>16JNF7vYYzO;)-zI3c@C4cRE`iz=n; zeUy>z^eBYLN=^mRL`SgH;*ule6dlki8nN?^FRyC3yzK-$AdAJu*}8h3%A2~HDpUXx z08+ZLtQk4EqS<*lYZ5i3SF(>&Wka){q)OB*s99TP*eSO(l_pD7r>9g@sxqssHhhEd zkkOf_v zG}b0}(&C`(BHNo`_(lll@mj~rK+6J>__WK9_5(sF3zIn72OEuouo!jmI2~jcy&!6d zn{WOvdCb-72eFP$qk}sud*jXM=>CI7bn-YbNr%9YmV8b?mJR5xEIUD2)=g3_GCeHI zD`lnFwfKo?alvDZ&(Pg7H2hNt;p8O(;fDPJ DU49VK literal 0 HcmV?d00001 diff --git a/new.py b/new.py new file mode 100644 index 0000000..5846492 --- /dev/null +++ b/new.py @@ -0,0 +1,9 @@ +from retro.game import Game +from person import Spaceship +from spawner import AsteroidSpawner + +board_size = (25, 25) +ship = Spaceship(board_size) +spawner = AsteroidSpawner() +game = Game([ship, spawner], {"score": 0,"lives":5}, board_size=board_size) +game.play() \ No newline at end of file diff --git a/path.py b/path.py new file mode 100644 index 0000000..547fef0 --- /dev/null +++ b/path.py @@ -0,0 +1,41 @@ +class Asteroid: + character = 'O' + + def __init__(self, position): + self.position = position + + #def play_turn(self, game): + # lives = 5 + # 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: + # lives = lives-1 + # game.state["lives"] -=1 + # if game.state["lives"] == 0: + # game.end() + #else: + # self.position = new_position + + def play_turn(self, game): + lives = 5 + 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: + lives = lives-1 + game.state["lives"] -=1 + if game.state["lives"] == 0: + game.end() + else: + self.position = new_position \ No newline at end of file diff --git a/person.py b/person.py new file mode 100644 index 0000000..a473abf --- /dev/null +++ b/person.py @@ -0,0 +1,24 @@ +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","KEY_UP", "KEY_DOWN"): + if keystroke.name == "KEY_LEFT": + new_position = (x - 1, y) + if keystroke.name == "KEY_RIGHT": + new_position = (x + 1, y) + if keystroke.name == "KEY_UP": + new_position = (x, y - 1) + if keystroke.name == "KEY_DOWN": + new_position = (x, y + 1) + 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 diff --git a/spawner.py b/spawner.py new file mode 100644 index 0000000..884e579 --- /dev/null +++ b/spawner.py @@ -0,0 +1,15 @@ +from random import randint +from path 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