[Python] Pygame interacting with object

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a game in python using pygame-ce where you can interact with a character and a book but I'm having trouble as I can't understand why I cannot interact with the character or book more than once. Here is the code for the player interacting with an object.

    def input(self):
    keys = pygame.key.get_just_pressed()

    if keys[pygame.K_z]:
    if not self.dialogue_tree:
    for character in self.character_sprites:
    if check_connections(200, self.player, character):
    self.player.start_interaction(character)
    self.create_dialogue(character)


    for interactable in self.interactable_sprites:
    if check_connections(100, self.player, interactable):
    self.player.start_interaction(interactable)
    self.create_dialogue(interactable)

    self.player.end_interaction()
    return



    def create_dialogue(self, character):
    self.dialogue_tree = DialogueTree(character, self.player, self.all_sprites, self.fonts['dialogue'])


    Here is my interactable class:

    class Interactable(Entity):
    def __init__(self, pos, frames, groups, facing_direction, z, interactable_data):
    if frames is None:
    frames = {
    'down': [pygame.Surface((TILE_SIZE, TILE_SIZE))],
    'up': [pygame.Surface((TILE_SIZE, TILE_SIZE))],
    'left': [pygame.Surface((TILE_SIZE, TILE_SIZE))],
    'right': [pygame.Surface((TILE_SIZE, TILE_SIZE))]
    }
    for direction in frames:
    frames[direction][0].fill((0, 0, 0, 0))

    super().__init__(pos, frames, groups, facing_direction, z)
    self.interactable_data = interactable_data
    self.name = interactable_data.get('name', 'Unknown')

    def get_dialogue(self):
    return self.interactable_data['dialogue']['start']

    def interact(self, player):
    dialogue = self.get_dialogue()

    if self.name == 'book' and not player.inventory['key']:
    player.inventory['key'] = True
    dialogue.append("You found a key!")

    return dialogue


    Here is my DialogueTree class:

    class DialogueTree:
    def __init__(self, target, player, all_sprites, font):
    self.player = player
    self.target = target
    self.font = font
    self.all_sprites = all_sprites

    if hasattr(target, 'interact'):
    self.dialogue = target.interact(player)
    else:
    self.dialogue = target.get_dialogue()

    if not isinstance(self.dialogue, list):
    self.dialogue = [self.dialogue]

    self.dialogue_num = len(self.dialogue)
    self.dialogue_index = 0

    self.current_dialogue = DialogueSprite(self.dialogue[self.dialogue_index], self.target, self.all_sprites, self.font)

    self.cooldown = 1000
    self.last_input_time = pygame.time.get_ticks()
    self.can_advance = False

    def input(self):
    now = pygame.time.get_ticks()

    if now - self.last_input_time > self.cooldown:
    self.can_advance = True

    keys = pygame.key.get_pressed()

    if self.can_advance and keys[pygame.K_z]:
    self.can_advance = False
    self.last_input_time = now

    if self.current_dialogue:
    self.current_dialogue.kill()
    self.current_dialogue = None

    self.dialogue_index += 1

    if self.dialogue_index < self.dialogue_num:
    self.current_dialogue = DialogueSprite(
    self.dialogue[self.dialogue_index],
    self.target,
    self.all_sprites,
    self.font
    )
    else:
    self.player.end_interaction()
    self.dialogue_tree = None



    def update(self):
    self.input()

    Continue reading...

Compartilhe esta Página