Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Simple Turn-Based Battle
Index -> Programming, Python -> Python Submissions
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zren




PostPosted: Thu Jan 14, 2010 10:19 pm   Post subject: Simple Turn-Based Battle

Learning Python. Python board looks lonely. Thus I will post some slightly interesting things with my increasing knowledge in Python.

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 <= 0:
            doBattle = False
            print "\n\n" , attacker.name , " won the battle!"
        else:
            temp = defender
            defender = attacker
            attacker = temp
            turn = turn + 1
   
   
name = raw_input("Your name good sir.\nName: ")

character = Unit(name, random.randint(10,30), random.randint(2,5), random.randint(1,3), random.randint(1,5) )
while raw_input("Fight? [y/n]: ") == "y":
    character.curHP = character.maxHP
    enemy = Unit("Monster", random.randint(10,30), random.randint(2,5), random.randint(1,3), random.randint(1,5) )
    battle( character, enemy )

Sponsor
Sponsor
Sponsor
sponsor
Notthebbq




PostPosted: Fri Apr 01, 2011 11:52 am   Post subject: Re: Simple Turn-Based Battle

you should put delays because when i run this it runs down the screen in like 5 sec
apython1992




PostPosted: Fri Apr 01, 2011 11:59 am   Post subject: RE:Simple Turn-Based Battle

Great to see some action on the Python boards. Do you plan to make this into a game of sorts?
Zren




PostPosted: Mon Apr 04, 2011 5:26 pm   Post subject: RE:Simple Turn-Based Battle

No intent at all of building upon this. I think I wrote it to give the idea across to someone making a game in Turing. I'm not much interested in plaguing the world with another text based adventure either.
apython1992




PostPosted: Mon Apr 04, 2011 6:24 pm   Post subject: RE:Simple Turn-Based Battle

That's a great idea. Python's a great next step from Turing.
QuantumPhysics




PostPosted: Fri Jun 01, 2012 11:10 am   Post subject: RE:Simple Turn-Based Battle

apython i bump that. Its more of a scripting language though.
Aange10




PostPosted: Fri Jun 01, 2012 12:04 pm   Post subject: RE:Simple Turn-Based Battle

--- Turn 1208109 : Monster ---
Monster : 18
David : 19

Monster did 0 damage to David .


Oh, how amazing I am.
Display posts from previous:   
   Index -> Programming, Python -> Python Submissions
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: