First analyze the elements within the game interface:
Infinite scrolling background, can operate the protagonist, the protagonist of the bullet, the main character of the blood, two kinds of monsters (enemy aircraft), a boss, boss of the explosion effect.
Look at the effect chart first
1, the first implementation of the infinite Scrolling background diagram principle: Define two Bitmap objects when the first bitmap to the end is the second bitmap from the end of the first bitmap to keep up.
public class GAMEBG {
///Game background picture resource//
for loop playback, here defines two bitmap objects,
//Its resources refer to the same picture
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 with the first one
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;}}}
And then call the method inside the 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;
/** * 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);
/** * 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);
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: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 () {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.