因此,我正在制作一个战斗游戏,直到我添加了攻击功能之前,它一直运行良好。现在,当我运行它时它崩溃了。我不知道我做错了什么!
我试图更改用于攻击的变量并移动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")
我希望玩家能够进行攻击和防御,而机器人可以做到这一点,但是只要按下“攻击”按钮,游戏就会自动冻结。
这是完整的代码:
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")
谢谢!
答案 0 :(得分:0)
由于其他原因,由于您未提供所有代码,因此无法确定您的问题是什么。我会指出一些可疑的东西,因为这很可能是您的问题...
您的第二段代码是一个循环,因此有冻结的机会。退出该循环的唯一方法是使“ eHP”和“ pHP”都变为0。从我看到的代码中,很可能永远不会发生这种情况,并且您将永远陷入该循环。 >
没有看到更多的代码,我不确定这一点,但是非常可疑的是,您只对正在测试的两个变量进行了减法运算。我不知道您要减去的值从何而来,但是如果它们都是正数,那么您很容易让正在测试的两个值都变为负数,并且无法摆脱循环将它们都恢复为0。
也许您想测试'<0'而不是'!= 0'的值,也许您想要'and'而不是'or'。不可能知道您提供了什么。
答案 1 :(得分:0)
一切顺利,直到我添加了攻击功能。现在它 运行时崩溃。我不知道我做错了什么!
此代码有很多问题,其中一些好的语法检查程序应该警告过您:使用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 "=="
变量pDMG
和eDMG
从未在任何地方定义。
变量turnToken
在代码中的不同位置也以名称TurnToken
命名。
全局变量turnToken
和eHP
在buttons()
中进行了修改,因此必须声明:
def buttons(x, y):
global turnToken, eHP
这些呼叫对您没有任何帮助,因此您可以将其删除:
m1.begin_poly()
m1.end_poly()
m2.begin_poly()
m2.end_poly()
最后,您的主循环针对乌龟的事件性质进行工作,应将其重铸为计时器事件。
下面,我已尽最大可能修补并重新编写了您的代码-我可能在过程中引入了新的错误,但现在基本上可以运行了:
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()