一、設計心得
1.首先定義出磚塊、球、拍子的長與寬(藉由icon大小取得)。
2.繪出磚塊數量及位置
3.滑鼠移動時拍子隨著移動(滑鼠事件、注意邊界)。
4.以Thread或Timer及移動距離來控制球的速度。
5.每次的移動便判斷球是否碰撞磚塊或者超出邊界,並進行對應動作(反彈、消掉磚塊)。
6.當球移動到拍子以下則結束。
2.繪出磚塊數量及位置
3.滑鼠移動時拍子隨著移動(滑鼠事件、注意邊界)。
4.以Thread或Timer及移動距離來控制球的速度。
5.每次的移動便判斷球是否碰撞磚塊或者超出邊界,並進行對應動作(反彈、消掉磚塊)。
6.當球移動到拍子以下則結束。
二、遊戲Demo
三、原始碼
import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.*; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.Timer; public class Block extends JApplet implements ActionListener,MouseListener,MouseMotionListener{ private Image imBorad = new ImageIcon(getClass().getResource("picture/bar.gif")).getImage(); private Image imBall = new ImageIcon(getClass().getResource("picture/ball.gif")).getImage(); private Image imBar = new ImageIcon(getClass().getResource("picture/bar1.gif")).getImage(); private int xBorad = 0,yBorad = 0,xBall = 0,yBall = 0,xMove = 5,yMove = 5,iWidth,iHeight; private int iBallWidth,iBallHeight,iBoradWidth,iBoradHeight,iBarWidth,iBarHeight,iScore = 0,iScorex,iScorey; private boolean b; private Timer t; private Image imBuffer;//緩衝區 private Graphics back; boolean bCheckbar[][]; public void init(){ // super("打磚塊"); this.setSize(480, 620); this.setVisible(true); iWidth = this.getWidth(); iHeight = this.getHeight(); iBallWidth = imBall.getWidth(this);//球寬 iBallHeight = imBall.getHeight(this);//球高 iBoradWidth = imBorad.getWidth(this);//拍寬 iBoradHeight = imBorad.getHeight(this);//拍高 iBarWidth = imBar.getWidth(this);//磚塊寬 iBarHeight = imBar.getHeight(this);//磚塊高 iScorex = iWidth/6*5; iScorey = iHeight/2; bCheckbar = new boolean[13][15];//磚塊數量 initial(); this.addMouseMotionListener(this); this.addMouseListener(this); t=new Timer(15,this); } private void initial() {//初始化 xBorad = iWidth/2; yBorad = iHeight-iBoradHeight-5; xBall = xBorad+iBoradWidth/2-iBallWidth/2; yBall = yBorad- iBallHeight; b = false; for (int i = 0; i < bCheckbar.length; i++) { for (int j = 0; j < bCheckbar[i].length; j++) { bCheckbar[i][j] = true; } } repaint(); } public void paint(Graphics g) { // super.paintComponents(g);//畫出元件 imBuffer = createImage(iWidth,iHeight); back = imBuffer.getGraphics(); back.drawImage(imBall,xBall , yBall, this); back.drawImage(imBorad,xBorad , yBorad, this); for (int i = 0; i < bCheckbar.length; i++) {//尋訪所有磚塊 for (int j = 0; j < bCheckbar[i].length; j++) { if (!bCheckbar[i][j]) continue;//已打到則跳過不繪出 int iBarX = (i+1)*iBarWidth; int iBarY = (j+1)*iBarHeight; int iBarAreaX = iBarX + iBarWidth; int iBarAreaY = iBarY+iBarHeight; if (xBall >= iBarX && xBall <= iBarAreaX && yBall > iBarY && yBall <= iBarAreaY) {//已打中 if ((xBall >= iBarX && xBall <= (iBarX + iBallWidth))||(xBall <= iBarAreaX && xBall >= (iBarAreaX - xMove))) {xMove = -xMove;} else yMove = -yMove; bCheckbar[i][j] = false; iScore += 100; break; } back.drawImage(imBar, iBarX, iBarY, this); } } back.setColor(Color.red); back.drawString("Score:" + iScore, iScorex, iScorey); g.drawImage(imBuffer, 0, 0, this); if (xBall <= 0 || xBall >= iWidth-iBallWidth) xMove = -xMove;//左右邊界 if (yBall <= 20) yMove = -yMove;//上邊界 if (xBall >= xBorad && xBall <= xBorad+iBoradWidth-iBallWidth && yBall > yBorad - iBallHeight){//拍子高度 yMove = -yMove; }else if (yBall >= iHeight) {//下邊界 t.stop(); initial(); } } @Override public void actionPerformed(ActionEvent e) { xBall -= xMove; yBall -= yMove; repaint(); } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) {t.start();b=true;} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { int mx = e.getX() - iBoradWidth/2; if (mx + iBoradWidth >= iWidth || mx <= 0) return; xBorad = mx; if (!b) {//未開始移動 xBall = mx + iBoradWidth /2 ; repaint(); } } // public static void main(String[] args) { // Block b = new Block(); // b.setDefaultCloseOperation(EXIT_ON_CLOSE); // } }
留言
張貼留言