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

My game keeps crashing after I added the attack code

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

我尝试更改攻击变量并移动 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")

我希望玩家能够攻击和防御,而机器人也能做同样的事情,但是每当按下 'attack' 按钮时,游戏就会自动停止。

完整代码如下:

import turtle
from turtle import *
import time
import random
penup()
hideturtle()
window = Screen()
wn = window
wn.screensize()
wn.setup(width = 1.0, height = 1.0)
m1 = Turtle()
m1.speed(0)
m1.hideturtle()
m1.fillcolor("lime")
m2 = Turtle()
m2.speed(0)
m2.hideturtle()
m2.fillcolor("red")
txt = Turtle()
txt.speed(0)
txt.pensize(15)
txt.hideturtle()
txt.speed(0)
pHP = 100
pATK = 10
pDEF = 1
eHP = 50
eATK = 10
eDEF = 1
turnToken = 1
def pSlime ():
    m1.begin_poly()
    m1.penup()
    m1.goto(-300,200)
    m1.pendown()
    m1.begin_fill()
    m1.left(180)
    m1.forward(180)
    m1.circle(20,90)
    m1.forward(360)
    m1.circle(20,90)
    m1.forward(360)
    m1.circle(20,90)
    m1.forward(360)
    m1.circle(20,90)
    m1.forward(180)
    m1.end_fill()
    m1.fillcolor("black")
    m1.penup()
    m1.goto(-350,100)
    m1.pendown()
    m1.begin_fill()
    m1.forward(20)
    m1.right(90)
    m1.forward(20)
    m1.right(90)
    m1.forward(20)
    m1.right(90)
    m1.forward(20)
    m1.end_fill()
    m1.left(90)
    m1.penup()
    m1.goto(-250,120)
    m1.pendown()
    m1.begin_fill()
    m1.forward(20)
    m1.right(90)
    m1.forward(20)
    m1.right(90)
    m1.forward(20)
    m1.right(90)
    m1.forward(20)
    m1.end_fill()
    m1.penup()
    m1.goto(-400,0)
    m1.pendown()
    m1.begin_fill()
    m1.forward(20)
    m1.right(90)
    m1.forward(100)
    m1.right(90)
    m1.forward(20)
    m1.right(90)
    m1.forward(100)
    m1.end_fill()
    m1.end_poly()
def fireSlime ():
    m2.begin_poly()
    m2.penup()
    #m2.goto(-300,200)
    m2.pendown()
    m2.begin_fill()
    m2.left(180)
    m2.forward(180)
    m2.circle(20,90)
    m2.forward(360)
    m2.circle(20,90)
    m2.forward(360)
    m2.circle(20,90)
    m2.forward(360)
    m2.circle(20,90)
    m2.forward(180)
    m2.end_fill()
    m2.fillcolor("black")
    m2.penup()
    m2.goto(-350 +550,100 +150)
    m2.pendown()
    m2.begin_fill()
    m2.forward(20)
    m2.right(90)
    m2.forward(20)
    m2.right(90)
    m2.forward(20)
    m2.right(90)
    m2.forward(20)
    m2.end_fill()
    m2.left(90)
    m2.penup()
    m2.goto(-250 + 550,120 + 150)
    m2.pendown()
    m2.begin_fill()
    m2.forward(20)
    m2.right(90)
    m2.forward(20)
    m2.right(90)
    m2.forward(20)
    m2.right(90)
    m2.forward(20)
    m2.end_fill()
    m2.penup()
    m2.goto(-400 + 650,150)
    m2.pendown()
    m2.begin_fill()
    m2.forward(20)
    m2.right(90)
    m2.forward(100)
    m2.right(90)
    m2.forward(20)
    m2.right(90)
    m2.forward(100)
    m2.end_fill()
    m2.end_poly()
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)
pSlime()
txt.penup()
txt.goto(-800, -230)
txt.pendown()
txt.forward(750)
txt.left(50)
txt.forward(100)
txt.right(50)
txt.forward(800)
txt.penup()
txt.goto(-800, 230)
txt.pendown()
txt.pensize(5)
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=("Agency FB", 15, "bold"))
txt.penup()
txt.forward(20)
txt.goto(300,300)
txt.pensize(3)
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.up()
m2.up()
m2.goto(300,350)
m2.down()
fireSlime()
txt.goto(110,-220)
txt.write("Attack ", align="left", font=("Agency FB", 40, "bold"))
txt.forward(100)
txt.write("Defend ", align="left", font=("Agency FB", 40, "bold"))
txt.left(90)
txt.forward(200)
txt.write("Exit Battle ", align="left", font=("Agency FB", 40, "bold"))
txt.left(90)
txt.forward(100)
txt.write("Item ", align="left", font=("Agency FB", 40, "bold"))
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")

提前致谢!

由于其他人所说的原因,由于您没有提供所有代码,因此无法确定您的问题是什么。我看到一些可疑的东西,我会指出来,因为它很可能是你的问题...

您的第二个代码块是一个循环,因此有机会冻结。跳出该循环的唯一方法是 'eHP' 和 'pHP' 都变为 0。从我看到的代码来看,这种情况很可能永远不会发生,您将陷入该循环永远。

没有看到更多你的代码,我不能肯定这一点,但你只对你正在测试的两个变量进行减法是非常可疑的。我不知道你减去的值从哪里来,但如果它们都是正数,那么你可以很容易地让你正在测试的任何一个值变为负数并且无法摆脱你的循环通过让它们都回到 0.

也许您想测试这些值是否为“< 0”而不是“!= 0”,也许您想要 'and' 而不是 'or'。不可能知道你提供的内容。

it was going well until I added the functionality of attack. Now it crashes when I run it. I don't know what I did wrong!

这段代码有很多问题,其中一些问题是一个好的语法检查器应该警告过你的:你对 randint() 的使用不符合模块名称:

import random
AtkDef = randint(1,2)

任一:

import random
AtkDef = random.randint(1,2)

或:

from random import randint
AtkDef = randint(1,2)

您在此变量赋值中存在语法错误:

if pATK == 1:
    AtkDef == 1  # should be "=" not "=="

变量 pDMGeDMG 从未在任何地方定义。

变量 turnToken 在代码的不同位置也使用名称 TurnToken

全局变量turnTokeneHPbuttons()中被修改,所以必须声明:

def buttons(x, y):
    global turnToken, eHP

这些调用对您没有任何作用,因此您可以删除它们:

m1.begin_poly()
m1.end_poly()
m2.begin_poly()
m2.end_poly()

最后,您的主循环与 turtle 的事件性质背道而驰,应该重铸为计时器事件。

下面我已经尽我所能修补和修改了你的代码——我可能在过程中引入了新的错误,但它现在基本上可以运行了:

from turtle import Screen, Turtle
from random import randint

LARGE_FONT = ("Agency FB", 40, "bold")
SMALL_FONT = ("Agency FB", 15, "bold")

def pSlime():
    m1.penup()
    m1.goto(-480, 200)
    m1.left(180)
    m1.pendown()

    m1.begin_fill()
    for _ in range(4):
        m1.circle(20, 90)
        m1.forward(360)
    m1.end_fill()

    m1.penup()
    m1.goto(-350, 100)
    m1.pendown()
    m1.fillcolor("black")

    m1.begin_fill()
    for _ in range(4):
        m1.forward(20)
        m1.right(90)
    m1.end_fill()

    m1.penup()
    m1.goto(-250, 120)
    m1.left(180)
    m1.pendown()

    m1.begin_fill()
    for _ in range(4):
        m1.forward(20)
        m1.right(90)
    m1.end_fill()

    m1.left(90)
    m1.penup()
    m1.goto(-400, 0)
    m1.pendown()

    m1.begin_fill()
    for _ in range(2):
        m1.forward(20)
        m1.right(90)
        m1.forward(100)
        m1.right(90)
    m1.end_fill()

def fireSlime():
    m2.penup()
    m2.goto(120, 350)
    m2.left(180)
    m2.pendown()

    m2.begin_fill()
    for _ in range(4):
        m2.circle(20, 90)
        m2.forward(360)
    m2.end_fill()

    m2.penup()
    m2.goto(550 - 350, 100 + 150)
    m2.pendown()
    m2.fillcolor("black")

    m2.begin_fill()
    for _ in range(4):
        m2.forward(20)
        m2.right(90)
    m2.end_fill()

    m2.penup()
    m2.goto(550 - 250, 120 + 150)
    m2.left(180)
    m2.pendown()

    m2.begin_fill()
    for _ in range(4):
        m2.forward(20)
        m2.right(90)
    m2.end_fill()

    m2.penup()
    m2.goto(650 - 400, 150)
    m2.left(90)
    m2.pendown()

    m2.begin_fill()
    for _ in range(2):
        m2.forward(20)
        m2.right(90)
        m2.forward(100)
        m2.right(90)
    m2.end_fill()

def buttons(x, y):
    global turnToken, eHP

    if 17 < x < 299 and y -230 < y < -161:
        eHP -= pDMG
        screen.textinput("", "attack")
        turnToken -= 1
    if 300 < x < 671 and -234 < y < -153:
        screen.textinput("", "Coming soon")
    if 300 < x < 671 and -356 < y < -235:
        screen.textinput("", "You can't run during the tutorial!")
    if 17 < x < 299 and -356 < y < -235:
        if eATK != 1:
            eDMG -= pDEF
            screen.textinput("", "defend")
            turnToken -= 1
        else:
            screen.textinput("", "Whoops! looks like the enemy's attack has reached 1 or less! Try something else!")

def play():
    global turnToken

    pHP = 100
    pATK = 10

    if eHP != 0 or pHP != 0:
        if turnToken == 1:
            screen.onscreenclick(buttons, 1)
        else:
            screen.onscreenclick(None, 1)

            turnToken += 1

            AtkDef = randint(1, 2)
            if pATK == 1:
                AtkDef = 1

            if AtkDef == 1:
                pHP -= eATK
                screen.textinput("", "attack")
            else:
                pATK -= eDEF
                screen.textinput("", "defend")

        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 = 1
eDEF = 1

pDMG = 5
eDMG = 5

eHP = 50
eATK = 10

turnToken = 1

pSlime()

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()  # only needs to be called once, can't be undone

play()

screen.mainloop()