欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现Flappy Bird游戏源码

程序员文章站 2024-03-02 20:49:34
本文实例为大家分享了java实现flappy bird游戏的具体代码,供大家参考,具体内容如下 1.首先在mainactivity.xml中放置一个view,id为v...

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

1.首先在mainactivity.xml中放置一个view,id为viewdraw

2.开始编程,程序中自定义一个view类的子类,与viewdraw关联,程序除了放置如一个view控件,没有其他控件,程序上面的所有图片都是通过控制canvas画图实现

3.游戏是依据flappybird的游戏过程重新写的算法,功能与原版游戏相似,可能有些地方不足,完全是学习练习编程而已,做的不好见谅

4.原图片大小为384*512,在展示图片时将图片进行了放大,尽可能满足800*1280的全屏展示,如果你使用三星note10,这个程序可以直接复制粘贴运行,否则,可能会遇到图片画错位置的问题,请适当调整

5.程序游戏中使用到的图片最后,只需要按照图片上的名字命名该图片,并导入到程序的图片资源中,就能顺利运行本程序

代码:

public class mainactivity extends activity
{
 static final int img_width=384; //图片的长宽
 static final int img_height=512;
 private view viewdraw;
 
 @override
 protected void oncreate(bundle savedinstancestate)
 {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 viewdraw = (view) findviewbyid(r.id.viewdraw);
 myview v = new myview(viewdraw.getcontext(), null);
 
 setcontentview(v);
 }
 
 @override
 public boolean onkeydown(int keycode, keyevent event)
 {
 if (keycode == keyevent.keycode_back)
 {
 finish();
 return true;
 }
 return super.onkeydown(keycode, event);
 }
 //定义水管障碍物类
 class obstacle
 {
 int x;
 int h;
 public obstacle()
 {
 x=0;
 h=0;
 }
 }
 //自定义view子类,完成动画的刷新
 class myview extends view implements runnable
 {
 static final int speed=2;
 // 图形当前坐标
 private int x = 20, y = 20,a,b,c,birdheight,time,point,v,record;
 obstacle pillar[]= new obstacle[3];
 
 boolean binit,bcourse,bgame,bgameover;
 
 refreshhandler mredrawhandler;
 
 bitmap birdupbitmap,birddownbitmap,birdmiddlebitmap,groundbitmap,otherstartbitmap;
 bitmap courseupbitmap,coursedownbitmap,coursemiddlebitmap;
 bitmap pillarupbitmap,pillardownbitmap,gameoverbitmap,recordbitmap,playagainbitmap,listbitmap,goldbitmap,silverbitmap,bronzebitmap,whitegoldbitmap,gamebackgroundbitmap;
 
 // 构造方法
 public myview(context context, attributeset attrs)
 {
 super(context, attrs);
 // todo auto-generated constructor stub
 // 获得焦点
 setfocusable(true);
 binit=false;
 bcourse=false;
 bgame=false;
 bgameover=false;
 //障碍物初始化 
 pillar[0]=new obstacle();
 pillar[1]=new obstacle();
 pillar[2]=new obstacle();
 //载入图片 
 otherstartbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_start);
 birdupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.bird_up);
 birddownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.bird_down);
 birdmiddlebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.bird_middle);
 groundbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.background_ground); 
 pillardownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.pillar_downside);
 pillarupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.pillar_upside);
 gameoverbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_gameover);
 recordbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_record);
 playagainbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_restart);
 listbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_list);
 bronzebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_bronze);
 silverbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_silver);
 goldbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_gold);
 whitegoldbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_whitegold);
 //游戏界面刷新handler 
 mredrawhandler = new refreshhandler();
 
 // 启动线程
 new thread(this).start();
 }
 
 @override
 public void run()
 {
 // todo auto-generated method stub
 
 while (true)
 {
 // 通过发送消息更新界面
 message m = new message();
 m.what = 0x101;
 mredrawhandler.sendmessage(m);
 try
 {
 thread.sleep(20);
 } catch (interruptedexception e)
 {
 // todo auto-generated catch block
 e.printstacktrace();
 }
 }
 }
 
 //游戏画面更新
 void gamedraw(canvas canvas)
 {
 if(!binit)
 {
 bitmap bm;
 bm = bitmapfactory.decoderesource(getresources(), r.drawable.other_load);
 canvas.drawbitmap(bm, 0, 0, null); 
 paint p1 = new paint();
 p1.setantialias(true);
 p1.setcolor(color.white);
 p1.settextsize(20);//设置字体大小
 }
 else if(binit)
 {
 if(!bcourse)
 {
 a-=speed;
 if(a<=0)
 a=384;
 
 canvas.drawbitmap(otherstartbitmap, 0, 0, null);
 canvas.drawbitmap(groundbitmap, a, 448, null);
 canvas.drawbitmap(groundbitmap, a-384, 448, null);
 
 b=a%128;
 if(b>=0&&b<32)
 {
 canvas.drawbitmap(birdmiddlebitmap, 175, 220, null);
 }
 if(b>=32&&b<64)
 {
 canvas.drawbitmap(birdupbitmap, 175, 216, null);
 }
 if(b>=64&&b<96)
 {
 canvas.drawbitmap(birdmiddlebitmap, 175, 220, null);
 } 
 if(b>=96&&b<=128)
 {
 canvas.drawbitmap(birddownbitmap, 175, 224, null);
 }
 canvas.drawbitmap(groundbitmap, a, 448, null);
 }
 else if (bcourse)
 {
 if(!bgame)
 {
 time += 1;
 
 int temp = time % 64;
 if (temp >= 0 && temp < 16)
 canvas.drawbitmap(courseupbitmap, 0, 0, null);
 if (temp >= 16 && temp < 32)
 canvas.drawbitmap(coursemiddlebitmap, 0, 0, null);
 if (temp >= 32 && temp < 48)
 canvas.drawbitmap(coursedownbitmap, 0, 0, null);
 if (temp >= 48 && temp < 64)
 canvas.drawbitmap(coursemiddlebitmap, 0, 0, null);
 a-=speed;
 if(a<=0)
 a=384;
 canvas.drawbitmap(groundbitmap, a, 448, null);
 canvas.drawbitmap(groundbitmap, a-384, 448, null);
 
 }
 else if(bgame)
 {
 if(!bgameover)
 {
 time+=1;
 
 /***************计算高度************************/ 
 v+=9.8;
 if(v>120)
 v=120;
 else if(v<-150)
 v=-150;
 if(v>=0)
 birdheight+=((v*5.0)/77);
 else if(v<0)
 birdheight+=((v*4.5)/77);
 if(birdheight<0)
 birdheight=0;
 else if(birdheight>415)
 birdheight=415;
 
 /***************柱子的移动**********************/
 for(c=0;c<3;c++)
 {
  pillar[c].x-=speed;
  if(pillar[c].x<=-70)
  pillar[c].x=650;
 if(pillar[c].x==512)
 {
  pillar[c].h=(new random()).nextint(200)+200;
 } 
 }
 
 /***************计算分数************************/
 for(c=0;c<3;c++)
 {
 if(pillar[c].x==100)
  point++;
 }
 
 /****************地面移动***********************/
 a-=speed;
 
 /****************判断碰撞,+32判断下侧柱子,-170判断上侧柱子***********************/
 if(pillar[0].x>=30&&pillar[0].x<=145&&(pillar[0].h<(birdheight+32)||(pillar[0].h-170)>birdheight))
  bgameover=true;
 if(pillar[1].x>=30&&pillar[1].x<=145&&(pillar[1].h<(birdheight+32)||(pillar[1].h-170)>birdheight))
  bgameover=true;
 if(pillar[2].x>=30&&pillar[2].x<=145&&(pillar[2].h<(birdheight+32)||(pillar[2].h-170)>birdheight))
 bgameover=true;
 //是否碰到地面
 if(birdheight>414)
 bgameover=true;
 }
 
 
 /***************显示图像************************/ 
 //显示背景 
 canvas.drawbitmap(gamebackgroundbitmap, 0, 0, null);
 
 //显示柱子
 for(c=0;c<3;c++)
 {
 canvas.drawbitmap(pillardownbitmap, pillar[c].x, pillar[c].h, null);
 canvas.drawbitmap(pillarupbitmap, pillar[c].x, pillar[c].h-420, null);
 }
 
 if(a<=0)
 a=384;
 canvas.drawbitmap(groundbitmap, a, 448, null);
 canvas.drawbitmap(groundbitmap, a-384, 448, null);
 
 
 int temp=time%16;
 if(temp>=0&&temp<4)
 canvas.drawbitmap(birdmiddlebitmap, 100, birdheight, null);
 if(temp>=4&&temp<8)
 canvas.drawbitmap(birdupbitmap, 100, birdheight, null);
 if(temp>=8&&temp<12)
 canvas.drawbitmap(birdmiddlebitmap, 100, birdheight, null);
 if(temp>=12&&temp<16)
 canvas.drawbitmap(birddownbitmap, 100, birdheight, null);
 
 
 if(!bgameover)
 {
 /*******************显示分数**********************/
 paint p1 = new paint();
 p1.setantialias(true);
 p1.setcolor(color.white);
 p1.settextsize(20);//设置字体大小
 canvas.drawtext("score:"+point, 171, 50, p1);
 canvas.drawtext("acc:"+v, 171, 80, p1);
 canvas.drawtext("h:"+birdheight, 171, 110, p1);
 }
 else if(bgameover)
 {
 canvas.drawbitmap(gameoverbitmap, 55, 60, null);
 canvas.drawbitmap(recordbitmap, 35, 150, null);
 canvas.drawbitmap(playagainbitmap, 35, 350, null);
 canvas.drawbitmap(listbitmap, 200, 350, null);
  
 if(point>=10&&point<20)
 canvas.drawbitmap(bronzebitmap, 67, 200, null);
 if(point>=20&&point<30)
 canvas.drawbitmap(silverbitmap, 67, 200, null);
 if(point>=30&&point<40)
 canvas.drawbitmap(goldbitmap, 67, 200, null);
 if(point>=40)
 canvas.drawbitmap(whitegoldbitmap, 67, 200, null);
 
 paint p1 = new paint();
 p1.setantialias(true);
 p1.setcolor(color.white);
 p1.settextsize(20);//设置字体大小
 canvas.drawtext(""+point, 260, 210, p1);
 //最高纪录
 canvas.drawtext("1000", 260, 268, p1);
 
 }
 }
 }
 } 
 
 // 实例化画笔
 paint p = new paint();
 p.setcolor(color.black);
 p.setcolor(color.white);
 canvas.drawtext("init: "+binit+" course: "+bcourse+" game: "+bgame+" over: "+bgameover, 0, 10, p);
 }
 
 @override
 protected void ondraw(canvas canvas)
 {
 // todo auto-generated method stub
 super.ondraw(canvas);
 canvas.drawcolor(color.black);
 //这里的操作是为了在我的平板电脑竖屏尽量全屏显示,原图片很小,我的屏幕分辨率为1280*800,如果不需要对图片进行缩放,可以在只而立只保留gamedraw()部分  
 canvas.save();
 canvas.translate(16, 80);
 float scale=(float) 2.0;
 canvas.scale(scale, scale);
 canvas.cliprect(0, 0, img_width, img_height);
 gamedraw(canvas);
 canvas.restore();
 }
 
 // 更新界面处理器
 class refreshhandler extends handler
 {
 @override
 public void handlemessage(message msg)
 {
 // todo auto-generated method stub
 if (msg.what == 0x101)
 {
 myview.this.update();
 myview.this.invalidate();
 }
 super.handlemessage(msg);
 }
 }
 
 // 更新坐标
 private void update()
 {
 }
 //触屏事件 
 @override
 public boolean ontouchevent(motionevent event)
 {
 switch (event.getpointercount())
 {
 case 1:
 return onsingletouchevent(event);
 case 2:
 return ondoubletouchevent(event);
 default:
 return false;
 }
 }
 //单手指触屏处理
 private boolean onsingletouchevent(motionevent event)
 {
 int x = (int) event.getx();
 int y = (int) event.gety();
 
 switch (event.getaction())
 {
 case motionevent.action_up:
 if(!binit)
 { 
 a=-2;
 binit=true; 
 }
 else if(binit)
 {
 if(!bcourse)
 {
 if((new random()).nextint(3)==0)
 {
 gamebackgroundbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.background_night);
 coursedownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_night_down);
 courseupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_night_up);
 coursemiddlebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_night_middle);
 }
 else
 {
 gamebackgroundbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.background_day);
 coursedownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_day_down);
 courseupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_day_up);
 coursemiddlebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_day_middle);
 } 
 time = 0;
 a=0;
 
 bcourse=true; 
 }
 else if(bcourse)
 {
 if(!bgame)
 {
 time=0;
 birdheight=150;
 point=0;
 pillar[0].x=700;
 pillar[1].x=940;
 pillar[2].x=1180;
 a=0;
 b=0;
 c=0;
 
 bgame=true;
 }
 else if(bgame)
 {
 if(!bgameover)
 v-=250;
 else if(bgameover)
 {
 bgameover=false;
 bcourse=false;
 bgame=false;
 }
 }
 }
 }
 break;
 case motionevent.action_down:
 
 break;
 case motionevent.action_move:
 
 break;
 }
 return true;
 }
 //双指触屏处理
 private boolean ondoubletouchevent(motionevent event)
 {
 switch (event.getaction() & motionevent.action_mask)
 {
 case motionevent.action_pointer_up:
 
 break;
 case motionevent.action_pointer_down:
 {
 
 break;
 }
 case motionevent.action_move:
 
 postinvalidate();
 break;
 }
 return true;
 }
 }
}

图片:

background_day.png

Java实现Flappy Bird游戏源码

background_ground.png

Java实现Flappy Bird游戏源码

background_night.png

Java实现Flappy Bird游戏源码

bird_down.png

Java实现Flappy Bird游戏源码

bird_middle.png

Java实现Flappy Bird游戏源码

bird_up.png

Java实现Flappy Bird游戏源码

intro_day_down.png

Java实现Flappy Bird游戏源码

intro_day_middle.png

Java实现Flappy Bird游戏源码

intro_day_up.png

Java实现Flappy Bird游戏源码

intro_night_down.png

Java实现Flappy Bird游戏源码

intro_night_middle.png

Java实现Flappy Bird游戏源码

intro_night_up.png

Java实现Flappy Bird游戏源码

medals_bronze.png

Java实现Flappy Bird游戏源码

medals_gold.png

Java实现Flappy Bird游戏源码

medals_silver.png

Java实现Flappy Bird游戏源码

medals_whitegold.png

Java实现Flappy Bird游戏源码

 

other_gameover.png

Java实现Flappy Bird游戏源码 

other_list.png

Java实现Flappy Bird游戏源码

other_load.png

Java实现Flappy Bird游戏源码

other_record.png

Java实现Flappy Bird游戏源码

 other_restart.png

Java实现Flappy Bird游戏源码

 other_start.png

Java实现Flappy Bird游戏源码

pillar_downside.png

Java实现Flappy Bird游戏源码

pillar_upside.png

Java实现Flappy Bird游戏源码

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