I wanted to see if anyone would like to review the existing code I have for a beginner program that simulates a battle. I have learned a heck of a lot while doing it, and I had a few questions regarding some things that I found worked, but I am not sure on the why of them working.
Please do note that this is not finished and I am definitely going to see this program through to the end with a few more features that what I have now. However, at this stage it is functional and I wanted to see if the things I am doing now will lead to a functional program at the end.
The main issues I have had so far come from sticking the dictionaries inside of a list. I had to deal with a lot of errors passing a dictionary function to a list and vice versa.
Here is the code:
# A combat simulator which allows input for your name, your weapon, randomly chooses your enemy, and simulates a battle based on simple stats such as HP and dmg.
# a dice roll will modify dmg for things such as ambushes and counter attacks. <- Maybe!
# TODO: X implement dictionary (?) for stats along with enemy list and weapon list, input validation, don't submit to feature creep!, X verify return variable from
# local scope, battle sequence, end program
# 6/5 - Plan to implement: Final scripted battle sequence to simulate one battle, will need to understand how to modify the HP value accordingly...
import random, time
enemyOrc = {"Name" : "Orc", "HP" : "90", "Attack" : "18"}
enemyGK = {"Name" : "Goblin King", "HP" : "182", "Attack" : "16"}
enemyGR = {"Name" : "Giant Rat", "HP" : "32", "Attack" : "3"}
enemyWitch = {"Name" : "Witch", "HP" : "68", "Attack" : "32"}
enemyEM = {"Name" : "Evil Mirror", "HP" : "350", "Attack" : "11"}
weaponSword = {"Name" : "Sword", "Damage" : "12"}
weaponAxe = {"Name" : "Axe", "Damage" : "18"}
weaponQS = {"Name" : "Quarter-Staff", "Damage" : "22"}
weaponDagger = {"Name" : "Dagger", "Damage" : "8"}
weaponFists = {"Name" : "Fists", "Damage" : "28"}
enemyList = [enemyOrc, enemyGK, enemyGR, enemyWitch, enemyEM]
weaponList = [weaponSword, weaponAxe, weaponQS, weaponDagger, weaponFists]
def introduction(): # Get player name and randomly select opponent
print("Greetings combatant!\n" + "Please enter your name:")
playerName = input()
enemyChosen = random.choice(enemyList)
print ("Nice to meet you, " + playerName + ". I hope you are ready for a fight!\n" + "Today you will be facing...")
time.sleep(2)
print ("The " + enemyChosen["Name"] + "!")
time.sleep(2)
print ("Hope you're ready for a tough one!\n")
return enemyChosen, playerName
def weaponSelection(): # Get player equipment
print ("It's time to choose your weapon: ")
print("\n" .join(d["Name"] for d in weaponList))
weaponChosen = input()
while True:
if any(d['Name'] == weaponChosen for d in weaponList):
return weaponChosen.lower()
else:
print("I'm sorry, you can only choose what is available.")
weaponChosen = input()
def weaponReturn(weapon): # to test return variables // probably unnecessary?
print("Let's see if your " + weapon.lower() + " can defeat your adversary. Good luck!")
def battleSequence(enemy, player, weapon):
ambushChance = random.randint(0,1)
if ambushChance == 0:
print("The " + enemy["Name"] + " launched an attack on you from behind!")
print (player + " loses 10hp.")
else:
print("You get the first attack. You swing your " + weapon + "!")
#time.sleep(1)
print(player + " does 5 damage to the " + enemy["Name"] + "!")
playAgain = 'yes'
while playAgain == 'yes' or 'y':
enemyName, characterName = introduction()
time.sleep(1.5)
battleWeapon = weaponSelection()
weaponReturn(battleWeapon)
battleSequence(enemyName, characterName, battleWeapon)