mercredi 20 mai 2020

Having trouble with ''if'' in turtle python

Hello guys thanks for your time i'm new to python and after a few tutorials on turtle i wanted to make a little game like the snake game were you move a turtle to another turtle and when you get close to it it disappears and reappears in another place but i'm having a problem with the if statement nothing happens when i get the turtles close to each others here is the code

import turtle as t
import random as rd
import time as ti


obj1 = t.Turtle()
obj1.penup()
obj1.shape('turtle')
obj1.setpos(-200, -100)
obj1.speed(1)
obj1.color('yellow')



obj2 = t.Turtle()
obj2.penup()
obj2.shape('turtle')
obj2.color('red')
obj2.setpos(0, -100)


angel = range(0, 271, 90)
color = ['green', 'red', 'blue', 'orange', 'purple']

if obj1.distance(obj2) < 15:
    x = rd.randint(-300, 300)
    y = rd.randint(-300, 300)
    ang = rd.choice(angel)
    col = rd.choice(color)
    obj2.hideturtle()
    obj2.setpos(x, y)
    obj2.setheading(ang)
    obj2.color(col)
    obj2.showturtle()


def up():
    if obj1.heading() == 0 or obj1.heading() == 180:
        obj1.setheading(90)

def down():
    if obj1.heading() == 0 or obj1.heading() == 180:
        obj1.setheading(270)

def right():
    if obj1.heading() == 90 or obj1.heading() == 270:
        obj1.setheading(0)

def left():
    if obj1.heading() == 90 or obj1.heading() == 270:
        obj1.setheading(180)


def go():
    obj1.forward(25)


t.onkey(up, 'Up')
t.onkey(down, 'Down')
t.onkey(left, 'Left')
t.onkey(right, 'Right')
t.onkey(go, 'space')
t.listen()
t.mainloop()

Aucun commentaire:

Enregistrer un commentaire