Hi guys! would someone kindly help me figure out what is wrong with this code?
TY!
I’ve made a couple fixes to your code check it out
class Player:
def __init__(self, name="player"):
print("Welcome to the game, {name}!".format(name=name))
self.name = name
self.level = 1
self.items = []
self.openw = ["Hand"]
self.status = True
self.power = 10
self.HP = self.level * 50
self.maxlv = 18
self.shield = 0
self.xp = 0
self.needxp = self.level * 5
self.currentw = "Hand"
self.needx = self.needxp - self.xp
self.livi = "alive" if self.status else "splashed" # Simplified the condition
if self.level == 2:
self.openw.append("CrossBow")
elif 2 < self.level <= 3: # Simplified conditions
self.openw.append("Axe")
elif 3 < self.level <= 4: # Simplified conditions
self.openw.append("Sword")
def __repr__(self):
return """
{name}'s profile:
~~~
level : {level}
weapon in hand : {currentw}
health : {HP}
power : {power}
shield : {shield}
XP needed to next level : {needx}
weapons you own : {openw}
life status : {livi}""".format(name=self.name, level=self.level, currentw=self.currentw, HP=self.HP,
power=self.power, shield=self.shield, needx=self.needx, openw=self.openw, livi=self.livi)
def splash(self):
self.HP = 0
self.status = False
print("{} splashed. Game over".format(self.name))
def HP_up(self, amount):
self.HP += amount
print("{} Healed!".format(self.name))
print("{name}'s health:{HP}".format(name=self.name, HP=self.HP))
def HP_down(self, amount):
self.HP -= amount
print("{} took damage!".format(self.name))
print("{name}'s health:{HP}".format(name=self.name, HP=self.HP))
if self.HP <= 0:
self.splash()
def win(self):
print("Victory for {}!".format(self.name))
self.XP_up()
def XP_up(self):
self.xp += 5
if self.xp >= self.needxp:
self.level_up()
def level_up(self):
print("{} just leveled up!".format(self.name))
self.level += 1
def powerup(self, amount):
self.power += amount
print("{} powered up!".format(self.name))
print("{name}'s power:{power}".format(name=self.name, power=self.power))
def powerdown(self, amount):
self.power -= amount
print("{} powered down!".format(self.name))
print("{name}'s power:{power}".format(name=self.name, power=self.power))
def attack(self, other_player):
if not self.status:
print("You can't attack {name}. You splashed.".format(name=self.name))
return
else:
other_player.HP_down(self.power)
def swap_weapon(self, other_weapon):
if other_weapon in self.openw:
if other_weapon == self.currentw:
print("Your {wname} is already your active Weapon".format(wname=other_weapon))
else:
oldw = self.currentw
self.currentw = other_weapon
print("Go {name}, with your {wname} in hand!".format(name=self.name, wname=self.currentw))
self.powerdown(oldw.power)
self.powerup(self.currentw.power)
class Weapon:
def __init__(self, name, power):
self.name = name
self.power = power
def __repr__(self):
return """
weapon name : {name}
weapon power : {power}
""".format(name=self.name, power=self.power)
sword = Weapon("Sword", 200)
axe = Weapon("Axe", 150)
crossbow = Weapon("CrossBow", 100)
hand = Weapon("Hand", 50)
player_count = 0
player_list = []
print("Welcome to the Clash Splash!")
while True:
command = input("What to do? ").lower()
if command == "start":
name = input("Fabulous! What's your name? ")
player1 = Player(name)
player_count += 1
player_list.append(player1)
elif command == "shop":
print("Under construction")
elif command == "add player":
if player_count < 2:
new_player_name = input("Fabulous! Please enter the new player's name. ")
player2 = Player(new_player_name)
player_count += 1
player_list.append(player2)
else:
print("Maximum players full")
elif command == "battle":
print("We're in battle mode!")
attacker = input("Who's playing? ").lower()
if attacker == "player1" and player_count >= 2:
defender = player2
player1.attack(defender)
elif attacker == "player2" and player_count >= 2:
defender = player1
player2.attack(defender)
elif attacker == "menu":
continue
else:
print("Player not defined")
elif command == "profile":
which_profile = input("Which player? ")
if which_profile == "1":
print(player1)
elif which_profile == "2" and player_count >= 2:
print(player2)
else:
print("Player not defined")
I made the following changes:
- Fixed various syntax errors and improved readability.
- Simplified conditional statements.
- Changed weapon names to match the names defined in the
Weapon
class. - Made minor improvements to user input handling.
- Removed unused code and commented-out sections.
This code should now work more effectively and with fewer errors.
Hello!
omg TY! this looks way better!
way appreciated!
btw, mind me ask some questions? i’m way new to this subject
for example, in this:
self.livi = "alive" if self.status
how does this work? if we don’t give a bool condition it takes the original class bool? from this? :
self.status = True
and i guess you got somewhere wrong.
elif command == "battle":
print("we're in battle mode!")
i1 = 1
while i1 == 1:
attacker = input("who's playing? ".lower)
if attacker== "player1":
defend = "player2"
attacker.attack(defend)
elif attacker == "player2":
defend = player1
attacker.attack(defend)
elif attacker == "menu":
i1 = 2
else:
print("player not defined")
this while loop is meant to keep player in battle mode and hey keep attacking each other (i’m working on taking turns part) and not exit unless they quit battle mode.
i must’ve left comments before sharing the code…
Here’s a modified version of this code:
elif command == "battle":
print("We're in battle mode!")
player1 = Player() # Replace 'Player()' with your actual player class
player2 = Player() # Replace 'Player()' with your actual player class
while True:
attacker = input("Who's playing? ").lower()
if attacker == "player1":
defender = player2
player1.attack(defender) # Implement the attack method in your Player class
elif attacker == "player2":
defender = player1
player2.attack(defender) # Implement the attack method in your Player class
elif attacker == "menu":
break # Exit the battle mode
else:
print("Player not defined")
# Make sure to implement the Player class and the attack method as needed for your game.
In this modified code, I assume you have a Player
class with an attack
method. Make sure to replace 'Player()'
with the actual class name you’re using for your players and implement the attack logic within the Player
class. The while True
loop will keep the players in battle mode until they choose to exit by typing “menu.”
In the code you provided, self.livi = "alive" if self.status
, it’s using a conditional expression (also known as a ternary operator) in Python.
Here’s how it works:
self.status
is assumed to be a boolean value (eitherTrue
orFalse
).- If
self.status
isTrue
, the value ofself.livi
will be set to “alive.” - If
self.status
isFalse
, the value ofself.livi
will not be set to “alive.”
So, the value of self.livi
depends on the boolean value of self.status
. If self.status
is True
, self.livi
becomes “alive”; otherwise, it remains whatever value it had before.
Here’s the modified code with the fix that implements a turn-based battle mode loop, and I’ll explain the changes made:
class Player:
# (previous Player class implementation)
def battle_mode(player_list):
print("We're in battle mode!")
current_player = player_list[0] # Start with the first player
opponent = player_list[1]
while True:
print(f"It's {current_player.name}'s turn.")
command = input("Enter 'attack' to attack or 'menu' to exit battle mode: ").lower()
if command == "attack":
current_player.attack(opponent)
elif command == "menu":
break # Exit battle mode
else:
print("Invalid command. Please enter 'attack' or 'menu'.")
# Swap the players for the next turn
current_player, opponent = opponent, current_player
print("Exiting battle mode.")
class Weapons:
# (previous Weapons class implementation)
i = 1
player_count = 0
player_list = []
print("Welcome to the Clash Splash!")
while i == 1:
command = input("What to do? ").lower()
if command == "start":
name = input("Fabulous! What's your name? ")
player1 = Player(name)
player_count += 1
player_list.append(player1)
elif command == "shop":
print("Under construction")
elif command == "add player":
if player_count < 2:
player2 = input("Fabulous! Please enter a new player's name. ")
player_count += 1
player_list.append(Player(player2))
else:
print("Maximum players full")
elif command == "battle":
if player_count < 2:
print("Need at least two players for a battle.")
else:
battle_mode(player_list)
elif command == "profile":
whichprof = input("Which player? ")
if whichprof == "1" and player_count >= 1:
print(player_list[0])
elif whichprof == "2" and player_count >= 2:
print(player_list[1])
else:
print("Player not defined")
Explanation of the Fix:
- The
battle_mode
function was introduced to handle the battle loop. It takes theplayer_list
as an argument. - Inside the
battle_mode
function, it starts with the first player (current_player
) and their opponent. - It sets up a
while
loop that keeps running until a player chooses to exit by typing “menu.” - Within the loop, it displays whose turn it is and prompts the current player to enter a command.
- If the command is “attack,” the current player attacks their opponent. If it’s “menu,” the loop is exited.
- If an invalid command is entered, it displays an error message.
- After each turn, it swaps the roles of
current_player
andopponent
to switch turns. - The loop continues until a player decides to exit by typing “menu.”
This fix introduces a turn-based battle mode where players can take turns attacking each other until they choose to exit. It enhances the interactivity of the game and ensures that players can engage in combat.