From 1b5d9c5af298fff8340bdad3c9f3472df128c543 Mon Sep 17 00:00:00 2001 From: Chris Proctor Date: Thu, 19 Mar 2026 12:43:05 -0400 Subject: [PATCH] Allow knife to slice into fruit --- fruit_catcher/catcher.py | 12 ++++++++++++ fruit_catcher/fruit.py | 2 ++ 2 files changed, 14 insertions(+) diff --git a/fruit_catcher/catcher.py b/fruit_catcher/catcher.py index ed21c6d..0904f0f 100644 --- a/fruit_catcher/catcher.py +++ b/fruit_catcher/catcher.py @@ -1,4 +1,5 @@ from random import randint +from .fruit import FruitPiece class CatcherPiece: character = "-" @@ -28,10 +29,21 @@ class Catcher: new_x = max(0, x - 3) self.position = (new_x, y) self.update_piece_positions() + self.check_fruit_collisions(game) if keystroke.name == "KEY_RIGHT": new_x = min(width - self.width, x + 3) self.position = (new_x, y) self.update_piece_positions() + self.check_fruit_collisions(game) + + def check_fruit_collisions(self, game): + agents_by_pos = game.get_agents_by_position() + seen = set() + for piece in self.pieces: + for agent in agents_by_pos.get(piece.position, []): + if isinstance(agent, FruitPiece) and agent.parent.alive and id(agent.parent) not in seen: + seen.add(id(agent.parent)) + agent.parent.check_catcher_collision(game) def create_pieces(self, game): x, y = self.position diff --git a/fruit_catcher/fruit.py b/fruit_catcher/fruit.py index 6c2ed51..0eb65c4 100644 --- a/fruit_catcher/fruit.py +++ b/fruit_catcher/fruit.py @@ -17,6 +17,7 @@ class FruitPiece: self.position = position self.color = color self.z = z + self.parent = None class Fruit: display = False @@ -67,6 +68,7 @@ class Fruit: x, y = self.position ox, oy = offset piece = FruitPiece((x + ox, y + oy), self.color, z) + piece.parent = self self.pieces[offset] = piece game.add_agent(piece)