Add features--multiple fruits

This commit is contained in:
Chris Proctor
2026-03-19 12:33:25 -04:00
parent 9276ed9aa0
commit 610ef88ece
5 changed files with 81 additions and 42 deletions

View File

@@ -1,18 +1,31 @@
from .fruit import Fruit, SHAPE_DEFINITIONS
from .fruit import Fruit, FRUIT_TYPES
from random import choice, randint
from retro.errors import AgentNotFoundByName
class FruitManager:
display = False
def __init__(self):
self.active_fruits = []
self.last_spawn_turn = 0
self.next_z = 0
def play_turn(self, game):
try:
game.get_agent_by_name("fruit")
except AgentNotFoundByName:
self.active_fruits = [f for f in self.active_fruits if f.alive]
spawn_interval = max(12, 72 - game.turn_number // 10)
if game.turn_number - self.last_spawn_turn >= spawn_interval:
self.create_piece(game)
self.last_spawn_turn = game.turn_number
def create_piece(self, game):
x = randint(0, 25)
fruit = Fruit((x, 1), game, choice(SHAPE_DEFINITIONS))
width, _ = game.board_size
fruit_type = choice(FRUIT_TYPES)
shape = fruit_type['shape']
max_ox = max(ox for ox, oy in shape)
x = randint(0, width - 1 - max_ox)
dx = choice([-1, 0, 0, 1])
speed = randint(2, 5)
fruit = Fruit((x, 1), game, shape, fruit_type['color'], dx, speed, self.next_z)
self.next_z += len(shape)
game.add_agent(fruit)
self.active_fruits.append(fruit)