generated from mwc/project_game
Add slice effect
This commit is contained in:
@@ -1,6 +1,33 @@
|
||||
from random import randint
|
||||
from .fruit import FruitPiece
|
||||
|
||||
class SliceEffectPiece:
|
||||
character = "-"
|
||||
color = "yellow_on_indigo"
|
||||
z = 2
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
class SliceEffect:
|
||||
display = False
|
||||
|
||||
def __init__(self, game):
|
||||
self.pieces = []
|
||||
self.turns_remaining = 8
|
||||
width, height = game.board_size
|
||||
for y in range(height - 3, height):
|
||||
for x in range(width):
|
||||
piece = SliceEffectPiece((x, y))
|
||||
self.pieces.append(piece)
|
||||
game.add_agent(piece)
|
||||
|
||||
def play_turn(self, game):
|
||||
self.turns_remaining -= 1
|
||||
if self.turns_remaining <= 0:
|
||||
for piece in self.pieces:
|
||||
game.remove_agent(piece)
|
||||
game.remove_agent(self)
|
||||
|
||||
class CatcherPiece:
|
||||
character = "-"
|
||||
color = "white_on_indigo"
|
||||
@@ -35,6 +62,10 @@ class Catcher:
|
||||
self.position = (new_x, y)
|
||||
self.update_piece_positions()
|
||||
self.check_fruit_collisions(game)
|
||||
if str(keystroke) == 'z' and game.state['slices'] > 0:
|
||||
game.state['slices'] -= 1
|
||||
game.add_agent(SliceEffect(game))
|
||||
self._slice_fruits(game)
|
||||
|
||||
def check_fruit_collisions(self, game):
|
||||
agents_by_pos = game.get_agents_by_position()
|
||||
@@ -45,6 +76,21 @@ class Catcher:
|
||||
seen.add(id(agent.parent))
|
||||
agent.parent.check_catcher_collision(game)
|
||||
|
||||
def _slice_fruits(self, game):
|
||||
width, height = game.board_size
|
||||
agents_by_pos = game.get_agents_by_position()
|
||||
seen = set()
|
||||
for y in range(height - 3, height):
|
||||
for x in range(width):
|
||||
for agent in agents_by_pos.get((x, y), []):
|
||||
if isinstance(agent, FruitPiece) and agent.parent.alive and id(agent.parent) not in seen:
|
||||
seen.add(id(agent.parent))
|
||||
game.state['score'] += 1
|
||||
for p in agent.parent.pieces.values():
|
||||
game.remove_agent(p)
|
||||
game.remove_agent(agent.parent)
|
||||
agent.parent.alive = False
|
||||
|
||||
def create_pieces(self, game):
|
||||
x, y = self.position
|
||||
self.pieces = []
|
||||
|
||||
@@ -12,6 +12,6 @@ def play():
|
||||
Catcher((11, 29)),
|
||||
FruitManager(),
|
||||
]
|
||||
state = {'score': 0, 'lives': 5}
|
||||
state = {'score': 0, 'lives': 5, 'slices': 3}
|
||||
game = Game(agents, state, board_size=(WIDTH, HEIGHT), framerate=24, color="white_on_indigo", dump_state="result.json")
|
||||
game.play()
|
||||
|
||||
Reference in New Issue
Block a user