当前位置:编程学习 > JAVA >>

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

 

前几天学习了多线程,现在总结自己做的一个打砖块的游戏,以此来加深对多线程的理解(如有不正确的地方欢迎指正!)。首先来看游戏的效果图:

首先要有一个界面,界面的实现在前面已经作过很多次了,具体代码如下:

/**

* 初始化窗体

*/

public void initFrame(){

this.setTitle("喷怒的小球");//设置窗体的标题

this.setSize(500, 750);//设置窗体的大小

//this.getContentPane().setBackground(Color.BLACK);

this.setLayout(new FlowLayout());//设置流式布局管理器

JButton bt = new JButton("开始");

JButton bt1 = new JButton("停止");

 

JPanel panel = new JPanel ();

 

Dimension d = new Dimension(495,650);

 

panel.setBackground(Color.BLACK);

panel.setPreferredSize(d);

 

this.add(bt);

this.add(bt1);

this.add(panel);

 

this.setResizable(false);//设置窗体的大小不可变

this.setDefaultCloseOperation(3);//点击关闭时退出窗体

this.setVisible(true);//将窗体显示在屏幕上

 

//设置焦点

bt.setFocusable(false);

bt1.setFocusable(false);

panel.setFocusable(true);

 

final Graphics g = panel.getGraphics();//得到画布

}

得到窗体之后,需要一个挡板,所以定义一个挡板类,并且这个挡板能够在窗体底部水平移动,所以这里是定义的挡板类是实现MouseMotionListener的接口,使得画出的挡板能够随着鼠标的移动而移动,然后在这个类里面定义挡板的属性和实现画挡板的方法。挡板的属性有左上角的坐标,长,宽以及颜色等,为了美观,这里是直接画挡板的一张图片,具体代码如下所示:

public class Fender implements MouseMotionListener{

 

public static int x = 0;

public static int getX() {

return x;

}

 

public int y = 630;

public int width = 100;

private int height = 20;

private JPanel panel;

private Graphics g;

 

public Fender(){}

public Fender(netjava.wxh0807pm1.BallFrame.mypanel panel){

 

this.panel = panel;

g = panel.getGraphics();

}

//重写父类的方法

public void mouseMoved(MouseEvent e){

//清除图像

    g.setColor(panel.getBackground());

    g.fillRect(x, y, width, height);

x = e.getX();

 

//g.setColor(Color.RED);

if(x>=400){

x=400;

}

//画挡板

    createFender(g,x,y,width,height);

    }

//画挡板的方法

public void createFender(Graphics g,int x,int y,int width,int height){

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

g.drawImage(icon.getImage(), x, y, width, height, null);

}

   

public void mouseDragged(MouseEvent e){

 

}

}

然后需要画出自己设计的砖块,这里是直接以地图的形式画砖块的。首先是准备几张砖块的图片,然后是把要画得区域看成一个二维数组,二维数组中的元素为零的地方表示该区域没有画砖块,以不同数字表示不同的砖块,然后在另外一个文件中设计二维数组的元素以画出自己想要画得地图。二维数组设计好之后,首先要定义一个方法来把文件读取到内存中,这时就用到了输入输出流的知识,在前面已经总结过,这里不再罗嗦了。但是读取到的是字符串,所以还需要定义一个方法将字符串转化为数组,然后还要定义一个得到图片的静态方法,最后要定义一个根据得到的数组和图片创建地图的方法。将前面三个方法写成一个类,具体代码如下所示:

public class MapTest {

 

/**

* 读取文件中的地图数据

*

* @param path

* @return

*/

public static int[][] readMap(String path) {

try {

// 创建文件输入流

FileInputStream fis = new FileInputStream(path);

BufferedInputStream bis = new BufferedInputStream(fis);

 

byte[] bs = new byte[bis.available()];

// 将数据从流中读取到数组中

bis.read(bs);

 

String str = new String(bs);

 

// 对字符串进行处理

 

// System.out.println(str);

 

int[][] arr = changeToArray(str);

return arr;

 

} catch (Exception ef) {

ef.printStackTrace();

}

 

return null;

}

 

/**

* 将字符串转化为数组

*

* @param str

* @return

*/

private static int[][] changeToArray(String str) {

 

// 根据回车换行符将字符串分割为字符串数组

String[] strs = str.split("\r\n");

 

int[][] array = new int[strs.length][strs[0].length()];

 

// 遍历字符串数组

for (int i = 0; i < strs.length; i++) {

String s = strs[i];

// 对字符串进行解析

char[] cs = s.toCharArray();

 

for (int j = 0; j < cs.length; j++) {

char c = cs[j];

// 将字符串转成数字

int num = Integer.parseInt(c + "");

 

array[i][j] = num;

 

}

 

}

return array;

}

 

//根据路径得到图片对象的方法

public static ImageIcon createImageIcon(String path) {

java.net.URL url = MapTest.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<array.length;i++){

for(int j=0;j<array[i].length;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.getGraphic

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,