程序员教你用代码制作飞翔的小鸟--Java小游戏,正好拿去和给女神一起玩

简介: 《飞扬的小鸟》Java实现摘要:使用IntelliJ IDEA和JDK 16开发,包含小鸟类`Bird`,处理小鸟的位置、速度和碰撞检测。代码示例展示小鸟图像的加载、绘制与旋转。同时有`Music`类用于循环播放背景音乐。游戏运行时检查小鸟是否撞到地面、柱子或星星,并实现翅膀煽动效果。简单易懂,可直接复制使用。

先点赞后观看,养成好习惯

一、写在前面:

《飞扬的小鸟》是一款曾经比较火热的小游戏

语言
Java工具

IntelliJ IDEA,JDK 16

二效果图:

代码部分

代码如下仅供参考
可以直接拿去复制粘贴

public class Bird {
private int x;// 横坐标
private int y;// 纵坐标

private BufferedImage bird;

private BufferedImage[] birds;
private int index;

private int g;// 重力加速度
private double t;// 计算时间间隔
private double v0;// 初速度(像素/秒)
private double vt;// 当前速度
private double s;// 运动距离
private double angle;// 小鸟飞行角度

private int size=26;

private World world;

public Bird(int x, int y) throws IOException {

  bird = ImageIO.read(this.getClass().getResource("0.png"));

  this.x = x;
  this.y = y;
  birds = new BufferedImage[3];
  for (int i = 0; i < 3; i++) {
     birds[i] = ImageIO.read(this.getClass().getResource(i + ".png"));
  }

  bird = birds[0];
  this.g = 4;//重力加速度
  this.t = 0.25;//每次计算的间隔时间
  this.v0 = 20;//初始上抛速度

}

public void paint(Graphics g) {
// g.drawImage(bird, x, y, null);
Graphics2D g2d = (Graphics2D)g;
//绘制旋转坐标系
g2d.rotate(angle,this.x,this.y);
//小鸟的旋转中心
int x = this.x - bird.getWidth()/2;
int y = this.y - bird.getHeight()/2;
g.drawImage(bird, x, y, null);
//旋转回来
g2d.rotate(-angle, this.x, this.y);

}

public void step() {

  /**
   * 竖直方向上的位移计算 
   * (1)上抛距离计算:s = V0t - 1/2gt^2 
   * (2)上抛初速度计算:Vt = V0 - gt
   */

  //vt1记录本次速度
  double vt1 = vt;
  //计算垂直运动之后,经过t时间之后的速度
  double v = vt1 - g*t;
  //记录并作为下一次计算的初速度
  vt = v;
  //计算垂直距离下的实际运行距离
  s = vt1 * t - 0.5 * g * g * t;
  y = y - (int)s;

  angle = -Math.atan(s/15);


  // 煽动翅膀的算法

// if (index > 2) {
// index = 0;
// }
// bird = birds[index++];

  index++;
  bird = birds[index / 8 % 3];//

}

public void flappy() {
vt = v0;
}

public boolean hit(Ground ground,Column col1,Column col2) {
if(y-size/2 >= ground.getY() || y-size/2 <=0) {
return true;
}

  return hit(col1) || hit(col2);

}

public boolean hit(Column column) {
if(x+size/2 > column.getX()-column.getWidth()/2 && x-size/2 < column.getX() + column.getWidth()/2) {
if(y > column.getY() - column.getGap()/2 + size/2 && y < column.getY() + column.getGap()/2 - size/2) {
return false;
}
return true;
}
return false;
}

public boolean hitstart(Star star) {
// if(x+size/2 > star.getX() && x+size/2 < star.getX()+star.getWidth()) {
// if(y+size/2 > star.getY() && y+size/2 < star.getY()+star.getHeight()) {
//
// System.out.print("star.getX():" + star.getX() + " ");
// System.out.print("star.getY():" + star.getY() + " ");
//
// return true;
// }
// return false;
// }
// return false;

  if(x+size/2 > star.getX() && x+size/2 < star.getX()+star.getWidth()) {
     if(y+size/2 > star.getY() && y-size/2 < star.getY()+star.getHeight()) {

        return true;
     }
     return false;
  }
  return false;

}

public boolean pass(Column col1, Column col2) {
return col1.getX() == x || col2.getX() == x;
}

public boolean pass1(Column col1, Column col2) {
return col1.getX() == 2x || col2.getX() == 2x;
}

public boolean passstar(Star star) {
// star.setY(-30);
return star.getX() == x ;

}

public boolean passstar1(Star star) {
// star.setY(-30);
return star.getX() == 2*x ;
}

public class Music extends Thread {
private String fileName;
private final int EXTERNAL_BUFFER_SIZE = 524288;

public Music(String wavFile) {
    this.fileName = wavFile;
}

@SuppressWarnings("unused")
public void run() {
    File soundFile = new File("D:\\Bird\\src\\src\\com\\icss\\bird\\稻香.wav"); // 播放音乐的文件名
    if (!soundFile.exists()) {
        System.err.println("Wave file not found:" + fileName);
        return;
    }
    while (true) { // 设置循环播放
        AudioInputStream audioInputStream = null; // 创建音频输入流对象
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile); // 创建音频对象
        } catch (UnsupportedAudioFileException e1) {
            e1.printStackTrace();
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }
        AudioFormat format = audioInputStream.getFormat(); // 音频格式
        SourceDataLine auline = null; // 源数据线
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (auline.isControlSupported(FloatControl.Type.PAN)) {
            FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
        }
        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0)
                    auline.write(abData, 0, nBytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();

// auline.close();
}
}
}
}

运行步骤:导入到idea或者eclipse点击运行即可运行成功了快快和爱慕女孩一起玩,说不定就能收获到爱情啦。(如果有不想动手的小伙伴可以私聊博主获取哦!!!!)

相关文章
|
4月前
|
Java 开发工具
【Azure Storage Account】Java Code访问Storage Account File Share的上传和下载代码示例
本文介绍如何使用Java通过azure-storage-file-share SDK实现Azure文件共享的上传下载。包含依赖引入、客户端创建及完整示例代码,助你快速集成Azure File Share功能。
439 5
|
4月前
|
Java 数据处理 API
为什么你的Java代码应该多用Stream?从循环到声明式的思维转变
为什么你的Java代码应该多用Stream?从循环到声明式的思维转变
311 115
|
4月前
|
安全 Java 编译器
为什么你的Java代码需要泛型?类型安全的艺术
为什么你的Java代码需要泛型?类型安全的艺术
227 98
|
4月前
|
Java 编译器 API
java最新版和java8的区别,用代码展示
java最新版和java8的区别,用代码展示
415 43
|
4月前
|
安全 Java 容器
告别空指针噩梦:Optional让Java代码更优雅
告别空指针噩梦:Optional让Java代码更优雅
450 94
|
4月前
|
安全 Java 容器
告别繁琐判空:Optional让你的Java代码更优雅
告别繁琐判空:Optional让你的Java代码更优雅
|
5月前
|
IDE Java 关系型数据库
Java 初学者学习路线(含代码示例)
本教程为Java初学者设计,涵盖基础语法、面向对象、集合、异常处理、文件操作、多线程、JDBC、Servlet及MyBatis等内容,每阶段配核心代码示例,强调动手实践,助你循序渐进掌握Java编程。
733 3
|
4月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
265 1
|
4月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
280 1
|
5月前
|
数据采集 存储 弹性计算
高并发Java爬虫的瓶颈分析与动态线程优化方案
高并发Java爬虫的瓶颈分析与动态线程优化方案