以打砖块游戏来理解多线程(二)

2014-11-24 01:45:30 · 作者: · 浏览: 1
apTest.class.getResource(path);

ImageIcon icon = new ImageIcon(url);

return icon;

}

}

最后一个方法的代码如下:

/**

* 根据地图数组创建地图

* @param array

*/

public static void createMap(int[][] array,Graphics g){

for(int i=0;i

for(int j=0;j

if(array[i][j]!=0){

int num = array[i][j];

String path = "image/"+num+".png";

//根据路径构造图片对象

ImageIcon icon = MapTest.createImageIcon(path);

g.drawImage(icon.getImage(), 35*j, 15*i, null);

}

}

}

}

上面的方法都写成之后只要调用就可以实现砖块的绘制了。现在还需要绘制一个小球,这个小球是一个线程,所以定义一个小球类,在该类里面定义小球的属性和画得方法,在小球的移动过程中还要判断小球与界面的左右以及上边的碰撞反弹以及小球与砖块的碰撞。小球与砖块的碰撞主要分别从砖块的四条边考虑与小球的碰撞,因为根据上面的方法得到数组可以得到每个砖块的位置,然后在遍历数组,判断数组中的每一个砖块是否与小球相撞,然后在做相应的反弹,砖块碰到小球之后要把砖块消掉,所谓消掉就是把砖块画成与背景一样的颜色,把数组中对应的元素变为零。然后在判断小球是否与挡板碰撞,如果碰撞,则弹回,如果挡板没有接住小球,则游戏结束。然后在写一个方法判断是否赢了,同样是遍历上面得到的数组,如果数组的元素全为零,则说明砖块全被打完了,则赢了。具体点的代码如下所示:

/**

* 小球类

* @author lenovo

*

*/

public class Ball extends Thread{

java.util.Random rd = new java.util.Random();

public static int x0=240;

public static int y0=605;

private int width=20;

private int height=20;

private int x1;

private int y1;

private JPanel panel;

private Graphics g;

private Fender fd;

public static boolean isStop=false;

public static boolean isPause=false;

public Ball(){}

public Ball(JPanel panel,Fender fd){

this.fd = fd;

this.panel = panel;

g = panel.getGraphics();

//小球的增量

x1 = 8;

y1 = -8;

}

public void run(){

draw();

}

public void draw(){

Fender fd = new Fender();

while(!isStop){

while(!isPause){

//javax.swing.ImageIcon icon = new javax.swing.ImageIcon("src\\netjava\\wxh0807pm1\\image\\6.png");

//清除图像

g.setColor(panel.getBackground());

g.fillRect(x0, y0, width, height);

//遍历数组,判断是否与砖块相撞

//int[][] array = MapTest.readMap("src\\netjava\\wxh0806\\image\\map");

for (int i=0;i

for (int j=0;j

if(BallFrame.arr[i][j]!=0){

if (x0>=35*j-1&&x0<=35*j+10&&y0<=15*i+15&&y0>=15*i){//砖块左边碰撞

g.setColor(panel.getBackground());

g.fillRect(35*j, 15*i, 36, 15);

//System.out.println("11");

BallFrame.arr[i][j] = 0;

x1=-x1;

}else if (y0>=15*i-1&&y0<=15*i+15&&x0<=35*j+35&&x0>=35*j){//砖块上边判断

g.setColor(panel.getBackground());

g.fillRect(35*j, 15*i, 36, 15);

//System.out.println("12");

BallFrame.arr[i][j]=0;

y1=-y1;

}else if (y0<=15*i+15+1&&y0>=15*i&&x0<=35*j+35&&x0>=35*j){//砖块下边判断

g.setColor(panel.getBackground());

g.fillRect(35*j, 15*i, 36, 15);

//System.out.println("13");

BallFrame.arr[i][j]=0;

y1=-y1;

}else if(x0>=35*j+35+1&&x0<=35*j+35-10&&y0<=15*i+15&&y0>=15*i){//砖块右边判断

g.setColor(panel.getBackground());

g.fillRect(35*j, 15*i, 36, 15);

//System.out.println("14");

BallFrame.arr[i][j]=0;

x1=-x1;

}

}

}

}

isWin(BallFrame.arr);

if (x1!=0){

if (x0<=0||x0>=470){//左右两壁

x1=-x1;

//System.out.println("1");

}else if (y0<=0){//上下两壁

y1=-y1;

//System.out.println("2");

}else if ((x0<=0&&y0<=0)||x0<=0||(x0>=470&&y0<=0)||x0>=470){//垂直碰撞四壁

x1=-x1;y1=-y1;

//System.out.println("3");

}

else if (y0>=630-20&&y0<=630-20+10&&x0<=Fender.getX()+100-10&&x0>=Fender.getX()-10){

//System.out.println("------------");

y1=-y1;

//System.out.println("0");

}else if (y0>640&&y0<650){

javax.swing.JOptionPane.showMessageDialog(null, "加油哦!");

}

x0+=x1;

y0+=y1;

//画球

createBall(g,x0,y0);

// g.drawImage(icon.getImage(), x0+=x1, y0+=y1, null);

// g.setColor(Color.RED);

// g.fillOval(x0+=x1,y0+=y1,width,height);

}

try{

Thread.sleep(40);

}catch(Exception ep){

ep.printStackTrace();

}

}