generated from mwc/project_game
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
from random import randint
|
|
|
|
SHAPE_DEFINITIONS = [
|
|
[(0,0)],
|
|
[(0, 0), (1, 0), (0, 1), (1, 1)],
|
|
]
|
|
|
|
class FruitPiece:
|
|
character = "@"
|
|
color = "green_on_indigo"
|
|
display = True
|
|
def __init__(self, position):
|
|
self.position = position
|
|
|
|
class Fruit:
|
|
width = 2
|
|
height = 1
|
|
display = False
|
|
pieces = []
|
|
name = "fruit"
|
|
character = "@"
|
|
color = "green_on_indigo"
|
|
|
|
def __init__(self, position, game, shape_offsets):
|
|
self.position = position
|
|
self.pieces = {}
|
|
for offset in shape_offsets:
|
|
self.create_shape(game, offset)
|
|
|
|
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)
|
|
|
|
def create_shape(self, game, offset):
|
|
x, y = self.position
|
|
ox, oy = offset
|
|
piece = FruitPiece((x + ox, y + oy))
|
|
self.pieces[offset] = piece
|
|
game.add_agent(piece)
|
|
|
|
def update_piece_positions(self):
|
|
x, y = self.position
|
|
for offset, piece in self.pieces.items():
|
|
ox, oy = offset
|
|
piece.position = (x + ox, y + oy)
|
|
|
|
def check_catcher_collision(self, game):
|
|
catcher = game.get_agent_by_name("catcher")
|
|
catcher_positions = {p.position for p in catcher.pieces}
|
|
for piece in self.pieces.values():
|
|
if piece.position in catcher_positions:
|
|
game.state['Score'] += 1
|
|
for p in self.pieces.values():
|
|
game.remove_agent(p)
|
|
game.remove_agent(self)
|
|
return |