Home>

First analyze the elements in the game interface:

Infinite scroll background image, Operable protagonist,Protagonist's bullet, The hero's health,Explosion effect of two monsters (enemy aircraft), one boss, boss.

Look at the effect map first

1.First realize the background image of infinite scroll.

public class gamebg {
 //Picture resources for the game background
 //for loop playback,Two bitmap objects are defined here, //its resource references the same image
 private bitmap bmpbackground1;
 private bitmap bmpbackground2;
 //game background coordinates
 private int bg1x, bg1y, bg2x, bg2y;
 private int speed=3;
 public gamebg (bitmap bmpbackground) {
  this.bmpbackground1=bmpbackground;
  this.bmpbackground2=bmpbackground;
  //First fill the screen first
  bg1y=-math.abs (bmpbackground.getheight ()-mysurfaceview.screenh);
  bg2y=bg1y-bmpbackground1.getheight () +50;
 }
 public void draw (canvas canvas, paint paint) {
  canvas.drawbitmap (bmpbackground1, bg1x, bg1y, paint);
  canvas.drawbitmap (bmpbackground2, bg2x, bg2y, paint);
 }
 public void logic () {
  bg1y +=speed;
  bg2y +=speed;
  if (bg1y>mysurfaceview.screenh) {
   bg1y=bg2y-bmpbackground1.getheight () +50;
  }
  if (bg2y>mysurfaceview.screenh) {
   bg2y=bg1y-bmpbackground1.getheight () +50;
  }
 }
}

Then call the method in mysurfaceview

public class mysurfaceview extends surfaceview implements callback, runnable {
 private surfaceholder sfh;
 private paint paint;
 private thread th;
 private boolean flag;
 private canvas canvas;
 //1 define the game state constant
 public static final int game_menu=0;//game menu
 public static final int gameing=1;//in game
 public static final int game_win=2;//game victory
 public static final int game_lost=3;//game failed
 public static final int game_pause=-1;//game menu
 //Current game state (default is initially in the game menu interface)
 public static int gamestate=game_menu;
 //Declare a resources instance to load images
 private resources res=this.getresources ();
 //Declare the image resources used by the game (picture declaration)
 private bitmap bmpbackground;//game background
 private bitmap bmpboom;//explosion effect
 private bitmap bmpboosboom;//boos explosion effect
 private bitmap bmpbutton;//game start button
 private bitmap bmpbuttonpress;//the game start button is clicked
 private bitmap bmpenemyduck;//Monster Duck
 private bitmap bmpenemyfly;//monster fly
 private bitmap bmpenemyboos;//monster pig head boos
 private bitmap bmpgamewin;//game victory background
 private bitmap bmpgamelost;//Game failure background
 private bitmap bmpplayer;//Game lead plane
 private bitmap bmpplayerhp;//Leader plane health
 private bitmap bmpmenu;//menu background
 public static bitmap bmpbullet;//bullet
 public static bitmap bmpenemybullet;//enemy bullets
 public static bitmap bmpbossbullet;//boss bullet
 public static int screenw;
 public static int screenh;
 //
 private gamemenu gamemenu;
 private gamebg gamebg;
 /**
  * surfaceview initialization function
  * /
 public mysurfaceview (context context) {
  super (context);
  sfh=this.getholder ();
  sfh.addcallback (this);
  paint=new paint ();
  paint.setcolor (color.white);
  paint.setantialias (true);
  setfocusable (true);
 }
 /**
  * surfaceview view creation,Respond to this function
  * /
 @override
 public void surfacecreated (surfaceholder holder) {
  screenw=this.getwidth ();
  screenh=this.getheight ();
  initgame ();
  flag=true;
  //instance thread
  th=new thread (this);
  //start the thread
  th.start ();
 }
 /**
  * Load game resources
  * /
 private void initgame () {
  //Load game resources
  bmpbackground=bitmapfactory
    .decoderesource (res, r.drawable.background);
  bmpboom=bitmapfactory.decoderesource (res, r.drawable.boom);
  bmpboosboom=bitmapfactory.decoderesource (res, r.drawable.boos_boom);
  bmpbutton=bitmapfactory.decoderesource (res, r.drawable.button);
  bmpbuttonpress=bitmapfactory.decoderesource (res,    r.drawable.button_press);
  bmpenemyduck=bitmapfactory.decoderesource (res, r.drawable.enemy_duck);
  bmpenemyfly=bitmapfactory.decoderesource (res, r.drawable.enemy_fly);
  bmpenemyboos=bitmapfactory.decoderesource (res, r.drawable.enemy_pig);
  bmpgamewin=bitmapfactory.decoderesource (res, r.drawable.gamewin);
  bmpgamelost=bitmapfactory.decoderesource (res, r.drawable.gamelost);
  bmpplayer=bitmapfactory.decoderesource (res, r.drawable.player);
  bmpplayerhp=bitmapfactory.decoderesource (res, r.drawable.hp);
  bmpmenu=bitmapfactory.decoderesource (res, r.drawable.menu);
  bmpbullet=bitmapfactory.decoderesource (res, r.drawable.bullet);
  bmpenemybullet=bitmapfactory.decoderesource (res,    r.drawable.bullet_enemy);
  bmpbossbullet=bitmapfactory
    .decoderesource (res, r.drawable.boosbullet);
  //Menu class instantiation
  gamemenu=new gamemenu (bmpmenu, bmpbutton, bmpbuttonpress);
  gamebg=new gamebg (bmpbackground);
 }
 /**
  * Game drawing
  * /
 public void mydraw () {
  try {
   canvas=sfh.lockcanvas ();
   if (canvas!=null) {
    canvas.drawcolor (color.white);
    //The drawing function draws differently depending on the state of the game
    switch (gamestate) {
    case game_menu:
     gamemenu.draw (canvas, paint);
     break;
    case gameing:
     gamebg.draw (canvas, paint);
     break;
    case game_win:
     break;
    case game_lost:
     break;
    case game_pause:
     break;
    default:
     break;
    }
   }
  } catch (exception e) {
   //todo:handle exception
  } finally {
   if (canvas!=null)
    sfh.unlockcanvasandpost (canvas);
  }
 }
 /**
  * Touch screen event monitoring
  * /
 @override
 public boolean ontouchevent (motionevent event) {
  switch (gamestate) {
  case game_menu:
   gamemenu.ontouchevent (event);
   break;
  case gameing:
   break;
  case game_win:
   break;
  case game_lost:
   break;
  case game_pause:
   break;
  }
  return true;
 }
 /**
  * Key event monitoring
  * /
 @override
 public boolean onkeydown (int keycode, keyevent event) {
  switch (gamestate) {
  case game_menu:
   break;
  case gameing:
   break;
  case game_win:
   break;
  case game_lost:
   break;
  case game_pause:
   break;
  }
  return super.onkeydown (keycode, event);
 }
 @override
 public boolean onkeyup (int keycode, keyevent event) {
  switch (gamestate) {
  case game_menu:
   break;
  case gameing:
   break;
  case game_win:
   break;
  case game_lost:
   break;
  case game_pause:
   break;
  }
  return super.onkeyup (keycode, event);
 }
 /**
  * Game logic
  * /
 private void logic () {
  switch (gamestate) {
  case game_menu:
   break;
  case gameing:
   gamebg.logic ();
   break;
  case game_win:
   break;
  case game_lost:
   break;
  case game_pause:
   break;
  }
 }
 @override
 public void run () {
  while (flag) {
   long start=system.currenttimemillis ();
   mydraw ();
   logic ();
   long end=system.currenttimemillis ();
   try {
    if (end-start<50) {
     thread.sleep (50-(end-start));
    }
   } catch (interruptedexception e) {
    e.printstacktrace ();
   }
  }
 }
 /**
  * surfaceview view state changes,Respond to this function
  * /
 @override
 public void surfacechanged (surfaceholder holder, int format, int width,   int height) {
 }
 /**
  * When the surfaceview view dies,Respond to this function
  * /
 @override
 public void surfacedestroyed (surfaceholder holder) {
  flag=false;
 }
}
  • Previous Six ways to create AlertDialog in Android
  • Next AngularJS expression detailed explanation and example code