Aug-18-2020, 11:25 PM
This program moves a Turtle in 4 directions using keys. I want to control the Turtle collision with the Screen boundaries too. But I'm having a very weird problem!
For example, when I move the Turtle to the right side it works OK, but when I turn the Turtle to the left side, graphically it turns OK but the coordinate values that Print command gives me, instead decreasing the X-coordinate value, increase one time it and then start to decrease!
Of course this messes up with the collision control I'm trying to create!
I just tested everything I could think of but so far got no luck!
I'm using Python 3.7.7 with Turtle graphics, in Thonny 3.2.7. I tested it in Repl.it and the result was the same!
Here is the code:
For example, when I move the Turtle to the right side it works OK, but when I turn the Turtle to the left side, graphically it turns OK but the coordinate values that Print command gives me, instead decreasing the X-coordinate value, increase one time it and then start to decrease!
Of course this messes up with the collision control I'm trying to create!
I just tested everything I could think of but so far got no luck!
I'm using Python 3.7.7 with Turtle graphics, in Thonny 3.2.7. I tested it in Repl.it and the result was the same!
Here is the code:
import turtle def turtleUp(): t1.setheading(90) if not colisao(): t1.sety(t1.ycor() + 10) def turtleDown(): t1.setheading(270) if not colisao(): t1.sety(t1.ycor() - 10) def turtleRight(): t1.setheading(0) if not colisao(): t1.setx(t1.xcor() + 10) def turtleLeft(): t1.setheading(180) if not colisao(): t1.setx(t1.xcor() - 10) def colisao(): print(t1.xcor(), t1.ycor()) if t1.xcor() < -470 or t1.xcor() > 460 or t1.ycor() < -370 or t1.ycor() > 360: return True else: return False screen_width = 1000 screen_height = 800 s = turtle.Screen() s.setup(screen_width, screen_height) s.bgcolor("lime") t1 = turtle.Turtle() t1.speed(0) t1.shape("turtle") t1.color("black") t1.up() t1.goto(0, 0) s.onkeypress(turtleUp, "w") s.onkeypress(turtleDown, "s") s.onkeypress(turtleRight, "p") s.onkeypress(turtleLeft, "o") s.listen() s.mainloop()