Padクラスのmoveメソッド内でのパッドと壁およびボールとの衝突判定処理を追加し、以下2つのの条件のようにしたいのですが、ボールの衝突判定のif文はどのようにして、他をどのように変えたらいいでしょうか?
•パッドを3分割し,ボールが当たった位置で反射の仕方が変わる
•ボールが下の壁にあたったらゲームオーバーとするか,ボールの数を一つ減らしてボールの数がゼロになったらゲームオーバー
// BlockMain.java
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class BlockMain extends JFrame{
public static void main(String[] args) {
BlockMain w = new BlockMain();
w.setResizable(false);
w.setTitle("ブロック崩し?");
w.setSize(505, 540); //Window のサイズをセット
BlockCanvas bc = new BlockCanvas(500,500);
w.getContentPane().add(bc);
w.setVisible(true); //表示する
}
}
―――――――――――――――――――――――――――――――――――――――――――――
// BlockCanvas.java
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class BlockCanvas extends Canvas implements Runnable {
Ball ball = new Ball();
Pad pad = new Pad();
Image background = getToolkit().getImage("blockback.png");
Thread thread; // スレッドを使うためのインスタンスを宣言
Dimension dim; // 描画領域の大きさ
public BlockCanvas(int w, int h) { dim = new Dimension(w, h); setPreferredSize(dim);
// 描画領域の大きさを調べる
thread = new Thread(this); // スレッドを生成
thread.start(); // スレッドを開始 (run()が呼ばれる)
}
@Override public void paint(Graphics g) { super.paint(g); // アンチエイリアス ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); Dimension dim = getSize(); // 描画領域の大きさを調べる Image m_image = createImage(dim.width, dim.height);// 裏画面の画像イメージ Graphics m_g = m_image.getGraphics(); // 裏画面に付随するグラフィックスオブジェクト m_g.drawImage(background, 0, 0, dim.width, dim.height, null); ball.draw(m_g, dim); g.drawImage(m_image, 0, 0, this); // 裏画面を表画面にコピー pad.draw(g, dim); } public void run() { try { while (true) { Thread.sleep(50); // ballはEDTでの再描画時に参照する情報なので、 // 同期をとらずに更新するなら // runLaterを通じてEDTで実行するようにするのが無難 SwingUtilities.invokeLater(() -> { ball.move(dim); pad.move(dim, ball); repaint(); // 再描画要求 }); } } catch (InterruptedException e) { // sleep中に割り込まれたらこの例外が起きるので行儀よくループを終了する. // 割り込まれているにもかかわらず強情にループを継続するのは良くない。 } } // ダブルバッファリング public class Ball { Image ballImage = getToolkit().getImage("smileyball2.png"); int x = 10, y = 100; // ボール中心の座標 int vx = 4, vy = 5; // ボールの速度ベクトル static final double RADIUS = 0.02; // ボールの半径はウインドウの2% int radius; // ボールの半径 public void draw(Graphics g, Dimension dim) { radius = (int)(RADIUS * dim.width); g.drawImage(ballImage, x - radius, y - radius, radius + radius, radius + radius, null);
}
public void move(Dimension dimension) { x += vx; if (x < radius) { x = radius; vx = -vx; } else if (x > dimension.width-radius) { // 左または右に当たったらx方向速度の符号を反転させる x = dimension.width- radius; vx = -vx; } y += vy; if (y < radius) { y = radius; vy = -vy; } else if (y > dimension.height- radius) { y = dimension.height- radius; vy = -vy; } } } public class Pad implements MouseListener { int x = 100, y = 380; // パッドの中心座標 int vx = 0; // パッドの速度ベクトル static final double WIDTH = 0.25; // パッド幅のウインドウ幅に対する比率 static final double HEIGHT = 0.95; // パッド位置のy座標のウインドウ高さに対する比率 int width, height; // パッドの幅,高さ public void draw(Graphics m_g, Dimension dim) { width = (int)(WIDTH * dim.width); height = width / 8; y = (int)(HEIGHT * dim.height); m_g.fillRect(x - width / 2, y - height / 2, width, height); } public void move(Dimension dim, Ball ball) { x += vx; // パッドの移動 if (x < width/2) { x = width/2; vx = -vx; } else if (x > dim.width-width/2) { // 左または右に当たったらx方向速度の符号を反転させる x = dim.width- width/2; vx = -vx; } // 左右の壁との衝突判定を行う.当たった場合は壁に接した位置で止める. if() // ボールとの衝突判定 } @Override public void mouseClicked(MouseEvent me) { } @Override public void mouseEntered(MouseEvent me) { } @Override public void mouseExited(MouseEvent me) { } @Override public void mousePressed(MouseEvent me) { int a = 8; if (me.getButton() == MouseEvent.BUTTON1) {// 左ボタンクリック? vx = -a; // 左向きに移動させる } else { vx = a; // 右向きに移動させる } } @Override public void mouseReleased(MouseEvent me) { vx = 0; // パッドを止める } }
}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/15 13:57