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
I will actually make it based on the Bouncing Ball I made earlier. As a process of making
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()
――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.
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()
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.
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