実現したいこと
Processingでブロック崩しを作っていますが、ボタンに当たるとbarが変化するように作りたいです。(赤のボタンはbarが大きくなる、黄色はリセット、青は小さくなる)
発生している問題・分からないこと
ボタンの追加は出来たがその後の当たり判定、barを変化させる方法が全くわからない。
該当のソースコード
//以下途中からになります /** 1フレームごとの描画処理 */ void draw() { int breakBlockNum = 0; // 壊れているブロックの数を初期化 for (int i = 0; i < blockNumY; i++) { for (int j = 0; j < blockNumX; j++) { boolean isBroken = block[i][j].isBroken(); if (isBroken == true) { breakBlockNum ++; } else { block[i][j].show(); block[i][j].collision(ball); } } } if (breakBlockNum < blockNumAll) { ball.move(); // ボールの移動処理 if (ball.collision() == false) { // 壁との衝突判定 fill(0, 0, 0); textSize(35); textAlign(CENTER, CENTER); text("Game Over!!", width / 2, height / 2); } else { bar.collision(ball); // バーとの衝突判定 } } else { // ブロックがすべて壊れているならば textSize(35); textAlign(CENTER, CENTER); // テキストの配置を整える text("Clear!!", width / 2, height / 2); // クリア表示 } fill(0, 0, 0); textSize(12); textAlign(CENTER, CENTER); text("Block:" + (blockNumAll - breakBlockNum), width - 40, height - 25); // 残りブロック数表示 bar.show(); // ボールを打ち返すためのボードを表示 ball.show(); // ボールの描画 for (int i=0; i<3; i++) { button[i].show(); } } } class Button { float x; int y; int d; int r; int g; int b; Button(int x, int y, int d, int r, int g, int b) { this.x=x; this.y=y; this.d=d; this.r=r; this.g=g; this.b=b; } void show() { fill(r, g, b); ellipse(x, y, d, d); } } /** * ボール(主にボールの移動) */ class Ball { int x; // ボールのX座標 int y; // ボールのY座標 int vx; // ボールのX軸速度 float vy; // ボールのY軸速度 int d; // ボールの直径 boolean penetrability; // ボールのブロック貫通性 // コンストラクタ Ball(int x, int y, int vx, int vy, int d) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.d = d; penetrability = false; } // ボールの移動 void move() { x += vx; y += vy; } // 壁との衝突判定 boolean collision() { if (x <= d / 2) { // 左壁 x = d / 2; vx = -vx; } if (y <= d / 2) { // 天井 y = d / 2; vy = -vy; } if (x >= (width - d / 2)) { // 右壁 x = width - d / 2; vx = -vx; } if (y >= (height - d / 2)) { // 底面(ゲームオーバー) vx = 0; vy = 0; return false; } return true; } // ボールの描画 void show() { if (penetrability) { fill(255, 0, 0); // 貫通弾の時は赤 } else { fill(0, 0, 0); // 通常時は黒 } ellipse(x, y, d, d); // ボールの描画 } // これ以降、SetterとGetter int getX() { return x; } void setY(int y) { this.y = y; } int getY() { return y; } void setVx(int vx) { this.vx = vx; } int getVx() { return vx; } void setVy(float vy) { this.vy = vy; } float getVy() { return vy; } int getD() { return d; } void setPenetrability(boolean penetrability) { this.penetrability = penetrability; } boolean getPenetrability() { return penetrability; } } /** * バー */ class Bar { final int y = height - 50; // バーの左上のy座標 (底から50で固定) int sizeX; // バー全体の幅(なるべく5の倍数が良い) int sizeY; // バーの高さ int boxSizeX; // 分割した時の箱一つ分の幅(全体の1/5) int[] boxX; // バーの各区切りのx座標(左から0) boolean ballbar; Bar(int sizeX, int sizeY) { this.sizeX = sizeX; this.sizeY = sizeY; boxSizeX = sizeX / 5; boxX = new int[6]; } int getsizey() { return sizeY; } void collision(Ball ball) { if ((ball.getY() >= (y - ball.getD() / 2)) && (ball.getY() <= (y + sizeY - ball.getD() / 2))) { // ボールのy座標がバーと重なる時 if ((ball.getX() >= boxX[0]) && (ball.getX() <= boxX[1])) { // 左黒Boxに衝突 ball.setVx(-2); ball.setY(y - ball.getD() / 2); ball.setVy(-ball.getVy()); ball.setPenetrability(false); // ボールのブロック貫通性なし } else if ((ball.getX() > boxX[1]) && (ball.getX() <= boxX[2])) { // 左白Boxに衝突 ball.setVx(-1); ball.setY(y - ball.getD() / 2); ball.setVy(-ball.getVy()); ball.setPenetrability(false); // ボールのブロック貫通性なし } else if ((ball.getX() > boxX[2]) && (ball.getX() <= boxX[3])) { // 中心赤Boxに衝突 ball.setY(y - ball.getD() / 2); ball.setVy(-ball.getVy()); ball.setPenetrability(true); // ボールのブロック貫通性あり } else if ((ball.getX() > boxX[3]) && (ball.getX() <= boxX[4])) { // 右白Boxに衝突 ball.setVx(1); ball.setY(y - ball.getD() / 2); ball.setVy(-ball.getVy()); ball.setPenetrability(false); // ボールのブロック貫通性なし } else if ((ball.getX() > boxX[4]) && (ball.getX() <= boxX[5])) { // 右黒Boxに衝突 ball.setVx(2); ball.setY(y - ball.getD() / 2); ball.setVy(-ball.getVy()); ball.setPenetrability(false); // ボールのブロック貫通性なし } } } // バー表示メソッド void show() { boxX[0] = mouseX - this.sizeX / 2; // 左黒Boxの左上頂点のx座標 boxX[1] = mouseX - 3 * (boxSizeX / 2); // 左白Boxの左上頂点のx座標 boxX[2] = mouseX - boxSizeX / 2; // 中央赤Boxの左上頂点のx座標 boxX[3] = mouseX + boxSizeX / 2; // 右白Boxの左上頂点のx座標 boxX[4] = mouseX + 3 * (boxSizeX /2); // 右黒Boxの左上頂点のx座標 boxX[5] = mouseX + this.sizeX / 2; // 右黒Boxの"右上"頂点のx座標 // バーの描画 fill(173); rect(boxX[0], y, boxSizeX, sizeY); // 左黒Box fill(255, 255, 255); rect(boxX[1], y, boxSizeX, sizeY); // 左白Box fill(255, 120, 130); rect(boxX[2], y, boxSizeX, sizeY); // 中央赤Box fill(255, 255, 255); rect(boxX[3], y, boxSizeX, sizeY); // 右黒Box fill(173); rect(boxX[4], y, boxSizeX, sizeY); // 右白Box } int getX(int i) { return boxX[i]; } int getY() { return y; } int getSizeX() { return sizeX; } int getSizeY() { return sizeY; } } /** * ブロック(ボールとブロックの衝突判定など) */ class Block { int x; // 左上頂点のx座標 int y; // 左上頂点のy座標 int sizeX; // ブロックの幅 int sizeY; // ブロックの高さ boolean broken; // ブロックが破壊されているか否か int R=255; int G=150; int B=240; int count; // コンストラクタ Block(int x, int y, int sizeX, int sizeY) { this.x = x; this.y = y; this.sizeX = sizeX; this.sizeY = sizeY; broken = false; } // ブロックの描画メソッド void show() { fill(R, G, B); // ブロックの色は青 rect(x, y, sizeX, sizeY); } // ボールとブロックとの衝突と破壊判定 // 今回,ボールの衝突判定はボールの中心座標のみで行っている void collision(Ball ball) { // ボールが貫通弾でないなら、衝突した際にボールの進行方向を変える(速度を変える) if (!ball.getPenetrability()) { // ブロック左側に当たった場合 if ((ball.getX() >= x) && (ball.getX() <= (x + 5)) && (ball.getY() >= y) && (ball.getY() <= (y + sizeY))) { ball.setVx(-1 * ball.getVx()); } // ブロック右側に当たった場合 if ((ball.getX() >= (x + sizeX - 5)) && (ball.getX() <= (x + sizeX)) && (ball.getY() >= y) && (ball.getY() <= (y+sizeY))) { ball.setVx(-1 * ball.getVx()); } // ブロック上側に当たった場合 if ((ball.getX() >= x) && (ball.getX() <= (x + sizeX)) && (ball.getY() >= y) && (ball.getY() <= (y + 5))) { ball.setVy(-1 * ball.getVy()); } // ブロック下側に当たった場合 if ((ball.getX() >= x) && (ball.getX() <= (x + sizeX)) && (ball.getY() >= (y + sizeY - 5)) && (ball.getY() <= (y + sizeY))) { ball.setVy(-1 * ball.getVy()); } } // 衝突による破壊判定 if ((ball.getX() >= x) && (ball.getX() <= (x + sizeX)) && (ball.getY() >= y) && (ball.getY() <= (y + sizeY))) { count+=1; if (count==1) { R=150; G=255; B=190; } if (count==2) { R=150; G=250; B=255; } if (count==3) { broken = true; } } } boolean isBroken() { return broken; } }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ボタン設置の方法を調べたが出てこなかった。
初心者のため、調べ方も分からなかった。
補足
特になし