Checkpoint 2: Attaching the "asteroid" to the player if the player touches the "asteroid" This part was challenging as there were new commands I learned while trying to incorporate the retro lab componemets. I included the "asteroid sprawner" to get more than on "asteroid" on the screen. The next part is to end the game.

This commit is contained in:
erbrown
2025-12-14 22:07:22 -05:00
parent 0141fafc98
commit 1a02f78bb1
7 changed files with 73 additions and 5 deletions

46
apple.py Normal file
View File

@@ -0,0 +1,46 @@
class Apple:
character = 'A'
def __init__(self, position):
self.position = position
self.attached = False
self.offset = (0,0)
def play_turn(self, game):
width, height = game.board_size
player = game.get_agent_by_name('player')
if self.attached:
px, py = player.position
ox, oy = self. offset
self.position = (px + ox, py + oy)
return
x, y = self.position
if game.turn_number % 2 == 0:
new_position= (x +1, y)
if new_position == player.position:
self.attach_to_player(player)
elif game.on_board(new_position):
self.position = new_position
else:
game.remove_agent(self)
def attach_to_player(self, player):
self.attached = True
index = len(player.attached_apples)
self.offset = (-1 - index, 0)
player.attached_apples.append(self)
# if y == height - 1:
# game.remove_agent(self)
# else:
# player = game.get_agent_by_name('player')
# new_position = (x+1, y)
# if new_position == player.position:
# game.end()
# else:
# self.position = new_position