All pastes #2126629 Raw Edit

Game code

public python v1 · immutable
#2126629 ·published 2012-03-10 16:08 UTC
rendered paste body
import randomimport sysplayerHP = 100.0enemyHP = 100.0basePlayerDamage = 10.0baseEnemyDamage = 10.0basePlayerDefense = 10.0baseEnemyDefense = 10.0wins = 0losses = 0playerName = ""class weapon:    name = "Fists"    damage = 0class armor:    name = "Clothes"    defense = 0def mainMenu():    print "Welcome to TBCGame. What would you like to do? Options with '#' are not available."    print "1. Start fighting"    print "2. View character"    print "3. View stats"    print "4. #Save game"    print "5. #Load game"    print "6. Help"    print ""    print "0. Quit game"    selection = raw_input("")    if selection == "1" or selection == "2" or selection == "3" or selection == "4" or selection == "5" or selection == "6" or selection == "0":        return selection    else:        print "Error. Selection an option (1-6), or enter 0 to quit."        mainMenu()def combat(playerHP, enemyHP, basePlayerDamage, baseEnemyDamage, basePlayerDefense, baseEnemyDefense):        combatAction = 0    global wins    global losses    #Pre-combat:    print "You see an enemy. Enemy stats are:"    print "Damage: " + `baseEnemyDamage`    print "Defense: " + `baseEnemyDefense`    print ""    #Start combat, loop until one player dies:    while playerHP > 0 and enemyHP > 0:                print "1. Normal attack"        print "2. Power attack"        print "3. Defend\n"        print "0. Quit"        #Player attack phase: set attack method and calculate damage/defense                while combatAction != "1" or combatAction != "2" or combatAction != "3" or combatAction != "0":            combatAction = raw_input("")            if combatAction == "1": #Normal attack                playerDamage = basePlayerDamage + weapon.damage                playerDefense = basePlayerDefense + armor.defense                break            elif combatAction == "2": #Power attack                playerDamage = (basePlayerDamage + weapon.damage)*1.25                playerDefense = (basePlayerDefense + armor.defense)/1.25                break            elif combatAction== "3": #Defend                playerDamage = (basePlayerDamage + weapon.damage)/1.25                playerDefense = (basePlayerDefense + armor.defense)*1.25                break            elif combatAction == "0": #Quit                playerDefense = basePlayerDefense                playerDamage = basePlayerDamage                playerHP = 0                break                        else:#If the player enters an invalid option, print error and restart loop.                print "Please choose an option or type 0 to quit.\n"        if playerHP <= 0: #If player is dead, end combat without attacking            break        #Enemy attack phase: Same as player attack phase but randomize attack method        combatAction = random.randint(1,3)        if combatAction == 1: #Normal attack            enemyDamage = baseEnemyDamage + weapon.damage            enemyDefense = baseEnemyDefense + armor.defense        elif combatAction == 2: #Power attack            enemyDamage = (baseEnemyDamage + weapon.damage)*1.25            enemyDefense = (baseEnemyDefense + armor.defense)/1.25        elif combatAction== 3: #Defend            enemyDamage = (baseEnemyDamage + weapon.damage)/1.25            enemyDefense = (baseEnemyDefense + armor.defense)*1.25        if enemyHP <= 0: #If enemy is dead, end combat without attacking            break        #Defend phase: Subtract defense from damage        enemyDamage = enemyDamage - playerDefense        playerDamage = playerDamage - enemyDefense        #If damage is negative, set damage = 0        if playerDamage < 0:            playerDamage = 0.0        if enemyDamage < 0:            enemyDamage = 0.0                    #End phase: Deal damage. Print damage and HP if both players are alive.        enemyHP = enemyHP - playerDamage        playerHP = playerHP - enemyDamage        if playerHP > 0 and enemyHP > 0:                        print "You did " + `playerDamage` + " damage."            print "The enemy did " + `enemyDamage` + " damage."            print "HP = " + `playerHP`            print "Enemy HP = " + `enemyHP`            print ""    #Post-combat: Decide winner and update stats accordingly, then print wins/losses.    if enemyHP <= 0:        if playerHP <= 0: #If player and enemy are both dead, set playerHP to 1. This avoids negative playerHP values if the player wins.            playerHP = 1.0        print "You win with " + `playerHP` + " HP left!"        wins += 1    elif playerHP <= 0:        print "You lose! The enemy had " + `enemyHP` + " HP left.\n"        losses += 1    print "You have won " + `wins` + " times, and lost " + `losses` + " times.\n"        fightAgain = ""    while fightAgain != "y" and fightAgain != "n":        fightAgain = raw_input("Do you want to fight another enemy? (y/n)").lower()        if fightAgain == "y":            combat()        if fightAgain == "n":            break        else:            print "Please type 'y' or 'n'."            pass    print ""        def viewCharacter():#View character and equipment info    print "Damage = " + `basePlayerDamage`    print "Defense = " + `basePlayerDefense`    print "Weapon = " + weapon.name    print "Weapon damage = " + `weapon.damage`    print "Armor = " + armor.name    print "Armor defense = " + `armor.defense`    print ""    def stats():#View player stats (win/loss, other?)    print "Total wins: " + `wins`    print "Total losses: " + `losses`    if losses > 0: #If losses = 0, don't try to divide by 0.        print "Win/loss ratio: " + `wins/losses*10` + "%"    else:        print "Win/loss ratio: 100%"def showHelp():    print "To select an option, type its number, then press enter. There are 3 different combat methods, and each one has different effects."    print "1. Normal attack: Does normal damage and normal defense."    print "2. Power attack: Does 1.25 times the normal damage, but divides defense by 1.25."    print "3. Defend: Divides damage by 1.25, but multiplies defense by 1.25.\n"    print "Defense is subtracted from attack after each turn."    print ""    raw_input("Press enter to go back to the main menu.")    #Main function begins here.menuSelection = ""while menuSelection != "0":    menuSelection = mainMenu()    if menuSelection == "1":#Fight        combat(playerHP, enemyHP, basePlayerDamage, baseEnemyDamage, basePlayerDefense, baseEnemyDefense)    if menuSelection == "2":#View character        viewCharacter()    if menuSelection == "3":#View stats        stats()    if menuSelection == "4":#Save        save()    if menuSelection == "5":#Load        load()    if menuSelection == "6":#Help        showHelp()          if menuSelection == "0":#Quit        sys.exit()