java实现flappy Bird小游戏

 更新时间:2018年12月24日 08:47:52   作者:Chenny丶  
这篇文章主要为大家详细介绍了java实现flappy 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();//游戏开始
 }
 
}

程序截图:

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

相关文章

  • Elasticsearch中FST与前缀搜索应用实战解析

    Elasticsearch中FST与前缀搜索应用实战解析

    这篇文章主要为大家介绍了Elasticsearch中FST与前缀搜索应用实战解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • java获取用户输入的字符串方法

    java获取用户输入的字符串方法

    今天小编就为大家分享一篇java获取用户输入的字符串方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • 如何解决redis的NOAUTH Authentication required异常

    如何解决redis的NOAUTH Authentication required异常

    这篇文章主要介绍了Jedis异常解决:NOAUTH Authentication required,,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
    2019-07-07
  • Java 切割字符串的几种方式集合

    Java 切割字符串的几种方式集合

    这篇文章主要介绍了Java 切割字符串的几种方式集合,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 使用JAXBContext 设置xml节点属性

    使用JAXBContext 设置xml节点属性

    这篇文章主要介绍了使用JAXBContext 设置xml节点属性的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 浅谈Java并发之同步器设计

    浅谈Java并发之同步器设计

    这篇文章主要介绍Java并发之同步器设计,本文以记录方式并发编程中同步器设计的一些共性特征。并简单介绍了Java中的AQS,需要的朋友可以参考一下文章的详细内容
    2021-10-10
  • 手把手教你排查解决Java编译报错:找不到符号

    手把手教你排查解决Java编译报错:找不到符号

    这篇文章主要介绍了手把手教你排查解决Java编译报错:找不到符号的相关资料,提供了排查步骤和解决方法,通过这些步骤,开发者可以有效地找到并解决编译器抛出的找不到符号错误,从而提高开发效率,需要的朋友可以参考下
    2025-04-04
  • Spring Boot 与 Vue.js 整合流程

    Spring Boot 与 Vue.js 整合流程

    本文重点介绍我在Spring Boot 与 Vue.js 整合实践过程中的基本流程,以及遇到的问题,感兴趣的朋友跟随小编一起看看吧
    2018-09-09
  • Spring注解配置AOP导致通知执行顺序紊乱解决方案

    Spring注解配置AOP导致通知执行顺序紊乱解决方案

    这篇文章主要介绍了Spring注解配置AOP导致通知执行顺序紊乱解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • spring boot中的properties参数配置详解

    spring boot中的properties参数配置详解

    这篇文章主要介绍了spring boot中的properties参数配置,需要的朋友可以参考下
    2017-09-09

最新评论