Hi, I've got some big problems with my program. I'm trying to learn how to use Python to create interactive programs with a GUI, but I'm having a really hard time even implementing a simple cursor scrolling around a screen. I think I have a little bit of an understanding of how to do this, but I'm getting all sorts of problems. Like, okay, I'm making just a square black cursor that moves around the screen, right? When it touches the edge of the screen, it should "crash" and be reset in the middle of the screen, sort of like a game piece colliding with a wall or something. But I can't ever reach the edge of the screen. I only get part way and then it breaks and starts over.

Another problem I'm having is that the square doesn't move the way I want it to. For example, when it's moving forward in any direction, the square gets squashed down. I don't really know how to describe it, but it gets shortened in the direction of motion for some reason. Otherwise I guess I figured out how to make it move and stop the way I want, so I don't think that any of the move commands need to be changed, but maybe they do to fix the problem. If you do need to fix them, please don't make it so the user can go backwards. So if the cursor is currently moving left, it can't just start going right. It needs to "turn" by either going up or down first. That's why I made the statements like "if cursor.direction != "down":" to check for turning behavior.

Here's the code I wrote, could you please help me figure it out?

# import modules
import turtle
import time

#Set tickrate
tick = 0.01

# Create a window
wn = turtle.Screen()
wn.title("Cursor Test")
wn.bgcolor("white")

# Set window size
window_width = 1000
window_height = 1000
wn.setup(window_width, window_height)
wn.tracer(0)


# Implement cursor
cursor = turtle.Turtle()
cursor.shape("square")
cursor.color("black")
cursor.penup()
cursor.goto(0, 0)
cursor.direction = "Stop"


# Implement inputs
def move_up():
	if cursor.direction != "down":
		cursor.direction = "up"
def move_down():
	if cursor.direction != "up":
		cursor.direction = "down"
def move_left():
	if cursor.direction != "right":
		cursor.direction = "left"
def move_right():
	if cursor.direction != "left":
		cursor.direction = "right"
def halt():
	cursor.direction = "Stop"


def move():
	if cursor.direction == "up":
		y = cursor.ycor()
		cursor.sety(y+10)
	if cursor.direction == "down":
		y = cursor.ycor()
		cursor.sety(y-10)
	if cursor.direction == "left":
		x = cursor.xcor()
		cursor.setx(x-10)
	if cursor.direction == "right":
		x = cursor.xcor()
		cursor.setx(x+10)

# Keyboard inputs
wn.listen()
wn.onkeypress(move_up, "w")
wn.onkeypress(move_down, "s")
wn.onkeypress(move_left, "a")
wn.onkeypress(move_right, "d")
wn.onkeypress(halt, "space")


# Scroll Cursor based on user inputs
while True:
	wn.update()
	if cursor.xcor() > 290 or cursor.xcor() < -290 or cursor.ycor() > 290 or cursor.ycor() < -290:
		time.sleep(1)
		cursor.goto(0, 0)
		cursor.direction = "Stop"
	move()
	time.sleep(tick)
wn.mainloop()
system
namaste, mujhe apane kaarykram ke saath kuch badi samasyaayein mili hai.main yah jaanane kii koshish kar rahaa hum ki GUI ke saath interactive program banane ke liye paythan kaa upyog kaise karen, lekin main ek screen ke chaaron ore ek saadhaaran cursar scroling ko lagu karne main vastav main kathin samay hum.mujhe lagata he ki mujhe yah karne kii thodi samaj he ki yah kaise karnaa he, lekin mujhe har tarah kii samasyaayein mil rahi hai.jaise, thik he, main sirf ek varg kaala cursar bana rahaa hum jo screen ke chaaron ore ghumta he, he naa?jab yah screen ke kinare ko choota he, to use "krash" karnaa chahie or screen ke bich main riset karnaa chahie, jaise ki ek game pis kii tarah ek diwaar yaa kuch ke saath takaraa rahaa he.lekin main kabhi screen ke kinare tak nahin pahunch saktaa.mujhe keval bhaag miltaa he or phir yah tut jaataa he or shuru hota he.

ek or samasya jo mujhe ho rahi he, vah yah he ki varg us tarah se aage nahin badhata he jis tarah se main ise chahta hum.udhaaharan ke liye, jab yah kisi bhi disha main aage bad rahaa hota he, to varg neeche gir jaataa he.main vastav main nahin jaanata ki iskaa varnan kaise kiya jaae, lekin yah kisi kaaran se gati kii disha main chota ho jaataa he.anyathaa mujhe lagata he ki mujhe pataa chalaa he ki ise kaise sthanantarit kiya jaae or jis tarah se main chahta hum use roke, isliye mujhe nahin lagata ki kisi bhi chaal ke aadesh ko badalane kii aavashyaktaa he, lekin shaayad ve samasya ko thik karne ke liye karte hai.yadi aapako unhen thik karne kii aavashyaktaa he, to kripaya ise na banaaen taaki upyogkarta peeche kii ore ja sake.isliye yadi cursar vartmaan main bain ore bad rahaa he, to yah sirf sahi nahin ho saktaa he.ise pehle yaa neeche jaakar "turn" karne kii aavashyaktaa he.isliye maine "IF CURSOR.DIRECTION! =" DOWN ":" jaise bayaan die.

yahan maine jo code likhaa he, kya aap kripaya mujhe yah pataa lagane main madad kar sakate hai?

# aayaat modul
aayaat karnaa
aayaat kaal

#Set tikrat
tik = 0.01

# ek windo banaaen
wn = turtle.screen ()
wn.title ("cursar test")
wn.bgcolor ("safed")

# windo kaa aakaar set karen
window_width = 1000
window_height = 1000
wn.setup (window_width, window_height)
wn.tracer (0)


# cursar ko lagu karen
cursar = turtle.turtle ()
cursar. shape ("squaire")
cursar.color ("kaala")
cursor.penup ()
cursor.goto (0, 0)
cursor.direction = "stop"


# input lagu karen
def move_up ():
yadi cursor.direction! = "neeche":
cursor.direction = "ap"
def move_down ():
yadi cursor.direction! = "upar":
cursor.direction = "neeche"
def move_left ():
yadi cursor.direction! = "sahi":
cursor.direction = "baayen"
def move_right ():
yadi cursor.direction! = "baayen":
cursor.direction = "sahi"
def halt ():
cursor.direction = "stop"


def move ():
yadi cursor.direction == "ap":
y = cursor.ycor ()
cursor.sety (y+10)
yadi cursor.direction == "neeche":
y = cursor.ycor ()
cursar. seti (Y-10)
yadi cursor.direction == "baayen":
x = cursor.xcor ()
cursor.setx (x-10)
yadi cursor.direction == "sahi":
x = cursor.xcor ()
cursor.setx (x+10)

# keyboard input
wn.listen ()
wn.onkeypress (move_up, "w")
wn.onkeypress (move_down, "s")
wn.onkeypress (move_left, "a")
wn.onkeypress (move_right, "d")
wn.onkeypress (padaav, "antariksh")


# upyogkarta input ke aadhaar par cursar scrol karen
jabaki sach:
wn.update ()
yadi cursor.xcor ()> 290 yaa cursor.xcor () <-290 yaa cursor.ycor ()> 290 yaa cursor.ycor () <-290:
time.sleep (1)
cursor.goto (0, 0)
cursor.direction = "stop"
kadam()
time.sleep (tik)
wn.mainloop ()
user
aapke dwaara pradaan kiye gaye code kii samiksha karte hue, mujhe kuch mudde dikhaai dete hai jo aapke dwaara varnit apratyaashit vyavhaar kaa kaaran ban sakate hai.

aapke dwaara bataae gaye pehla mudda, cursar khidki ke kinare tak pahunchane main vifal rahata he, khidki ke aakaar or IF statement ke bich bemail se sambandhit he jo khidki ke kinare kaa pataa lagaataa he.yah sambhav he ki aapke pehle ke pareekshan main, aap nimnlikhit kathan main age ditection ke karyanvayan ke aadhaar par, 600 pixel dwaara 600 ke windo aakaar kaa upyog kar rahe the:

`` `paythan
yadi cursor.xcor ()> 290 yaa cursor.xcor () <-290 yaa cursor.ycor ()> 290 yaa cursor.ycor () <-290:
`` `
jis tarah se code kaam karata he vah yah he ki yah us duuri ko maapta he jis par cursar ne mul 0,0 se yatra kii he jo aapke code dwaara banaai gai khidki kaa centre he.aap khidki ke ayaamon ko 600 pixel kii oonchai or chaudaai main badalakar is samasya ko thik kar sakate hai, yaa aap khidki ke saapeksh simaaon ko banane ke liye IF statement ko sanshodhit kar sakate hai.yahan ek saapeksh seemaa ko lagu karne kaa ek tarika he:

`` `paythan
# khidki ke kinaaron ko paribhaashit karen
LEFT_EDGE = 0 - (Window_width / 2) + 10
right_edge = 0 + (window_width / 2) - 10
top_edge = 0 + (window_height / 2) - 10
bottom_edge = 0 - (window_height / 2) + 10
`` `

in chaar nae char ke saath, jo windo ke aakaar par aadhaarit hai, IF statement jo age takaraavon ke liye cursar kii sthiti kii nigaraanee karata he, char kaa upyog karne ke liye badla ja saktaa he.

`` `paythan
yadi cursor.xcor ()> right_edge yaa cursor.xcor () <left_edge yaa cursor.ycor ()> top_edge yaa cursor.ycor () <bott_edge:
`` `

aapke dwaara bataae gaye doosraa mudda, andolan kii disha main cursar kaa apratyaashit rup se chota hona, sambhawatah cursar ke andolan kii gati or kaarykram kii tik dar ke sangyojan se sambandhit he.tik char nirdhaarit karata he ki windo kitni baar apadet kii jaati he.vartmaan main, tik char ko 0.01 kaa maan saunpa gaya he, jiskaa arth he ki windo prati second 100 baar apadet karegi.yah apratyaashit vyavhaar kaa kaaran ho saktaa he, khaskar agar windo ko 60-hertz rifresh dar ke saath disple par dekha jaataa he.tik dar ko ek bade mulya par sanshodhit kiya ja saktaa he, jo aapke dwaara varnit pradarshan samasya ko hal kar saktaa he.tik vairieble ke maan ko 0.016 par set karne se khidki ko prati second 60 baar apadet karne kaa kaaran hoga, jiske parinamswaroop behatar pradarshan ho saktaa he or aapke dwaara varnit drishy kalakritiyon se batch saktaa he.cursar kii gati kii gati main atirikt parivartan bhi upyogi ho sakate hai.cursar vartmaan main yatra kii vartmaan disha main 10 ikaayiyon kii gati par set he.is maan ko kam karne se cursar kii gati ko or adhik chikna ho saktaa he.

neeche apane anurodh main saajha kiye gaye code kaa ek sanshodhit sanskaran he, jo saapeksh screen age takaraav kaa pataa lagane or kam tik dar ko shaamil karne ke liye badal gaya he.


`` `paythan
# aayaat modul
aayaat karnaa
aayaat kaal

# tik dar set karen
tik = 0.016

# ek windo banaaen
wn = turtle.screen ()
wn.title ("cursar test")
wn.bgcolor ("safed")

# windo kaa aakaar set karen
window_width = 1000
window_height = 1000
wn.setup (window_width, window_height)
wn.tracer (0)

# khidki ke kinaaron ko paribhaashit karen
LEFT_EDGE = 0 - (Window_width / 2) + 10
right_edge = 0 + (window_width / 2) - 10
top_edge = 0 + (window_height / 2) - 10
bottom_edge = 0 - (window_height / 2) + 10

# cursar ko lagu karen
cursar = turtle.turtle ()
cursar. shape ("squaire")
cursar.color ("kaala")
cursor.penup ()
cursor.goto (0, 0)
cursor.direction = "stop"

# input lagu karen
def move_up ():
    yadi cursor.direction! = "neeche":
        cursor.direction = "ap"
def move_down ():
    yadi cursor.direction! = "upar":
        cursor.direction = "neeche"
def move_left ():
    yadi cursor.direction! = "sahi":
        cursor.direction = "baayen"
def move_right ():
    yadi cursor.direction! = "baayen":
        cursor.direction = "sahi"
def halt ():
    cursor.direction = "stop"

def move ():
    yadi cursor.direction == "ap":
        y = cursor.ycor ()
        cursor.sety (y + 10)
    yadi cursor.direction == "neeche":
        y = cursor.ycor ()
        cursor.sety (y - 10)
    yadi cursor.direction == "baayen":
        x = cursor.xcor ()
        cursor.setx (x - 10)
    yadi cursor.direction == "sahi":
        x = cursor.xcor ()
        cursor.setx (x + 10)

# keyboard input
wn.listen ()
wn.onkeypress (move_up, "w")
wn.onkeypress (move_down, "s")
wn.onkeypress (move_left, "a")
wn.onkeypress (move_right, "d")
wn.onkeypress (padaav, "antariksh")

# upyogkarta input ke aadhaar par cursar scrol karen
jabaki sach:
    wn.update ()
    yadi cursor.xcor ()> right_edge yaa cursor.xcor () <left_edge yaa cursor.ycor ()> top_edge yaa cursor.ycor () <bott_edge:
        time.sleep (1)
        cursor.goto (0, 0)
        cursor.direction = "stop"
    kadam()
    time.sleep (tik)

wn.mainloop ()
`` `
assistant
<|system|>
</s>
<|user|>
namaste, mujhe apane kaarykram ke saath kuch badi samasyaayein mili hai.main yah jaanane kii koshish kar rahaa hum ki GUI ke saath interactive program banane ke liye paythan kaa upyog kaise karen, lekin main ek screen ke chaaron ore ek saadhaaran cursar scroling ko lagu karne main vastav main kathin samay hum.mujhe lagata he ki mujhe yah karne kii thodi samaj he ki yah kaise karnaa he, lekin mujhe har tarah kii samasyaayein mil rahi hai.jaise, thik he, main sirf ek varg kaala cursar bana rahaa hum jo screen ke chaaron ore ghumta he, he naa?jab yah screen ke kinare ko choota he, to use "krash" karnaa chahie or screen ke bich main riset karnaa chahie, jaise ki ek game pis kii tarah ek diwaar yaa kuch ke saath takaraa rahaa he.lekin main kabhi screen ke kinare tak nahin pahunch saktaa.mujhe keval bhaag miltaa he or phir yah tut jaataa he or shuru hota he.

ek or samasya jo mujhe ho rahi he, vah yah he ki varg us tarah se aage nahin badhata he jis tarah se main ise chahta hum.udhaaharan ke liye, jab yah kisi bhi disha main aage bad rahaa hota he, to varg neeche gir jaataa he.main vastav main nahin jaanata ki iskaa varnan kaise kiya jaae, lekin yah kisi kaaran se gati kii disha main chota ho jaataa he.anyathaa mujhe lagata he ki mujhe pataa chalaa he ki ise kaise sthanantarit kiya jaae or jis tarah se main chahta hum use roke, isliye mujhe nahin lagata ki kisi bhi chaal ke aadesh ko badalane kii aavashyaktaa he, lekin shaayad ve samasya ko thik karne ke liye karte hai.yadi aapako unhen thik karne kii aavashyaktaa he, to kripaya ise na banaaen taaki upyogkarta peeche kii ore ja sake.isliye yadi cursar vartmaan main bain ore bad rahaa he, to yah sirf sahi nahin ho saktaa he.ise pehle yaa neeche jaakar "turn" karne kii aavashyaktaa he.isliye maine "IF CURSOR.DIRECTION! =" DOWN ":" jaise bayaan die.

yahan maine jo code likhaa he, kya aap kripaya mujhe yah pataa lagane main madad kar sakate hai?

# aayaat modul
aayaat karnaa
aayaat kaal

#Set tikrat
tik = 0.01

# ek windo banaaen
wn = turtle.screen ()
wn.title ("cursar test")
wn.bgcolor ("safed")

# windo kaa aakaar set karen
window_width = 1000
window_height = 1000
wn.setup (window_width, window_height)
wn.tracer (0)


# cursar ko lagu karen
cursar = turtle.turtle ()
cursar. shape ("squaire")
cursar.color ("kaala")
cursor.penup ()
cursor.goto (0, 0)
cursor.direction = "stop"


# input lagu karen
def move_up ():
yadi cursor.direction! = "neeche":
cursor.direction = "ap"
def move_down ():
yadi cursor.direction! = "upar":
cursor.direction = "neeche"
def move_left ():
yadi cursor.direction! = "sahi":
cursor.direction = "baayen"
def move_right ():
yadi cursor.direction! = "baayen":
cursor.direction = "sahi"
def halt ():
cursor.direction = "stop"


def move ():
yadi cursor.direction == "ap":
y = cursor.ycor ()
cursor.sety (y+10)
yadi cursor.direction == "neeche":
y = cursor.ycor ()
cursar. seti (Y-10)
yadi cursor.direction == "baayen":
x = cursor.xcor ()
cursor.setx (x-10)
yadi cursor.direction == "sahi":
x = cursor.xcor ()
cursor.setx (x+10)

# keyboard input
wn.listen ()
wn.onkeypress (move_up, "w")
wn.onkeypress (move_down, "s")
wn.onkeypress (move_left, "a")
wn.onkeypress (move_right, "d")
wn.onkeypress (padaav, "antariksh")


# upyogkarta input ke aadhaar par cursar scrol karen
jabaki sach:
wn.update ()
yadi cursor.xcor ()> 290 yaa cursor.xcor () <-290 yaa cursor.ycor ()> 290 yaa cursor.ycor () <-290:
time.sleep (1)
cursor.goto (0, 0)
cursor.direction = "stop"
kadam()
time.sleep (tik)
wn.mainloop ()</s>
<|assistant|>
aapke dwaara pradaan kiye gaye code kii samiksha karte hue, mujhe kuch mudde dikhaai dete hai jo aapke dwaara varnit apratyaashit vyavhaar kaa kaaran ban sakate hai.

aapke dwaara bataae gaye pehla mudda, cursar khidki ke kinare tak pahunchane main vifal rahata he, khidki ke aakaar or IF statement ke bich bemail se sambandhit he jo khidki ke kinare kaa pataa lagaataa he.yah sambhav he ki aapke pehle ke pareekshan main, aap nimnlikhit kathan main age ditection ke karyanvayan ke aadhaar par, 600 pixel dwaara 600 ke windo aakaar kaa upyog kar rahe the:

`` `paythan
yadi cursor.xcor ()> 290 yaa cursor.xcor () <-290 yaa cursor.ycor ()> 290 yaa cursor.ycor () <-290:
`` `
jis tarah se code kaam karata he vah yah he ki yah us duuri ko maapta he jis par cursar ne mul 0,0 se yatra kii he jo aapke code dwaara banaai gai khidki kaa centre he.aap khidki ke ayaamon ko 600 pixel kii oonchai or chaudaai main badalakar is samasya ko thik kar sakate hai, yaa aap khidki ke saapeksh simaaon ko banane ke liye IF statement ko sanshodhit kar sakate hai.yahan ek saapeksh seemaa ko lagu karne kaa ek tarika he:

`` `paythan
# khidki ke kinaaron ko paribhaashit karen
LEFT_EDGE = 0 - (Window_width / 2) + 10
right_edge = 0 + (window_width / 2) - 10
top_edge = 0 + (window_height / 2) - 10
bottom_edge = 0 - (window_height / 2) + 10
`` `

in chaar nae char ke saath, jo windo ke aakaar par aadhaarit hai, IF statement jo age takaraavon ke liye cursar kii sthiti kii nigaraanee karata he, char kaa upyog karne ke liye badla ja saktaa he.

`` `paythan
yadi cursor.xcor ()> right_edge yaa cursor.xcor () <left_edge yaa cursor.ycor ()> top_edge yaa cursor.ycor () <bott_edge:
`` `

aapke dwaara bataae gaye doosraa mudda, andolan kii disha main cursar kaa apratyaashit rup se chota hona, sambhawatah cursar ke andolan kii gati or kaarykram kii tik dar ke sangyojan se sambandhit he.tik char nirdhaarit karata he ki windo kitni baar apadet kii jaati he.vartmaan main, tik char ko 0.01 kaa maan saunpa gaya he, jiskaa arth he ki windo prati second 100 baar apadet karegi.yah apratyaashit vyavhaar kaa kaaran ho saktaa he, khaskar agar windo ko 60-hertz rifresh dar ke saath disple par dekha jaataa he.tik dar ko ek bade mulya par sanshodhit kiya ja saktaa he, jo aapke dwaara varnit pradarshan samasya ko hal kar saktaa he.tik vairieble ke maan ko 0.016 par set karne se khidki ko prati second 60 baar apadet karne kaa kaaran hoga, jiske parinamswaroop behatar pradarshan ho saktaa he or aapke dwaara varnit drishy kalakritiyon se batch saktaa he.cursar kii gati kii gati main atirikt parivartan bhi upyogi ho sakate hai.cursar vartmaan main yatra kii vartmaan disha main 10 ikaayiyon kii gati par set he.is maan ko kam karne se cursar kii gati ko or adhik chikna ho saktaa he.

neeche apane anurodh main saajha kiye gaye code kaa ek sanshodhit sanskaran he, jo saapeksh screen age takaraav kaa pataa lagane or kam tik dar ko shaamil karne ke liye badal gaya he.


`` `paythan
# aayaat modul
aayaat karnaa
aayaat kaal

# tik dar set karen
tik = 0.016

# ek windo banaaen
wn = turtle.screen ()
wn.title ("cursar test")
wn.bgcolor ("safed")

# windo kaa aakaar set karen
window_width = 1000
window_height = 1000
wn.setup (window_width, window_height)
wn.tracer (0)

# khidki ke kinaaron ko paribhaashit karen
LEFT_EDGE = 0 - (Window_width / 2) + 10
right_edge = 0 + (window_width / 2) - 10
top_edge = 0 + (window_height / 2) - 10
bottom_edge = 0 - (window_height / 2) + 10

# cursar ko lagu karen
cursar = turtle.turtle ()
cursar. shape ("squaire")
cursar.color ("kaala")
cursor.penup ()
cursor.goto (0, 0)
cursor.direction = "stop"

# input lagu karen
def move_up ():
    yadi cursor.direction! = "neeche":
        cursor.direction = "ap"
def move_down ():
    yadi cursor.direction! = "upar":
        cursor.direction = "neeche"
def move_left ():
    yadi cursor.direction! = "sahi":
        cursor.direction = "baayen"
def move_right ():
    yadi cursor.direction! = "baayen":
        cursor.direction = "sahi"
def halt ():
    cursor.direction = "stop"

def move ():
    yadi cursor.direction == "ap":
        y = cursor.ycor ()
        cursor.sety (y + 10)
    yadi cursor.direction == "neeche":
        y = cursor.ycor ()
        cursor.sety (y - 10)
    yadi cursor.direction == "baayen":
        x = cursor.xcor ()
        cursor.setx (x - 10)
    yadi cursor.direction == "sahi":
        x = cursor.xcor ()
        cursor.setx (x + 10)

# keyboard input
wn.listen ()
wn.onkeypress (move_up, "w")
wn.onkeypress (move_down, "s")
wn.onkeypress (move_left, "a")
wn.onkeypress (move_right, "d")
wn.onkeypress (padaav, "antariksh")

# upyogkarta input ke aadhaar par cursar scrol karen
jabaki sach:
    wn.update ()
    yadi cursor.xcor ()> right_edge yaa cursor.xcor () <left_edge yaa cursor.ycor ()> top_edge yaa cursor.ycor () <bott_edge:
        time.sleep (1)
        cursor.goto (0, 0)
        cursor.direction = "stop"
    kadam()
    time.sleep (tik)

wn.mainloop ()
`` `</s>
