jeudi 15 mars 2018

python turtle if error

I wanna make python snake game by using turtles and make levels. So, I checked score to decide user's level But, when the programs run [if statements], the movement of the turtle I already made gonna be slow. I don't know the reason of this error and How to figure out. I need your help ;(

def level():
global speed
if 0 <= score < 5:
    mypen.penup()
    mypen.hideturtle()
    mypen.setposition(-290, 310)
    levelstring = "level: 1"
    mypen.write(levelstring, False, align="left", font=('Consolas', 15, 'bold'))
    speed += 1
if 5 <= score < 10:
    mypen.penup()
    mypen.hideturtle()
    mypen.setposition(-290, 310)
    levelstring = "level: 2"
    mypen.write(levelstring, False, align="left", font=('Consolas', 15, 'bold'))
    speed += 2
elif 10 <= score:
    exit()


while True:
alex.forward(speed)

#boundary checking
if alex.xcor() > 300 or alex.xcor() < -300:
    alex.right(180)

if alex.ycor() > 300 or alex.ycor() < -300:
    alex.right(180)

# boundary checking
if ts.xcor() > 300 or ts.xcor() < -300:
    ts.right(180)

if ts.ycor() > 300 or ts.ycor() < -300:
    ts.right(180)

#collision.checking
d = math.sqrt(math.pow(alex.xcor()-ts.xcor(),2) + math.pow(alex.ycor()-ts.ycor(),2))
if d < 20:
    ts.setposition(random.randint(-300, 300), random.randint(-300, 300))
    score += 1
    level()

#Draw the score
mypen.penup()
mypen.hideturtle()
mypen.setposition(250,310)
scorestring = "Score: %s" %score
mypen.write(scorestring, False, align= "left", font=('Consolas', 15, 'bold'))

Aucun commentaire:

Enregistrer un commentaire