From 9a4ef2eb8db1afaeb365259f49628855a8b6e31d Mon Sep 17 00:00:00 2001 From: caglazir Date: Mon, 1 Dec 2025 21:31:36 -0500 Subject: [PATCH] This program differed from Unit 1 programs because it felt more foreign, if that makes sense. The code used in this program was not my work hence, I did not feel as though I completely understood how each agent was doing what it was doing. I guess I can see the value of agents because they really do cut things short and functional. But I much prefer the labs and programs that had more original input for the learning experience! At this stage I don't know if I am comfortable or confident going into the game project but I will try to think more about the agents to understand better. --- __pycache__/asteroid.cpython-312.pyc | Bin 0 -> 1166 bytes __pycache__/asteroid_spawner.cpython-312.pyc | Bin 0 -> 1202 bytes __pycache__/spaceship.cpython-312.pyc | Bin 0 -> 1214 bytes asteroid.py | 19 ++++++++++++++++++ asteroid_spawner.py | 15 ++++++++++++++ nav_game.py | 9 +++++++++ spaceship.py | 20 +++++++++++++++++++ 7 files changed, 63 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..792902542834a13dcc3fc4d80d24a820515b1690 GIT binary patch literal 1166 zcmZuw%}X0W6rb6T81q45Vu_$&0+u!hjiL3{6a;ClprzMP7&h58Sxt6zcB4($P!CdK zp%4!edulwj#r`KfX(=vy5_+;HF^57=eY4qED}6AJ_wk!I?`yt>!vO^Jb9qMn!Xfm7 zDFYHbp*IFX2XW{rlF=@b@i8D7=h#zv6CiYug7R2Kc}~W8UgmaDUXXd< zVqHj!j(^@T6{@SUgP#|@knVLR^Kg1GH;9ETL|g}Bb(F>{Y3%qnbVD^&T}yM0U?`PU z>I1LA?k1DJbD6SURWil0RHdZ$hK;hNl}-TOm(}M&D1p2e2}R~1ws{*>KVz) zW@e-A_z6+LO-N&8kmSt3@l|5U)TcPki!hIEjH*BIm$y@Xtw zT$2ay&FVdrj2lpp%&p%?h^iJ^8;s*d6gPs<6fM!>WxfHUbkQ-0ICKN%i55Ec#)F}Y zfMG@4B3T$0FsMfI(imOokUaJsqfKy=ZW#N#PmsG`qS#S#9Ujjlc{dDyLmKs z*l36B_+8P&$^L~TD1U?e_CM-B|Cpx0!KQ4mzF+bf<6nUo&R!v4?u9>_3K@dKiL*iwvnBy1oO@j=Qm})PGJHuXE~nB3+tSndf@Yd&OIt9C zbLz6;7Sz1W^t82{e~>N}4fkQ%)N(4R%89PyW_m-+k0>)Y-b{ZL~Muzs;>F{WEc=S_b z>?}U;E`HE?F!g@&@WsiU(GT=E@#rM}c=LHJ+OeJ4%IuAQjt*YPBsyM)Q^=J*!%F#m zmH=ayxIqWQ3t>XQYK1NUDKjJ~W_)&x?-0T80co!}p@2f-C@BCl> zZeZ*!Q0wF@*1fZ|H+T{o*uPtijT|c@JPwXbGUO?$S_JY`)eEbtWwVlrc2rfDN}Ad9 zv@pZLvhr>S@>4h@K0qTUd{Z2i0>9G~@OkYstN-cIFIV>j-}K+}Zqz1x=G zP8>6AbeE6Qk5f#}$G%i18aX}T1F(iwbkKPrQc8b@3GMwt`hQ6gn)*RN`GLOy3k45m literal 0 HcmV?d00001 diff --git a/__pycache__/spaceship.cpython-312.pyc b/__pycache__/spaceship.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d03002d38fba73e91a1022309b770d3e8ef2d416 GIT binary patch literal 1214 zcmaJ=%}*0S6rb4-*wSJvB??4BQ4TJc7Gi=2fy4v^R5&OX8{%Z?E_9*Ymf1yYvn3oj zz`+0qQjU1T@DKPucmcyIAM;l1TM?%$;L@+<$Z7)%yvu7T!%mxdCULyNA5t}Tp9$`S_`mYR=X3EacuPV_bb+%7x(mET zs9|1k5%E(v6p$)TpL3#fej^n#C7-iR$IMx(1Sc7pdDb+S)2L2tvuLnjRbMrAC(S}t zBW;)qX~*VWh=E*xrKz-J8LkS+Bk45TCTGheO}GPx_u8 zw+(GdpPMTXg;KmJb-8ZRCA=vk7cJI3e?0xhi6zO!i@YzlM8_WyP5#3tEImeuD$r_? zx7wBFg_3MF`Zn-XZC6;7Z;^7Jd{kQr+6xhl|BX;w`R^e@25-zQIjY$h#>{`JG_49u zc|G=?ycv5r!vb7;J2^fvLt7w}DgdfXITMpCEo(3(XAytMOfhX@Wb;meiH4=C4U~Tl zEU;u0Y#88@!K4MAfIl%@U?Iy`CG|bB8z7P|Ml43}@T5puv-FHXu5*hWH*>!2YlaNl z0f2djquZse(w4M|HwV06>-Naj$Z_Z3NpR2$w{5R&tsQjy2={rh?t>R!#JGxISSaLY7ywOw~hMd2IpMda9)ygVHeAv=iVf@_GE?DPKV!_zu|j z05*^pyt_W?MWXAImy#e4SCpXKRZZd1(<-iJ@vXmMx>AO!7a5_Cz`>Wt=0{$L7~@|} LQQUQj0Q`jC8C)D; literal 0 HcmV?d00001 diff --git a/asteroid.py b/asteroid.py index 257354b..25c52be 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): + 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..4a61c8f 100644 --- a/asteroid_spawner.py +++ b/asteroid_spawner.py @@ -2,3 +2,18 @@ # ------------------- # 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..de13edd 100644 --- a/nav_game.py +++ b/nav_game.py @@ -2,3 +2,12 @@ # ------------ # 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/spaceship.py b/spaceship.py index 9f3552a..3802f1c 100644 --- a/spaceship.py +++ b/spaceship.py @@ -2,3 +2,23 @@ # ------------ # 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