user9115295
user9115295

Reputation:

Having issue with health bar in the game

I am currently coding a game but, when i do subtract the health from the player each time it subtracts from full health, but I want it to subtract from the remained health, and I've tried couple of code for the player swap but I can't get it work.

import random

health1=health2=100

char1='|' * (health1 / 2)
char2='|' * (health2 / 2)
players=[char1,char2]

char1 = raw_input("Player One: ")
while char1 == "":
    char1 = raw_input("Please enter your name: ")


char2 = raw_input("Player two: ")
while char2 == char1:
    char2 = raw_input(char1 + " name is taken, please choose another name: ")
while char2 == "":
    char2 = raw_input("Please enter your name: ")
print char1 + " and " + char2 + " welcome to the game."

toss = random.randint(0, 1)
if toss == 0:
    print char1+" will start the game"
else:
    print char2+" will start the game"

print char1,'|' * (health1 / 2),
print char2,'|' * (health2 / 2)

def magnitude(n):
    if toss==0:
        print char1,'|' * ((health1 /2)-(n))
        magnitude(input(char1 + " select n force: "))
        return
    else:
        print char2,'|' * ((health2 /2)-(n))
        magnitude(input(char2 + " select n force: "))
        return
magnitude(input("select n force: "))

Upvotes: 0

Views: 120

Answers (1)

furas
furas

Reputation: 142661

I don't know what you try to do but I would do it without fuction, only using while loop

import random

# --- 

char1 = raw_input("Player One: ")

while char1 == "":
    char1 = raw_input("Please enter your name: ")

char2 = raw_input("Player two: ")
while char2 == char1:
    char2 = raw_input(char1 + " name is taken, please choose another name: ")
while char2 == "":
    char2 = raw_input("Please enter your name: ")
print char1, "and", char2, "welcome to the game."

# --- 

health1 = 50
health2 = 50

print char1, '|' * health1
print char2, '|' * health2

toss = random.randint(0, 1)

if toss == 0:
    print char1, "will start the game"
else:
    print char2, "will start the game"

# ---

while health1 > 0 and health2 > 0:
    if toss == 0:
        n = input(char1 + " select n force: ")
        health1 -= n
        print char1, '|' * health1
        toos = 1 # change player
    else:
        n = input(char2 + " select n force: ")
        health2 -= n
        print char2, '|' * health2
        toos = 0 # change player

# ---

if health1 > 0:
    print char1, 'wins'
else:
    print char2, 'wins'

Or using also lists to keeps chars and healths

import random

# ---

char1 = raw_input("Player One: ")

while char1 == "":
    char1 = raw_input("Please enter your name: ")

char2 = raw_input("Player two: ")
while char2 == char1:
    char2 = raw_input(char1 + " name is taken, please choose another name: ")
while char2 == "":
    char2 = raw_input("Please enter your name: ")
print char1, "and", char2, "welcome to the game."

chars = [char1, char2]

# ---

healths = [50, 50]

print chars[0], '|' * healths[0]
print chars[1], '|' * healths[1]

toss = random.randint(0, 1)

print chars[toss], "will start the game"

# ---

while healths[0] > 0 and healths[1] > 0:
    n = input(chars[toss] + " select n force: ")
    healths[toss] -= n
    print chars[toss], '|' * healths[toss]
    toss = (toss + 1) % 2 # change player

# ---

if healths[0] > 0:
    print chars[0], 'wins'
else:
    print chars[1], 'wins'

Upvotes: 2

Related Questions