[Python] I cannot find a way of resetting player positions once health is lower than or equal...

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

  1. Stack

    Stack Membro Participativo

    I am creating a 2 player same-screen game which uses both players playing on the same computer, both using the keyboard. I am struggling to find a way to code it in OOP so that once one of the player's health is below or equal to 0, both player positions are reset back to the starting position (standard x and y coordinate) and score being incremented by 1 for the player which was left standing. Instead, the players' health reaches 0 and the character disappears off screen but can still shoot, run, jump etc. I intend on this being an instantaneous change to the next round, with perhaps a second delay between the health reaching 0 and the next round starting. Here is the code relevant to my problem:

    class Player:
    def __init__(self, x, y, width, height, speed, images_right, images_left, idle_image):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.speed = speed
    self.left = False
    self.right = False
    self.jump_status = False
    self.jump_time = 10
    self.anim_num = 0
    self.images_right = images_right
    self.images_left = images_left
    self.idle_image = idle_image
    self.hitbox = (self.x + 17, self.y + 11, 29, 52)
    self.health = 10
    self.visible = True
    self.maxhealth = 10
    self.score = 0
    self.start = (x,y)

    def hit(self):
    if self.health > 0:
    self.health -= 1
    else:
    self.visible = False
    print('hit')

    class ResettableSprite:
    def __init__(self, idle_image, x, y):
    self.idle_image = idle_image
    self.rect = self.image.get_rect()
    self.start_x = x
    self.start_y = y
    self.reset()

    def reset(self):
    self.rect.x = self.start_x
    self.rect.y = self.start_y
    self.health = Player.maxhealth

    player1 = Player(200, 317, 64, 64, 10, walkRight, walkLeft, char)
    player2 = Player(500, 285, 92, 85, 10, walkRight2, walkLeft2, dragon)


    I have tried to create a 'ResettableSprite' class and then allow self.start coordinates to equal the original x coordinates used when loading in the players as soon as the program is run. However, this does not proivde an error message, but rather continues to allow both players' health to reach 0, allow their character to disappear off the screen, but still allowing them to run, jump and shoot as if they still had health remaining.

    Continue reading...

Compartilhe esta Página