CombatSimulation.py

From WLCS
Revision as of 12:54, 24 April 2013 by Admin (talk | contribs) (Created page with "# This class will run a combat simulation using the Weapon and Creature classes from creature import * #Warrior WARRIOR_NAME = "Gnusto Frotz"; WARRIOR_TYPE = "valiant warrior"; ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  1. This class will run a combat simulation using the Weapon and Creature classes

from creature import *

  1. Warrior

WARRIOR_NAME = "Gnusto Frotz"; WARRIOR_TYPE = "valiant warrior"; WARRIOR_PRONOUN = "his"; WARRIOR_HP = 50; WARRIOR_STR = 10; WARRIOR_SKILL = 15; WARRIOR_AC = 5;

  1. monster creature

MONSTER_NAME = "Schmoo"; MONSTER_TYPE = "gruesome monster"; MONSTER_PRONOUN = "its"; MONSTER_HP = 50; MONSTER_STR = 15; MONSTER_SKILL = 10; MONSTER_AC = 7;

  1. Monster's claws weapon

CLAWS_DAMAGE_MOD = 3; CLAWS_NAME = "claws"; CLAWS_HIT = "slashes"; CLAWS_MISS = "swipes at but misses";

  1. Warrior's sword weapon

SWORD_DAMAGE_MOD = 5; SWORD_NAME = "sword"; SWORD_HIT = "stabs"; SWORD_MISS = "swings at but misses";

  1. Create the warrior's weapon (sword)

sword = Weapon(SWORD_NAME, SWORD_DAMAGE_MOD, SWORD_HIT, SWORD_MISS);

  1. Create the monster's weapon (claws)

claws = Weapon() claws.damageModifier = CLAWS_DAMAGE_MOD claws.bane = CLAWS_NAME claws.hitVerb = CLAWS_HIT claws.missVerb = CLAWS_MISS

  1. Create the warrior

warrior = Creature() warrior.hitPoints = WARRIOR_HP warrior.strenght = WARRIOR_STR warrior.skill = WARRIOR_SKILL warrior.armorClass = WARRIOR_AC warrior.name = WARRIOR_NAME warrior.type = WARRIOR_TYPE warrior.pronoun = WARRIOR_PRONOUN

  1. Create the monster

monster = Creature(MONSTER_NAME, MONSTER_TYPE, MONSTER_PRONOUN, MONSTER_HP, MONSTER_STR, MONSTER_SKILL, MONSTER_AC)

  1. Arm the creatures with their weapons

monster.weapon = claws warrior.weapon = sword

  1. While both creatures are still alive...

while monster.isAlive() and warrior.isAlive():

   # The monster tries to attack the warrior
   if monster.tryToAttack(warrior.armorClass):
       # If the monster hit, the warrior takes damage
       print(monster.constructHitString(warrior))
       warrior.takeDamage(monster.calcHitDamage())
   else:
       print(monster.constructMissString(warrior))
   print(warrior.name + ":" + str(warrior.hitPoints) + " HP")
   print(monster.name + ":" + str(monster.hitPoints) + " HP")
   print()
           
   # If the warrior dies, break out of this loop
   if warrior.isAlive() == False:
       break
   
   # The warrior tries to attack the monster
   if warrior.tryToAttack(monster.armorClass):
       # If the warrior hit, the monster takes damage
       print(warrior.constructHitString(monster))
       monster.takeDamage(warrior.calcHitDamage())
   else:
       print(warrior.constructMissString(monster))
   print(warrior.name + ":" + str(warrior.hitPoints) + " HP")
   print(monster.name + ":" + str(monster.hitPoints) + " HP")    
   print()
  1. Figure out whose alive and dead (good example of the ? : operator)

if warrior.isAlive() == True:

   alive = warrior
   dead = monster

else:

   alive = monster
   dead = warrior
  1. print out a message to indicate the outcome of the battle.

print(dead.name + " dies. ") print(alive.name + " is victorious!")