添加攻击代码后,我的游戏一直崩溃

所以我正在制作一个战斗游戏,在我添加了攻击功能之前,它进行得很顺利。现在当我运行它时它崩溃了。我不知道我做错了什么!


我试图改变攻击的变量并移动 if 语句,但没有任何效果。


第一个添加部分


def buttons(x,y):

    if y > -230 and y < -161 and x > 17 and x < 299:

        eHP -= pDMG

        textinput("","attack")

        TurnToken -= 1

    if x > 300 and x < 671 and y > -234 and y < -153:

        textinput("","Coming soon")

    if x > 300 and x < 671 and y < -235 and y > -356:

         textinput("","you can't run during the tutorial!")

    if x > 17 and x < 299 and y < -235 and y > -356:

        if eATK != 1:

            eDMG -= pDEF

            textinput("","defend")

            TurnToken -= 1

        else:

            textinput("","Whoops! looks like the enemy's attack has reached 1 or less! Try something else!")

    print(x,y)

第二个添加部分


while eHP != 0 or pHP != 0:

    if turnToken == 1:

        onscreenclick(buttons ,1)

        listen()

    else:

        turnToken += 1

        AtkDef = randint(1,2)

        if pATK == 1:

            AtkDef == 1

        if AtkDef == 1:

            pHP -= eATK

            textinput("","attack")

        else:

            pATK -= eDEF

            textinput("","defend")

我希望玩家能够进行攻击和防御,而机器人也能够做到这一点,但是每当按下“攻击”按钮时,游戏就会自动冻结。


倚天杖
浏览 180回答 1
1回答

慕田峪7331174

一直很顺利,直到我添加了攻击功能。现在当我运行它时它崩溃了。我不知道我做错了什么!这段代码有很多问题,一个好的语法检查器应该警告你其中一些问题:你的使用randint()没有被模块名称限定:import randomAtkDef = randint(1,2)任何一个:import randomAtkDef = random.randint(1,2)或者:from random import randintAtkDef = randint(1,2)您在此变量赋值中存在语法错误:if pATK == 1:&nbsp; &nbsp; AtkDef == 1&nbsp; # should be "=" not "=="变量pDMG和eDMG永远不会在任何地方定义。该变量在代码的不同位置turnToken也有名称TurnToken。全局变量turnToken和eHP被修改buttons(),因此必须声明:def buttons(x, y):&nbsp; &nbsp; global turnToken, eHP这些调用对您没有任何作用,因此您可以删除它们:m1.begin_poly()m1.end_poly()m2.begin_poly()m2.end_poly()最后,您的主循环与乌龟的事件性质相悖,应重新转换为计时器事件。下面我已经尽我所能修补和重新编写了你的代码——我可能在过程中引入了新的错误,但它现在基本上可以运行:from turtle import Screen, Turtlefrom random import randintLARGE_FONT = ("Agency FB", 40, "bold")SMALL_FONT = ("Agency FB", 15, "bold")def pSlime():&nbsp; &nbsp; m1.penup()&nbsp; &nbsp; m1.goto(-480, 200)&nbsp; &nbsp; m1.left(180)&nbsp; &nbsp; m1.pendown()&nbsp; &nbsp; m1.begin_fill()&nbsp; &nbsp; for _ in range(4):&nbsp; &nbsp; &nbsp; &nbsp; m1.circle(20, 90)&nbsp; &nbsp; &nbsp; &nbsp; m1.forward(360)&nbsp; &nbsp; m1.end_fill()&nbsp; &nbsp; m1.penup()&nbsp; &nbsp; m1.goto(-350, 100)&nbsp; &nbsp; m1.pendown()&nbsp; &nbsp; m1.fillcolor("black")&nbsp; &nbsp; m1.begin_fill()&nbsp; &nbsp; for _ in range(4):&nbsp; &nbsp; &nbsp; &nbsp; m1.forward(20)&nbsp; &nbsp; &nbsp; &nbsp; m1.right(90)&nbsp; &nbsp; m1.end_fill()&nbsp; &nbsp; m1.penup()&nbsp; &nbsp; m1.goto(-250, 120)&nbsp; &nbsp; m1.left(180)&nbsp; &nbsp; m1.pendown()&nbsp; &nbsp; m1.begin_fill()&nbsp; &nbsp; for _ in range(4):&nbsp; &nbsp; &nbsp; &nbsp; m1.forward(20)&nbsp; &nbsp; &nbsp; &nbsp; m1.right(90)&nbsp; &nbsp; m1.end_fill()&nbsp; &nbsp; m1.left(90)&nbsp; &nbsp; m1.penup()&nbsp; &nbsp; m1.goto(-400, 0)&nbsp; &nbsp; m1.pendown()&nbsp; &nbsp; m1.begin_fill()&nbsp; &nbsp; for _ in range(2):&nbsp; &nbsp; &nbsp; &nbsp; m1.forward(20)&nbsp; &nbsp; &nbsp; &nbsp; m1.right(90)&nbsp; &nbsp; &nbsp; &nbsp; m1.forward(100)&nbsp; &nbsp; &nbsp; &nbsp; m1.right(90)&nbsp; &nbsp; m1.end_fill()def fireSlime():&nbsp; &nbsp; m2.penup()&nbsp; &nbsp; m2.goto(120, 350)&nbsp; &nbsp; m2.left(180)&nbsp; &nbsp; m2.pendown()&nbsp; &nbsp; m2.begin_fill()&nbsp; &nbsp; for _ in range(4):&nbsp; &nbsp; &nbsp; &nbsp; m2.circle(20, 90)&nbsp; &nbsp; &nbsp; &nbsp; m2.forward(360)&nbsp; &nbsp; m2.end_fill()&nbsp; &nbsp; m2.penup()&nbsp; &nbsp; m2.goto(550 - 350, 100 + 150)&nbsp; &nbsp; m2.pendown()&nbsp; &nbsp; m2.fillcolor("black")&nbsp; &nbsp; m2.begin_fill()&nbsp; &nbsp; for _ in range(4):&nbsp; &nbsp; &nbsp; &nbsp; m2.forward(20)&nbsp; &nbsp; &nbsp; &nbsp; m2.right(90)&nbsp; &nbsp; m2.end_fill()&nbsp; &nbsp; m2.penup()&nbsp; &nbsp; m2.goto(550 - 250, 120 + 150)&nbsp; &nbsp; m2.left(180)&nbsp; &nbsp; m2.pendown()&nbsp; &nbsp; m2.begin_fill()&nbsp; &nbsp; for _ in range(4):&nbsp; &nbsp; &nbsp; &nbsp; m2.forward(20)&nbsp; &nbsp; &nbsp; &nbsp; m2.right(90)&nbsp; &nbsp; m2.end_fill()&nbsp; &nbsp; m2.penup()&nbsp; &nbsp; m2.goto(650 - 400, 150)&nbsp; &nbsp; m2.left(90)&nbsp; &nbsp; m2.pendown()&nbsp; &nbsp; m2.begin_fill()&nbsp; &nbsp; for _ in range(2):&nbsp; &nbsp; &nbsp; &nbsp; m2.forward(20)&nbsp; &nbsp; &nbsp; &nbsp; m2.right(90)&nbsp; &nbsp; &nbsp; &nbsp; m2.forward(100)&nbsp; &nbsp; &nbsp; &nbsp; m2.right(90)&nbsp; &nbsp; m2.end_fill()def buttons(x, y):&nbsp; &nbsp; global turnToken, eHP&nbsp; &nbsp; if 17 < x < 299 and y -230 < y < -161:&nbsp; &nbsp; &nbsp; &nbsp; eHP -= pDMG&nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "attack")&nbsp; &nbsp; &nbsp; &nbsp; turnToken -= 1&nbsp; &nbsp; if 300 < x < 671 and -234 < y < -153:&nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "Coming soon")&nbsp; &nbsp; if 300 < x < 671 and -356 < y < -235:&nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "You can't run during the tutorial!")&nbsp; &nbsp; if 17 < x < 299 and -356 < y < -235:&nbsp; &nbsp; &nbsp; &nbsp; if eATK != 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eDMG -= pDEF&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "defend")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turnToken -= 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "Whoops! looks like the enemy's attack has reached 1 or less! Try something else!")def play():&nbsp; &nbsp; global turnToken&nbsp; &nbsp; pHP = 100&nbsp; &nbsp; pATK = 10&nbsp; &nbsp; if eHP != 0 or pHP != 0:&nbsp; &nbsp; &nbsp; &nbsp; if turnToken == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.onscreenclick(buttons, 1)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.onscreenclick(None, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turnToken += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AtkDef = randint(1, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if pATK == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AtkDef = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if AtkDef == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pHP -= eATK&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "attack")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pATK -= eDEF&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.textinput("", "defend")&nbsp; &nbsp; &nbsp; &nbsp; screen.ontimer(play, 1000)screen = Screen()screen.setup(width=1.0, height=1.0)m1 = Turtle(visible=False)m1.speed('fastest')m1.fillcolor("lime")m2 = Turtle(visible=False)m2.speed('fastest')m2.fillcolor("red")txt = Turtle(visible=False)txt.speed('fastest')txt.penup()pDEF = 1eDEF = 1pDMG = 5eDMG = 5eHP = 50eATK = 10turnToken = 1pSlime()fireSlime()txt.pensize(15)txt.goto(-800, -230)txt.pendown()txt.forward(750)txt.left(50)txt.forward(100)txt.right(50)txt.forward(800)txt.penup()txt.pensize(5)txt.goto(-800, 230)txt.pendown()txt.forward(700)txt.left(90)txt.forward(200)txt.left(90)txt.penup()txt.forward(570)txt.left(90)txt.forward(105)txt.write("\nWelcome to Evolve, a game of monsters, genetics, \nand battle. The slime on the left will be your first monster.\n Click the attack button to attack the fire slime on the right", align="left", font=SMALL_FONT)txt.pensize(3)txt.goto(300, 300)txt.forward(450)txt.pendown()txt.forward(85)txt.left(90)txt.forward(500)txt.left(180)txt.forward(900)txt.left(90)txt.forward(200)txt.left(180)txt.forward(200)txt.left(90)txt.right(180)txt.forward(362)txt.right(90)txt.forward(200)txt.penup()txt.goto(110, -220)txt.write("Attack", align="left", font=LARGE_FONT)txt.forward(100)txt.write("Defend", align="left", font=LARGE_FONT)txt.left(90)txt.forward(200)txt.write("Exit Battle", align="left", font=LARGE_FONT)txt.left(90)txt.forward(100)txt.write("Item", align="left", font=LARGE_FONT)screen.listen()&nbsp; # only needs to be called once, can't be undoneplay()screen.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python