pythonfreebasic

BASIC to Python program


I have created a small program to see if I'm as proficient in Python as I am in FreeBasic (and I'm not that good with FreeBasic). Obviously, I'm asking this question because the answer is no.

So the this program is a small Dungeons and Dragons (2nd edition) combat generator. For some reason, many functions don't execute at all. They are simply skipped over. This is what happens with attaque1(), attaque2() and most likely with calcInitiative() (since the cnt variable is not incremented at all). I tried globalizing a lot of variables thinking this might be the issue (I think all variables are globalized by default with FreeBasic). Well, this doesn't seem to be the answer. The bug is still there and I have absolutely no idea what might cause it.

(The code has some French in it.)

#-*- coding: iso8859_1 -*-

import random

ca1 = 10
ca2 = 10
taco = 20
pv1 = random.randint(1,10)
pv1 = (pv1)
pv2 = random.randint(1,10)
pv2 = str(pv2)
cnt = 0
pv1Depart = pv1
pv2Depart = pv2
ast = "*" * 7
d20_1 = random.randint(1,20)
d8_1 = random.randint(1,8)
d20_2 = random.randint(1,20)
d8_2 = random.randint(1,8)

def intro():
    global ca1
    global ca2
    global pv1
    global pv2

    print "Imaginez deux guerriers de D&D 2e édition qui se battent."
    print
    print "Guerrier 1: " + str(pv1) + " PV, épée longue (1-8 points de dégât), TACO de 20, CA de " + str(ca1) + "."
    print "Guerrier 2: " + str(pv2) + " PV, épée longue (1-8 points de dégât), TACO de 20, CA de " + str(ca2) + "."
    print ""

def nouveauCombat():
    global cnt

    print ast + "NOUVEAU COMBAT" + ast
    print
    while ((pv1 <= 0) or (pv2 <= 0)):
        cnt = cnt + 1
        print ast + "ROUND " + str(cnt) + ast
        print
        calcInitiative()
        print
    print ast + "RESULTAT" + ast
    print
    resultat()

def calcInitiative():
    global pv1
    global pv2
    global initiative1
    global initiative2

    initiative1 = random.randint(1,10)
    initiative2 = random.randint(1,10)
    print "Le guerre 1 fait son jet d'initiative."
    print str(initiative1) + "!"
    print
    print "Le guerre 2 fait son jet d'initiative."
    print str(initiative2) + "!"
    print
    if initiative1 == initiative2:
        print "Les deux guerriers attaquent au même moment."
        print
        print ast + "ATTAQUE" + ast
        print
        attaque1()
        print
        attaque2()
    elif initiative1 < initiative2:
        print "Le guerrier 1 attaque en premier."
        print
        print ast + "ATTAQUE" + ast
        print
        attaque1()
        print
        if pv2 <= 0:
            print
            attaque2()
    else:
        print "Le guerrier 2 attaque en premier."
        print
        print ast + "ATTAQUE" + ast
        print
        attaque2()
        print
        if pv1 <= 0:
            print
            attaque2()

def attaque1():
    global d20_1
    global d8_1
    global pv2
    global ca2
    global pv2dep

    print "Le guerrier 1 fait son jet de toucher."
    print str(d20_1) + "!"
    if d20_1 >= ca2:
        print "Touché!"
        pv2 = pv2 - d8_1
        print str(d8_1) + "points de dégât!"
        print "Le guerrier 2 est à " + str(pv2) + "/" + str(pv2dep) + " PV!"
    else:
        print "Raté!"

def attaque2():
    global d20_2
    global d8_2
    global pv1
    global ca1
    global pv1dep

    print "Le guerrier 2 fait son jet de toucher."
    print str(d20_2) + "!"
    if d20_2 >= ca1:
        print "Touché!"
        pv1 = pv1 - d8_2
        print str(d8_2) + "points de dégât!"
        print "Le guerrier 1 est à " + str(pv1) + "/" + str(pv1dep) + " PV!"
    else:
        print "Raté!"

def resultat():
    global cnt

    print "Le combat prend fin au round " + str(cnt) + "."
    print 


intro()
nouveauCombat()

Solution

  • attaque1() and attaque2() are called from calcInitiative() so if it doesn't get called they won't either.

    Your while loop executes while ((pv1 <= 0) or (pv2 <= 0))

    but you've defined them to be

    pv1 = random.randint(1,10)
    pv1 = (pv1) # this line does nothing
    pv2 = random.randint(1,10)
    pv2 = str(pv2)
    

    So neither can ever be <= 0 so the while loop will never be entered, and calcInitiative() will never be called.

    You're writing your Python code like it were BASIC. You should go through the Python tutorial, and probably a general tutorial on object oriented programming, to learn about things like classes.

    A good test for yourself is you should be able to write that program without using globals at all.