java实现flappy Bird小游戏

小编 2026-07-04 阅读:355 评论:0
本文实例为大家分享了java实现flappy Bird游戏的具体代码,供大家参考,具体内容如下 整个游戏由3个类构成。Bird类,Pipe类,stage类 第一步:首先写一个Bird类 //鸟类...

本文实例为大家分享了java实现flappy Bird游戏的具体代码,供大家参考,具体内容如下

整个游戏由3个类构成。Bird类,Pipe类,stage类

第一步:首先写一个Bird类

//鸟类
public class Bird {
 private int flyHeight;//飞行高度
 private int xpos;//距离y轴(窗口左边缘)的位置,
 public static int Up=1;//向上飞
 public static int Down=-1;//向下飞
 public Bird()
 {
 flyHeight=200;
 xpos=30;
 }
 public void fly(int direction)
 {
 if(direction==Bird.Up)
 flyHeight-=20;//每次向上飞20m
 if(direction==Bird.Down)
 flyHeight+=20;//每次向下飞20m
 }
 public int getFlyHeight()//获得当前飞行高度
 {
 return flyHeight;
 }
 public int getXpos()//获得当前鸟的水平位置
 {
 return xpos;
 }
 public Boolean hit(Pipe pipe[])//检测是否碰到管道。只有在鸟经过管道的过程才有可能相撞
 {
 for(int i=0;i<pipe.length;i++)//遍历管道进行检测,是否相撞
 {
 if(getXpos()+20>=pipe[i].getXpos()&&getXpos()<=pipe[i].getXpos()+20)//鸟经过管道
 if(flyHeight<pipe[i].getUpHeight()||flyHeight>pipe[i].getDownHeight())//鸟与管道相撞
  return true;
 }
 return false;
 }
}

第二步:写一个Pipe类,Pipe类 里有3个成员,upHeight表示顶管道端的高度,downHeight表示底端管道段的高度,同样要记录管道的水平位置。

public class Pipe {
 private int upHeight;//表示顶管道端的高度
 private int downHeight;//表示底端管道段的高度
 private int xpos;
 public Pipe()
 {
 upHeight=0;
 downHeight=0;
 xpos=0;
 }
 public Pipe(int maxHeight,int xpos)//给管道一个最大总长度(maxHeight)=upHeight+downHeight。还有管道的水平位置
 {
 double num;
 num=Math.random();
 while(num==0)
 num=Math.random();
 upHeight=(int) (maxHeight*num);//随机产生一个顶端管道段高度(<maxHeight)
 downHeight=maxHeight-upHeight;//用总长度减去upHeight
 this.xpos=xpos;
 }
 public void setXpos(int xpos)
 {
 this.xpos=xpos;
 }
 public int getXpos()
 {
 return xpos;
 }
 public int getUpHeight()
 {
 return upHeight;
 }
 public int getDownHeight()
 {
 return downHeight;
 }
 public String toSting()
 {
 return \"(\"+upHeight+\",\"+downHeight+\")\";
 }
}

最后一步,写好舞台类,即操作游戏的类

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.Array;
import java.util.Timer;
import java.util.TimerTask;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class stage extends JPanel{
 private Pipe pipe[];//管道数组
 private Bird bird;//鸟
 private int space;//上下管道之间的间隔
 public JLabel scoreBoard;//计分面板
 private int score;//计分
 public stage()
 {
 space=150;//<span style=\"font-family: Arial, Helvetica, sans-serif;\">上下管道之间的间隔为150</span>
 score=0;
 scoreBoard=new JLabel(\"得分:\"+score);
 pipe=new Pipe[5];//总共5跟根
 for(int i=0;i<pipe.length;i++)
 {
 pipe[i]=new Pipe(400-space,i*130+110);//柱子每隔110m放一根
 //System.out.println(pipe[i].toSting());
 }
 bird=new Bird();
 }
 public void play()
 {
 Timer timer=new Timer();//定时任务,即使不操作也能动
 timer.schedule(new TimerTask()
 {
  public void run()
  {
  
  if(bird.hit(pipe))//碰到,重置所有数据成员
  {
  //System.out.println(\"碰到了\");
  score=0;
  scoreBoard.setText(\"得分:\"+score);
  pipe=new Pipe[10];
  for(int x=0;x<pipe.length;x++)
  pipe[x]=new Pipe(400-space,x*130+110);
  bird=new Bird();
  }
  else{//没碰到
  //System.out.println(\"没碰到\");
  bird.fly(Bird.Down);//鸟默认向下飞
  for(int x=0;x<pipe.length;x++)//管道每次往前移动10m,造成鸟向右移动的效果
  {
   pipe[x].setXpos(pipe[x].getXpos()-10);
  }
  score=score+10;
  scoreBoard.setText(\"得分:\"+score);
  }
  repaint();
  }
 }, 0, 1000);//在不操作的情况下,每一秒飞一次
 this.requestFocus();//获取”输入“焦点
 this.addKeyListener(new KeyAdapter() {//加入键盘时间
 public void keyPressed(KeyEvent e)
 {
 if(e.getKeyCode()==38)
 <span style=\"white-space:pre\"> </span>action(Bird.Up);
 else if(e.getKeyCode()==40)
 action(Bird.Down);
 });
 }
 public void action(int direction)//按下上下方向键后执行的函数
 {
 
 if(bird.hit(pipe))
 {
 //System.out.println(\"碰到了\");
 score=0;
 scoreBoard.setText(\"得分:\"+score);
 pipe=new Pipe[10];
 for(int x=0;x<pipe.length;x++)
 pipe[x]=new Pipe(400-space,x*130+110);
 bird=new Bird();
 }
 else{
 //System.out.println(\"没碰到\");
 if(direction==Bird.Up)
 {
 bird.fly(Bird.Up);
 }
 else if(direction==Bird.Down)
 {
 bird.fly(Bird.Down);
 }
 for(int x=0;x<pipe.length;x++)//管道每次往前移动10m,造成鸟向右移动的效果
 {
 pipe[x].setXpos(pipe[x].getXpos()-10);
 }
 score=score+10;
 scoreBoard.setText(\"得分:\"+score);
 }
 repaint();
 }
 public void paint(Graphics g)
 {
 g.setColor(g.getColor());
 g.fillRect(0, 0, getWidth(), getHeight());//用默认颜色清屏
 g.setColor(Color.red);
 g.fill3DRect(bird.getXpos(), bird.getFlyHeight(), 20, 20, true);//红色画鸟
 g.setColor(Color.green);
 for(int i=0;i<pipe.length;i++)//绿色画管道
 {
 g.fill3DRect(pipe[i].getXpos(), 0, 20, pipe[i].getUpHeight(), true);
 g.fill3DRect(pipe[i].getXpos(),pipe[i].getUpHeight()+space, 20, pipe[i].getDownHeight(), true);
 }
 if(pipe[0].getXpos()+20<=0)//如果第一根管道出界,就删掉第一根管道,把后面的往前挪,再新创建一根管道
 {
 for(int i=1;i<pipe.length;i++)
  pipe[i-1]=pipe[i];
 pipe[pipe.length-1]=new Pipe(400-space,(pipe.length-1)*130+110);
 //System.out.println(pipe[pipe.length-1].toSting());
 }
 }
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 JFrame jf=new JFrame(\"flappyBird\");
 stage app=new stage();
 jf.setLayout(null);
 app.setBounds(0,20,600,400);
 app.scoreBoard.setBounds(0, 0, 100, 20);
 jf.add(app);
 jf.add(app.scoreBoard);
 jf.setSize(610, 460);
 jf.setVisible(true);
 app.play();//游戏开始
 }
 
}

程序截图:

\"\"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表