I am new to python but have experience with Java. I am creating a bouncing ball program which throuhg a for loop creates a large sum of bouncing balls. I appreciate any help! Thank you

import time
from random import randint

x0 = 10.0
y0 = 10.0
ball_diameter = 15

from tkinter import *
window = Tk()
canvas = Canvas(window, width=500, height=600, bg='white')
canvas.pack()

for i in range(1, 50):
    i=i+1
    x = [randint(1,400)]
    y = [randint(1,400)]
    vx = randint(1,20)
    vy = randint(1,20)
    x_min = 0.0
    y_min = 0.0
    x_max = 500.0
    y_max = 600.0

    for t in range(1, 5000):
        new_x = x[t-1] + vx
        new_y = y[t-1] + vy
        if new_x >= x_max or new_x <= x_min:
            vx = vx*-1.0
        if new_y >= y_max or new_y <= y_min:
            vy = vy*-1.0  
        x.append(new_x)
        y.append(new_y)
        canvas.create_oval(x[t], y[t], x[t]+ball_diameter, y[t]+ball_diameter, fill='blue', tag='ball')
        canvas.update()
        time.sleep(0.1)
        canvas.delete('ball')

    window.mainloop()

Apologies for my vagueness but as of now my program only creates one ball. I would like for it to create multiple bouncing balls as per the first for loop. Is it possible to create multiple 'objects' in python through the use of such a for loop? If not, how would I be able to create 10 bounching balls by only defining one as I have above.

You would save movement status of each ball in Ball object itself, list or dictionary. Then you would have after event which you would set up to call the updating function for movement, say after 0.05 seconds.

This article has been dead for over six months. Start a new discussion instead.