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')

for i in range(1, 50):
    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  
        canvas.create_oval(x[t], y[t], x[t]+ball_diameter, y[t]+ball_diameter, fill='blue', tag='ball')


Recommended Answers

All 3 Replies

Can you be specific on where exactly ou need help?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.