LoginSignup
3
0

More than 3 years have passed since last update.

DVD休止中のやつ

Last updated at Posted at 2020-07-06
1 / 12

何をつくろうかなあ

僕が何か動くものを作ってきなさいという課題があるということを聞いて、いろいろな案が出たのですが、なかなか自分のレベルにあったものが(難易度の高いものしか)思いつきませんでした。そこで、あれこれ模索しているときに、ふとBouncing Ball を見たときに、ある馴染み深い映像を思い出しました。それがこの動画です。DVDのマークが動く映像です。そういうわけで今回はこれを作っていきたいと思います。


  • 結局の完成品は、これ

制作開始~

これから以前作成したBouncing Ball をもとにして、実際に作ってみる。
作る工程としては、
1.背景とボールの形を修正
2.壁に当たるごとに色を変化させるようにする


背景とボールの形を修正

example07-06-1.pyの、背景を黒くして、ボールを楕円形にする。背景はcanvasメソッドの最後の引数をbg="black"にすると黒くなる。楕円に変形させるには、円のx軸方向の半径を長くする。


修正した結果
#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()

壁に当たるごとにボールの色を変化させる

  • なかなかうまくいかない!

何度もエラーになってそのたびに改善しても全然うまくいきませんでした。なので、先生にアドバイスをいただきました。

壁に当たったときself.colorをrandomにすればいいことがわかりました。


#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()

それでもできない!どこをどういじってもできない。失敗しました。


ソースコード

#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()

感想

自分の思ったようにプログラミングができなくてとても悔しいです。でも、エラーが続いたおかげで新しい知識も増えたので、とても勉強になりました。


参考文献

colorのランダム化 https://qiita.com/daddygongon/items/d658cd7877104975564f#colorのrandom化
scopeについて https://qiita.com/iverson3kobe0824/items/067cca581bf1ca349dd1
dvdの動画 https://www.youtube.com/watch?v=gAAp4n7xqbo

3
0
1

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
3
0