text "The easiest Python introductory class" by Fumitaka Osawa, (Sotec Publishing, 2017) "Can I increase more?" That is the motive. Anyway, the color muzzles are retrofitted.

--The video was noticed by Narupen -Use ScreenToGif --Reference: Sleeping log article
: random.randint(0,9) Should be improved.
random.randint(0, 800), random.randint(0, 600) #x,y position
random.randint(0, 10)-5 # dx
color1 = "#e8f" # rgb color
How should I do this?
color1 = "#"+
      hex(random.randint(1, 15)).lstrip("0x") +
      hex(random.randint(1, 15)).lstrip("0x") + 
      hex(random.randint(1, 15)).lstrip("0x")
Like. What you are doing
After that, append a lot of balls to balls.
balls = []
for j in range(100):
    balls.append(Ball(x, y, dx, dy, color)
In fact, it fails twice in three times.
  File "C:\Users\shige\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
    self.tk.call(
_tkinter.TclError: invalid color name "#12"
Failed to generate color characters (fixed, maybe).
I will expose everything.
# Copyright (c) 2020 daddygongon
# Released under the MIT license
# https://opensource.org/licenses/mit-license.php
import tkinter as tk
import random
speed = 1
class Ball:
    global speed
    def __init__(self, x, y, dx, dy, color):
        self.x = x
        self.y = y
        self.dx = dx*speed
        self.dy = dy*speed
        self.color = color
        self.shape = None
    def move(self, canvas):
        self.erase(canvas)
        self.x = self.x + self.dx
        self.y = self.y + self.dy
        self.draw(canvas)
        if (self.x >= canvas.winfo_width() or self.x <= 0):
            self.dx = -self.dx
        if (self.y >= canvas.winfo_height() or self.y <= 0):
            self.dy = -self.dy
    def erase(self, canvas):
        canvas.delete(self.shape)
    def draw(self, canvas):
        self.shape = canvas.create_oval(self.x - 20, self.y - 20, self.x +
                                        20, self.y + 20, fill=self.color, width=0)
def loop():
    for b in balls:
        b.move(canvas)
    root.after(10, loop)
root = tk.Tk()
root.geometry("800x600")
color1 = "#"+hex(random.randint(1, 2)
                 ).lstrip("0x")+hex(random.randint(1, 2)
                                    ).lstrip("0x") + hex(random.randint(1, 2)
                                                         ).lstrip("0x")
canvas = tk.Canvas(root, width=800, height=600, bg=color1)
canvas.place(x=0, y=0)
balls = []
for j in range(100):
    color1 = "#"+hex(random.randint(8, 15)
                     ).lstrip("0x")+hex(random.randint(8, 15)
                                        ).lstrip("0x") + hex(random.randint(8, 15)
                                                             ).lstrip("0x")
    balls.append(Ball(random.randint(0, 800), random.randint(0, 600),
                      random.randint(0, 10)*2-10, random.randint(0,10)*2-10, color1))
root.after(10, loop)
root.mainloop()
It seems that the last main loop is not needed for idle.
Recommended Posts