Python程序的BASIC

2024-06-01 02:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个小程序,看看我对Python的熟练程度是否和FreeBasic一样高(我对FreeBasic不是很在行)。显然,我问这个问题是因为答案是否定的

所以这个程序是一个小型的地下城和龙(第二版)战斗发生器。由于某些原因,许多函数根本不执行。它们只是被跳过。这就是attaque1()attaque2()和{}最有可能发生的情况(因为cnt变量根本没有递增)。我试着全球化了很多变量,认为这可能是问题所在(我认为所有变量都是默认使用FreeBasic全球化的)。嗯,这似乎不是答案。虫子还在那里,我完全不知道是什么引起的。在

(代码中有一些法语。)

#-*- 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()

Tags: ledefderandomastglobalprintrandint
2条回答

BASIC和Python的构建原理不同。这有点像从使用降落伞到驾驶飞机。通过一个好的Python教程,比如Learn Python the Hard Way来学习,它将消除您的困惑和问题。在

attaque1()attaque2()是从calcInitiative()调用的,因此如果不调用它们,它们也不会被调用。在

您的while循环在((pv1 <= 0) or (pv2 <= 0))时执行

但你已经把它们定义为

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

所以两者都不能<= 0,所以while循环永远不会进入,并且calcInitiative()也永远不会被调用。在

您编写的Python代码就像它是基本的一样。您应该阅读Python教程,可能还有面向对象编程的一般教程,以了解类之类的东西。在

对你自己来说,一个很好的测试是你应该能够在根本不使用globals的情况下编写该程序。在

相关问题 更多 >