I am learning python canvas Tkinter, i am trying to make a ball animation continuously moving from top left corner to bottom right.
the problem is there is ball when the ball leave top corner and another in bottom right.the ball in the middle is moving from top left to bottom right
here is the code
from graphics import Canvas
CANVAS_WIDTH = 400
CANVAS_HEIGHT = 400
BALL_SIZE = 50
DELAY =0.001
def main():
canvas = Canvas(CANVAS_WIDTH, CANVAS_HEIGHT)
ball = canvas.create_oval(0,0 ,BALL_SIZE,BALL_SIZE, 'blue')
change_x = 1
change_y = 1
while (True):
left_x = canvas.get_left_x(ball)
top_y = canvas.get_top_y(ball)
if left_x < 0 or left_x + BALL_SIZE >= CANVAS_WIDTH:
ball = canvas.create_oval(change_x,change_y ,BALL_SIZE,BALL_SIZE, 'blue')
if top_y < 0 or top_y + BALL_SIZE >= CANVAS_HEIGHT:
ball = canvas.create_oval(change_x,change_y ,BALL_SIZE,BALL_SIZE, 'blue')
canvas.move(ball,change_x, change_y)
time.sleep(DELAY)
if __name__ == '__main__':
main()
i want the ball to move from top left to bottom right without ball in the top left or the bottom right.
New contributor
MHB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.