Advertisement

Java 小项目:飞机大战源码(已测试,无Bug)

  • 5星
  •     浏览量: 0
  •     大小:None
  •      文件类型:None


简介:
本项目为一个基于Java编程语言开发的“飞机大战”游戏,经过全面测试确保代码中没有明显错误。适合初学者学习和参考。 ```java public abstract class FlyingObject { public static final int LIFE = 0; // 表示对象存活状态 public static final int DEAD = 1; // 表示对象死亡状态(先爆破) public static final int REMOVE = 2; // 表示对象被移除(爆破后删除) protected int state = LIFE; // 当前的状态,默认为生存 protected int width; // 宽度 protected int height; // 高度 protected int x; // X坐标位置 protected int y; // Y坐标位置 public FlyingObject() {} /** * 构造方法,适用于小敌机、大敌机和蜜蜂等对象。 */ public FlyingObject(int width, int height) { this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH - width); // X坐标在0到(窗口宽度-物体宽度)之间的随机值 y = -height; // Y坐标的初始位置为负高度,表示从屏幕外开始移动 } /** * 构造方法,适用于英雄机、子弹和天空等对象。 */ public FlyingObject(int width, int height, int x, int y) { this.width = width; this.height = height; this.x = x; this.y = y; } /** * 加载图片资源 */ public static BufferedImage loadImage(String fileName){ try{ return ImageIO.read(FlyingObject.class.getResource(fileName)); }catch(Exception e) { e.printStackTrace(); throw new RuntimeException(); } } /** * 抽象方法,定义飞行物的移动逻辑。 */ public abstract void step(); /** * 获取对象对应的图片资源 */ public abstract BufferedImage getImage(); /** * 检查当前飞行物体是否处于存活状态 */ public boolean isLife() { return state == LIFE; } /** * 判断飞行物是否已经死亡。 */ public boolean isDead(){ return state==DEAD; } /** * 检查对象是否已经被移除(删除)。 */ public boolean isRemove(){ return state == REMOVE; } /** * 画出飞行物 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测物体是否超出边界。*/ public abstract boolean outOfBounds(); /** * 判断两个对象(敌人与子弹或英雄机)之间是否有碰撞 */ public boolean hit(FlyingObject other){ int enemyX1 = this.x - other.width; int enemyX2 = this.x + this.width; int enemyY1 = this.y - other.height; int enemyY2 = this.y + this.height; return (other.x >= enemyX1 && other.x <= enemyX2) && (other.y >= enemyY1 && other.y <= enemyY2); } /** * 将对象的状态设置为已死亡。 */ public void goDead(){ state = DEAD; } } ```

全部评论 (0)

还没有任何评论哟~
客服
客服
  • Java Bug
    优质
    本项目为一个基于Java编程语言开发的“飞机大战”游戏,经过全面测试确保代码中没有明显错误。适合初学者学习和参考。 ```java public abstract class FlyingObject { public static final int LIFE = 0; // 表示对象存活状态 public static final int DEAD = 1; // 表示对象死亡状态(先爆破) public static final int REMOVE = 2; // 表示对象被移除(爆破后删除) protected int state = LIFE; // 当前的状态,默认为生存 protected int width; // 宽度 protected int height; // 高度 protected int x; // X坐标位置 protected int y; // Y坐标位置 public FlyingObject() {} /** * 构造方法,适用于小敌机、大敌机和蜜蜂等对象。 */ public FlyingObject(int width, int height) { this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH - width); // X坐标在0到(窗口宽度-物体宽度)之间的随机值 y = -height; // Y坐标的初始位置为负高度,表示从屏幕外开始移动 } /** * 构造方法,适用于英雄机、子弹和天空等对象。 */ public FlyingObject(int width, int height, int x, int y) { this.width = width; this.height = height; this.x = x; this.y = y; } /** * 加载图片资源 */ public static BufferedImage loadImage(String fileName){ try{ return ImageIO.read(FlyingObject.class.getResource(fileName)); }catch(Exception e) { e.printStackTrace(); throw new RuntimeException(); } } /** * 抽象方法,定义飞行物的移动逻辑。 */ public abstract void step(); /** * 获取对象对应的图片资源 */ public abstract BufferedImage getImage(); /** * 检查当前飞行物体是否处于存活状态 */ public boolean isLife() { return state == LIFE; } /** * 判断飞行物是否已经死亡。 */ public boolean isDead(){ return state==DEAD; } /** * 检查对象是否已经被移除(删除)。 */ public boolean isRemove(){ return state == REMOVE; } /** * 画出飞行物 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测物体是否超出边界。*/ public abstract boolean outOfBounds(); /** * 判断两个对象(敌人与子弹或英雄机)之间是否有碰撞 */ public boolean hit(FlyingObject other){ int enemyX1 = this.x - other.width; int enemyX2 = this.x + this.width; int enemyY1 = this.y - other.height; int enemyY2 = this.y + this.height; return (other.x >= enemyX1 && other.x <= enemyX2) && (other.y >= enemyY1 && other.y <= enemyY2); } /** * 将对象的状态设置为已死亡。 */ public void goDead(){ state = DEAD; } } ```
  • Python
    优质
    《Python小项目:飞机大战》是一款使用Python编程语言开发的经典射击游戏。玩家控制一架战机,在无尽的天空中击毁敌机,躲避障碍物,提高分数,适合学习Python和游戏开发的新手练习。 记得刚开始学习Python的时候,我做过一个飞机大战的小项目。这个项目非常经典,可以帮助初学者提高动手能力。今天我想把这个项目分享出来,并重新编写一下这段文字。
  • 游戏可运行.zip
    优质
    这是一个包含完整“飞机大战”游戏源代码的压缩包,已经过测试确保可以正常运行。适合学习和研究使用。 这几天闲着没事用HTML+Canvas编写了一个简单的“飞机大战”游戏。游戏中飞机掉落速度逐渐加快,结束后会显示分数,适合用来进行分数比拼。如果有其他功能需求可以继续开发,这款游戏比较适合学习使用。
  • Python版
    优质
    本项目为使用Python语言开发的经典飞机大战游戏的完整源代码,适合编程爱好者学习和研究游戏开发。 飞机大战游戏以太空为主题背景,玩家通过键盘操控英雄飞机向敌机总部发起攻击。在进攻过程中,玩家可以发射子弹或炮弹击毁敌机获得分数,并且可以通过拾取道具增强战斗力。如果英雄飞机被敌机撞毁,则游戏结束。
  • Unity实游戏《
    优质
    本教程通过开发经典游戏《飞机大战》,深入讲解Unity引擎的基础知识与高级技巧,适合初学者快速上手并掌握核心技能。 这是我个人学习期间使用Unity制作的一个小型项目——飞机大战。该项目的素材资源主要来源于Unity官方提供的资源。
  • C++ MFC游戏
    优质
    本项目是一款使用C++和MFC框架开发的“飞机大战”小游戏。游戏以经典的空战为主题,玩家需操作战机躲避障碍、射击敌机,挑战高分记录。 C++小游戏:飞机大战 这是一款用C++编写的经典游戏——飞机大战。玩家可以控制自己的战机,在游戏中躲避敌机的攻击并摧毁敌人以取得胜利。该游戏具有精美的画面、流畅的操作体验以及丰富的关卡设计,适合各年龄段的游戏爱好者尝试挑战。 开发这款游戏不仅可以帮助学习者掌握基础编程技能如变量使用、条件判断等概念,还能进一步了解面向对象程序设计的相关知识,并在实践中提高解决问题的能力和创造力。希望对C++初学者或游戏开发者有所帮助与启发。
  • Java游戏ZIP+
    优质
    这是一款基于Java语言开发的经典“飞机大战”游戏,提供完整的游戏代码和资源文件。适合编程学习者研究与实践使用。 飞机大战游戏的源码适合初学者学习使用。
  • Java
    优质
    《Java版飞机大战源码》是一款使用Java语言编写的经典飞行射击游戏代码,适合编程爱好者学习和研究。通过这款游戏源码的学习,玩家可以深入了解游戏开发的基本原理和技术实现方法。 package cn.feike.shoot; import java.awt.Graphics; import java.awt.image.BufferedImage; public abstract class FlyingObject { protected double x; // 物体的x坐标 protected double y; // 物体的y坐标 protected double width; // 物体的宽 protected double heigth; // 物体的高度 protected BufferedImage image; // 当前正在显示的图片 protected int index = 0; // 图片数组下标序号,子类中使用 protected double step; // 飞行物每次(1/24秒)移动的距离 protected int life; // 命 protected int state; // 飞行情状 public static final int ACTIVE = 0; // 活着状态 public static final int DEAD = 1; // 死亡状态 public static final int REMOVE = 2; // 回收状态 public FlyingObject() { life = 1; state = ACTIVE; } public FlyingObject(double width, double heigth) { this(); // 调用无参数的构造器,必须写在第一行 this.x = (int)(Math.random() * (480 - width)); this.y = -heigth; this.width = width; this.heigth = heigth; step = Math.random() * 3 + 0.8; // 初始化step为[0.8,3.8)之间的数 } public String toString() { return x + , + y + , + width + , + heigth + , + image; } public void paint(Graphics g) { g.drawImage(image, (int)x, (int)y, null); // 绘制图片 } public void move(){ if(state == ACTIVE){ y += step; return ; } else if(state == DEAD){ BufferedImage img = nextImage(); if(img == null) state = REMOVE; else image = img; if(y >= 825) state = REMOVE; } } protected abstract BufferedImage nextImage(); public void hit(){ if(life > 0){ life--; } if(life==0) {state=DEAD;} } public boolean duang(FlyingObject obj){ double x1 = this.x - obj.width; double x2 = this.x + width; double y1 = this.y - obj.heigth; double y2 = this.y + heigth; return (x1 < obj.x && obj.x
  • Java
    优质
    Java版飞机大战源码是一款使用Java语言编写的经典射击游戏《飞机大战》的完整代码库。它不仅适合编程爱好者学习和研究,还为开发者提供了开发2D游戏所需的技巧与知识基础。 使用Java原生实现的飞机大战游戏,包含关卡设计,并提供源代码(jar包)及图片资源。该jar包可以直接运行。