飞机大战(Java)

news/2024/10/18 18:30:09/

飞机大战 游戏规则:游戏可以产生小的敌机、大的敌机、小蜜蜂,这三类都是随机概率出现的,游戏打开的时候,鼠标单击,游戏开始, 自动发射子弹,英雄机跟随鼠标移动,当鼠标移到窗口外时,游戏暂停,当鼠标又移回时,游戏继续,子弹打中敌机和小蜜 蜂,当生命降到0时,消失,敌机 撞击到英雄机, 英雄机生命值-1, 直到0时, 游戏结束, 小敌机: 分数+,大敌机: 分数+ 奖励(生命值加成, 火力加成),小蜜蜂: 奖励(生命值加成, 火力加成)。

源码地址:https://gitee.com/lxlxlxl233/fly-battle

package com.chinasofti.shoot;import java.util.Random;//敌机
public class Airplane  extends FlyingObject implements Enemy{private int speed=2;//走步/*构造方法*/public Airplane(){image=ShootGame.airplane;width=image.getWidth();height=image.getHeight();Random rand =new Random();x=rand.nextInt(ShootGame.WIDTH-this.width);y=-this.height;}@Overridepublic int getScore() {return 5;}@Overridepublic void step() {y+=speed;}@Overridepublic boolean outOfBounds() {return this.y>ShootGame.HEIGHT;}}

 

package com.chinasofti.shoot;
//奖励
public interface Award {public int LIFE=1;//命public int DOUBLE_FIRE=0;//火力值/*获取奖励 0:火力值,1:命*/public int getType();
}
package com.chinasofti.shoot;import java.util.Random;//小蜜蜂
public class Bee extends FlyingObject implements Award {private int xspeed=1;//x轴速度private int yspeed=2;//y轴速度private int awardType;public Bee() {image=ShootGame.bee;width=image.getWidth();height=image.getHeight();Random rand=new Random();x=rand.nextInt(ShootGame.WIDTH-this.width);y=-this.height;awardType= rand.nextInt(2);}@Overridepublic int getType() {return awardType;}@Overridepublic void step() {y+=yspeed;x+=xspeed;if(x>ShootGame.WIDTH-this.width){//最右边xspeed=-1;}if(x<0){//最左边xspeed=1;}}@Overridepublic boolean outOfBounds() {return this.y>ShootGame.HEIGHT;}}
package com.chinasofti.shoot;import java.util.Random;//子弹
public class Bullet extends FlyingObject{private int speed=3;public Bullet(int x,int y) {image=ShootGame.bullet;width=image.getWidth();height=image.getHeight();Random rand =new Random();this.x=x;//随着英雄机移动this.y=y;//随着英雄机移动}@Overridepublic void step() {y-=speed;}@Overridepublic boolean outOfBounds() {return this.y<-this.height;}}
package com.chinasofti.shoot;
//敌人
public interface Enemy {/*得分*/public int getScore();
}
package com.chinasofti.shoot;import java.awt.image.BufferedImage;//飞行物
public abstract class FlyingObject {protected BufferedImage image;//图片protected int width;//长protected int height;//宽protected int x;//横坐标xprotected int y;//纵坐标y/*飞行物走步*/public abstract void step();/*判断飞行物是否越界*/public abstract boolean outOfBounds();/*敌人被子弹撞击*/public boolean shootBy(Bullet bullet){int x1=this.x;//敌人xint x2=this.x+this.width;//敌人宽int y1=this.y;//敌人yint y2=this.y+this.height;//敌人高int x=bullet.x;//子弹xint y=bullet.y;//子弹yreturn x>x1&&x<x2&&y>y1&&y<y2;}
}
package com.chinasofti.shoot;import java.awt.image.BufferedImage;public class Hero extends FlyingObject {private int life;private int doubleFire;private BufferedImage[] images = {};private int index;//协助切换图片public Hero() {image = ShootGame.hero0;width = image.getWidth();height = image.getHeight();x = 150;y = 400;life = 3;doubleFire = 0;//默认火力值images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};index = 0;}//图片切换@Overridepublic void step() {//每100毫秒走一次image = images[index++/10%images.length];}@Overridepublic boolean outOfBounds() {return false;}/*英雄机发射子弹*/public Bullet[] shoot(){int xStep = this.width/4; //1/4英雄机宽度if(doubleFire > 0){//双倍火力Bullet[] bullets = new Bullet[2];//两发子弹bullets[0] = new Bullet(this.x+1*xStep,this.y - 20);bullets[1] = new Bullet(this.x+3*xStep,this.y - 20);doubleFire -= 2;//发射一次双倍火力,活力值-2return bullets;}else {//单倍火力Bullet[] bullets = new Bullet[1];//一发子弹bullets[0] = new Bullet(this.x+2*xStep,this.y - 20);return bullets;}}/*英雄机随着鼠标移动*/public void moveTo(int x,int y){this.x = x - this.width/2;this.y = y - this.height/2;}//加一条命public void addLife(){life++;}//获取生命public int getLife(){return life;}/*加火力值*/public void addDoubleFire() {doubleFire += 40;}/*判断英雄机与敌机碰撞*/public boolean hit(FlyingObject obj) {int x1 = obj.x - this.width/2;int x2 = obj.x + obj.width + this.width/2;int y1 = obj.y - this.height/2;int y2 = obj.y + obj.height +this.height/2;int x = this.x + this.width/2;int y = this.y + this.height/2;return x > x1 && x < x2&&y > y1 && y < y2;}//减少生命public void subtractLift() {life --;}public void setDoubleFire(int doubleFire) {this.doubleFire = doubleFire;}
}
package com.chinasofti.shoot;import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;//测试类
public class ShootGame extends JPanel {//窗口高public static final int WIDTH = 400;public static final int HEIGHT = 654;public static final int GAME_OVER=3;//游戏结束状态public static final int START=0;//游戏开始状态public static final int PAUSE=2;//游戏暂停状态public static final int RUNING=1;//游戏结束状态private int state=0;//当前状态public static BufferedImage backgroud;//背景图public static BufferedImage start;//启动图public static BufferedImage pause;//暂停图public static BufferedImage gameover;//结束图public static BufferedImage airplane;//敌机图public static BufferedImage bee;//小蜜蜂图public static BufferedImage bullet;//子弹图public static BufferedImage hero0;//英雄0图public static BufferedImage hero1;//英雄1图private Hero hero = new Hero();private FlyingObject[] flyings = {};private Bullet[] bullets = {};/*ShootGame() {flyings = new FlyingObject[2];//敌机,小蜜蜂flyings[0] = new Airplane();flyings[1] = new Bee();bullets = new Bullet[1];bullets[0] = new Bullet(130, 110);}*//*生成敌人,小蜜蜂*/public static FlyingObject nextOne() {Random rand = new Random();int type = rand.nextInt(20);//0-19之间的随机数//控制小蜜蜂数量if (type == 0) {return new Bee();} else {return new Airplane();}}//入场计数 int indxe=1++,使用index控制敌机数量,每400ms产生一架int flyEnteredIndex = 0;static {//静态初始化资源try {backgroud = ImageIO.read(ShootGame.class.getResource("background.png"));start = ImageIO.read(ShootGame.class.getResource("start.png"));pause = ImageIO.read(ShootGame.class.getResource("pause.png"));gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));bee = ImageIO.read(ShootGame.class.getResource("bee.png"));bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));} catch (IOException e) {e.printStackTrace();}}/*敌人入场*/public void enterAction() {//将敌机放入flying数组flyEnteredIndex++;if (flyEnteredIndex % 40 == 0) {//每400毫秒产生一个敌人FlyingObject obj = nextOne();flyings = Arrays.copyOf(flyings, flyings.length + 1);//扩容flyings[flyings.length - 1] = obj;//将敌人赋值给flyings最后一个元素}}//飞行物走步public void stepAction() {hero.step();//敌人走步for (int i = 0; i < flyings.length; i++) {flyings[i].step();//敌人走一步}//子弹走步for (int i = 0; i < bullets.length; i++) {bullets[i].step();//子弹走一步}}/*英雄机发射子弹*/int shootindex = 0;public void shootAction() {shootindex++;if (shootindex % 30 == 0) {//每300ms走一次//创建子弹对象,添加到bullets数组中Bullet[] bs = hero.shoot();bullets=Arrays.copyOf(bullets,bullets.length+bs.length);//扩容弹夹System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);}}//飞行物越界删除public void outOfBoundsAction() {int index=0;FlyingObject[] flyingLives=new FlyingObject[flyings.length];for(int i=0;i<flyings.length;i++){FlyingObject f = flyings[i];if(!f.outOfBounds()){flyingLives[index]=f;index++;}}flyings=Arrays.copyOf(flyingLives,index);//System.out.println(flyings.length);//子弹越界index=0;Bullet[] bulletLives=new Bullet[bullets.length];for(int i=0;i<bullets.length;i++){Bullet b=bullets[i];if (!b.outOfBounds()){bulletLives[index]=b;index++;}}bullets=Arrays.copyOf(bulletLives,index);}//检查游戏结束public void  checkGameOver(){if(isGameOver()){state=GAME_OVER;}}
/*判断游戏是否结束*/public boolean isGameOver(){for(int i=0;i<flyings.length;i++){FlyingObject f = flyings[i];if(hero.hit(f)){hero.subtractLift();//英雄机-life;//火力清零hero.setDoubleFire(0);//删除被撞敌人FlyingObject temp=flyings[i];flyings[i]=flyings[flyings.length-1];flyings[flyings.length-1]=temp;//缩容flyings=Arrays.copyOf(flyings,flyings.length-1);}}return hero.getLife()<=0;}/*启动执行action*/public void action() {//监听器对象MouseAdapter l=new MouseAdapter() {@Override//重写鼠标移动方法public void mouseMoved(MouseEvent e) {//监听器对象if(state==RUNING){//只有运行 时可以控制int x=e.getX();int y=e.getY();hero.moveTo(x,y);}}//重写鼠标点击事件public void mouseClicked(MouseEvent e){switch (state){case START:state=RUNING;break;case GAME_OVER:state=START;break;}}public void mouseExited(MouseEvent e){if(state==RUNING){state=PAUSE;}}public void mouseEntered(MouseEvent e){if(state==PAUSE){state=RUNING;}}};this.addMouseListener(l);//处理鼠标操作事件this.addMouseMotionListener(l);//处理鼠标滑动事件Timer timer = new Timer();//定时器int intervel = 10;timer.schedule(new TimerTask() {@Overridepublic void run() {//每10ms运行一次if(state==RUNING) {//只有运行状态能动enterAction(); //敌人入场stepAction();//飞行物shootAction();//子弹outOfBoundsAction();//删除越界飞行物bangAction();//子弹碰撞checkGameOver();//检查游戏结束}repaint();//重画}}, intervel, intervel);}int score=0;//玩家得分/*所有子弹撞*/public void bangAction(){for (int i=0;i<bullets.length;i++){Bullet b=bullets[i];//获取每一个子弹bang(b);//碰撞}}/*一颗子弹与敌人的碰撞*/public void bang(Bullet b){int index=-1;//被撞敌人下标,-1表示没撞上for(int i=0;i<flyings.length;i++){FlyingObject f=flyings[i];//是否被撞if(f.shootBy(b)){index=i;//记录下标break;}}if(index !=-1){FlyingObject obj=flyings[index];if(obj instanceof Enemy){Enemy e= (Enemy)obj;score+=e.getScore();}if(obj instanceof Award){Award a =(Award)obj;int type=a.getType();switch (type){case Award.DOUBLE_FIRE://双倍火力hero.addDoubleFire();//英雄机增加火力break;case Award.LIFE:hero.addLife();//加生命break;}}//将被撞敌人删除FlyingObject temp=flyings[index];flyings[index]=flyings[flyings.length-1];flyings[flyings.length-1]=temp;//缩容flyings=Arrays.copyOf(flyings,flyings.length-1);}}/*重写paint g画笔*/@Overridepublic void paint(Graphics g) {g.drawImage(backgroud, 0, 0, null);//画背景paintHero(g);paintFlyingObjects(g);paintBullets(g);paintScore(g);pantState(g);}//画状态public void pantState(Graphics g){switch (state){case START:g.drawImage(start,0,0,null);break;case PAUSE:g.drawImage(pause,0,0,null);break;case GAME_OVER://清理现场hero=new Hero();flyings=new FlyingObject[0];bullets=new Bullet[0];g.drawImage(gameover,0,0,null);break;}}/*画分 命*/public void paintScore(Graphics g){g.setColor(new Color(0xff));g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));g.drawString("SCORE:"+score,10,25);g.drawString("LIFE:"+hero.getLife(),10,45);}/*画英雄机*/public void paintHero(Graphics g) {g.drawImage(hero.image, hero.x, hero.y, null);}/*画敌机和小蜜蜂*/public void paintFlyingObjects(Graphics g) {for (int i = 0; i < flyings.length; i++) {FlyingObject f = flyings[i];//获取每一个敌人g.drawImage(f.image, f.x, f.y, null);}}/*画子弹*/public void paintBullets(Graphics g) {for (int i = 0; i < bullets.length; i++) {Bullet b = bullets[i];//获取每一个子弹g.drawImage(b.image, b.x, b.y, null);}}public static void main(String[] args) {JFrame frame = new JFrame("Fly");ShootGame game = new ShootGame();frame.add(game);//将面板添加到窗口//设置宽高frame.setSize(WIDTH, HEIGHT);frame.setAlwaysOnTop(true);//设置总在最上面frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//在关闭时离开frame.setLocationRelativeTo(null);//设置居中frame.setVisible(true);//设置窗口可见 尽快调用paint()方法//启动计时器game.action();}
}


http://www.ppmy.cn/news/246792.html

相关文章

Python飞机大战(完整版)

简介&#xff1a;一共分为2个py文件&#xff0c;分别是主类、和精灵类 飞机大战图片地址&#xff1a;链接: https://pan.baidu.com/s/18T6n9JFIDxBqYX9CnHi7ZQ 密码: tqbr 注释&#xff1a;项目启动后如果报libpng warning: iCCP: known incorrect sRGB profile无须处理&#…

Python升级版飞机大战

Python升级版飞机大战&#xff0c;程序运行截图&#xff1a; 敌方共有大中小3款飞机&#xff0c;分为高中低三种速度; 子弹的射程并非全屏,而大概是屏幕长度的80%;消灭小飞机需要1发子弹,中飞机需要8发,大飞机需要20发子弹;每消灭一架小飞机得1000分,中飞机6000分,大飞机10000…

飞机大战一触即发

1&#xff1a;飞机的移动&#xff0c;发射子弹&#xff0c;手雷&#xff0c;生命值&#xff0c;生命条\n\n2&#xff1a;敌飞机有3种形态&#xff08;小&#xff0c;中&#xff0c;大&#xff09;不同的飞机大小不一样&#xff0c;生命值不一样&#xff0c;爆炸动画也不一样\n\…

飞机小游戏流畅版

提示&#xff1a;新人入坑&#xff0c;采用的新手模板草草写就&#xff0c;望谅解 文章目录 前言一、代码原理和功能二、成果展示1.源代码 总结 前言 在做课下的游戏设计作业时&#xff0c;因为一时乐趣加入了一些功能&#xff0c;最后的完成代码可玩性不错&#xff0c;于是决…

疯狂的世界

最近看《中国二十年重案追踪-造假案》&#xff0c;其讲的都是近年来大的造假案件。有造假币&#xff0c;有造假药&#xff0c;有造假车票&#xff0c;甚至还有造假老虎的。其中既有目不识丁的农民&#xff0c;也有优秀有文化的人。所有造假案的共同点都是利令智昏。 由斯皮尔伯…

python的飞机大战

python的飞机大战的完整代码 alien.py import pygame from pygame.sprite import Sprite class Alien(Sprite):"""表示单个外星人的类"""def __init__(self, ai_settings, screen):"""初始化外星人并设置其他位置""&…

Java窗口游戏开发,飞机大战,打飞机,打大飞机,打无敌飞机妙啊!!!!————————香啊~~~~~~~~~~~~~~~~~

身无分文宅家&#xff0c;细发日渐稀疏。 双亲日益劳累&#xff0c;奈何无心寻工。 复试遥遥无期&#xff0c;心情惨惨戚戚。 若问此时作甚&#xff1f;抽烟喝酒扣叮。---杂记2020/3/25-2020/4/12 矫情下&#xff0c;莫认真 接下来奉上近日学习内容 ok这也是本人的一幅作品&am…

Html飞机大战(十): 消灭敌机

好家伙&#xff0c;本篇是带着遗憾写完的。 很遗憾&#xff0c;我找了很久&#xff0c;找到了bug但并没有成功修复bug 再上一篇中我们看到 子弹射中了敌机&#xff0c;但是敌机并没有消失&#xff0c;所以这篇我们要来完善这个功能 按照惯例我们来捋一下思路&#xff1a; 看看…