イメージはナメクジ



## 初めに

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

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

![com　最終.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/648773/4743d10b-b61e-0113-8e8f-51bfe46eb4a9.jpeg)

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

##### 変更部分①
```python: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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/648773/18f018b7-5f02-d3d0-04ce-dadff9f7ecc8.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)に変えます。



```python:qiita.py

canvas.create_oval(self.x  , self.y , self.x  , self.y  , fill="white" , width=0)

```

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

------------------------------------------------------------------------------------------------------




![com　最終2.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/648773/cab0bbd2-6b41-dd88-faeb-3ce9caf5a295.gif)

完成！！！
ナメクジ感でたかな？

---------------------------------------------------------------------------------------------------
## ソースコード



```python: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入門教科書　大澤文考[著]

https://pythondatascience.plavox.info/matplotlib/色の名前

-------------------------------------------------------------------------------

## 感想

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


![com　最終3.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/648773/0b32e25f-2362-c183-f307-7b9840d36662.gif)




<img width="595" alt="2020-07-05 (3).png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/648773/7417ef37-d6dc-6975-6924-66531ada11f0.png">


綺麗だね!