溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java如何實(shí)現(xiàn)Flappy Bird游戲

發(fā)布時(shí)間:2021-04-15 11:49:47 來(lái)源:億速云 閱讀:198 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹Java如何實(shí)現(xiàn)Flappy Bird游戲,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體內(nèi)容如下

1.首先在mainActivity.xml中放置一個(gè)View,ID為viewDraw

2.開始編程,程序中自定義一個(gè)View類的子類,與viewDraw關(guān)聯(lián),程序除了放置如一個(gè)view控件,沒(méi)有其他控件,程序上面的所有圖片都是通過(guò)控制canvas畫圖實(shí)現(xiàn)

3.游戲是依據(jù)flappybird的游戲過(guò)程重新寫的算法,功能與原版游戲相似,可能有些地方不足,完全是學(xué)習(xí)練習(xí)編程而已,做的不好見(jiàn)諒

4.原圖片大小為384*512,在展示圖片時(shí)將圖片進(jìn)行了放大,盡可能滿足800*1280的全屏展示,如果你使用三星Note10,這個(gè)程序可以直接復(fù)制粘貼運(yùn)行,否則,可能會(huì)遇到圖片畫錯(cuò)位置的問(wèn)題,請(qǐng)適當(dāng)調(diào)整

5.程序游戲中使用到的圖片最后,只需要按照?qǐng)D片上的名字命名該圖片,并導(dǎo)入到程序的圖片資源中,就能順利運(yùn)行本程序

代碼:

public class MainActivity extends Activity
{
 static final int IMG_WIDTH=384; //圖片的長(zhǎng)寬
 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子類,完成動(dòng)畫的刷新
 class MyView extends View implements Runnable
 {
 static final int SPEED=2;
 // 圖形當(dāng)前坐標(biāo)
 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;
 
 // 構(gòu)造方法
 public MyView(Context context, AttributeSet attrs)
 {
 super(context, attrs);
 // TODO Auto-generated constructor stub
 // 獲得焦點(diǎn)
 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();
 
 // 啟動(dòng)線程
 new Thread(this).start();
 }
 
 @Override
 public void run()
 {
 // TODO Auto-generated method stub
 
 while (true)
 {
 // 通過(guò)發(fā)送消息更新界面
 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);//設(shè)置字體大小
 }
 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;
 
 /***************計(jì)算高度************************/ 
 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;
 
 /***************柱子的移動(dòng)**********************/
 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;
 } 
 }
 
 /***************計(jì)算分?jǐn)?shù)************************/
 for(c=0;c<3;c++)
 {
 if(pillar[c].x==100)
  point++;
 }
 
 /****************地面移動(dòng)***********************/
 a-=SPEED;
 
 /****************判斷碰撞,+32判斷下側(cè)柱子,-170判斷上側(cè)柱子***********************/
 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)
 {
 /*******************顯示分?jǐn)?shù)**********************/
 Paint p1 = new Paint();
 p1.setAntiAlias(true);
 p1.setColor(Color.WHITE);
 p1.setTextSize(20);//設(shè)置字體大小
 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);//設(shè)置字體大小
 canvas.drawText(""+point, 260, 210, p1);
 //最高紀(jì)錄
 canvas.drawText("1000", 260, 268, p1);
 
 }
 }
 }
 } 
 
 // 實(shí)例化畫筆
 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,如果不需要對(duì)圖片進(jìn)行縮放,可以在只而立只保留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);
 }
 }
 
 // 更新坐標(biāo)
 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如何實(shí)現(xiàn)Flappy Bird游戲

background_ground.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

background_night.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

bird_down.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

bird_middle.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

bird_up.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

intro_day_down.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

intro_day_middle.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

intro_day_up.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

intro_night_down.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

intro_night_middle.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

intro_night_up.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

medals_bronze.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

medals_gold.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

medals_silver.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

medals_whitegold.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

other_gameover.png

Java如何實(shí)現(xiàn)Flappy Bird游戲 

other_list.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

other_load.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

other_record.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

 other_restart.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

 other_start.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

pillar_downside.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

pillar_upside.png

Java如何實(shí)現(xiàn)Flappy Bird游戲

以上是“Java如何實(shí)現(xiàn)Flappy Bird游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI