1 回答
TA贡献1828条经验 获得超13个赞
一直很顺利,直到我添加了攻击功能。现在当我运行它时它崩溃了。我不知道我做错了什么!
这段代码有很多问题,一个好的语法检查器应该警告你其中一些问题:你的使用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()
添加回答
举报