先点赞后观看,养成好习惯
一、写在前面:
《飞扬的小鸟》是一款曾经比较火热的小游戏
语言
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() == 2*x || col2.getX() == 2*x; } 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点击运行即可运行成功了快快和爱慕女孩一起玩,说不定就能收获到爱情啦。(如果有不想动手的小伙伴可以私聊博主获取哦!!!!)
获取途径:私聊博主获取
下载源码地址:程序员教你用代码制作飞翔的小鸟-Java小游戏,正好拿去和给女神一起玩-Java文档类资源-CSDN下载