Look at the effect chart first
New Player Class
public class Player {private int playerhp = 3;
Private Bitmap BMPPLAYERHP;
Lead coordinates and bitmap private int x, y;
Private Bitmap Bmpplayer;
Lead moving speed private int speed = 5;
Protagonist Mobile Logo Private Boolean isUp, Isdown, Isleft, isright;
The constructor of the protagonist is 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 ();
}//Lead game rendering method public void Draw (Canvas Canvas, Paint Paint) {//Draw lead Canvas.drawbitmap (Bmpplayer, x, Y, Paint); Draw the blood volume for (int i = 0; i < PLAYERHP i++) {canvas.drawbitmap (BMPPLAYERHP, I * bmpplayerhp.getwidth (), MyS
Urfaceview.screenh-bmpplayerhp.getheight (), paint); }/** * Key event Listener/public void OnKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_dpad_u
P) {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; }//Judge screen x boundary if (x + bmpplayer.getwidth () >= mysurfaceview.screenw) {x = MYSURFACEVIEW.SCREENW-BMPPLAYER.GETW
Idth ();
else if (x <= 0) {x = 0; }//Judge screen y boundary if (y + bmpplayer.getheight () >= mysurfaceview.screenh) {y = Mysurfaceview.screenh-bmpplayer.get
Height (); else if (y <= 0) {y = 0;
}//Set lead blood volume public void setplayerhp (int hp) {THIS.PLAYERHP = HP;
//Get the lead blood volume public int getplayerhp () {return PLAYERHP;
}
}
Then call 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 Definition game state constant public static final int game_menu = 0;//Game menu public static final int gameing = 1;//game public static fi nal int game_win = 2;//game wins public static final int game_lost = 3;//game failure public static final int game_pause = -1;//Game
Menu//Current game status (Default initial in Game menu interface) public static int gamestate = Game_menu;
Declare a resources instance to facilitate loading pictures private resources res = this.getresources (); Declare the game needs to use the picture resources (picture statement) 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;//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 P Rivate Bitmap bmpgamelost;//Game failure background Private BITMAP bmpplayer;//game lead airplane private Bitmap bmpplayerhp;//lead airplane blood volume private Bitmap bmpmenu;//menu background public static Bitmap Bmpbull et;//bullets public static Bitmap bmpenemybullet;//enemy bullets public static Bitmap bmpbossbullet;//boss Bullets public static int SCR
Eenw;
public static int screenh;
Private Gamemenu Gamemenu;
Private GAMEBG GAMEBG;
Private player player;
/** * Surfaceview initialization function/public Mysurfaceview {super (context);
SFH = This.getholder ();
Sfh.addcallback (this);
Paint = new paint ();
Paint.setcolor (Color.White);
Paint.setantialias (TRUE);
Setfocusable (TRUE); /** * Surfaceview View creation, response to this function/@Override public void surfacecreated (Surfaceholder holder) {screenw = This.get
Width ();
Screenh = This.getheight ();
Initgame ();
Flag = true;
instance thread th = new thread (this);
Start thread Th.start (); /** * Load game resource/private void Initgame () {//load game resource 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);
Drawing functions vary according to game state to draw 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; (Exception e) {//Todo:handle Exception} finally {if (canvas!= null) Sfh.unlockcanvasan
Dpost (canvas); /** * Touchscreen Event Monitor/@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 Monitor/@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 () {a while (flag) {Long start = System.currenttimemillis ();
Mydraw ();
Logic ();
Long end = System.currenttimemillis ();
try {if (End-start <) {Thread.Sleep (End-start));
} catch (Interruptedexception e) {e.printstacktrace (); /** * Surfaceview view state changed in response to this function//@Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {}/** * Surfaceview View dies, respond to this function/@Override public void surfacedestroyed (surfacehold
ER holder) {flag = false;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.