# ブロック崩しの素 # https://news.mynavi.jp/article/zeropython- […]
# ブロック崩しの素 # https://news.mynavi.jp/article/zeropython- […]
# ブロック崩しの素# https://news.mynavi.jp/article/zeropython-10/# 上記URLの最初のサンプルをオブジェクトに置換したやつ# ブロック崩しfrom tkinter import *# ウィンドウの作成win = Tk()cv = Canvas(win, width = 600, height = 400)cv.pack()# ボールを表すオブジェクトclass Ball:def __init__(self):self.dirx=8 # X方向のボールの速さself.diry=-8 # Y方向のボールの速さx=350 # ボールの位置y=300self.w=10self.ball=cv.create_oval(x - self.w, y - self.w,x + self.w, y + self.w,fill="green")def left(self):return cv.coords(self.ball)[0]def top(self):return cv.coords(self.ball)[1]def right(self):return cv.coords(self.ball)[2]def bottom(self):return cv.coords(self.ball)[3]ball=Ball()# オブジェクトを描画するdef draw_objects():pass# ボールの移動def move_ball():# 上左右の壁に当たった?if ball.left() < 0 or ball.right() > 600: ball.dirx *= -1if ball.bottom() < 0 or ball.top() > 400: ball.diry *= -1# 移動内容を反映cv.move(ball.ball,ball.dirx,ball.diry)# ゲームループdef game_loop():draw_objects()move_ball()win.after(33,game_loop)game_loop()win.mainloop()