I tried to multiply bouncing balls (occasionally x100)

Basic policy

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.


bouncing_ball.gif


How to make a video

--The video was noticed by Narupen -Use ScreenToGif --Reference: Sleeping log article


Randomly set initial values for position, speed, and color

: 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?


Randomize color

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

  1. Prepare a string called "#"
  2. Give an integer of 1..15 (1..f) and
  3. Convert to hexadecimal such as "0xe" with hex
  4. Strip the left (l) "0x" with lstrip -Hex + lstrip reference site

Lots of balls

After that, append a lot of balls to balls.

balls = []
for j in range(100):
    balls.append(Ball(x, y, dx, dy, color)

Does not generate well

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


Source

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

I tried to multiply bouncing balls (occasionally x100)
I tried to debug.
I tried to paste
I tried to learn PredNet
I tried to organize SVM.
I tried to implement PCANet
I tried to reintroduce Linux
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried mushrooms Pepper x IBM Bluemix Text to Speech
I tried to implement Deep VQE
I tried to create Quip API
I tried to touch Python (installation)
I tried to implement adversarial validation
I tried to explain Pytorch dataset
I tried Watson Speech to Text
I tried to touch Tesla's API
I tried to implement hierarchical clustering
I tried to organize about MCMC.
I tried to implement Realness GAN
I tried to move the ball
I tried to estimate the interval.
I tried to display GUI on Mac with X Window System
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried using Azure Speech to Text.
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to implement permutation in Python
Wrangle x Python book I tried it [2]
I tried to create a linebot (preparation)
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried to get started with Hy
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to classify text using TensorFlow
I tried adding post-increment to CPython Implementation
I tried to implement ADALINE in Python
I tried to let optuna solve Sudoku
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement PPO in Python
I tried to implement CVAE with PyTorch
Wrangle x Python book I tried it [1]
I tried to make a Web API
I tried to solve TSP with QAOA
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
I tried my best to return to Lasso
I tried to summarize Ansible modules-Linux edition
I tried to predict Covid-19 using Darts
I tried to predict next year with AI
I tried to build a super-resolution method / ESPCN
I tried to program bubble sort by language
I tried web scraping to analyze the lyrics.
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
I tried to build a super-resolution method / SRCNN ①