LoginSignup
2
0

More than 3 years have passed since last update.

3つのボールでブロック崩し

Last updated at Posted at 2020-07-09
1 / 8

最終成果物で挑戦したもの

・サッカーのPKゲーム
・あみだくじ
 etc...      しかし、、、


自分の能力では作ることがまだ出来ず断念。考え直し思いついたのは


三つのボールでブロック崩し



#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):
        # いまの円を消す
        self.erase(canvas)
        # X座標、y座標を動かす
        self.x = self.x + self.dx
        self.y = self.y + self.dy
        #次の位置に円を描画する
        self.draw(canvas)
        # 端を超えていたら反対向きにする
        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

    def erase(self, canvas):
        canvas.create_oval(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill="white", width=0)

    def draw(self, canvas):
        canvas.create_oval(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill=self.color, width=0)

class Rectangle(Ball):
    def erase(self, canvas):
        canvas.create_rectangle(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill="white", width=0)

    def draw(self, canvas):
        canvas.create_rectangle(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill=self.color, width=0)

class Triangle(Ball):
    def erase(self, canvas):
        canvas.create_rectangle(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill="white", width=0)

    def draw(self, canvas):
        canvas.create_polygon (self.x, self.y - 20, self.x + 20,self.y + 20, self.x -20, self.y + 20, fill=self.color, width=0)

# 円3つを、まとめて用意する
balls = [
    Ball(400, 300, 1, 1, "green"),
    Ball(200, 100, -1, 1, "green"),
    Ball(100, 200, 1, -1, "green")
]
def loop():
  #動かす
  for b in balls:
      b.move(canvas)

 #壊すブロック
 class Block:
        w_x = 100 #ブロックの幅(x座標)
        w_y = 30 #ブロックの幅(y座標)
        global dy, score #衝突の際にボールのクラスの移動量およびスコアを変更したいので、グローバル宣言を行う。

        #ブロックのスイッチ。1がON,0がOFF
        block_list =[[1,1,1,1,1,1,1,1,1,1,1,1], # j = 0 , i = 0 ~ 11
                     [1,1,1,1,1,1,1,1,1,1,1,1], # j = 1 , i = 0 ~ 11
                     [1,1,1,1,1,1,1,1,1,1,1,1]] # j = 2 , i = 0 ~ 11 行・列の順番
        def draw(self):
            for i in range(6):
                for j in range(3):
                        cv.create_rectangle(i*self.w_x, j*self.w_y, (i+1)*self.w_x, (j+1)*self.w_y, fill = "orange", tag = "block"+str(j)+str(i))

        def reflect(self):
            for i in range(12):
                for j in range(3):
                    #ボールが上から反射
                    if (ball.y-ball.w < (j+1)*self.w_y #ボールがブロックよりも下
                        and i*self.w_x < ball.x < (i+1)*self.w_x #ボールがブロックの左右に挟まれている
                        and self.block_list[j][i] == 1): #スイッチが1
                            ball.dy *= -1 #反射させる
                            cv.delete("block"+str(j)+str(i)) #ブロックの描画を消す
                            self.block_list[j][i] = 0 #スイッチを切る
                            score.score += 1 #スコアの加点
                            score.delete() #スコアを更新(削除-生成)
                            score.draw()

  #もう一回
  root.after(10,loop)

# ウィンドウを描く
root = tk.Tk()
root.geometry("800x600")

```pyhon

#ゲームオーバー
    def gameover():
        global w, dx, dy
        if ball.y + ball.w > win_height :
            cv.delete("paddle")
            cv.delete("ball")
            cv.create_text(win_center_x, win_center_y, text = "GAME OVER(T_T)", font = ('FixedSys', 40))
            ball.w = 0
            ball.dx = 0
            ball.dy = 0

    #ゲームクリア
    def gameclear():
        global w, dx, dy
        if score.score == 18 :
            cv.delete("paddle")
            cv.delete("ball")
            cv.create_text(win_center_x, win_center_y, text = "GAME CLEAR(^0^)", font = ('FixedSys', 40))
            ball.w = 0
            ball.dx = 0
            ball.dy = 0

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

# タイマーをセットする
root.after(10, loop)

root.mainloop()

2020-07-09.png


まとめ

こうすることにより授業でしたボール、三角形、四角形をすべてボールにして、そして壊す用のブロックをプログラムすることによってブロック崩しができる。できなかったサッカーのpkゲームやあみだくじも再挑戦しようと思います。


参考

2
0
3

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