[Tutor] Python - RPG Combat System

David Hutto smokefloat at gmail.com
Sat Jul 31 20:04:30 CEST 2010


On Sat, Jul 31, 2010 at 1:51 PM, bob gailer <bgailer at gmail.com> wrote:
> On 7/30/2010 10:49 PM, Jason MacFiggen wrote:
>>
>> what can I do instead of writing print so many times?
>> import random
>>    my_hp = 50
>>    mo_hp = 50
>>    my_dmg = random.randrange(1, 20)
>>    mo_dmg = random.randrange(1, 20)
>>    while True:
>>        if mo_hp < 0:
>>            print "The Lich King has been slain!"
>>        elif my_hp < 0:
>>            print "You have been slain by the Lich King!"
>>        if mo_hp <= 0:
>>            break
>>        elif my_hp <= 0:
>>            break
>>        else:
>>            print "Menu Selections: "
>>            print "1 - Attack"
>>            print "2 - Defend"
>>            print
>>            choice = input ("Enter your selection. ")
>>            choice = float(choice)
>>            print
>>        if choice == 1:
>>            mo_hp = mo_hp - my_dmg
>>            print "The Lich King is at ", mo_hp, "Hit Points"
>>            print "You did ", my_dmg, "damage!"
>>            print
>>            my_hp = my_hp - mo_dmg
>>            print "I was attacked by the lk for ", mo_dmg," damage!"
>>            print "My Hit Points are ", my_hp
>>            print
>>        elif choice == 2:
>>            mo_hp = mo_hp - my_dmg / 2
>>            print "The Lich King is at", mo_hp, "Hit Points"
>>            print "you did ", my_dmg / 2, "damage!"
>>            print
>>            my_hp = my_hp - mo_dmg
>>            print "I was attacked by the lk for ", mo_dmg," damage!"
>>            print "My Hit Points are ", my_hp
>>            print
>
> Most of the statements in each choice block are identical. Factor them out,
> giving:
>
>        if choice == 1:
>            mo_hp = mo_hp - my_dmg
>            print "you did ", my_dmg /, "damage!"
>        elif choice == 2:
>            mo_hp = mo_hp - my_dmg / 2
>            print "you did ", my_dmg / 2, "damage!"
>        print "The Lich King is at", mo_hp, "Hit Points"
>        my_hp = my_hp - mo_dmg
>        print "You did ", my_dmg, "damage!"
>        print
>        print "I was attacked by the lk for ", mo_dmg," damage!"
>        print "My Hit Points are ", my_hp
>        print
>
> You could (better) move these statements into a function, passing 1 or 2 as
> the divisor for my_dmg and returning the updated values for my_ho

You mean my_dmg I think.

 and my_hp.
>
> def update(factor):
>    print "The Lich King is at", mo_hp, "Hit Points"
>    print "you did ", my_dmg / factor, "damage!"
>    print
>    print "I was attacked by the lk for ", mo_dmg," damage!"
>    print "My Hit Points are ", my_hp
>    print
>    return mo_hp - my_dmg / factor, my_hp - mo_dmg
> ...
>        if choice == 1:
>            mo_hp, my_hp = update(1)
>        elif choice == 2:
>            mo_hp, my_hp = update(2)
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list