飞机大战编程过程 java飞机大战小游戏( 四 )


int c = new Random().nextInt(650) + 50;
EnemyPlaneArrayList.get(i).x = c;
EnemyPlaneArrayList.get(i).y = -150;
}
} else {
EnemyPlaneArrayList.get(i).drawMe(g);
}
}
}
//里面类,创造一个线程不停的革新界面
class updateGame extends Thread {
@Override
public void run() {
while (flage) {
repaint();
}
}
}
}
2.enetiy实业层
2.1玩耍实业笼统类
package com.eneity;
import java.awt.*;
/**
* 设置玩耍中一切东西的父类
*/
public abstract class GameObject {
public int x,y,width,height;//设置铁鸟的坐标,长,宽
public abstract void drawMe(Graphics g);//铁鸟画本人的本领
public abstract Rectangle getRect();//获得一个矩形用来检验和测定有没有和铁鸟相撞
}
2.2战机类
package com.eneity;
import com.utils.P;
import com.utils.PUtils;
import com.view.GamePanel;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
/**
* 战役机
*/
public class FightPlane extends GameObject {
public List<Bullet> arrayList =new LinkedList<Bullet>();
public static Image image =PUtils.getImage(“images/zdj.png”);//铁鸟图像资源
public static Image zd=PUtils.getImage(“images/zd.png”);//枪弹图片资源
public ImageIcon bz2=new
ImageIcon(PUtils.getImage(“images/dd2.png”));
public FightPlane(){
x=300;
y=300;
width=120;
height=120;
new CreateBullet().start();//创造画出枪弹的线程
}
@Override
public void drawMe(Graphics g) {
g.drawImage(image,x,y,width,height,null);
//画出枪弹;
for(int i=0;i<arrayList.size();i++){
//检验和测定枪弹能否与敌机相撞
for(int j=0;j<GamePanel.EnemyPlaneArrayList.size();j++){
if(GamePanel.EnemyPlaneArrayList.get(j).getRect().intersects(arrayList.get(i).getRect())){
/* int x=GamePanel.EnemyPlaneArrayList.get(j).x;
int y=GamePanel.EnemyPlaneArrayList.get(j).y;
GamePanel.EnemyPlaneArrayList.remove(j);
g.drawImage(bz2.getImage(),500,500,150,150,null);*/
GamePanel.EnemyPlaneArrayList.get(j).flat=1;
}
}
if(arrayList.get(i).y<20){
arrayList.remove(i);//优化枪弹类,当枪弹到达确定场所时让其消逝
}
arrayList.get(i).drawMe(g);
}
}
@Override
public Rectangle getRect() {
return new Rectangle(x,y,width,height);
}
//创造枪弹里面类
class Bullet extends GameObject{
public Bullet(){
width=60;
height=120;
}
@Override
public void drawMe(Graphics g) {//在面板上画出枪弹
g.drawImage(zd,x,y,width,height,null);
y-=3;
}
@Override
public Rectangle getRect() {
return new Rectangle(x,y,width,height);
}
}
/**
* 创造一个连接画出枪弹的线程
*/
class CreateBullet extends Thread{
@Override
public void run() {
while(true){
try {
Bullet bullet=new Bullet();
bullet.x=x+32;
bullet.y=y+20;
arrayList.add(bullet);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
2.3敌机类
package com.eneity;
import com.utils.P;
import com.utils.PUtils;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
/**
* 敌机类
*/
public class EnemyPlane extends GameObject {
public int flat=0;//检验和测定枪弹能否与敌机相撞
public static Image dj =PUtils.getImage(“images/wugui.png”);//铁鸟图像资源
/* public static Image zd=PUtils.getImage(“images/zd.png”);//枪弹图片资源*/
public EnemyPlane(){
x=300;
y=300;
width=120;
height=120;
// new CreateBullet().start();//创造画出枪弹的线程
}
@Override
public void drawMe(Graphics g) {
g.drawImage(dj,x,y,width,height,null);
y+=3;//历次画完革新y坐标
if(y> P.HEIGHT){//俭朴资源反复运用每架铁鸟
y=-150;
}
/* //画出枪弹
for(int i=0;i<arrayList.size();i++){
arrayList.get(i).drawMe(g);
}*/
}
@Override
public Rectangle getRect() {
return new Rectangle(x,y,width,height);
}
/* //创造枪弹里面类
class Bullet extends GameObject{
public Bullet(){
width=60;
height=120;
}
@Override
public void drawMe(Graphics g) {//在面板上画出枪弹
g.drawImage(zd,x,y,width,height,null);
y-=3;

推荐阅读