Ask coding questions

← Back to all posts
I need help... why is the ball...
P0GCHAMPB0i (53)

So this is the code:

from tkinter import *
import random
import time
tk = Tk() # create toolkit object
WIDTH=500
HEIGHT=500
canvas = Canvas(tk, width=WIDTH, height=HEIGHT)
tk.title("2 Ball")
canvas.pack() 
ball = canvas.create_oval(5,5, 30, 30, fill="blue")
xs = 1
ys= 2
ball2=canvas.create_oval(5,5,30,30, fill="red")

x=2
y=1
while True:
  canvas.move(ball, xs, ys)
  canvas.move(ball2, x, y)

  pos = canvas.coords(ball)
  pos2 = canvas.coords(ball2)
  if pos[0]<0 or pos[2]>WIDTH:

    xs=-xs

  if pos[1]<0 or pos[3]>HEIGHT:

    ys=-ys

  canvas.update()
  time.sleep(0.01)
tk.mainloop()

The red ball went out of the window...

So how do I fix this?

Btw this is python

Answered by TheBest156 (43) [earned 5 cycles]
View Answer
Comments
hotnewtop
TheBest156 (43)

Fix it in tinkercad, then print it out. Btw how r u using tinkercad in repl python?