Home>

Android actual combat aircraft game bullet generation,New Bullet Class

public class bullet {
 //bullet image resource
 public bitmap bmpbullet;
 //coordinates of the bullet
 public int bulletx, bullety;
 //bullet speed
 public int speed;
 //type of bullet 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;
 //Bullet related member variables in boss crazy state
 private int dir;//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;
 //current bullet 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;
 }
 }
 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;
 case bullet_boss:
  //Bullet logic in the crazy state of the boss to be implemented
  //boundary processing
  if (bullety>mysurfaceview.screenh || bullety<= -40
   || bulletx>mysurfaceview.screenw || bulletx<= -40) {
  isdead=true;
  }
  break;
 }
 }
}

Called in mysurfacview to generate bullet protagonist and own

package com.gsf;
import java.util.random;
import java.util.vector;
import android.content.context;
import android.content.res.resources;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.view.keyevent;
import android.view.motionevent;
import android.view.surfaceholder;
import android.view.surfaceholder.callback;
import android.view.surfaceview;
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;
 //declare an enemy aircraft container
 private vector<enemy>vcenemy;
 //Time to generate enemy aircraft (ms)
 private int createenemytime=50;
 private int count;//counter
 //enemy array:1 and 2 represent the type of enemy aircraft,-1 means boss
 //Each dimension of the 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}};
 //Take the index of the 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;
 //
 private gamemenu gamemenu;
 private gamebg gamebg;
 private player player;
 //enemy bullet container
 private vector<bullet>vcbullet;
 //add bullet counter
 private int countenemybullet;
 //main character bullet container
 private vector<bullet>vcbulletplayer;
 //add bullet counter
 private int countplayerbullet;
 /**
 * 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);
 //Example game background
 gamebg=new gamebg (bmpbackground);
 //instance protagonist
 player=new player (bmpplayer, bmpplayerhp);
 //Example of enemy bomb container
 vcbullet=new vector<bullet>();
 //The main character bullet container instance
 vcbulletplayer=new vector<bullet>();
 //Instance enemy aircraft container
 vcenemy=new vector<enemy>();
 //instance random library
 random=new random ();
 }
 /**
 * 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);
   if (isboss == false) {
   //enemy drawing
   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 {
   //boss draws
   }
   //Handle the main character bullet drawing
   for (int i=0;i<vcbulletplayer.size ();i ++) {
   vcbulletplayer.elementat (i) .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 ();
  //enemy logic
  if (isboss == false) {
  //enemy 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 ++) {
   //flies
   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 to determine if the next group is the last group (boss)
   if (enemyarrayindex == enemyarray.length-1) {
   isboss=true;
   } else {
   enemyarrayindex ++;
   }
  }
  //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) {
   //flies
   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 aircraft bullet logic
  for (int i=0;i<vcbullet.size ();i ++) {
   bullet b=vcbullet.elementat (i);
   if (b.isdead) {
   vcbullet.removeelement (b);
   } else {
   b.logic ();
   }
  }
  //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));
  }
  //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;
   }
   }
  }
  //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 ();
   }
  }
  }
  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;
 }
}

Now that the bullet collided with the protagonist, nothing happened. Collision detection between the bullet and the protagonist is required.

//Modify the player class and add collision detection
 ////judge collision (the protagonist and the enemy bullet)
 public boolean iscollsionwith (bullet bullet) {
 //is invincible time
 if (iscollision == false) {
  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 + bmpplayer.getwidth ()<= x2) {
  return false;
  } else if (y>= y2&&y>= y2 + h2) {
  return false;
  } else if (y<= y2&&y + bmpplayer.getheight ()&=;y2) {
  return false;
  }
  //The collision is invincible
  iscollision=true;
  return true;
  //invincible,Ignore collision
 } else {
  return false;
 }
 }

Add the collision between the protagonist and the enemy bullet in the logic function of the main interface mysrufaceview

//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;
   }
   }
  }

Of course, the protagonist's bullet collided with the enemy aircraft. Collision detection was also required.

//Judgment collision (the enemy aircraft collides with 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;
 }
 //Collision,Let it die
 isdead=true;
 return true;
 }

Add logic in main view logic for main character bullet and enemy collision

//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 to be completed
   }
  }

The effect picture completed above

The effect of the explosion is completed below

Create a new explosion boom

/**
 * Instant explosion
 * @author liuml
 * @time 2016-1-1 11:32:56 AM
 * /
public class boom {
 //Explode effect resource map
 private bitmap bmpboom;
 //Position coordinates of the explosion effect
 private int boomx, boomy;
 //Explode animation to play the current frame index
 private int cureentframeindex;
 //Total number of frames of the explosion effect
 private int totleframe;
 //Width and height of each frame
 private int framew, frameh;
 //whether it is finished,Optimization
 public boolean playend;
 //Constructor of explosion effect
 public boom (bitmap bmpboom, int x, int y, int totleframe) {
 this.bmpboom=bmpboom;
 this.boomx=x;
 this.boomy=y;
 this.totleframe=totleframe;
 framew=bmpboom.getwidth ()/totleframe;
 frameh=bmpboom.getheight ();
 }
 //Draw the explosion effect
 public void draw (canvas canvas, paint paint) {
 canvas.save ();
 canvas.cliprect (boomx, boomy, boomx + framew, boomy + frameh);
 canvas.drawbitmap (bmpboom, boomx-cureentframeindex * framew, boomy, paint);
 canvas.restore ();
 }
 //Logic of explosion effect
 public void logic () {
 if (cureentframeindex<totleframe) {
  cureentframeindex ++;
 } else {
  playend=true;
 }
 }
}

The following is the old routine. On the main interface, declare the explosion container, then draw, and then implement the logic.

package com.gsf;
import java.util.random;
import java.util.vector;
import android.content.context;
import android.content.res.resources;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.view.keyevent;
import android.view.motionevent;
import android.view.surfaceholder;
import android.view.surfaceholder.callback;
import android.view.surfaceview;
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;
 //declare an enemy aircraft container
 private vector<enemy>vcenemy;
 //Time to generate enemy aircraft (ms)
 private int createenemytime=50;
 private int count;//counter
 //enemy array:1 and 2 represent the type of enemy aircraft,-1 means boss
 //Each dimension of the 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}};
 //Take the index of the 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;
 //
 private gamemenu gamemenu;
 private gamebg gamebg;
 private player player;
 //enemy bullet container
 private vector<bullet>vcbullet;
 //add bullet counter
 private int countenemybullet;
 //main character bullet container
 private vector<bullet>vcbulletplayer;
 //add bullet counter
 private int countplayerbullet;
 //Explosion effect container
 private vector<boom>vcboom;
 /**
 * 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);
 //Example game background
 gamebg=new gamebg (bmpbackground);
 //instance protagonist
 player=new player (bmpplayer, bmpplayerhp);
 //Example of enemy bomb container
 vcbullet=new vector<bullet>();
 //The main character bullet container instance
 vcbulletplayer=new vector<bullet>();
 //Instance enemy aircraft container
 vcenemy=new vector<enemy>();
 //instance random library
 random=new random ();
 //Explosion effect container instance
 vcboom=new vector<boom>();
 }
 /**
 * 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);
   if (isboss == false) {
   //enemy drawing
   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);
   }
   //Draw the explosion effect
   for (int i=0;i<vcboom.size ();i ++) {
    vcboom.elementat (i) .draw (canvas, paint);
   }
   } else {
   //boss draws
   }
   //Handle the main character bullet drawing
   for (int i=0;i<vcbulletplayer.size ();i ++) {
   vcbulletplayer.elementat (i) .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 ();
  //enemy logic
  if (isboss == false) {
  //enemy 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 ++) {
   //flies
   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 to determine if the next group is the last group (boss)
   if (enemyarrayindex == enemyarray.length-1) {
   isboss=true;
   } else {
   enemyarrayindex ++;
   }
  }
  //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) {
   //flies
   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 aircraft bullet logic
  for (int i=0;i<vcbullet.size ();i ++) {
   bullet b=vcbullet.elementat (i);
   if (b.isdead) {
   vcbullet.removeelement (b);
   } else {
   b.logic ();
   }
  }
  //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));
  }
  //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;
   }
   }
  }
  //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 ();
   }
  }
  //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));
   }
   }
  }
  }
  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 jquerymousewheel to achieve full screen flip effect
  • Next Detailed fopen () and fdopen () functions in C