I wanted to make something different by slightly changing the existing one, so I made it based on the program I made in Lesson 7-7 of Chapter 7.
I wanted to use a lot of circles, so I summarized it with reference to P212 of "The easiest python introductory textbook".
qiita.py
balls = [
Ball(900,200,-1,1,"lightcoral"),
Ball(850,200,-1,1,"red"),
Ball(800,200,-1,1,"darkred"),
Ball(750,200,-1,1,"tomato"),
Ball(700,200,-1,1,"yellow"),
Ball(650,200,-1,1,"orange"),
Ball(600,200,-1,1,"olive"),
Ball(550,200,-1,1,"limegreen"),
Ball(500,200,-1,1,"green"),
Ball(450,200,-1,1,"aquamarine"),
Ball(400,200,-1,1,"cyan"),
Ball(350,200,-1,1,"steelblue"),
Ball(300,200,-1,1,"midnightblue"),
Ball(250,200,-1,1,"blue"),
Ball(200,200,-1,1,"darkviolet"),
Ball(150,200,-1,1,"violet"),
Ball(100,200,-1,1,"magenta"),
Ball(150,200,-1,1,"deeppink"),
Ball(50,200,-1,1,"pink"),
Ball(0,200,-1,1,"palevioletred")]
In this way, the dictionary is used to summarize the position of the circle and the direction of travel. For details, please refer to P206 to P212 of the easiest python introductory textbook, Fumiko Osawa [Author].
canvas.create_oval (self.x -20, self.y --20, self.x + 20, self.y + 20, fill = "white", width = 0) to canvas.create_oval (self.x, self.y) , self.x, self.y, fill = "white", width = 0).
qiita.py
canvas.create_oval(self.x , self.y , self.x , self.y , fill="white" , width=0)
By doing this, we continue to make progress.
Complete! !! !! Was it a slug feeling?
qiita.py
# coding:utf-8
import tkinter as tk
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 move(self,canvas):
canvas.create_oval(self.x , self.y , self.x , self.y , fill="white" , width=0)
self.x = self.x + self.dx
self.y = self.y + self.dy
#Draw a circle at the next position
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 = -1
if self.x <= 0:
self.dx = +1
if self.y >= canvas.winfo_height():
self.dy = -1
if self.y <= 0:
self.dy = +1
balls = [
Ball(900,200,-1,1,"lightcoral"),
Ball(850,200,-1,1,"red"),
Ball(800,200,-1,1,"darkred"),
Ball(750,200,-1,1,"tomato"),
Ball(700,200,-1,1,"yellow"),
Ball(650,200,-1,1,"orange"),
Ball(600,200,-1,1,"olive"),
Ball(550,200,-1,1,"limegreen"),
Ball(500,200,-1,1,"green"),
Ball(450,200,-1,1,"aquamarine"),
Ball(400,200,-1,1,"cyan"),
Ball(350,200,-1,1,"steelblue"),
Ball(300,200,-1,1,"midnightblue"),
Ball(250,200,-1,1,"blue"),
Ball(200,200,-1,1,"darkviolet"),
Ball(150,200,-1,1,"violet"),
Ball(100,200,-1,1,"magenta"),
Ball(150,200,-1,1,"deeppink"),
Ball(50,200,-1,1,"pink"),
Ball(0,200,-1,1,"palevioletred")]
def loop():
#move
for b in balls:
b.move(canvas)
#again
root.after(2,loop)
#Draw a window
root = tk.Tk()
root.geometry("800x600")
#Put Canvas
canvas = tk.Canvas(root, width =800, height =600, bg="white")
canvas.place(x=0 , y=0)
#Timer again
root.after(2, loop)
root.mainloop()
The easiest python introductory textbook, Fumiko Osawa [Author]
https://pythondatascience.plavox.info/matplotlib/色の名前
This class was the first time I was involved in the program. It was fun to try this time, so I wanted to take this opportunity to make more.
That's beautiful!
Recommended Posts