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.
This commit is contained in:
Rebecca Hankey 2024-12-15 10:50:29 -05:00
parent 18c8b0732d
commit f8606a776c
7 changed files with 77 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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()