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,49 +1,72 @@
from random import randint
from random import randint, choice
SHAPE_DEFINITIONS = [
[(0,0)],
[(0, 0), (1, 0), (0, 1), (1, 1)],
FRUIT_TYPES = [
{'shape': [(0, 0)], 'color': 'green_on_indigo'},
{'shape': [(0, 0), (1, 0)], 'color': 'yellow_on_indigo'},
{'shape': [(0, 0), (1, 0), (2, 0)], 'color': 'red_on_indigo'},
{'shape': [(0, 0), (0, 1)], 'color': 'cyan_on_indigo'},
{'shape': [(0, 0), (1, 0), (0, 1), (1, 1)], 'color': 'magenta_on_indigo'},
{'shape': [(0, 0), (1, 1), (2, 0)], 'color': 'white_on_indigo'},
]
class FruitPiece:
character = "@"
color = "green_on_indigo"
display = True
def __init__(self, position):
def __init__(self, position, color, z):
self.position = position
self.color = color
self.z = z
class Fruit:
width = 2
height = 1
display = False
pieces = []
name = "fruit"
character = "@"
color = "green_on_indigo"
def __init__(self, position, game, shape_offsets):
def __init__(self, position, game, shape_offsets, color, dx, speed, start_z):
self.position = position
self.pieces = {}
for offset in shape_offsets:
self.create_shape(game, offset)
self.color = color
self.dx = dx
self.speed = speed
self.alive = True
for i, offset in enumerate(shape_offsets):
self.create_shape(game, offset, start_z + i)
def play_turn(self, game):
if game.turn_number % 3 == 0:
x, y = self.position
if y >= 29:
for piece in self.pieces.values():
game.remove_agent(piece)
game.remove_agent(self)
game.end()
else:
self.position = (x, y + 1)
self.update_piece_positions()
self.check_catcher_collision(game)
if game.turn_number % self.speed == 0:
self.move(game)
def create_shape(self, game, offset):
def move(self, game):
x, y = self.position
width, height = game.board_size
offsets = list(self.pieces.keys())
max_ox = max(ox for ox, oy in offsets)
min_ox = min(ox for ox, oy in offsets)
max_oy = max(oy for ox, oy in offsets)
new_x = x + self.dx
if new_x + min_ox < 0 or new_x + max_ox >= width:
self.dx = -self.dx
new_x = x + self.dx
new_y = y + 1
if new_y + max_oy >= height:
for piece in self.pieces.values():
game.remove_agent(piece)
game.remove_agent(self)
self.alive = False
game.state['Lives'] -= 1
if game.state['Lives'] <= 0:
game.end()
else:
self.position = (new_x, new_y)
self.update_piece_positions()
self.check_catcher_collision(game)
def create_shape(self, game, offset, z):
x, y = self.position
ox, oy = offset
piece = FruitPiece((x + ox, y + oy))
piece = FruitPiece((x + ox, y + oy), self.color, z)
self.pieces[offset] = piece
game.add_agent(piece)
@@ -62,4 +85,5 @@ class Fruit:
for p in self.pieces.values():
game.remove_agent(p)
game.remove_agent(self)
return
self.alive = False
return