Flappy Bird java 源代码.docx_第1页
Flappy Bird java 源代码.docx_第2页
Flappy Bird java 源代码.docx_第3页
Flappy Bird java 源代码.docx_第4页
Flappy Bird java 源代码.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package mcnk;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;import sun.audio.AudioPlayer;import sun.audio.AudioStream;/* * FlappyBird * author mcnk * version 1.0 */public class FlappyBird public static void main(String args) throws Exception JFrame frame = new JFrame(我是一只小小小小鸟!);World world = new World();frame.add(world);frame.setSize(432,644+30);frame.setVisible(true);/快捷键: 代码快速提示 alt + /frame.setResizable(false);/是否重设大小frame.setLocationRelativeTo(null);/设置位置frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/关闭操作world.action();/* * 游戏界面(鸟活动的世界) * author mcnk * 快捷键: alt + () 移动代码 * JPanel: javax.swing.JPanel 面板 * 快捷键: ctrl + shift + o 导入(引入)包 */class World extends JPanelBufferedImage background;/图像缓存区,保存图片Ground ground;Column column1;Column column2;Bird bird;boolean gameover;BufferedImage gameoverImage;BufferedImage startedImage;int score;boolean start;public World() throws IOExceptionbackground = ImageIO.read(getClass().getResource(bg.png);gameoverImage = ImageIO.read(getClass().getResource(gameover.png);startedImage = ImageIO.read(getClass().getResource(start.png);start();public void start() throws IOExceptionground = new Ground();column1 = new Column(1);column2 = new Column(2);bird = new Bird();gameover = false;score = 0;start = false;public void paint(Graphics g)g.drawImage(background,0,0,null);g.drawImage(column1.image,column1.x - column1.width/2, column1.y - column1.height/2,null);g.drawImage(column2.image,column2.x - column2.width/2, column2.y - column2.height/2,null);g.drawImage(ground.image,ground.x,ground.y,null);g.setColor(Color.WHITE);Font font = new Font(Font.SANS_SERIF,Font.BOLD,30);g.setFont(font);g.drawString(score+,20,40);Graphics2D g2d = (Graphics2D)g;g2d.rotate(-bird.alpha, bird.x, bird.y);/旋转坐标系g.drawImage(bird.image,bird.x - bird.width/2, bird.y - bird.height/2, null);g2d.rotate(bird.alpha, bird.x, bird.y);if(gameover)/绘制结束背景g.drawImage(gameoverImage,0,0,null);if(!start)/绘制开始背景g.drawImage(startedImage,0,0,null);public void action() throws Exception/定义鼠标监听器(匿名内部类)MouseListener l = new MouseAdapter()public void mousePressed(MouseEvent e) if(gameover)try start(); catch (IOException e1) e1.printStackTrace();elsestart = true;bird.flappy();addMouseListener(l);/绑定监听器/添加背景音乐new Thread()public void run()AudioStream as;try as = new AudioStream(getClass().getResourceAsStream(music.mid);AudioPlayer.player.start(as); catch (IOException e) e.printStackTrace();.start();while(true)if(!gameover)if(start)column1.step();column2.step();bird.step();ground.step();bird.fly();if(bird.x = column1.x | bird.x = column2.x)score+;if(bird.hit(ground) | bird.hit(column1) | bird.hit(column2)gameover = true;repaint();/尽快重新调用paint()绘图Thread.sleep(1000/100);/* * 地面 * author mcnk * */class GroundBufferedImage image;int x;int y;public Ground() throws IOExceptionimage = ImageIO.read(getClass().getResource(ground.png);x = 0; y = 500;/地面移动public void step()x-;if(x = -108)x = 0;/* * 柱子 * author mcnk * */class ColumnBufferedImage image;int x; /中心点X坐标int y; /中心点Y坐标int width;int height;int gap; /柱子缝隙int distance;/柱子与柱子之间间距Random random = new Random();public Column(int num) throws IOExceptionimage = ImageIO.read(getClass().getResource(column.png);width = image.getWidth();height = image.getHeight();gap = 144;distance = 245;x = (num -1) * distance + (432+width/2);y = random.nextInt(250) + 132;/132, 132+279public void step()x-;if(x = -width/2)x = 2 * distance - width/2;y = random.nextInt(250) + 132;/* * 小鸟 * author mcnk * */class BirdBufferedImage images;/保存鸟的所有活动状态BufferedImage image;/保存当前鸟的状态int x;int y;int width;int height;int size;double g;/重力加速度double h;/经过时间t秒后的垂直位移double s;/经过时间t秒后的水平位移double t;/时间double speed;/速度(初始速度:V0/经过时间t秒后的速度:Vt)double alpha;/倾角int index;/当前图片的下标public Bird() throws IOExceptionimages = new BufferedImage8;for(int i=0; i images.lengthimagesi = ImageIO.read(getClass().getResource(i+.png);image = images0;width = image.getWidth();height = image.getHeight();x = 132;y = 280;size = 40;g = 4;h = 0;s = 0;speed = 20;t = 0.25;alpha = 0;index = 0;public void step()double v0 = speed;h = v0*t - 0.5*g*t*t;double vt = v0 - g*t;s = v0 * t;y = y - (int)h;speed = vt;alpha = Math.atan(h/8);/鸟翅膀挥动public void fly()index+;image = images(index/8)%8;/0 1 2 3 4 5 6 7 8 9 10 11 12./8/0 0 0 0 0 0 0 0 1 1 1 1 1 ./%8/0 0 0 0 0 0 0 0 1 1 1 1 1 public void flappy()speed = 20;/鸟撞击地面检查public boolean hit(Ground ground)boolean hit = (y + size/2 = ground.y);if(hit)alpha = -Math.PI/2;return hit;/鸟撞击到柱子的检查public boolean hit(Column co

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论