'Problem with bouncing ball ui vertical instead of horinzontal
I was just wondering how I would make this go horizontal or diagonal? right now, it is bouncing vertically would I need to change any of the variables or the pos?
I have tried to change the y to x as well as change the the horizontal width to height but it isn't working can anyone help, mind you I started coding a few weeks ago so i am a little new to everything
from tkinter import *
import time
import tkinter
from turtle import width
tk = Tk()
tk.title("bouncing ball")
tk.resizable(0,0)
canvas_width= 400
canvas_height =500
ball_size = 25
timer_duration = 20
y_move = 2
def draw():
global y_move
canvas.move(ball1, 0, y_move)
pos = canvas.coords(ball1)
top_y = pos[1]
bottom_y = pos[2]
if top_y <= 0:
y_move = -y_move
elif bottom_y >= canvas_height:
y_move = -y_move
def master_timer():
draw()
tk.update_idletasks()
tk.update()
tk.after(timer_duration, master_timer)
canvas = tkinter.Canvas(tk, width=canvas_width, height=canvas_height, bd=0, highlightthickness= 0)
ball1 = canvas.create_oval(0, 0, ball_size,ball_size,fill="red")
#move ball to center
canvas.move(ball1, canvas_width/2, canvas_width/2)
master_timer()
canvas.pack()
tk.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|