2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

イメージはナメクジ

Posted at

初めに

今あるものを少し変えて違うものを作ろうと思い、Chapter 7 の Lesson 7-7 で作ったプログラムを基盤に作りました。

多くの円を使いたかったので、「一番やさしいpython入門教科書」のP212を参考にまとめました。

com 最終.jpg


ちょっと変更したり加えたり...

変更部分①
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")]

このようにディクショナリを用いて、円の位置と進む方向をまとめています。
詳しくは一番やさしいpython入門教科書 大澤文考[著] のP206~P212を参考にしてください。


com 最終.gif


変更部分②

canvas.create_oval(self.x - 20 , self.y - 20, self.x + 20 , self.y + 20 , fill="white" , width=0) を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)

このようにすることで進んだ跡を継続させています。


com 最終2.gif

完成!!!
ナメクジ感でたかな?


ソースコード

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
         # 次の位置に円を描く
         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():
  # 動かす
  for b in balls:
      b.move(canvas)
      
  # もう一回
  root.after(2,loop)
     
# ウィンドウを描く
root = tk.Tk()
root.geometry("800x600")

# Canvasを置く
canvas = tk.Canvas(root, width =800, height =600, bg="white")
canvas.place(x=0 , y=0)

# 再びタイマー
root.after(2, loop)

root.mainloop()


参考文献

一番やさしいpython入門教科書 大澤文考[著]


感想

プログラムに関わるのがこの授業が初めてでした。今回やってみて楽しかったのでこれを機にもっと色々作ってみたいと思いました。

com 最終3.gif

2020-07-05 (3).png

綺麗だね!

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?