制作一个简单的飞机大战

注意!!!!!,图片素材在我的资源里有。

下面代码排序,非先后顺序。请看清楚类名

Airplane类

package Flyplane;public class Airplane extends FlyingObject  implements Enemy{private int yspeed=2;public Airplane() {image=ShootGame.airplane;width=image.getWidth();height=image.getHeight();x=(int) (Math.random()*(ShootGame.WIDTH-this.width));y=-this.height;//  x=100;//y=100;}public int getScore() {return 5;}public void step() {y+=yspeed;}public boolean outOfBounds(){return this.y>=ShootGame.HEIGHT; }}

Award类

package Flyplane;public interface Award  {public int DOUBLE_FIRE=0;//双倍攻击 double firepublic int LIFE=1;//生命//获取奖励,0类型或者1类型public int getType();}

Bee类

package Flyplane;import java.util.Random;public class Bee extends FlyingObject implements Award{//走路步数private int xSpeed=1;private int ySpeed=2;//获取的奖励private int awardType;//蜜蜂显示方法public Bee() {image=ShootGame.bee;//图width=image.getWidth();//宽height=image.getHeight();//高y=-height;//长为y长度减去图Random rand=new Random();//设随机数x=rand.nextInt(ShootGame.WIDTH-this.width);//总宽-图宽,随即移动awardType=rand.nextInt(2);//随机获得奖励//  y=200;//  x=100;}public void step() {x+=xSpeed;y+=ySpeed;if(x>=ShootGame.WIDTH-this.width) {xSpeed=-1;}if(x<=0) {xSpeed=1;}}public boolean outOfBounds(){return this.y>=ShootGame.HEIGHT; //蜜蜂的y>=屏幕的高,即为越界}public int getType(){return awardType; //返回奖励类型}}

bullet类

package Flyplane;import Flyplane.ShootGame;public class Bullet extends FlyingObject  {private int speed=3;public Bullet(int x,int y) {image=ShootGame.bullet;this.x=x;this.y=y;} public void step() {y-=speed;}public boolean outOfBounds(){return this.y<=-this.height; //子弹的y<=负的子弹的高,即为越界}}

enemy接口

package Flyplane;
//敌方分数public interface Enemy{public int getScore();
}

Flyingobject类

package Flyplane;import java.awt.image.BufferedImage;public abstract class FlyingObject {protected int x;protected int y;protected int width;protected int height;protected BufferedImage image;public int getX() {return x;}public void setX(int x) {this.x=x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public BufferedImage getImage() {return image;}public void setImage(BufferedImage image) {this.image = image;}public abstract void step();public abstract boolean outOfBounds();public boolean shootBy(Bullet bullet) {int x=bullet.x;int y=bullet.y;return this.x<x&&x<this.x+width&&this.y<y&&y<this.y+height;}}

Hero类

package Flyplane;import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;public  class Hero extends FlyingObject  {private int life;//生命private int doubleFire;//火力private BufferedImage[] images= {};//图片切换数组private int index=0;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};}public void step() {if(images.length>0) {image=images[index++/10%images.length];}}//发射public Bullet[] shoot() {int xStep=this.width/4;int yStep=20;if(doubleFire>0) {Bullet[] bs=new Bullet[2];bs[0]=new Bullet(this.x+1*xStep,this.y-yStep);bs[1]=new Bullet(this.x+3*xStep,this.y-yStep);doubleFire-=2;return bs;}else {Bullet[] bs=new Bullet[1];bs[0] = new Bullet(this.x+2*xStep,this.y-yStep); return bs;}}public void addDoubleFire() {doubleFire+= 40; }public void addLife(){life++; //命数增1}public int getLife(){return life; //返回命数}public boolean outOfBounds(){return false; //永不越界}public void subtractLife(){life--;}public void setDoubleFire(int doubleFire) {this.doubleFire = doubleFire;}public boolean hit(FlyingObject other){int x1 = other.x-this.width/2; //x1:敌人的x-1/2英雄机的宽int x2 = other.x+other.width+this.width/2; //x2:敌人的x+敌人的宽+1/2英雄机的宽int y1 = other.y-this.height/2; //y1:敌人的y-1/2英雄机的高int y2 = other.y+other.height+this.height/2; //y2:敌人的y+敌人的高+1/2英雄机的高int x = this.x+this.width/2;  //x:英雄机的x+1/2英雄机的宽int y = this.y+this.height/2; //y:英雄机的y+1/2英雄机的高return x>x1 && x<x2&&y>y1 && y<y2; //x在x1和x2之间,并且,y在y1和y2之间,即为撞上了}public void moveTo(int x,int y){this.x = x - this.width/2;  //英雄机的x:鼠标的x-1/2英雄机的宽this.y = y - this.height/2; //英雄机的y:鼠标的y-1/2英雄机的高}}

Shootgame类

package Flyplane;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;//主程序类
public class ShootGame extends JPanel{public static final int WIDTH = 800;  //窗口宽public static final int HEIGHT = 1000; //窗口高public static BufferedImage background; //背景图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;      //英雄机0public static BufferedImage hero1;      //英雄机1private Hero hero = new Hero(); //英雄机对象private FlyingObject[] flyings = {}; //敌人(敌机+小蜜蜂)数组private Bullet[] bullets = {}; //子弹数组private Timer timer;  //定时器private int intervel = 1000/100;  //时间间隔(毫秒)private int score = 0; //玩家的得分private int state;public static final int START = 0;     //启动状态public static final int RUNNING = 1;   //运行状态public static final int PAUSE = 2;     //暂停状态public static final int GAME_OVER = 3; //游戏结束状态public ShootGame(){
//      flyings = new FlyingObject[2];
//      flyings[0] = new Airplane();
//      flyings[1] = new Bee();
//      bullets = new Bullet[1];
//      bullets[0] = new Bullet(150,180);}static{ //加载图片try{background = 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(Exception e){e.printStackTrace();}}/** 重写paint() g:画笔*/public void paint(Graphics g){g.drawImage(background,0,0,null); //画背景图paintHero(g); //画英雄机paintFlyingObjects(g); //画敌人(敌机+小蜜蜂)paintBullets(g); //画子弹paintScore(g); //画分数paintState(g); //画状态}/** 画英雄机对象 */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"); //创建一个Jframe对象ShootGame game = new ShootGame(); //创建一个JPanel对象frame.add(game); //将面板添加到框架中frame.setSize(WIDTH, HEIGHT); //设置窗口大小frame.setAlwaysOnTop(true); //设置总是在最上面frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置默认关闭操作(窗口关闭时退出程序)frame.setLocationRelativeTo(null); //设置居中显示frame.setVisible(true); //1.设置窗口可见  2.尽快调用paint()game.action();   //启动执行}/** 随机生成飞行物 */public FlyingObject nextOne(){Random rand = new Random(); //创建随机数对象int type = rand.nextInt(20); //生成0到19之间的随机数if(type==0){ //为0时返回蜜蜂对象return new Bee();}else{ //为1到19时返回敌机对象return new Airplane();}}int flyEnteredIndex = 0; //敌人入场计数/** 敌人(敌机+小蜜蜂)入场 */public void enterAction(){ //10毫秒走一次flyEnteredIndex++; //每10毫秒增1if(flyEnteredIndex%40==0){ //400(10*40)毫秒走一次FlyingObject obj = nextOne(); //获取敌人(敌机+小蜜蜂)对象flyings = Arrays.copyOf(flyings,flyings.length+1); //扩容(扩大一个容量)flyings[flyings.length-1] = obj; //将敌人对象赋值给数组的最后一个元素}}/** 飞行物走一步 */public void stepAction(){ //10毫秒走一次hero.step(); //英雄机走一步for(int i=0;i<flyings.length;i++){ //遍历所有敌人flyings[i].step(); //每个敌人走一步}for(int i=0;i<bullets.length;i++){ //遍历所有子弹bullets[i].step(); //每个子弹走一步}}/** 启动程序的执行 */public void action(){MouseAdapter l = new MouseAdapter(){ //创建侦听器对象/** 鼠标移动事件 */public void mouseMoved(MouseEvent e){if(state==RUNNING){ //运行状态时执行int x = e.getX();  //获取鼠标的x坐标int y = e.getY();  //获取鼠标的y坐标hero.moveTo(x, y); //英雄机随着鼠标动}}/** 鼠标点击事件 */public void mouseClicked(MouseEvent e){switch(state){ //不同状态时点击后有不同的反应case START: //启动状态时state = RUNNING; //当前状态变为运行状态break;case GAME_OVER: //游戏结束状态时score = 0; //清理现场hero = new Hero();flyings = new FlyingObject[0];bullets = new Bullet[0];state = START; //当前状态变为启动状态break;}}/** 鼠标移出事件 */public void mouseExited(MouseEvent e){if(state==RUNNING){ //运行状态时state=PAUSE; //当前状态改为暂停状态}}/** 鼠标移入事件 */public void mouseEntered(MouseEvent e){if(state==PAUSE){ //暂停状态时state=RUNNING; //当前状态改为运行状态}}};this.addMouseListener(l); //处理鼠标操作事件this.addMouseMotionListener(l); //处理鼠标滑动操作timer = new Timer(); //创建定时器对象timer.schedule(new TimerTask(){public void run(){ //10毫秒走一次--定时干的那个事if(state==RUNNING){ //运行状态时执行enterAction(); //敌人(敌机+小蜜蜂)入场stepAction();  //飞行物走一步shootAction(); //英雄机发射子弹--子弹入场bangAction();  //子弹与敌人的碰撞outOfBoundsAction(); //删除越界的飞行物checkGameOverAction(); //检测游戏是否结束}repaint();     //重画,调用paint()}},intervel,intervel);}int shootIndex = 0; //射击计数/** 英雄机发射子弹(子弹入场) */public void shootAction(){ //10毫秒走一次shootIndex++; //每10毫秒增1if(shootIndex%30==0){ //每300(10*30)毫秒走一次Bullet[] bs = hero.shoot(); //获取英雄机发射出来的子弹bullets = Arrays.copyOf(bullets, bullets.length+bs.length); //扩容(bs有几个元素就扩大几个容量)System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length); //数组的追加(将bs追加到bullets数组中)}}/** 所有子弹与所有敌人撞 */public void bangAction(){ //10毫秒走一次for(int i=0;i<bullets.length;i++){ //遍历所有子弹Bullet b = bullets[i]; //获取每一个子弹bang(b); //一个子弹与所有敌人撞}}/** 一个子弹与所有敌人撞 */public void bang(Bullet b){int index = -1; //被撞敌人的下标for(int i=0;i<flyings.length;i++){ //遍历所有敌人FlyingObject f = flyings[i]; //获取每一个敌人if(f.shootBy(b)){ //撞上了index = i; //记录被撞敌人的下标break; //其余敌人不再比较}}if(index != -1){ //有撞上的FlyingObject one = flyings[index]; //获取被撞的敌人对象if(one instanceof Enemy){  //若被撞对象是敌人Enemy e = (Enemy)one;  //将被撞对象强转为敌人score += e.getScore(); //累加分数}if(one instanceof Award){   //若被撞对象是奖励Award a = (Award)one;   //将被撞对象强转为奖励int type = a.getType(); //获取奖励类型switch(type){ //根据type的不同取值获取相应的奖励case Award.DOUBLE_FIRE:   //奖励类型为火力时hero.addDoubleFire(); //英雄机增火力break;case Award.LIFE:    //奖励类型为命时hero.addLife(); //英雄机增命break;}}//交换被撞敌人对象与数组中的最后一个元素FlyingObject t = flyings[index];flyings[index] = flyings[flyings.length-1];flyings[flyings.length-1] = t;//缩容(去掉最后一个元素,即:被撞敌人对象)flyings = Arrays.copyOf(flyings, flyings.length-1);}}/** 画分数 */public void paintScore(Graphics g){int x = 10;int y = 25;Font font = new Font(Font.SANS_SERIF,Font.BOLD,14);g.setColor(new Color(0x3A3B3B)); //设置颜色(纯红)g.setFont(font); //设置样式(字体:SANS_SERIF,样式:加粗,字号:24)g.drawString("SCORE: "+score,x,y); //画分y+=20;g.drawString("LIFE: "+hero.getLife(),x,y); //画命}/** 删除越界的飞行物 */public void outOfBoundsAction(){int index = 0; //1.不越界敌人数组下标  2.不越界敌人个数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++; //1.下标增一  2.不越界敌人个数增一}}flyings = Arrays.copyOf(flyingLives, index); //将不越界敌人复制到flyings数组中,index为flyings的新长度index = 0; //1.下标归零  2.不越界个数归零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++; //1.下标增一  2.不越界子弹个数增一}}bullets = Arrays.copyOf(bulletLives, index); //将不越界敌人复制到bullets数组中,index为bullets的新长度}/** 判断游戏是否结束  返回true表示游戏结束 */public boolean isGameOver(){for(int i=0;i<flyings.length;i++){ //遍历所有敌人FlyingObject f = flyings[i]; //获取每一个敌人if(hero.hit(f)){ //撞上了hero.subtractLife(); //英雄机减命hero.setDoubleFire(0); //英雄机清火力//交换被撞敌人与数组的最后一个元素FlyingObject t = flyings[i];flyings[i] = flyings[flyings.length-1];flyings[flyings.length-1] = t;//缩容(去掉最后一个元素,即:被撞的敌人对象)flyings = Arrays.copyOf(flyings,flyings.length-1);}}return hero.getLife()<=0; //命数<=0,即为游戏结束}/** 检测游戏是否结束 */public void checkGameOverAction(){if(isGameOver()){ //游戏结束时state = GAME_OVER; //当前状态改为游戏结束状态}}/** 画状态 */public void paintState(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: //游戏结束状态时画游戏结束图g.drawImage(gameover,0,0,null);break;}}}

用java写一个简单的飞机大战游戏相关推荐

  1. 用C语言写一个简单的飞机大战游戏(用到easyx图形库)

    学习内容:1.背景的运动 2.飞机爆炸效果 3.透明贴图工具 目录 一.需要用到的头文件.全局变量和一些函数.结构体 二.数据初始化以及图片处理 三.主体 四.物体碰撞 五.游戏运行 六.透明贴图工具 ...

  2. 用Java写一个简单的回合制游戏

    用Java写一个简单的回合制游戏 创建基本属性,血量,名字,技能,技能伤害. 如下: public class One3 {//创建三个属性private String name;private St ...

  3. 用JAVA写一个简单的21点游戏(不设置庄主的情况下)

    在某次的恶劣的JAVA上机中,菜鸟博主嗅到了一丝蔑视的味道--上个班的这个作业听说好像有点难! 怎么会! 看我! -- 好吧我看了,我好像也写不出来~ 当然事实可能是有点难度了,毕竟对于JAVA初学者 ...

  4. 用java写一个简单的区块链(下)

    用java写一个简单的区块链(下) 2018年03月29日 21:44:35 java派大星 阅读数:725 标签: 区块链java 更多 个人分类: 区块链 版权声明:本文为博主原创文章,转载请标明 ...

  5. 怎样用java写一个简单的文件复制程序

    怎样用java写一个简单的文件复制程序 代码来源:https://jingyan.baidu.com/article/c35dbcb0d6f1398916fcbc07.html package Num ...

  6. 用java写一个日历_使用JAVA写一个简单的日历

    JAVA写一个简单的日历 import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDa ...

  7. java编写日历思路_使用JAVA写一个简单的日历

    JAVA写一个简单的日历import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDat ...

  8. 使用JAVA写一个简单的日历

    JAVA写一个简单的日历 import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDa ...

  9. java递归怎么写_什么是递归?用Java写一个简单的递归程序

    什么是递归?用Java写一个简单的递归程序 递归的定义 递归(recursion):以此类推是递归的基本思想,将规模大的问题转化为规模小的问题来解决. 递归的要素 自定义递归函数,并确定函数的基本功能 ...

最新文章

  1. 计算机、数学、运筹学等领域的32个重要算法
  2. linux自动执行top,Linux top 命令使用
  3. 消费级GPU、速度提升3000倍,微软FastNeRF实现200FPS高保真神经渲染
  4. 模拟3个人排除买票,每人买1张票。售货员只有1张五元的钱,电影票5元一张
  5. UFLDL教程: Exercise: Implement deep networks for digit classification
  6. 首款搭载鸿蒙os的设备,华为发布会配件汇总,首款搭载 鸿蒙OS 的设备来了
  7. 我要带徒弟学写JAVA架构,引路架构师之路
  8. mysql 长事务查询_MySQL长事务详解
  9. iPhone 13 Pro手机壳曝光 网友:更丑了
  10. 定积分在计算机中的应用,计算机模拟定积分的定义
  11. C语言回溯算法求幂集,回溯求幂集算法
  12. 扫地机器人市场:米家、科沃斯激烈肉搏
  13. chrome浏览器完整保存整个网页 可离线访问
  14. html5 制作书架展示 PHP,基于HTML5 Canvas的CSG构造实体几何书架
  15. 工作中那些让人印象深刻的BUG(2)
  16. Java Swing扫雷游戏demo分享
  17. 先图科技的AGI模盒发布了!
  18. 解决Ubuntu和Windows不能相互复制粘贴的问题
  19. Nuxt学习(vue项目移植)
  20. Python面向对象之六:类的约束和super函数

热门文章

  1. ThoughtWorks.QRCode 生成QR二维码时提示“索引超出了数组界限”的原因和解决方法
  2. 用html、js、css实现通讯录功能
  3. 兔子数列(斐波那契数列)的优化
  4. 加了权重样本的AUC如何计算
  5. dw中css规则定义中文,Dreamweaver编辑CSS规则
  6. 一起探索圈外的世界 | GTLC峰会·厦门站
  7. Vue push() pop() shift().....
  8. 行云创新:用云原生,实现企业数字化转型最短路径
  9. C++窗体程序设计(一)窗体类WNDCLASSEX
  10. Ezreal--将一个动画切割为四动作动画成功---New Unity Project (3).