1.Award类
public class Award {
int ax,ay;
Image aImg;
int aSpeed;
int kind;
PlaneJPanel mainview;
public Award(int ax, int ay, Image aImg, int aSpeed, int kind,PlaneJPanel mainview) {
super();
this.ax = ax;
this.ay = ay;
this.aImg = aImg;
this.aSpeed = aSpeed;
this.kind = kind;
this.mainview = mainview;
}
public void drawAward(Graphics g) {
g.drawImage(aImg, ax, ay, null);
}
public void moveAward() {
ay+=aSpeed;
hint0();
hint1();
hint2();
}
public void hint0() {
if (kind==0) {//跟血量碰撞
if (ax+aImg.getWidth(null)>mainview.px&&ax<mainview.px+mainview.p[0].getWidth()&&ay<mainview.py+mainview.p[0].getHeight()&&ay+aImg.getHeight(null)>mainview.py) {
mainview.awards.remove(this);
mainview.blood++;
}
}
}
public void hint1() {//子弹增加
if (kind ==1) {
if (ax+aImg.getWidth(null)>mainview.px&&ax<mainview.px+mainview.p[0].getWidth()&&ay<mainview.py+mainview.p[0].getHeight()&&ay+aImg.getHeight(null)>mainview.py) {
mainview.awards.remove(this);
mainview.threeBullet = true;
}
}
}
public void hint2() {//速度
if (kind == 2) {
if (ax+aImg.getWidth(null)>mainview.px&&ax<mainview.px+mainview.p[0].getWidth()&&ay<mainview.py+mainview.p[0].getHeight()&&ay+aImg.getHeight(null)>mainview.py) {
mainview.awards.remove(this);
mainview.bulletSpeed = 10;
}
}
}
}
2.Bullet类
public class Bullet {
int bx,by;
Image bImg;
int bSpeed;
int bDirection;
boolean exist=true;
public Bullet(int bx,int by,Image bImg,int bSpeed,int bDirection) {
super();
this.bx = bx;
this.by = by;
this.bImg = bImg;
this.bSpeed = bSpeed;
this.bDirection = bDirection;
}
//画子弹的方法
public void drawBullet(Graphics g) {
g.drawImage(bImg, bx, by, null);
}
//子弹移动的方法
public void moveBullet0() {
by-=bSpeed;
if (by<0) {
exist=false;
}
}
public void moveBullet1() {
bx-=bSpeed;
by-=bSpeed;
if (by<0) {
exist=false;
}
}
public void moveBullet2() {
bx+=bSpeed;
by-=bSpeed;
if (by<0) {
exist=false;
}
}
}
3.Enemy类
public class Enemy {
int ex;
int ey;
Image eImg;
int eSpeed;
PlaneJPanel mainview;
boolean exist = true;
int eTime = 10;
//飞机和敌机撞击的标识
boolean Y = false;
public Enemy(int ex, int ey, Image eImg, int eSpeed, PlaneJPanel mainview) {
super();
this.ex = ex;
this.ey = ey;
this.eImg = eImg;
this.eSpeed = eSpeed;
this.mainview = mainview;
}
//画敌机
public void drawEnemy(Graphics g) {
g.drawImage(eImg, ex, ey, null);
}
//敌机移动
public void moveEnemy() {
ey+=eSpeed;
if(ey>mainview.getHeight()) {
exist = false;
}
}
//子弹和敌机撞击
public void hint(Bullet bullet,Enemy enemy) {
int lx = Math.abs((bullet.bx+bullet.bImg.getWidth(null)/2)-(enemy.ex+enemy.eImg.getWidth(null)/2));
int ly = Math.abs((bullet.by+bullet.bImg.getHeight(null)/2)-(enemy.ey+enemy.eImg.getHeight(null)/2));
boolean H = lx<= (bullet.bImg.getWidth(null)/2+enemy.eImg.getWidth(null)/2);
boolean R=ly<=(bullet.bImg.getHeight(null)/2+enemy.eImg.getHeight(null)/2);
if (R&&H) {
enemy.exist = false;
bullet.exist = false;
}
}
//飞机和敌机撞击
public void hint1(PlaneJPanel panel,Enemy enemy) {
int lx = Math.abs((enemy.ex+enemy.eImg.getWidth(null)/2)-(panel.px+panel.p[0].getWidth(null)/2));
int ly = Math.abs((enemy.ey+enemy.eImg.getHeight(null)/2)-(panel.py+panel.p[0].getHeight(null)/2));
boolean H= lx<=(panel.p[0].getWidth(null)/2+enemy.eImg.getWidth(null)/2);
boolean R= ly<=(panel.p[0].getHeight(null)/2+enemy.eImg.getHeight(null)/2);
if(H&&R) {
enemy.exist = false;
Y=true;//飞机和敌机撞击敌机死亡的标识
panel.Pexist=false;//飞机和敌机撞击飞机撞击的标识
}
}
}
4.PlaneJFrame类
public class PlaneJFrame extends JFrame{
public PlaneJFrame(){
this.setTitle("飞机大战");
this.setSize(420,640);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PlaneJPanel jpanel = new PlaneJPanel();
this.add(jpanel);
this.setVisible(true);
}
public static void main(String[] args) {
new PlaneJFrame();
}
}
5.PlaneJPanel类
//流式访问文件
public class PlaneJPanel extends JPanel implements Runnable, MouseMotionListener, MouseListener {
Thread t = null;
static Image startImg;
// 定义变量存储背景图片
int x = 0, y = 0;
// 游戏开始之前
boolean ck = true;
// 读取飞机图片
static BufferedImage[] p = new BufferedImage[2];
// 飞机坐标
int px = 100, py = 100;
int pc = 0;
// 暂停开关
boolean suspend = false;
// 子弹存储图片对象
static Image bImg;
List<Bullet> bullets = new ArrayList<Bullet>();
int count;
// 奖励存储图片对象
List<Award> awards = new ArrayList<Award>();
static BufferedImage[] awa = new BufferedImage[3];
// 飞机的血量
int blood = 1;
// 三个子弹标识
boolean threeBullet = false;
// 子弹存在的时间
int threeTime;
// 子弹加速
int bulletSpeed = 3;
int bulletTime;
// 存储敌机
List<Enemy> enemies = new ArrayList<Enemy>();
// 控制血量
boolean Pexist = true;
int time = 15;
// 得分
int score = 0;
static {// 静态代码块中进行最先的加载
try {
startImg = ImageIO.read(new File("image/GameInterface/interface_1.png"));
p[0] = ImageIO.read(new File("image/1.png"));
p[1] = ImageIO.read(new File("image/2.png"));
bImg = ImageIO.read(new File("image/bullet/bullet_1.png"));
awa[0] = ImageIO.read(new File("image/award/award_1.png"));
awa[1] = ImageIO.read(new File("image/award/award_2.png"));
awa[2] = ImageIO.read(new File("Image/award/award_3.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PlaneJPanel() {
t = new Thread(this);
// 添加鼠标监听
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.drawImage(startImg, x, y, null);
// 画飞机
if (ck == false) {
pc = pc == 0 ? 1 : 0;
g.drawImage(p[pc], px, py, null);
}
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
bullet.drawBullet(g);
}
// 画奖励
for (int i = 0; i < awards.size(); i++) {
Award award = awards.get(i);
award.drawAward(g);
}
// 画血量
if (ck == false) {
if (blood > 0 && blood <= 5) {
for (int i = 0; i < blood; i++) {
g.drawImage(awa[0], 37 * i, 10, null);
}
} else if (blood <= 0) {
try {
p[0] = ImageIO.read(new File("image/blast/blast_2.png"));
p[1] = ImageIO.read(new File("image/blast/blast_2.png"));
startImg = ImageIO.read(new File("image/GameInterface/jeimian_2.png"));
time--;
if (time <= 0) {
t.stop();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
for (int i = 0; i < 6; i++) {
g.drawImage(awa[0], 20 * i, 10, null);
}
}
}
// 画敌机
if (ck == false) {
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
enemy.drawEnemy(g);
}
}
// 写得分
if (ck == false) {
g.setFont(new Font("仿宋", Font.BOLD, 40));
g.drawString("得分:" + score, 0, 80);
//g.setColor(Color.white);
}
}
public void run() {
synchronized (this) {
while (true) {
count++;
if (suspend) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
y++;
if (y >= 0 && time > 0) {
y = -5400;
} else if (blood <= 0) {
y = 0;
}
// 子弹移动的方法
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if (bullet.bDirection == 0) {
bullet.moveBullet0();
}
if (bullet.bDirection == 1) {
bullet.moveBullet1();
}
if (bullet.bDirection == 2) {
bullet.moveBullet2();
}
}
// 创建子弹对象
if (count % 20 == 0) {
Bullet bullet0 = new Bullet(px + p[pc].getWidth() / 2 - bImg.getWidth(null) / 2, py, bImg,
bulletSpeed, 0);
bullets.add(bullet0);
if (threeBullet) {
Bullet bullet1 = new Bullet(px + p[pc].getWidth() / 2 - bImg.getWidth(null) / 2, py, bImg,
bulletSpeed, 1);
Bullet bullet2 = new Bullet(px + p[pc].getWidth() / 2 - bImg.getWidth(null) / 2, py, bImg,
bulletSpeed, 2);
bullets.add(bullet1);
bullets.add(bullet2);
}
}
// 奖励的创建
if (count % 100 == 0) {
int h = (int) (Math.random() * 3);
int aSpeed = (int) (Math.random() * 5 + 3);
Award award = new Award((int) (Math.random() * 300), -50, awa[h], aSpeed, h, this);
awards.add(award);
}
// 奖励的移动
for (int i = 0; i < awards.size(); i++) {
Award award = awards.get(i);
award.moveAward();
}
// 子弹变成三发
if (threeBullet) {
threeTime++;
}
if (threeTime == 300) {
threeBullet = false;
threeTime = 0;
}
// 子弹加速
if (bulletSpeed == 10) {
bulletTime++;
}
if (bulletTime == 300) {
bulletSpeed = 3;
bulletTime = 0;
}
// 创建敌机
if (count % 20 == 0) {
int eh = (int) (Math.random() * 5 + 2);
Enemy enemy = new Enemy((int) (Math.random() * 350), -50,
new ImageIcon("image/LittlePlane/plane" + eh + ".png").getImage(), eh, this);
enemies.add(enemy);
}
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
enemy.moveEnemy();
}
// 敌机和子弹碰撞
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
for (int j = 0; j < bullets.size(); j++) {
Bullet bullet = bullets.get(j);
enemy.hint(bullet, enemy);
}
}
// 敌机和子弹碰撞之后敌机消失
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
if (enemy.exist == false && enemy.eTime == 0) {
score++;
enemies.remove(i);
} else if (enemy.exist == false) {
enemy.eTime--;
enemy.eImg = new ImageIcon("image/blast/blast_1.png").getImage();
}
}
// 子弹消失
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if (bullet.exist == false) {
bullets.remove(i);
}
}
// 飞机和敌机撞击
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
enemy.hint1(this, enemy);
if (enemy.exist == false && enemy.Y) {
enemies.remove(i);
}
}
// 血量减少
if (Pexist == false) {
blood--;
Pexist = true;
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
private Object Pexist(boolean b) {
// TODO Auto-generated method stub
return null;
}
@Override
public void mouseClicked(MouseEvent e) {
// 在开始游戏范围内点击
if (ck && e.getX() >= 132 && e.getX() <= 259 && e.getY() >= 392 && e.getY() <= 432) {
ck = false;
try {
startImg = ImageIO.read(new File("image/background/background_1.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 改变背景图片坐标
y = -5400;
t.start();
}
if (ck == false && e.getModifiers() == e.BUTTON3_MASK) {
suspend = suspend ? resume() : suspend();
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
// 鼠标移动到这个范围内改变状态
if (ck && e.getX() >= 132 && e.getX() <= 259 && e.getY() >= 392 && e.getY() <= 432) {
setCursor(new Cursor(Cursor.HAND_CURSOR));// 将鼠标的状态变成小手
} else if (ck == false) {
setCursor(new Cursor(Cursor.HAND_CURSOR));// 将鼠标的状态变成小手
} else {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));// 将鼠标状态变成箭头
}
px = e.getX() - p[pc].getWidth() / 2;
py = e.getY() - p[pc].getHeight() / 2;
if (px <= 0) {
px = 0;
}
if (py <= 0) {
py = 0;
}
if (px >= 400 - p[pc].getWidth()) {
px = 400 - p[pc].getWidth();
}
if (py >= 600 - p[pc].getHeight()) {
py = 600 - p[pc].getHeight();
}
}
// 定义两个方法
public boolean suspend() {
suspend = true;
return suspend;
}
public synchronized boolean resume() {
suspend = false;
notify();
return suspend;
}
}