From 8e766bb11dfae342e777d5e0b40c23a377544001 Mon Sep 17 00:00:00 2001 From: jwberent Date: Thu, 13 Nov 2025 17:08:26 -0500 Subject: [PATCH] This is completely different than anything we did in unit 1. When creating the nav game all the code feels connected as the code was broken up into asteroid, asteroid spawner, game, and spaceship. I think this is so much easier to follow as it was easy to figure out what each thing did. Also, this uses classes which we did not use in unit 1. --- __pycache__/asteroid.cpython-312.pyc | Bin 0 -> 1350 bytes __pycache__/asteroid_spawner.cpython-312.pyc | Bin 0 -> 1205 bytes __pycache__/spaceship.cpython-312.pyc | Bin 0 -> 1217 bytes asteroid.py | 23 +++++++++++++++++++ asteroid_spawner.py | 15 ++++++++++++ nav_game.py | 9 ++++++++ spaceship.py | 20 ++++++++++++++++ 7 files changed, 67 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..aad2bff32ab9293dc8a0194214e4eee79ae1aba8 GIT binary patch literal 1350 zcmZux&1)M+6rcT)*7`t_ZL%sdjYJ*XZAlc-<`$QPrVTaJrI#K=VOVR&(mK+vJ-f23 zWkqxFK`9jELy>$aReGt5|B2pvl0)&br$SG@ndnf+sc%**V~5P)?VI<0^X7g0X8%g3 z6A0FC-`z4k7ZCcFZRSgilxZH6A>z?fq@zQmbEo{wQ$<}~6>4)l`*P7{x&q1&5me=L zRONN9D(L(ns*1V*QtFDT6v&TlhfvGV1MZtz6f!){bVvqG7sDVP=OG+AnCK#v+f=zg z-nDGQF)UN%1JNeUZ5jiw&CV8!ZyuE!R*RH(I$MMi(Wh)_E3LS4G>>_dd;$U{5xsa%T^Mv!_jFXoB5 z=)y!p^ddxzUxP)ehfbp@anMY_utMHix-`f1;-2KiASxiX`JU{_Ui@7-gf~9&_}Tqo zts%2+2G(N-`~m+S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%o%d z`5nbE!+L}HIDI(9HawL_fuibj-Cv-mzROZ|e%2L>Fgy*G%4ECULAX<>97Yv`jb4 zBTXYqt+}l0h%O{Sj_#$h`5aBn7iTD=wwuZs3zj`cik6!tnY>9;xngEGwQO7Nt<<71 zLl|{gKIOFQ8!D8zMu-J|Ldxfk>mF*q3&BCo6@V4w4-D0{{w|Z{^^B}3@1qTfLK)GQwjB5A5Z+XghvwU!Kb}GFgnwkOT2r$~iQ-ED0JyABbZ|BcW*_3t@ChG@(w*=^YvCM|xt zGNnsgdwl;Dd2;{3OCA#1^T~%3FWET=Wg394GQq?Z+sIL_lJ+V&9S|vYBbj7pL{dC!*ybW7$GPQ;Cv!gS zi$aFu0>HduiH*v7WnEdrYr}rHdt+>U>|5XMdib^<>)Cj>{;qcMdu-58_Sf!xP-{y& z<*ms6iLn`J3(-lbz*B``_VxN#Rm_>)u2O+$RkX+tehU*g^A!KRUSZsP&x2P?Y$r{YaxRkLN*sKrW6AE~O;+a^% z0K^VB=K)rcA3nP>?#B}=lZT3=jyANgdbyb*qNjA+%o1CF!gQpKG%qs7u7X1>k0Xve RkTJ%;x)S*EAp!^zegh~m9z_5E literal 0 HcmV?d00001 diff --git a/asteroid.py b/asteroid.py index 257354b..d2bca12 100644 --- a/asteroid.py +++ b/asteroid.py @@ -2,3 +2,26 @@ # ------------ # 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): + 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/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..ae26941 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,"lives":5}, 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