本文来自:小易博客专栏。转载请注明出处:http://blog.csdn.net/oldinaction
在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学完J2SE的小伙伴们一点启示!
坦克大战V0.3实现功能:
1、写一个Tank类,方便以后产生多辆坦克(重要:利用面向对象的思维)
2、让坦克可以朝着8个方向运动,是完全受控,可停可走
注意事项:
1、使用Tank类的好处,方便以后产生多辆坦克,和坦克相关的变量和方法都可以在Tank类中定义,要使用则只需TankClient中new一个Tank对象然后调用相应的方法,这就是面向对象的思维
2、写一个类时先考虑成员变量,再通过成员变量写构造方法,最后再考虑其他方法,当然一次不用全部写完,可之后需要用到的时候慢慢加进来
3、利用MyEclipse快速写类的构造方法:光标处于该类名上 - 单击右键 - Source - Generate Constructor using Fileds
坦克大战V0.3源代码:
TankClient类:
import java.awt.*;
import java.awt.event.*; public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600; Tank myTank = new Tank(50, 50); Image offScreenImage = null; //定义一个屏幕后的虚拟图片 @Override
public void paint(Graphics g) {
myTank.drawTank(g);
} //利用双缓冲消除圆圈移动时屏幕的闪动
@Override
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); //判断是为了避免每次重画时都给offScreenImage赋值
}
Graphics gOffScreen = offScreenImage.getGraphics(); //定义虚拟图片上的画笔gOffScreen
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); //重画背景,如果没有这句则在屏幕上会保留圆圈的移动路径
gOffScreen.setColor(c);
paint(gOffScreen); //把圆圈画到虚拟图片上
g.drawImage(offScreenImage, 0, 0, null); //再一次性把虚拟图片画到真实屏幕上,在真实屏幕上画则要用真实屏幕的画笔g
} public void luanchFrame() {
this.setLocation(400, 300);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("坦克大战 - By:小易 - QQ:381740148");
this.setResizable(false); //不允许改变窗口大小
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); //添加关闭功能,此处使用匿名类比较合适
this.setBackground(Color.GREEN); this.addKeyListener(new KeyMonitor()); setVisible(true); new Thread(new PaintThread()).start(); //启动线程,实例化线程对象时不要忘了new Thread(Runnable对象);
} public static void main(String[] args) {
TankClient tc = new TankClient();
tc.luanchFrame();
} //PaintThread只为TankClient服务,所以写成内部类好些
public class PaintThread implements Runnable { public void run() {
while (true) {
repaint(); //repaint()是TankClient或者他的父类的方法,内部类可以访问外部包装类的成员,这也是内部类的好处
try {
Thread.sleep(50); //每隔50毫秒重画一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} public class KeyMonitor extends KeyAdapter { @Override
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
} @Override
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
} }
Tank类:
import java.awt.*;
import java.awt.event.*; public class Tank {
public static final int XSPEED = 5; //定义常量X轴速度
public static final int YSPEED = 5; private int x , y; //定义变量画圆圈(坦克)时四边形左上点的x、y左边 private boolean bL = false, bU = false, bR = false, bD = false; //定义变量左上右下的按键是否被按下
enum Direction {L,LU,U,RU,R,RD,D,LD,STOP}; //定义枚举类型,值为左、左上、上、右上、右、右下、下、左下、停止 private Direction dir =Direction.STOP; public Tank(int x, int y) {
this.x = x;
this.y = y;
} public void drawTank(Graphics g) {
Color c = g.getColor(); //取得g(以后称为画笔)的颜色
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30); //"画圆",利用填充一个四边形(四边形的内切圆),参数分别代表:四边形左上点的坐标X,Y,宽度,高度
g.setColor(c); //用完画笔后把画笔默认的颜色(黑色)设置回去 move(); //每次按键都会重画,就会调用drawTank,在这里重画坦克的此时位置
} public void move() {
switch (dir) {
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
} public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); //得到按键的虚拟码,再和下面的KeyEvent.VK_LEFT等虚拟码比较看是否是某按键
switch (key) {
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDraction();
} public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDraction();
} //通过上右下的按键是否被按下判断坦克要运动的方向
void locateDraction() {
if(bL && !bU && !bR && !bD) dir =Direction.L;
else if(bL && bU && !bR && !bD) dir =Direction.LU;
else if(!bL && bU && !bR && !bD) dir =Direction.U;
else if(!bL && bU && bR && !bD) dir =Direction.RU;
else if(!bL && !bU && bR && !bD) dir =Direction.R;
else if(!bL && !bU && bR && bD) dir =Direction.RD;
else if(!bL && !bU && !bR && bD) dir =Direction.D;
else if(bL && !bU && !bR && bD) dir =Direction.LD;
else if(!bL && !bU && !bR && !bD) dir =Direction.STOP;
} }
知识点回顾:
1、枚举的使用:使用enum关键字;更多:http://www.iteye.com/topic/1116193