Files
project_game/fruit.py
kdang f80e4dca48 @Chris, If you run the game.py file and dont move
for a few seconds you get a crash. We also cannot
figure out how to make the fruit fall. please take
a look at the crash and tell us how to make the
fruit fall. Thank you!-Conor
2025-12-17 09:39:29 -05:00

71 lines
2.1 KiB
Python

class FruitPiece:
character = "@"
color = "green_on_indigo"
def __init__(self, position):
self.position = position
class Fruit:
width = 2
height = 2
display = False
pieces = []
character = "@"
color = "green_on_indigo"
def __init__(self, position):
self.position = position
def play_turn(self, game):
if not self.pieces:
self.create_pieces(game)
if game.turn_number % 2 == 0:
x, y = self.position
if y == 24:
game.remove_agent(self)
else:
catcher = game.get_agent_by_name("Player")
new_position = (x, y + 1)
if new_position == catcher.position:
catcher.explode()
game.end()
else:
self.position = new_position
def create_pieces(self, game):
x, y = self.position
self.pieces = []
for i in range(self.width):
piece = FruitPiece((x + i, y))
self.pieces.append(piece)
game.add_agent(piece)
for i in range(self.height):
piece = FruitPiece((x, y + i))
self.pieces.append(piece)
game.add_agent(piece)
def update_piece_positions(self):
if game.turn_number % 2 == 0:
self.set_color()
x, y = self.position
if y == HEIGHT - 1:
game.remove_agent(self)
else:
ship = game.get_agent_by_name("Player")
new_position = (x, y - 1)
if new_position == ship.position:
ship.explode()
game.end()
else:
self.position = new_position
class FruitSpawner:
display = False
def play_turn(self, game):
if self.should_spawn_fruit(game.turn_number):
asteroid = Fruit((randint(0, WIDTH - 1), 0))
game.add_agent(fruit)
def should_spawn_fruit(self, turn_number):
return randint(0, 1000) < turn_number