Home>

The examples in this article share the android code for the aircraft game boss and the victory and failure page design. The specific content is as follows

Modify the bullet class:

public class bullet {
 //bullet image resource
 public bitmap bmpbullet;
 //The coordinates of the bullet
 public int bulletx, bullety;
 //bullet speed
 public int speed;
 //Bullet type and constant
 public int bullettype;
 //Protagonist's
 public static final int bullet_player=-1;
 //duck
 public static final int bullet_duck=1;
 //flies
 public static final int bullet_fly=2;
 //boss
 public static final int bullet_boss=3;
 //whether the bullet is overscreen, Optimization
 public boolean isdead;
 //Boss member related variables in the crazy state
 private int dir;//The current boss bullet direction
 //8 direction constant
 public static final int dir_up=-1;
 public static final int dir_down=2;
 public static final int dir_left=3;
 public static final int dir_right=4;
 public static final int dir_up_left=5;
 public static final int dir_up_right=6;
 public static final int dir_down_left=7;
 public static final int dir_down_right=8;
 //Bullet current direction
 public bullet (bitmap bmpbullet, int bulletx, int bullety, int bullettype) {
 this.bmpbullet=bmpbullet;
 this.bulletx=bulletx;
 this.bullety=bullety;
 this.bullettype=bullettype;
 //Different bullet types have different speeds
 switch (bullettype) {
 case bullet_player:
  speed=4;
  break;
 case bullet_duck:
  speed=3;
  break;
 case bullet_fly:
  speed=4;
  break;
 case bullet_boss:
  speed=5;
  break;
 }
 }
 /**
 * Dedicated to handling bullets created in boss madness
 * @param bmpbullet
 * @param bulletx
 * @param bullety
 * @param bullettype
 * @param dir
 * /
 public bullet (bitmap bmpbullet, int bulletx, int bullety, int bullettype, int dir) {
 this.bmpbullet=bmpbullet;
 this.bulletx=bulletx;
 this.bullety=bullety;
 this.bullettype=bullettype;
 speed=5;
 this.dir=dir;
 }
 //Drawing of bullets
 public void draw (canvas canvas, paint paint) {
 canvas.drawbitmap (bmpbullet, bulletx, bullety, paint);
 }
 //Bullet logic
 public void logic () {
 //Different bullet types have different logic
 //The main character's bullet moves vertically upwards
 switch (bullettype) {
 case bullet_player:
  bullety-= speed;
  if (bullety<-50) {
  isdead=true;
  }
  break;
 //The bullets of the duck and the fly are moving vertically
 case bullet_duck:
 case bullet_fly:
  bullety +=speed;
  if (bullety>mysurfaceview.screenh) {
  isdead=true;
  }
  break;
 //8-direction bullet logic in the crazy state
 case bullet_boss:
  //The bullet logic in the crazy state is to be implemented
  switch (dir) {
  //Bullets in the direction
  case dir_up:
  bullety-= speed;
  break;
  //Bullets in the direction
  case dir_down:
  bullety +=speed;
  break;
  //left bullet
  case dir_left:
  bulletx-= speed;
  break;
  //direction right bullet
  case dir_right:
  bulletx +=speed;
  break;
  //direction left bullet
  case dir_up_left:
  bullety-= speed;
  bulletx-= speed;
  break;
  //direction right bullet
  case dir_up_right:
  bulletx +=speed;
  bullety-= speed;
  break;
  //Bottom left bullet
  case dir_down_left:
  bulletx-= speed;
  bullety +=speed;
  break;
  //Bottom right bullet
  case dir_down_right:
  bullety +=speed;
  bulletx +=speed;
  break;
  }
  //Boundary processing
  if (bullety>mysurfaceview.screenh || bullety<= -40 || bulletx>mysurfaceview.screenw || bulletx<= -40) {
  isdead=true;
  }
  break;
 }
 }
}

Below create a new boss class

Create boss

public class boss {
 //boss health
 public int hp=50;
 //boss image resources
 private bitmap bmpboss;
 //boss coordinates
 public int x, y;
 //boss width and height of each frame
 public int framew, frameh;
 //boss current frame index
 private int frameindex;
 //boss speed of movement
 private int speed=5;
 //boss movement track
 //will move towards the bottom of the screen for a certain time,And fired a wide range of bullets,(Whether crazy)
 //Under normal conditions, the bullet moves vertically downward
 private boolean iscrazy;
 //State interval for entering a crazy state
 private int crazytime=200;
 //counter
 private int count;
 //boss constructor
 public boss (bitmap bmpboss) {
 this.bmpboss=bmpboss;
 framew=bmpboss.getwidth ()/10;
 frameh=bmpboss.getheight ();
 //The x coordinate of the boss is centered
 x=mysurfaceview.screenw/2-framew/2;
 y=0;
 }
 //boss drawing
 public void draw (canvas canvas, paint paint) {
 canvas.save ();
 canvas.cliprect (x, y, x + framew, y + frameh);
 canvas.drawbitmap (bmpboss, x-frameindex * framew, y, paint);
 canvas.restore ();
 }
 //boss logic
 public void logic () {
 //Continuously loop the frames to form an animation
 frameindex ++;
 if (frameindex>= 10) {
  frameindex=0;
 }
 //no crazy state
 if (iscrazy == false) {
  x +=speed;
  if (x + framew>= mysurfaceview.screenw) {
  speed=-speed;
  } else if (x<= 0) {
  speed=-speed;
  }
  count ++;
  if (count%crazytime == 0) {
  iscrazy=true;
  speed=24;
  }
  //Crazy state
 } else {
  speed-= 1;
  //Create a lot of bullets when the boss returns
  if (speed == 0) {
  //Add 8-direction bullet
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_up));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_down));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_left));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_right));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_up_left));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_up_right));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_down_left));
  mysurfaceview.vcbulletboss.add (new bullet (mysurfaceview.bmpbossbullet, x + 40, y + 10, bullet.bullet_boss, bullet.dir_down_right));
  }
  y +=speed;
  if (y<= 0) {
  //Resume normal state
  iscrazy=false;
  speed=5;
  }
 }
 }
 //Judging the collision (boss was hit by the protagonist bullet)
 public boolean iscollsionwith (bullet bullet) {
 int x2=bullet.bulletx;
 int y2=bullet.bullety;
 int w2=bullet.bmpbullet.getwidth ();
 int h2=bullet.bmpbullet.getheight ();
 if (x>= x2&&x>= x2 + w2) {
  return false;
 } else if (x<= x2&&x + framew<= x2) {
  return false;
 } else if (y>= y2&&y>= y2 + h2) {
  return false;
 } else if (y<= y2&&y + frameh<= y2) {
  return false;
 }
 return true;
 }
 //Set boss blood volume
 public void sethp (int hp) {
 this.hp=hp;
 }
}

Instantiate in the main interfacebulletandboss

public class mysurfaceview extends surfaceview implements callback, runnable {
 private surfaceholder sfh;
 private paint paint;
 private thread th;
 private boolean flag;
 private canvas canvas;
 public static int screenw, screenh;
 //Define game state constants
 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
 //The 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;//Protect plane health
 private bitmap bmpmenu;//Menu background
 public static bitmap bmpbullet;//bullet
 public static bitmap bmpenemybullet;//enemy bullets
 public static bitmap bmpbossbullet;//boss bullet
 //Declare a menu object
 private gamemenu gamemenu;
 //Declaring a scrolling game background object
 private gamebg background;
 //Declaring the main character object
 private player player;
 //Declare an enemy aircraft container
 private vector<enemy>vcenemy;
 //Time to generate enemy aircraft each time (ms)
 private int createenemytime=50;
 private int count;//counter
 //Enemy array:1 and 2 indicate the type of enemy aircraft,-1 means boss
 //Each dimension of a two-dimensional array is a set of monsters
 private int enemyarray [] []={{1, 2}, {1, 1}, {1, 3, 1, 2}, {1, 2}, {2, 3}, {3, 1, 3} , {2, 2}, {1, 2}, {2, 2}, {1, 3, 1, 1}, {2, 1},  {1, 3}, {2, 1}, {-1}};
 //The index of the currently taken one-dimensional array
 private int enemyarrayindex;
 //Whether the boss flag appears
 private boolean isboss;
 //Random library, assign random coordinates to the created enemy aircraft
 private random random;
 //Enemy Bullet Container
 private vector<bullet>vcbullet;
 //Add bullet counter
 private int countenemybullet;
 //The main character bullet container
 private vector<bullet>vcbulletplayer;
 //Add bullet counter
 private int countplayerbullet;
 //Explosion effect container
 private vector<boom>vcboom;
 //declaration boss
 private boss boss;
 //boss bullet container
 public static vector<bullet>vcbulletboss;
 /**
 * 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);
 setfocusableintouchmode (true);
 //Set the background light
 this.setkeepscreenon (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 thread
 th.start ();
 }
 /*
 * Custom game initialization functions
 * /
 private void initgame () {
 //When the game is placed in the background and re-entered
The game is reset!
 //When the game state is in the menu,Before resetting the game
 if (gamestate == game_menu) {
  //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);
  //Explosion effect container instance
  vcboom=new vector<boom>();
  //Example of enemy bomb container
  vcbullet=new vector<bullet>();
  //The main character bullet container instance
  vcbulletplayer=new vector<bullet>();
  //Menu class instance
  gamemenu=new gamemenu (bmpmenu, bmpbutton, bmpbuttonpress);
  //Example game background
  background=new gamebg (bmpbackground);
  //Instance protagonist
  player=new player (bmpplayer, bmpplayerhp);
  //Instance enemy aircraft container
  vcenemy=new vector<enemy>();
  //Instance random library
  random=new random ();
  //--- boss related
  //Instance boss object
  boss=new boss (bmpenemyboos);
  //Instance boss bullet container
  vcbulletboss=new vector<bullet>();
 }
 }
 /**
 * Game drawing
 * /
 public void mydraw () {
 try {
  canvas=sfh.lockcanvas ();
  if (canvas!=null) {
  canvas.drawcolor (color.white);
  //Drawing functions draw differently according to different game states
  switch (gamestate) {
  case game_menu:
   //Menu drawing function
   gamemenu.draw (canvas, paint);
   break;
  case gameing:
   //Game background
   background.draw (canvas, paint);
   //Protagonist drawing function
   player.draw (canvas, paint);
   if (isboss == false) {
   //Drawing by enemy aircraft
   for (int i=0;i<vcenemy.size ();i ++) {
    vcenemy.elementat (i) .draw (canvas, paint);
   }
   //Drawing of enemy aircraft bullets
   for (int i=0;i<vcbullet.size ();i ++) {
    vcbullet.elementat (i) .draw (canvas, paint);
   }
   } else {
   //Drawing of boos
   boss.draw (canvas, paint);
   //boss bullet logic
   for (int i=0;i<vcbulletboss.size ();i ++) {
    vcbulletboss.elementat (i) .draw (canvas, paint);
   }
   }
   //Handle the main character bullet drawing
   for (int i=0;i<vcbulletplayer.size ();i ++) {
   vcbulletplayer.elementat (i) .draw (canvas, paint);
   }
   //Draw the explosion effect
   for (int i=0;i<vcboom.size ();i ++) {
   vcboom.elementat (i) .draw (canvas, paint);
   }
   break;
  case game_pause:
   break;
  case game_win:
   canvas.drawbitmap (bmpgamewin, 0, 0, paint);
   break;
  case game_lost:
   canvas.drawbitmap (bmpgamelost, 0, 0, paint);
   break;
  }
  }
 } catch (exception e) {
  //todo:handle exception
 } finally {
  if (canvas!=null)
  sfh.unlockcanvasandpost (canvas);
 }
 }
 /**
 * Touch screen event monitoring
 * /
 @override
 public boolean ontouchevent (motionevent event) {
 //Touch screen monitor event function monitors differently according to different game states
 switch (gamestate) {
 case game_menu:
  //Touch event handling of menu
  gamemenu.ontouchevent (event);
  break;
 case gameing:
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  break;
 }
 return true;
 }
 /**
 * Key press event listener
 * /
 @override
 public boolean onkeydown (int keycode, keyevent event) {
 //Handle the back button
 if (keycode == keyevent.keycode_back) {
  //The game returns to the menu by default when it wins, fails, and progresses
  if (gamestate == gameing || gamestate == game_win || gamestate == game_lost) {
  gamestate=game_menu;
  //boss status is set to not appear
  isboss=false;
  //Reset game
  initgame ();
  //Reset the monster appearance
  enemyarrayindex=0;
  } else if (gamestate == game_menu) {
  //The current game status is in the menu interface,Exit button by default
  mainactivity.instance.finish ();
  system.exit (0);
  }
  //Indicates that this key has been processed,No longer handed over to the system,  //This prevents the game from being cut into the background
  return true;
 }
 //Key listen event function monitors differently according to different game states
 switch (gamestate) {
 case game_menu:
  break;
 case gameing:
  //Key press event of the protagonist
  player.onkeydown (keycode, event);
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  break;
 }
 return super.onkeydown (keycode, event);
 }
 /**
 * Key up event monitoring
 * /
 @override
 public boolean onkeyup (int keycode, keyevent event) {
 //Handle the back button
 if (keycode == keyevent.keycode_back) {
  //The game returns to the menu by default when it wins, fails, and progresses
  if (gamestate == gameing || gamestate == game_win || gamestate == game_lost) {
  gamestate=game_menu;
  }
  //Indicates that this key has been processed,No longer handed over to the system,  //This prevents the game from being cut into the background
  return true;
 }
 //Key listen event function monitors differently according to different game states
 switch (gamestate) {
 case game_menu:
  break;
 case gameing:
  //Key up event
  player.onkeyup (keycode, event);
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  break;
 }
 return super.onkeydown (keycode, event);
 }
 /**
 * Game logic
 * /
 private void logic () {
 //Logical processing performs different processing according to different game states
 switch (gamestate) {
 case game_menu:
  break;
 case gameing:
  //Background logic
  background.logic ();
  //Protagonist logic
  player.logic ();
  //enemy aircraft logic
  if (isboss == false) {
  //enemy aircraft logic
  for (int i=0;i<vcenemy.size ();i ++) {
   enemy en=vcenemy.elementat (i);
   //Because the container keeps adding enemy aircraft, the isdead judgment of the enemy aircraft,   //If it is dead then delete it from the container,Optimized the container;
   if (en.isdead) {
   vcenemy.removeelementat (i);
   } else {
   en.logic ();
   }
  }
  //Generate enemy aircraft
  count ++;
  if (count%createenemytime == 0) {
   for (int i=0;i<enemyarray [enemyarrayindex] .length;i ++) {
   //fly
   if (enemyarray [enemyarrayindex] [i] == 1) {
    int x=random.nextint (screenw-100) + 50;
    vcenemy.addelement (new enemy (bmpenemyfly, 1, x, -50));
    //duck left
   } else if (enemyarray [enemyarrayindex] [i] == 2) {
    int y=random.nextint (20);
    vcenemy.addelement (new enemy (bmpenemyduck, 2, -50, y));
    //duck right
   } else if (enemyarray [enemyarrayindex] [i] == 3) {
    int y=random.nextint (20);
    vcenemy.addelement (new enemy (bmpenemyduck, 3, screenw + 50, y));
   }
   }
   //Here is whether the next group is the last group (boss)
   if (enemyarrayindex == enemyarray.length-1) {
   isboss=true;
   } else {
   enemyarrayindex ++;
   }
  }
  //Handle the collision between the enemy aircraft and the protagonist
  for (int i=0;i<vcenemy.size ();i ++) {
   if (player.iscollsionwith (vcenemy.elementat (i))) {
   //Collision,Protagonist HP
   player.setplayerhp (player.getplayerhp ()-1);
   //When the main character's health is less than 0, the game is judged to have failed
   if (player.getplayerhp ()<= -1) {
    gamestate=game_lost;
   }
   }
  }
  //Add an enemy bullet every 2 seconds
  countenemybullet ++;
  if (countenemybullet%40 == 0) {
   for (int i=0;i<vcenemy.size ();i ++) {
   enemy en=vcenemy.elementat (i);
   //Different types of enemy aircraft have different bullet trajectories
   int bullettype=0;
   switch (en.type) {
   //fly
   case enemy.type_fly:
    bullettype=bullet.bullet_fly;
    break;
   //duck
   case enemy.type_duckl:
   case enemy.type_duckr:
    bullettype=bullet.bullet_duck;
    break;
   }
   vcbullet.add (new bullet (bmpenemybullet, en.x + 10, en.y + 20, bullettype));
   }
  }
  //Handle enemy bullet logic
  for (int i=0;i<vcbullet.size ();i ++) {
   bullet b=vcbullet.elementat (i);
   if (b.isdead) {
   vcbullet.removeelement (b);
   } else {
   b.logic ();
   }
  }
  //Handle enemy aircraft bullets colliding with the protagonist
  for (int i=0;i<vcbullet.size ();i ++) {
   if (player.iscollsionwith (vcbullet.elementat (i))) {
   //Collision,Protagonist HP
   player.setplayerhp (player.getplayerhp ()-1);
   //When the main character's health is less than 0, the game is judged to have failed
   if (player.getplayerhp ()<= -1) {
    gamestate=game_lost;
   }
   }
  }
  //Handle the main character bullet colliding with the enemy aircraft
  for (int i=0;i<vcbulletplayer.size ();i ++) {
   //Remove each element of the main character bullet container
   bullet blplayer=vcbulletplayer.elementat (i);
   for (int j=0;j<vcenemy.size ();j ++) {
   //Add explosion effect
   //Take out each element of the enemy's container and traverse the main character bullet
   if (vcenemy.elementat (j) .iscollsionwith (blplayer)) {
    vcboom.add (new boom (bmpboom, vcenemy.elementat (j) .x, vcenemy.elementat (j) .y, 7));
   }
   }
  }
  } else {//boss related logic
  //Add a protagonist bullet every 0.5 seconds
  boss.logic ();
  if (countplayerbullet%10 == 0) {
   //boss's ordinary bullet before going crazy
   vcbulletboss.add (new bullet (bmpbossbullet, boss.x + 35, boss.y + 40, bullet.bullet_fly));
  }
  //boss bullet logic
  for (int i=0;i<vcbulletboss.size ();i ++) {
   bullet b=vcbulletboss.elementat (i);
   if (b.isdead) {
   vcbulletboss.removeelement (b);
   } else {
   b.logic ();
   }
  }
  //boss bullets collide with the protagonist
  for (int i=0;i<vcbulletboss.size ();i ++) {
   if (player.iscollsionwith (vcbulletboss.elementat (i))) {
   //Collision,Protagonist HP
   player.setplayerhp (player.getplayerhp ()-1);
   //When the main character's health is less than 0, the game is judged to have failed
   if (player.getplayerhp ()<= -1) {
    gamestate=game_lost;
   }
   }
  }
  //boss was hit by the protagonist bullet,Produce an explosive effect
  for (int i=0;i<vcbulletplayer.size ();i ++) {
   bullet b=vcbulletplayer.elementat (i);
   if (boss.iscollsionwith (b)) {
   if (boss.hp<= 0) {
    //Game victory
    gamestate=game_win;
   } else {
    //Delete the bullets in this collision in time,Prevent repeated determination of this bullet colliding with the boss,    b.isdead=true;
    //boss health reduced by 1
    boss.sethp (boss.hp-1);
    //Add three boss explosion effects on the boss
    vcboom.add (new boom (bmpboosboom, boss.x + 25, boss.y + 30, 5));
    vcboom.add (new boom (bmpboosboom, boss.x + 35, boss.y + 40, 5));
    vcboom.add (new boom (bmpboosboom, boss.x + 45, boss.y + 50, 5));
   }
   }
  }
  }
  //Add a protagonist bullet every 1 second
  countplayerbullet ++;
  if (countplayerbullet%20 == 0) {
  vcbulletplayer.add (new bullet (bmpbullet, player.x + 15, player.y-20, bullet.bullet_player));
  }
  //Process the main character bullet logic
  for (int i=0;i<vcbulletplayer.size ();i ++) {
  bullet b=vcbulletplayer.elementat (i);
  if (b.isdead) {
   vcbulletplayer.removeelement (b);
  } else {
   b.logic ();
  }
  }
  //Explosion effect logic
  for (int i=0;i<vcboom.size ();i ++) {
  boom boom=vcboom.elementat (i);
  if (boom.playend) {
   //Delete from container after playback
   vcboom.removeelementat (i);
  } else {
   vcboom.elementat (i) .logic ();
  }
  }
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  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;
 }
}

AndVictory and defeat pages

Attach mainactivit

public class mainactivity extends activity {
 public static mainactivity instance;
 @override
 public void oncreate (bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 instance=this;
 //Set full screen
 this.getwindow (). setflags (windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);
 requestwindowfeature (window.feature_no_title);
 //Display a custom surfaceview view
 setcontentview (new mysurfaceview (this));
 }
 @override
 protected void ondestroy () {
 //todo auto-generated method stub
 mysurfaceview.gamestate=mysurfaceview.game_menu;
 super.ondestroy ();
 }
}
  • Previous Detailed description of associative arrays and ordinary arrays in js
  • Next JS can't get the attributes of html tags
  • Trends