Home>

Look at the effect map first

New player class

public class player {
 private int playerhp=3;
 private bitmap bmpplayerhp;
 //Protagonist coordinates and bitmap
 private int x, y;
 private bitmap bmpplayer;
 //main character moving speed
 private int speed=5;
 //main character moving logo
 private boolean isup, isdown, isleft, isright;
 //The main character's constructor
 public player (bitmap bmpplayer, bitmap bmpplayerhp) {
  this.bmpplayer=bmpplayer;
  this.bmpplayerhp=bmpplayerhp;
  //aircraft initial position
  x=mysurfaceview.screenw/2-bmpplayer.getwidth ()/2;
  y=mysurfaceview.screenh-bmpplayer.getheight ();
 }
 //Protagonist game drawing method
 public void draw (canvas canvas, paint paint) {
  //draw the protagonist
  canvas.drawbitmap (bmpplayer, x, y, paint);
  //Plot blood
  for (int i=0;i<playerhp;i ++) {
   canvas.drawbitmap (bmpplayerhp, i * bmpplayerhp.getwidth (),     mysurfaceview.screenh-bmpplayerhp.getheight (), paint);
  }
 }
 /**
  * Key event monitoring
  * /
 public void onkeydown (int keycode, keyevent event) {
  if (keycode == keyevent.keycode_dpad_up) {
   isup=true;
  }
  if (keycode == keyevent.keycode_dpad_down) {
   isdown=true;
  }
  if (keycode == keyevent.keycode_dpad_left) {
   isleft=true;
  }
  if (keycode == keyevent.keycode_dpad_right) {
   isright=true;
  }
 }
 public void onkeyup (int keycode, keyevent event) {
  if (keycode == keyevent.keycode_dpad_up) {
   isup=false;
  }
  if (keycode == keyevent.keycode_dpad_down) {
   isdown=false;
  }
  if (keycode == keyevent.keycode_dpad_left) {
   isleft=false;
  }
  if (keycode == keyevent.keycode_dpad_right) {
   isright=false;
  }
 }
 /**
  * Game logic
  * /
 public void logic () {
  if (isup) {
   y-= speed;
  }
  if (isdown) {
   y +=speed;
  }
  if (isleft) {
   x-= speed;
  }
  if (isright) {
   x +=speed;
  }
  //determine the screen x boundary
  if (x + bmpplayer.getwidth ()>= mysurfaceview.screenw) {
   x=mysurfaceview.screenw-bmpplayer.getwidth ();
  } else if (x<= 0) {
   x=0;
  }
  //determine the y boundary of the screen
  if (y + bmpplayer.getheight ()>= mysurfaceview.screenh) {
   y=mysurfaceview.screenh-bmpplayer.getheight ();
  } else if (y<= 0) {
   y=0;
  }
 }
 //Set the main character's health
 public void setplayerhp (int hp) {
  this.playerhp=hp;
 }
 //Get the main character's health
 public int getplayerhp () {
  return playerhp;
 }
}

Called 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;
 private player player;
 /**
  * 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);
  player=new player (bmpplayer, bmpplayerhp);
 }
 /**
  * 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);
     player.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:
   player.onkeydown (keycode, event);
   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:
   player.onkeyup (keycode, event);
   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 ();
   player.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 js define object array (combination) multi-dimensional array method
  • Next ES6 Babel correct installation process in JavaScript