Created
April 24, 2020 08:25
-
-
Save danieldaeschle/65865aab3e38b37886ab30219d969931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
import time | |
import numpy as np | |
WIDTH = 800 | |
HEIGHT = 500 | |
SIZE = 5 | |
tk = Tk() | |
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="grey") | |
canvas.pack() | |
color = 'black' | |
class Ball: | |
def init(self): | |
self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color) | |
self.speedx = np.random.random()*5 # changed from 3 to 9 | |
self.speedy = np.random.random()*5 # changed from 3 to 9 | |
self.active = True | |
self.move_active() | |
def ball_update(self): | |
canvas.move(self.shape, self.speedx, self.speedy) | |
pos = canvas.coords(self.shape) | |
if pos[2] >= WIDTH or pos[0] <= 0: | |
self.speedx *= -1 | |
if pos[3] >= HEIGHT or pos[1] <= 0: | |
self.speedy *= -1 | |
def move_active(self): | |
if self.active: | |
self.ball_update() | |
tk.after(40, self.move_active) # changed from 10ms to 30ms | |
for _ in range(100): | |
Ball() | |
tk.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment