java飞机大战得分代码_java之飞机大战的记分标准

importjava.awt.Image;importjava.util.ArrayList;importjava.util.Timer;importjavax.swing.ImageIcon;public classPlane {publicImage image;public int y = 300, x = 100, width = 50, height = 50;privateString imageName;private boolean flag = true;//true表示子弹往下走,false表示子弹往上走

private intscore;privateTimer timer;privatePlaneDemo pd;public voidsetPD(PlaneDemo pd) {this.pd =pd;

}public voidsetTimer(Timer timer) {this.timer =timer;

}public Plane(int x, int y, int width, intheight, String imageName,boolean flag, intscore) {this.x =x;this.y =y;this.width =width;this.height =height;this.imageName =imageName;this.flag =flag;this.score =score;

image= new ImageIcon(this.getClass().getResource(imageName))

.getImage();

}public void move(ArrayListlist) {if (imageName.equals("plane.jpg")) {if(flag) {

y+= 3;if (y >= 400)

list.remove(this);

}else{

y-= 3;if (y <= 40)

y= 300;

}

}else if (imageName.equals("bullet.png")) {if(flag)

y+= 3;elsey-= 4;if (y >= 400)

list.remove(this);else if (y + height <= 0)

list.remove(this);

}else if (imageName.equals("background.jpg")) {

y+= 2;if (y >= 0)

y= -226;

}

}/*** 检测当前飞机对象和其他的子弹或者飞机对象是否发生碰撞

*

*@paramlist就是存储了所有敌方飞机和子弹的对象

* (包括自己的)*/

public void collision(ArrayListlist) {for (int i = 1; i < list.size(); i++) {

Plane plane= list.get(i);//获取队列中的飞机或者子弹对象

if (plane != this && plane.flag != flag) {//检测不是自己本身//计算两个圆的距离

double distance = Math.sqrt((plane.x - this.x)* (plane.x - this.x) + (plane.y - this.y)* (plane.y - this.y));//判断两个图片是否发生了碰撞

if (distance <= plane.height / 2 + this.height / 2) {//销毁这两个图片

list.remove(i);

list.remove(this);//判断是否要增加这一局游戏的分数

if (plane.imageName.equals("bullet.png") && !plane.flag) {if (this.imageName.equals("plane.jpg")) {

PlaneDemo.count+= this.score;

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

}

}else if (this.imageName.equals("bullet.png")&& !this.flag) {if (plane.imageName.equals("plane.jpg")) {

PlaneDemo.count+=plane.score;

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

}

}//停止我方飞机的自动生成子弹的线程对象

if ((!plane.flag && plane.imageName.equals("plane.jpg"))) {

plane.timer.cancel();newSaveInfo(plane.pd);

}else if (!this.flag && this.imageName.equals("plane.jpg")) {

timer.cancel();new SaveInfo(this.pd);

}

}

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值