자바 Flappy Bird 게임 소스 구현
1.먼저 mainActivity.xml 에 View 를 설치 하고 ID 는 view Draw 입 니 다.
2.프로 그래 밍 을 시작 합 니 다.프로그램 에서 View 류 의 하위 클래스 를 사용자 정의 합 니 다.view Draw 와 연결 되 어 있 습 니 다.프로그램 은 하나의 view 컨트롤 을 제외 하고 다른 컨트롤 이 없습니다.프로그램 위의 모든 그림 은 canvas 그림 을 제어 하여 이 루어 집 니 다.
3.게임 은 flappybird 의 게임 과정 에 근거 하여 재 작성 한 알고리즘 으로,원본 게임 과 기능 이 비슷 하 며,일부 부족 한 부분 이 있 을 수 있 으 며,완전히 연습 프로 그래 밍 을 배 웠 을 뿐,양 해 를 구하 기 어렵다
4.원본 그림 의 크기 는 384*512 이 고 그림 을 보 여줄 때 그림 을 확대 하여 가능 한 800*1280 의 전체 화면 전 시 를 만족 시 켰 습 니 다.만약 에 삼 성 노트 10 을 사용 하면 이 프로그램 은 직접 복사 하여 붙 여 넣 기 를 실행 할 수 있 습 니 다.그렇지 않 으 면 그림 의 잘못된 위치 에 문제 가 생 길 수 있 으 니 적당 하 게 조정 하 십시오.
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
background_ground.png
background_night.png
bird_down.png
bird_middle.png
bird_up.png
intro_day_down.png
intro_day_middle.png
intro_day_up.png
intro_night_down.png
intro_night_middle.png
intro_night_up.png
medals_bronze.png
medals_gold.png
medals_silver.png
medals_whitegold.png
other_gameover.png
other_list.png
other_load.png
other_record.png
other_restart.png
other_start.png
pillar_downside.png
pillar_upside.png
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.