
-----------------------------------
Zren
Thu Jan 14, 2010 10:19 pm

Simple Turn-Based Battle
-----------------------------------
Learning Python. Python board looks lonely. Thus I will post some slightly interesting things with my increasing knowledge in Python.


import random

class Unit:
    def __init__(self, n, hp, s, d, a):
        self.name = n
        self.maxHP = hp
        self.curHP = hp
        self.strength = s
        self.defence = d
        self.agility = a

def battle( a, b ):
    turn = 1
    
    if a.agility > b.agility:
        attacker = a
        defender = b
    else:
        attacker = b
        defender = a

    doBattle = True
    while (doBattle):
        print "\n ---  Turn " , turn , ": " , attacker.name , " ---"
        print attacker.name , ": " , attacker.curHP
        print defender.name , ": " , defender.curHP , "\n"
        
        if defender.agility > attacker.agility and random.randint(1,4) == 4:
            print attacker.name , " missed."
        else:
            attackDamage = attacker.strength - defender.defence
            if attackDamage < 0:
                attackDamage = 0
            defender.curHP -= attackDamage
            print attacker.name , " did " , attackDamage , " damage to " , defender.name , "."
                
            
        if defender.curHP 