I have recommended the exercises in "The easiest Python introductory class" [^ 1]. This text carefully explains how each code is located there. So it's a very good text for beginners to read carefully to understand coding. However, there are some deviations from the common sense of coding, probably because it emphasizes the flow of logic. This is the correction.
[^ 1]: "The easiest Python introductory class" by Fumitaka Osawa, (Sotec Publishing, 2017).
The code around here is bad.
If you do it for a long time or do a lot, you will be able to enjoy it. At first, I gave up because of cpu, but it was too late. .. ..
I had an idea for a fix somewhere, but I've fixed it a long time ago, so I don't know what was the original. -Tags and Bindings Is the same as the first idea.
The problem is,
example07-08-01.py
canvas.create_oval(self.x - 20, self.y - 20,
self.x + 20, self.y + 20, fill="white", width=0)
is. I overwrote it with'white'and erased it. This is the usual way to draw a picture with code. It is efficient when writing by managing the entire screen as pict. However, if you do this for the object, the object will proliferate more and more. As a result, the required memory increases and drawing becomes sluggish. maybe
Why delete object correctly.
First, create with tag for identification with create_oval.
canvas.create_oval(self.x - 20, self.y - 20,
self.x + 20, self.y + 20,
fill=self.color, width=0,
tag=self.color)
Instead of overwriting it with white
, delete it as follows.
canvas.delete(self.color)
Please try this. It should be very fast.
However, in windows, the shape may feel distorted. This seems to be due to the drawing library. It's perfect for the mac version.
Furthermore, the idea of erasing an object seems to be better than identifying it with a tag.
Basically,
canvas.delete(self.ball)
...Omission...
self.ball = canvas.create_oval(self.x - 20, self.y - 20,
self.x + 20, self.y + 20,
fill=self.color, width=0)
Just make it an object called self.ball and delete it. However, from the original text, it is necessary to change the order of codes considerably.
I will add a completed form, so if you have time, please worry about what is going on. The order is
import tkinter as tk
speed = 10
class Ball:
global speed
def __init__(self, x, y, dx, dy, color):
self.x = x
self.y = y
self.dx = dx
self.dy = dy
self.color = color
self.ball = canvas.create_oval(self.x - 20, self.y - 20,
self.x + 20, self.y + 20,
fill=self.color, width=0)
def test(self):
print(self.x)
print(self.y)
def move(self, canvas):
canvas.delete(self.ball)
self.x = self.x + self.dx
self.y = self.y + self.dy
self.ball = canvas.create_oval(self.x - 20, self.y - 20,
self.x + 20, self.y + 20,
fill=self.color, width=0)
if self.x >= canvas.winfo_width():
self.dx = -speed
if self.x < 0:
self.dx = +speed
if self.y >= canvas.winfo_height():
self.dy = -speed
if self.y < 0:
self.dy = +speed
root = tk.Tk()
root.geometry("600x400")
canvas = tk.Canvas(root, width=600, height=400, bg="white")
canvas.place(x=0, y=0)
balls = [
Ball(300, 400, 1, 1, "red"),
Ball(200, 400, 2, 1, "green"),
Ball(300, 200, 1, 2, "blue")
]
def loop():
for b in balls:
b.move(canvas)
root.after(10, loop)
root.after(10, loop)
root.mainloop()
Recommended Posts