I have recently started to learn python, and I have made my first fighting duel game. I'm planning on adding more stuff in the future like shops and items so you get a way to spend gold, but for now the current features are:
- Character creation (with saving and loading),
- Upgrading Str/Def,
- Keeping Score
- Battling AI or Battling 1v1.
- (you also get rewards for winning)
But for now I'm happy with my progress. Please review my code!
import random
import json
import time
r = random
#INSTRUCTIONS FOR FIRST TIME USE
#MAKE 3 COMPUTER ACCOUNTS NAMED "Computer_Easy", "Computer_Med" and "Computer_Hard" AND SET THEIR STATS HOWEVER YOU LIKE! OTHERWISE UNABLE TO PLAY VS AI
class Character():
#Change value of default if you want people to start with higher str/def when they make their account.
def __init__(self, name, default=1, health=None, strength=None,
defence=None, gold=0, exp=0, wins=0, losses=0,
):
self.name = name
self.health = 100
self.strength = default if strength is None else strength
self.defence = default if defence is None else defence
self.gold = 0
self.exp = 0
self.wins = 0
self.losses = 0
def make_account(self, name): #Create a new user to /accounts folder with default stats as listed above
save_name = name
path = 'accounts/{0}.json'.format(save_name)
data = {
"health": self.health,
"strength": self.strength,
"defence": self.defence,
"gold": self.gold,
"exp": self.exp,
"wins": self.wins,
"losses": self.losses,
}
with open(path, 'w+') as f:
json.dump(data, f)
def load_account(self, name):
load_name = name
path_two = 'accounts/{0}.json'.format(load_name)
with open(path_two, 'r') as f:
data = json.load(f)
return data
def upgrade_stat(self, name): #Upgrade either defence or strength
target = raw_input("What stat do you want to upgrade?")
if target in {"health", "gold", "exp", "wins", "losses"}:
print("you cannot do that!")
return
load_name = name
path_two = 'accounts/{0}.json'.format(load_name)
with open(path_two, 'r') as f:
data = json.load(f)
if data["exp"] < 100:
print("You don't have enough xp!")
else:
data["exp"] -= 100
data[target] += 1
with open (path_two, 'w+') as g:
json.dump(data, g)
def calculate_damage(self, damage_amount, attacker, defender):
attacker_stats = self.load_account(name=attacker)
defender_stats = self.load_account(name=defender)
#You get damage boost, depending on attacker strength level and defender defence level.
damage_amount_boost = (attacker_stats["strength"] * damage_amount / (attacker_stats["strength"] + defender_stats["defence"])) + damage_amount
if (damage_amount_boost > self.health):
overkill = abs(self.health - damage_amount_boost)
self.health = 0
if (overkill > 0):
print("{0} takes fatal damage from {1}, with {2} overkill!"
.format(self.name.capitalize(), attacker, overkill))
else:
print("{0} takes fatal damage from {1}!"
.format(self.name.capitalize(), attacker))
else:
self.health -= damage_amount_boost
print("{0} takes {1} damage from {2}!"
.format(self.name.capitalize(), damage_amount_boost, attacker))
def calculate_heal(self, heal_amount):
if (heal_amount + self.health > 100):
self.health = 100
print("{0} heals back to full health!"
.format(self.name.capitalize()))
else:
self.health += heal_amount
print("{0} heals for {1}!"
.format(self.name.capitalize(), heal_amount))
def parse_int(input):
try:
int(input)
return True
except ValueError:
return False
def get_selection():
valid_input = False
while (valid_input is False):
print()
choice = input("Select an attack: ")
if (parse_int(choice) is True):
return int(choice)
else:
print("The input was invalid. Please try again.")
def get_computer_selection(health):
sleep_time = random.randrange(2, 5)
print("Computer thinking...")
time.sleep(sleep_time)
if (health <= 35):
# Have the computer heal ~50% of its turns when <= 35
result = random.randint(1, 6)
if (result % 2 == 0):
return 3
else:
return random.randint(1, 2)
elif (health == 100):
return random.randint(1, 2)
else:
return random.randint(1, 3)
def play_round_ai(computer, human):
game_in_progress = True
current_player = computer
movesprint = '1. Mild, 2. Hard, 3. Heal'
while game_in_progress:
if (current_player == computer):
current_player = human
else:
current_player = computer
print(
"You have {0} health remaining and the "
"computer has {1} health remaining."
.format(human.health, computer.health))
if (current_player == human):
print(movesprint)
move = get_selection()
else:
move = get_computer_selection(computer.health)
if (move == 1):
damage = random.randrange(12, 22)
if (current_player == human):
computer.calculate_damage(damage, human.name, computer.name)
else:
human.calculate_damage(damage, computer.name, human.name)
elif (move == 2):
damage = random.randrange(5, 35)
if (current_player == human):
computer.calculate_damage(damage, human.name, computer.name)
else:
human.calculate_damage(damage, computer.name, human.name)
elif (move == 3):
heal = random.randrange(15, 28)
current_player.calculate_heal(heal)
else:
print ("The input was not valid. Please select a choice again.")
if (human.health == 0):
print("Sorry, you lose!")
computer.wins += 1
load_names = human.name
paths = 'accounts/{0}.json'.format(load_names)
with open(paths, 'r') as f:
data = json.load(f)
data["losses"] += 1
with open(paths, 'w+') as g:
json.dump(data, g)
game_in_progress = False
if (computer.health == 0):
print("Congratulations, you beat the computer!")
human.wins += 1
load_names = human.name
paths = 'accounts/{0}.json'.format(load_names)
with open(paths, 'r') as f:
data = json.load(f)
data["exp"] += 25
data["gold"] += 100
data["wins"] += 1
with open(paths, 'w+') as g:
json.dump(data, g)
game_in_progress = False
def play_round_multiplayer(player2, player1):
game_in_progress = True
current_player = player2
movesprint = '1. Mild, 2. Hard, 3. Heal'
while game_in_progress:
if (current_player == player2):
current_player = player1
else:
current_player = player2
#print()
print(
"You have {0} health remaining and the "
"player2 has {1} health remaining."
.format(player1.health, player2.health))
#print()
if (current_player == player1):
print(movesprint)
move = get_selection()
else:
print(movesprint)
move = get_selection()
if (move == 1):
damage = random.randrange(12, 22)
if (current_player == player1):
player2.calculate_damage(damage, player1.name, player2.name)
else:
player1.calculate_damage(damage, player2.name, player1.name)
elif (move == 2):
damage = random.randrange(5, 35)
if (current_player == player1):
player2.calculate_damage(damage, player1.name, player2.name)
else:
player1.calculate_damage(damage, player2.name, player1.name)
elif (move == 3):
heal = random.randrange(15, 28)
current_player.calculate_heal(heal)
else:
print ("The input was not valid. Please select a choice again.")
if (player1.health == 0):
print("{0} wins".format(player2.name))
player2.wins += 1
winner_name = player2.name
loser_name = player1.name
winner_path = 'accounts/{0}.json'.format(winner_name)
loser_path = 'accounts/{0}.json'.format(loser_name)
with open(winner_path, 'r') as f:
data = json.load(f)
data["exp"] += 25
data["gold"] += 100
data["wins"] += 1
with open(winner_path, 'w+') as g:
json.dump(data, g)
with open(loser_path, 'r') as f:
data = json.load(f)
data["losses"] += 1
with open(loser_path, 'w+') as g:
json.dump(data, g)
game_in_progress = False
if (player2.health == 0):
print("{0} wins".format(player1.name))
player1.wins += 1
winner_name = player1.name
loser_name = player2.name
winner_path = 'accounts/{0}.json'.format(winner_name)
loser_path = 'accounts/{0}.json'.format(loser_name)
with open(winner_path, 'r') as f:
data = json.load(f)
data["exp"] += 25
data["gold"] += 100
data["wins"] += 1
with open(winner_path, 'w+') as g:
json.dump(data, g)
with open(loser_path, 'r') as f:
data = json.load(f)
data["losses"] += 1
with open(loser_path, 'w+') as g:
json.dump(data, g)
game_in_progress = False
def start_game_ai(name): #Play vs Ai, which you select your difficulty.
target = raw_input("Easy, med or hard?")
if target == "easy":
computer = Character("Computer_Easy") #Make sure you have made all of these accounts in folder Accounts
if target == "med": #Set stats however you like
computer = Character("Computer_Med")
if target == "hard":
computer = Character("Computer_Hard")
human = Character(name)
keep_playing = True
while (keep_playing is True):
print("Current Score:")
print("You - {0}".format(human.wins))
print("Computer - {0}".format(computer.wins))
computer.health = 100
human.health = 100
play_round_ai(computer, human)
print()
response = raw_input("Play another round?(Y/N)")
if (response.lower() == "n"):
break
def start_game_multiplayer(name, target): #Play a local 1v1 game.
player2 = Character(target)
player1 = Character(name)
keep_playing = True
while (keep_playing is True):
print("Current Score:")
print("Player1 - {0}".format(player1.wins))
print("Player2 - {0}".format(player2.wins))
player2.health = 100
player1.health = 100
play_round_multiplayer(player2, player1)
#print()
response = raw_input("Play another 1v1? (y/n)")
if (response == "y"):
continue
else:
break
def options(): #Main menu
name = raw_input("Username to load or create?")
print("What do you want to do? If this is your first time, type 5 to create your new account!")
move = raw_input("1. Battle AI | 2. List stats | 3. Play 1v1 | 4. Upgrade str/def | 5. Make new user")
if move == "1":
print("Make sure you already have made all Computer accounts! (Computer_Easy, Computer_Med, Computer_Hard)")
start_game_ai(name)
if move == "2":
stats = Character(name).load_account(name)
print stats
return
if move == "3":
target = raw_input("Who do you want to fight?")
start_game_multiplayer(name, target)
if move == "4":
Character(name).upgrade_stat(name)
stats = Character(name).load_account(name)
print stats
if move == "5":
Character(name).make_account(name)
else:
print("Exiting..")
return
options()