Files
project_game/Tron_arcade/Trail.py
2026-06-12 10:49:57 -04:00

62 lines
2.5 KiB
Python

class TrailSegment:
"""Finally, we need an Agent for the snake's body segments.
SnakeBodySegment doesn't have ``play_turn`` or ``handle_keystroke`` methods because
it never does anything on its own. It only moves when the Bike, or the previous
segment, tells it to move.
Arguments:
segment_id (int): Keeps track of how far back this segment is from the head.
This is used to give the segment a unique name, and also to keep track
of how many points the player earns for eating the next apple.
position (int, int): The initial position.
Attributes:
character: '*'
next_segment: Initially ``None``, this is a reference to a TrailSegment
when this segment is not the last one in the snake's Trail.
"""
character = '*'
next_segment = None
def __init__(self, segment_id, position, player_number): # Added player_number
self.segment_id = segment_id
self.player_number = player_number
# Give it a completely unique name per player
self.name = f"P{player_number} Trail segment {segment_id}"
self.position = position
if self.player_number == 1:
self.color = "bright_blue"
else:
self.color = "bright_red"
def move(self, new_position, game, growing=False):
"""When Bike moves, it sets off a chain reaction, moving all its
Trail segments. Whenever the head or a Trail segment has another segment
(``next_segment``), it calls that segment's ``move`` method.
This method updates the TrailSegment's position. Then, if
``self.next_segment`` is not None, calls that segment's ``move`` method.
If there is no next segment and ``growing`` is True, then we set
``self.next_segment`` to a new SnakeBodySegment in this segment's old
position, and update the game's score.
Arguments:
new_position (int, int): The new position.
game (Game): A reference to the current game.
growing (bool): (Default False) When True, the snake needs to
add a new segment.
"""
old_position = self.position
self.position = new_position
if self.next_segment:
self.next_segment.move(old_position, game, growing=growing)
elif growing:
self.next_segment = TrailSegment(self.segment_id + 1, old_position, self.player_number)
game.add_agent(self.next_segment)