Python Forum
How can i continue the game?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i continue the game?
#1
First of all, I apologize for my bad English. Smile
I tried coding a simple console game

import time
import random


class player():
    def __init__(self, name):
        self.name = name
        self.health = 5

    def current_state(self):
        print("Name: ", self.name)
        print("Health: ", self.health)

    def attack(self):
        print('The fight continues. Wait.')

        for i in range(3):
            time.sleep(.3)
            print('.', end='', flush=True)

        value = self.calculate_attack_result()

        if value == 0:
            print('\nNO ONE IS INJURED!')

        if value == 1:
            print(f'\n{player1.name} WAS INJURED!')
            if player1.health < 1:
                print(f"{player1.health} DEAD!")
            else:
                player1.health -= 1

        if value == 2:
            print(f'\n{player2.name} WAS INJURED!')
            if player2.health < 1:
                print(f"{player2.name} DEAD!")
            else:
                player2.health -= 1

        if value == 3:
            print(f'\n{player3.name} WAS INJURED!')
            if player3.health < 1:
                print(f"{player3.name} DEAD!")
            else:
                player3.health -= 1

        if value == 4:
            print(f'\n{player1.name} and {player2.name} WERE INJURED!')
            if player1.health < 1:
                print(f"{player1.name} DEAD!")
            else:
                player1.health -= 1
            if player2.health < 1:
                print(f"{player2.name} DEAD!")
            else:
                player2.health -= 1

        if value == 5:
            print(f'\n{player1.name} and {player3.name} WERE INJURED!')
            if player1.health < 1:
                print(f"{player1.name} DEAD!")
            else:
                player1.health -= 1
            if player3.health < 1:
                print(f"{player3.name} DEAD!")
            else:
                player3.health -= 1

        if value == 6:
            print(f'\n{player2.name} and {player3.name} WERE INJURED!')
            if player2.health < 1:
                print(f"{player2.name} DEAD!")
            else:
                player2.health -= 1
            if player3.health < 1:
                print(f"{player3.name} DEAD!")
            else:
                player3.health -= 1

        if value == 7:
            print(f'\n{player1.name}, {player2.name} and {player3.name} WERE INJURED!')
            if player1.health < 1:
                print(f"{player1.name} DEAD!")
            else:
                player1.health -= 1
            if player2.health < 1:
                print(f"{player2.name} DEAD!")
            else:
                player2.health -= 1
            if player3.health < 1:
                print(f"{player3.name} DEAD!")
            else:
                player3.health -= 1

    def calculate_attack_result(self):
        return random.randint(0, 7)


var1 = input("First player: ")
var2 = input("Second player: ")
var3 = input("Third player: ")
player1 = player(var1)
player2 = player(var2)
player3 = player(var3)

while True:
    print("Press \"ENTER\" to attack / Press \"q\" to exit.")

    select = input("\n>")

    if select == "":
        player1.attack()
        print("CURRENT STATE: ")
        f"{player1.current_state()}"
        print()
        f"{player2.current_state()}"
        print()
        f"{player3.current_state()}"

    if select.lower() == "q":
        break
For example, if "player1" dies, the game continues for "player2" and "player3". But let's not run options for "player1" ("if value ==" not get 1,4,5,7 values.. by the way random.randint(0, 7))

(The game continues until 2 players die or all players die)
Reply
#2
The problem is that your player class is hard-coded with all the players inside where you refer to specific instances. An easy improvement could me to have the players in a list and remove the player when it dies. Regardless, you need to remove all the places in your player class that refer to specific other instances.

Here is my favorite video on classes that will help here and in the future:
https://www.youtube.com/watch?v=ZDa-Z5JzLYM
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020