Python Forum
Python 3 Turtle - Screen Events and Coords
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 Turtle - Screen Events and Coords
#1
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:

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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Graphics - Screen & Window Sizing jerryf 1 834 Feb-09-2023, 08:02 PM
Last Post: jerryf
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 1,613 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  wait for the first of these events Skaperen 4 1,944 Mar-07-2022, 08:46 PM
Last Post: Gribouillis
  Draw circle from GPS coords, and get GPX file? Winfried 0 2,183 Mar-29-2021, 07:19 PM
Last Post: Winfried
  Turtle python graphics Y0sh1 6 3,440 Jun-10-2020, 10:05 AM
Last Post: DPaul
  turtle.Screen() not working Jdawgg531 0 2,739 May-04-2020, 12:43 AM
Last Post: Jdawgg531
  For loops help, using python turtle SemiBeginnerPY 2 3,953 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY
  Python Turtle Help codinghero 1 16,044 Jan-04-2020, 12:46 AM
Last Post: ichabod801
  Python Turtle and order of implementation query Parsleigh 2 2,771 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,180 Feb-06-2019, 01:25 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020