Итак, я делаю игру в битву, и она шла хорошо, пока я не добавил функциональность атаки.Теперь он падает, когда я запускаю его.Я не знаю, что я сделал неправильно!
Я пытался изменить переменные для атаки и переместить операторы 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")
Заранее спасибо!