[PYTHON] The one who is not on DVD

I wonder what to make

When I heard that there was a problem to make something that works, I came up with various ideas, but the one that was quite at my level (** only the one with high difficulty **) ) I couldn't think of it. So, when I was exploring this and that, when I suddenly saw the Bouncing Ball, I remembered ** a familiar image **. That is this video. This is an image in which the ** DVD mark ** moves. That's why I would like to make this this time.

--The final product is this

Production start ~

I will actually make it based on the Bouncing Ball I made earlier. As a process of making

  1. Fixed background and ball shape
  2. Try to change the color each time it hits the wall

Fixed background and ball shape

In example07-06-1.py, make the background black and make the ball oval. The background turns black when the last argument of the canvas method is bg = "black". To transform it into an ellipse, increase the radius of the circle along the x-axis.

Corrected result


#cording:utf-8

import tkinter as tk
x = 400
y = 300

dx = 1
dy = 1

def move():
    global x, y , dx, dy
    canvas.create_oval(x - 30, y - 20, x + 30, y + 20, fill= "black", width= 0)
    x = x + dx
    y = y + dy 
    canvas.create_oval(x - 30, y - 20, x + 30, y + 20, fill= "red", width= 0)
    if x >= canvas.winfo_width():
        dx = -1
    if x <= 0:
        dx = +1
    if y >= canvas.winfo_height():
        dy = -1
    if y <= 0:
        dy = +1
    root.after(10,move)
root = tk.Tk()
root.geometry("600x400")

canvas = tk.Canvas(root, width = 600, height = 400, bg= "black")

canvas.place(x = 0, y =0)
root.after(10, move)

root.mainloop()

Change the color of the ball each time it hits the wall

――It doesn't go well!

I got an error many times and improved each time, but it didn't work at all. So I got some advice from my teacher.

I found out that I should set ** self.color to random ** when I hit the wall **.

python


#cording:utf-8
import tkinter as tk 
import random

class Ball:
    def __init__(self, x, y, dx, dy,color):
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy
        self.color = color
     def color_maker(init, fin):
        color1= "#"+ hex(random.randint(init, fin)
        ).lstrip("0x") + hex(random.randint(init, fin)
        ).lstrip("0x")+ hex(random.randint(init, fin)).lstrip("0x")
        return color1
       
         
    def move(self,canvas):
            canvas.create_oval(self.x - 20, self.y - 20, self.x + 20, self.y + 20, fill="black", wibdth = 0)
            self.x = self.x + self.dx
            self.y = self.y + self.dy
            canvas.create_oval(self.x - 20, self.y - 20, self.x + 20, self.y + 20, fill = self.color , width = 0)
            if self.x <= 0:
                self.dx = 1
                self.color = color_maker(8,15)
            
            if self.x >= canvas.winfo_width():
                self.dx= -1
                self.color = color_maker(8,15)
            if self.y <= 0:
                self.dy = 1
                self.color = color_maker(8,15)
            
            if self.y >= canvas.winfo_height():
                self.dy= -1
                self.color = color_maker(8,15)
    
          
b = Ball(400, 300, 1,1, self.color)



def loop():
    b.move(canvas)
    root.after(10, loop)
root = tk.Tk()
root.geometry("800x600")

canvas = tk.Canvas(root, width = 800, height = 600, bg = "black")
canvas.place(x = 0, y = 0)

root.after(10, loop)

root.mainloop()

Still can't! I can't play with it anywhere. I failed.

Source code

python


#cording:utf-8

import tkinter as tk
import random
x = 400
y = 300

dx = 1
dy = 1

def move():
    global x, y , dx, dy
    canvas.create_oval(x - 30, y - 20, x + 30, y + 20, fill= "black", width= 0)
    x = x + dx
    y = y + dy 
    canvas.create_oval(x - 30, y - 20, x + 30, y + 20,fill = "blue", width= 0)
    if x >= canvas.winfo_width():
        dx = -1
    if x <= 0:
        dx = +1
    if y >= canvas.winfo_height():
        dy = -1
    if y <= 0:
        dy = +1

    root.after(10,move)
root = tk.Tk()
root.geometry("600x400")


canvas = tk.Canvas(root, width = 600, height = 400, bg= "black")
canvas.place(x = 0, y =0)
root.after(10, move)

root.mainloop()

Impressions

I'm very disappointed that I can't program as I expected. However, I learned a lot because I gained new knowledge because of the continuous errors.

References

Color randomization https://qiita.com/daddygongon/items/d658cd7877104975564f #color randomization About scope https://qiita.com/iverson3kobe0824/items/067cca581bf1ca349dd1 dvd video https://www.youtube.com/watch?v=gAAp4n7xqbo

Recommended Posts

The one who is not on DVD
Test.py is not reflected on the web server in Python3.
[Linux] Who is the background job! ??
Build Python environment on Ubuntu (when pip is not the default)
The update of conda is not finished.
The value of pyTorch torch.var () is not distributed
The .pyd file is the DLL itself on Windows
Until the Sphinx documentation is published on GitHub
Until the web application is released on Sakura VPS
When the selected object in bpy.context.selected_objects is not returned
Follow all users who are not following on Twitter
Check if the LAN cable is disconnected on Linux
Suspend shutdown on linux When sleep is not possible
I got lost in the maze
The one who is not on DVD
The one that displays the progress bar in Python
The guy who gets fitbit data from the server
The image is displayed in the local development environment, but the image is not displayed on the remote server of VPS
python> link> dir_util.copy_tree ()> update = 1> Do not copy the acquired one?
What to do when Japanese is not displayed on matplotlib
The AWS IP range is different depending on the acquisition method.