Created
October 25, 2012 23:19
-
-
Save Tyaedalis/3956085 to your computer and use it in GitHub Desktop.
Fighter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, math, sys | |
from room_gen import * | |
running = True | |
winTarget = 4 # how many wins until victory | |
def rollDice(sides, times=1): | |
""" | |
Simulates the rolling of die. | |
""" | |
## Input: 1 or 2 ints; times defaults to 1 | |
## Returns: rand num between (1*times) and (sides*times) | |
## | |
## Variables: | |
## roll: cummulative value of dice rolls | |
roll = 0 | |
for i in range(times): | |
roll += random.randint(1, sides) | |
return roll | |
class Fighter: | |
def __init__(self, name='Unnamed Gladiator'): | |
self.stats = self.rollStats() | |
self.wins = 0 | |
self.name = name | |
## self.xpos, self.ypos = self.spawn() | |
self.printStats(True) | |
def rollStats(self): | |
""" | |
Fully rolls the stats and returns them as a dict. | |
""" | |
## Returns: dict of stats and their values | |
stats = { "Strength":0, | |
"Vitality":0, | |
"Speed":0 } | |
sum = 0 | |
for stat in stats: # Roll stats... | |
stats[stat] = rollDice(6, 3) | |
sum += stats[stat] | |
if (sum < len(stats)*6): # Sum of stats must be greater than | |
stats = rollStats() # 6 * (num of stats) to pass | |
stats['HP'] = stats['Vitality'] * 2 # More HP makes for better play | |
return stats | |
def printStats(self, name=False): | |
""" | |
Prints out a table of entity's stats. | |
""" | |
## Input: TRUE: print name on top | |
## (default)FALSE: omit name | |
## Output: Prints table of entity's stats. | |
if name == True: | |
print(self.name) | |
print('='*len(self.name)) | |
for stat in self.stats: | |
if stat != 'HP': | |
print("{:10} {}".format(str(stat)+':', \ | |
self.stats[stat])) | |
print() | |
def attack(self, opponent): | |
""" | |
Calculates damage of an attack to one opponent. | |
""" | |
## Input: opponent: the entity to attack | |
## Output: Prints informative text describing the attack. | |
## | |
## Notes: The calculations are probably placeholders | |
## and are not thought through. | |
## | |
## Variables: | |
## power: how much damage the attacker can hit for | |
## block: how much damage the opponent can block | |
## damage: total damage received by the opponent | |
power = math.floor((self.stats['Strength']+\ | |
(rollDice(4)+self.stats['Speed'])/2)/2) | |
block = math.floor(((opponent.stats['Strength']+\ | |
opponent.stats['Speed'])/2-\ | |
(self.stats['Speed']/\ | |
rollDice(3)))) | |
# block can't be less than 0; | |
# negative block equals unaccounted damage | |
if block < 0: | |
block = 0 | |
# complex calculations for damage | |
damage = power - block | |
# display attack message if damage is done | |
if damage > 0: | |
print("\n{} attacks {} for {} damage!".format(self.name, \ | |
opponent.name, \ | |
power)) | |
# make note of any blocked damage | |
if block > 0: | |
print("{} blocks {} of it.".format(opponent.name, block)) | |
# otherwise, we say the attacker missed | |
else: | |
damage = 0 | |
print("\n{} missed!".format(self.name)) | |
# we don't need negative HP, but it could be used to calculate | |
# "overkill" if we want; just use the 'else' in that case | |
if opponent.stats['HP'] < damage: | |
opponent.stats['HP'] = 0 | |
else: opponent.stats['HP'] -= damage # actually deals damage | |
class Player(Fighter): | |
def __init__(self, name='Unnamed Gladiator'): | |
self.stats = self.rollStats() | |
self.wins = 0 | |
self.name = input("What's your name?\n> ").capitalize() | |
## self.xpos, self.ypos = self.spawn() | |
chances = 3 # Player has this many chances to reroll | |
for x in range(chances): # reroll until player is happy or | |
# player runs out of chances | |
self.printStats(True) | |
reroll = input("\nRe-roll stats? (You have {} chances left.) (y/n)\n> "\ | |
.format(chances-x)) | |
if reroll.lower() == 'y': | |
self.stats = self.rollStats() | |
elif reroll.lower() != 'n': | |
print("Please enter 'y' or 'n'.") | |
else: | |
break | |
# Informative text... | |
print("You can press 'q' to quit any time after the game begins.") | |
player = Player() | |
while player.wins < winTarget: # keep fighting until win limit reached | |
opponent = Fighter() | |
round = 1 | |
while True: # Each iteration is one opponent | |
if input("\nRound {}: ".format(round)) == 'q': | |
break | |
player.attack(opponent) | |
print('Bandit HP: {}/{}'.format(opponent.stats['HP'], \ | |
opponent.stats['Vitality']*2)) | |
# check if opponent is dead | |
if opponent.stats['HP'] == 0: | |
print("\nBandit has been slain!\n") | |
player.wins += 1 | |
print("Kills:", player.wins) | |
break | |
opponent.attack(player) | |
print('Player HP: {}/{}'.format(player.stats['HP'], \ | |
player.stats['Vitality']*2)) | |
# check if player is dead | |
if player.stats['HP'] == 0: | |
print("\nPlayer has been slain!\n") | |
print("You lose. Game over.") | |
sys.exit() | |
round += 1 | |
if input("") == 'q': | |
break | |
print("Congratulations! You're the victor!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment