用户界面编程-基于坦克大战情景


本文基于java环境编写一款简易的坦克大战应用程序,程序详情可参见java源码
程序的重要构成元素包括以下几个方面:
坦克对象
通过定义坦克类对象可以简单清晰地包装其所附带的各个属性。该类所定义的属性主要由三个部分:驾驶员属性主要包括坦克驾驶员生命值、复活等待时长与姓名;坦克属性主要包括坦克的外观、前行速度、可前进方向;坦克发射炮弹属性主要包括其外观、速度以及伤害值。值得注意的是,对外观的定义可通过Panel.class.getResource函数实例化ImageIcon,以加载图片的方式实现。此外,每一个坦克对象还配置一个进度条属性,用以可视化其坦克驾驶员的生命值。坦克对象的定义如下所示:
class Tank
{
public int width=50,height=50;
public int moveSpeed = 15,spotHurt=5,fireSpeed=30;//移动速度
public int x,y,attackRange=50;
public int lifeFull=100,lifeValue=100;
public Timer mt;
public int state=1;
public boolean pressed=false;
public JProgressBar lifeBar;
public ImageIcon bodyIcon[],ballIcon[];;
public JLabel body;
public int resurrection=6000;
public String name="";
public Tank(Color foreground,String name,int x,int y,String imgString[])
{
this.x=x;
this.y=y;
this.name=name;
mt=new Timer();
lifeBar=new JProgressBar();
lifeBar.setStringPainted(true);//设置进度条显示提示信息
lifeBar.setForeground(foreground);
lifeBar.setFont(new Font("SansSerif",Font.BOLD,20));
lifeBar.setValue(lifeValue);
String sbase="image/";
bodyIcon=new ImageIcon[4];
for(int i=0;i<4;i++)
{
URL url=Panel.class.getResource(sbase+imgString[i]);
bodyIcon[i]=new ImageIcon(url);
bodyIcon[i].setImage(bodyIcon[i].getImage().getScaledInstance(width, height,Image.SCALE_DEFAULT));
}
body=new JLabel();
body.setIcon(bodyIcon[0]);
body.setBounds(x, y, width, height);
ballIcon=new ImageIcon[4];
String ballname[]= {"ballW.png","ballN.png","ballE.png","ballS.png"};
for(int i=0;i<ballIcon.length;i++)
{
URL url=Panel.class.getResource(sbase+ballname[i]);
ballIcon[i]=new ImageIcon(url);
}
}
}
键盘监听
通过复写keyReleased与keyPressed函数获得用户输入操作,规定键盘上up、down等四个箭头为一个坦克对象的控制器,W、S、A、D为另一个坦克对象的控制器。四个控制器分别对应了坦克的四个行驶方向,对控制器的应答主要包括坦克方向状态的更新、驾驶员生命值的更新、炮弹的发射方向更新以及战场边界的限制。
炮弹对象
class BulletInX
{
public BulletInX(int startx,int y,int orienta,Tank tank,Tank target,JPanel obj)
{
final int speed=tank.fireSpeed*orienta;
JLabel label=new JLabel();
label.setBounds(startx, y, 30, 15);
if(orienta>0)
label.setIcon(tank.ballIcon[2]);
else
label.setIcon(tank.ballIcon[0]);
obj.add(label);
Timer timer=new Timer();
timer.schedule(new TimerTask() {
int x=startx;
public void run() {
// TODO Auto-generated method stub
x+=speed;
label.setLocation(x, y);
if(x<borderW||x>borderE)
{
timer.cancel();
obj.remove(label);
}
if(Math.abs(x-target.x)<target.attackRange&&Math.abs(y-target.y)<target.attackRange)
{
target.lifeValue-=tank.spotHurt;
target.lifeBar.setValue(target.lifeValue);
if(target.lifeValue<0)
{
//Dialog tip = new
showInfo.setText(tank.name+" WIN");
Timer pausetimer=new Timer();
pausetimer.schedule(new TimerTask() {
public void run() {
// TODO Auto-generated method stub
showInfo.setText("PK");
target.lifeValue=target.lifeFull;
target.lifeBar.setValue(target.lifeFull);
tank.lifeValue=tank.lifeFull;
tank.lifeBar.setValue(tank.lifeFull);
}
},target.resurrection);
}
}
}
}, 0, 100);
}
}
小墨
2021年8月31日 下午10:33
太棒了