当前位置: 移动技术网 > IT编程>开发语言>Java > java实现flappy Bird小游戏

java实现flappy Bird小游戏

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了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();//游戏开始
 }
 
}

程序截图:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网