The example of this article for you to share the game to play the boss and the success of the failure page design of the Android code, specific content as follows
Modify the Bullet class:
public class Bullet {//Bullets picture Resources public Bitmap Bmpbullet;
The coordinates of the bullets are public int bulletx, bullety;
The speed of the bullet is public int speed;
Type of bullet and constant public int bullettype;
The main character of the public static final int bullet_player =-1;
The duck's public static final int bullet_duck = 1;
The fly's public static final int bullet_fly = 2;
BOSS's public static final int bullet_boss = 3;
Whether the bullet is super screen, the optimized processing public boolean isdead;
Boss Crazy State Bullets related member variable 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;
Bullets are currently directed to public Bullet (Bitmap bmpbullet, int bulletx, int bullety, int bullettype) {this.bmpbullet = Bmpbullet;
This.bulletx = Bulletx;
This.bullety = bullety;
This.bullettype = Bullettype; Different bullets.Type speed varies 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 the bullets created under Boss Madness * @param bmpbullet * @param bulletx * @param bullety * @param bullettype * @param D
IR/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;
///Bullets Draw public void Draw (Canvas Canvas, Paint Paint) {canvas.drawbitmap (Bmpbullet, Bulletx, Bullety, Paint); }//Bullets logic public void logic () {//different bullet types logic//lead bullets vertical upward motion switch (bullettype) {Case bullet_player:bullety =
Speed
if (Bullety < -50) {Isdead = true;
} break;
Ducks and flies bullets are vertical drop motion case bullet_duck:case bullet_fly:bullety + = speed;
if (Bullety > mysurfaceview.screenh) {isdead = true; } breAk BOSS Crazy State of the 8 direction Bullets logic case Bullet_boss://boss crazy state of bullets logic to be implemented switch (DIR) {//Direction bullet case dir_up:bullety-= speed
;
Break
The direction of the bullet case dir_down:bullety + = speed;
Break
Direction 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 on the bullet case dir_up_right:bulletx + = speed;
Bullety-= speed;
Break
Direction left the bullet case dir_down_left:bulletx-= speed;
Bullety + = speed;
Break
Direction right under the bullet case dir_down_right:bullety + = speed;
Bulletx + = speed;
Break }//Border processing if (bullety > Mysurfaceview.screenh | | bullety <= -40 | | bulletx > MYSURFACEVIEW.SCREENW | | bulletx
<= -40) {isdead = true;
} break;
}
}
}
New boss class below
Create boss
public class Boss {//boss's blood volume public int hp = 50;
Boss of the picture resources private Bitmap Bmpboss;
Boss coordinates public int x, y;
Boss every frame of the wide-high public int framew, Frameh;
Boss Current frame subscript private int frameindex;
Boss Movement Speed Private int speed = 5;
Boss's trajectory//a certain amount of time will move toward the bottom of the screen, and launch a large range of bullets, (whether crazy)//normal state, bullets vertically downward movement private Boolean iscrazy;
Enter the state of madness state time interval private int crazytime = 200;
counter private int count;
Boss's constructor public boss (Bitmap bmpboss) {this.bmpboss = Bmpboss;
Framew = Bmpboss.getwidth ()/10;
Frameh = Bmpboss.getheight ();
Boss of the X coordinate center x = MYSURFACEVIEW.SCREENW/2-FRAMEW/2;
y = 0;
//boss the draw 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 logical public void logic () {///looping the frames to form an animated frameindex++;
if (FrameIndex >=) {frameindex = 0;
//No crazy status 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;
}//Mad state} else {speed-= 1; When the boss returns, create a large number of bullets if (Speed = 0) {//Add 8 Direction Bullets 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) {//restore normal state Iscrazy = false;
Speed = 5;
}}//Judge Collision (Boss is hit by lead 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 main interface Bullets and boss
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 constant public static final int game_menu = 0;//Game menu public static final int gameing = 1;//game public static final I NT Game_win = 2;//game victory 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;//bo
OS 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 Private Bit Map bmpgamelost;//Game Failure backKing Private Bitmap bmpplayer;//game lead airplane private Bitmap bmpplayerhp;//lead airplane blood volume private Bitmap bmpmenu;//menu background public static Bi
TMap bmpbullet;//Bullets public static Bitmap bmpenemybullet;//enemy bullets public static Bitmap Bmpbossbullet;//boss bullets//Declaring a Menu object
Private Gamemenu Gamemenu;
Declare a scrolling game background object private GAMEBG backGround;
Declare actor object private player player;
Declare an enemy aircraft container private vector<enemy> vcenemy;
Time of each generation of enemy aircraft (milliseconds) private int createenemytime = 50; private int count;//counter//enemy array: 1 and 2 indicate the type of enemy aircraft,-1 means that each dimension of the Boss//two-dimensional array is a set of monsters private int enemyarray[][] = {1, 2}, {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 subscript private int enemyarrayindex of the one-dimensional array is currently removed;
Whether the presence of the boss identification bit private Boolean isboss;
Random library, for the creation of the enemy aircraft given immediately coordinates private Random Random;
Enemy Bullets container private vector<bullet> vcbullet;
Add bullets to the counter private int countenemybullet;
Lead bullet container private vector<bullet> vcbulletplayer; Add a bullet to the counter
private int countplayerbullet;
Explosion effect container private vector<boom> vcboom;
Statement boss Private boss boss;
Boss of the bullet container public static vector<bullet> Vcbulletboss;
/** * 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);
Setfocusableintouchmode (TRUE);
Sets the background illuminated steady this.setkeepscreenon (true); /** * Surfaceview View creation, response to this function/@Override public void surfacecreated (Surfaceholder holder) {screenw = This.getwid
Th ();
Screenh = This.getheight ();
Initgame ();
Flag = true;
instance thread th = new thread (this);
Start thread Th.start ();
}/* Custom game initialization function */private void Initgame () {//place game into the background to re-enter the game, the game is reset! Games are reset if the game status is in the menu (gamestate = = Game_menu) {//load game Resource Bmpbackground = Bitmapfactory.decoderesource (res, r.drawab
Le.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> ();
Enemy Bullet Container Instance Vcbullet = new vector<bullet> ();
Lead Bullet Container Instance Vcbulletplayer = new vector<bullet> ();
Menu class Instance Gamemenu = New Gamemenu (Bmpmenu, Bmpbutton, bmpbuttonpress);
Instance Game Background BackGround = new GAMEBG (bmpbackground);
Instance lead player = new player (Bmpplayer, BMPPLAYERHP);
Instance enemy Vessel Vcenemy = new vector<enemy> ();
Instance Random Library random = new Random ();
---boss related//Instance boss Object boss = new boss (Bmpenemyboos);
Example 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 vary according to game state to draw 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) {//enemy draw for (int i = 0; i < vcenemy.size ();i++) {Vcenemy.elementat (i). Draw (canvas, paint);
//Enemy bullets drawn for (int i = 0; i < vcbullet.size (); i++) {vcbullet.elementat (i); Draw (canvas, paint);
The drawing Boss.draw (canvas, paint) of {//boos)} else {;
Boss Bullets logic for (int i = 0; i < vcbulletboss.size (); i++) {vcbulletboss.elementat (i); Draw (canvas, paint); }///process lead bullets draw for (int i = 0; i < vcbulletplayer.size (); i++) {Vcbulletplayer.elementat (i). Draw (Canvas, p
aint);
///Explosion effect draw 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 {Exception e) {//Todo:handle Exception} finally {if (canvas!= null) sfh.unlockcanvasandpost (ca
Nvas); /** * Touchscreen Event Monitor/@Override public boolean ontouchevent (Motionevent event) {//Touch Screen listener event function different listening switch (gamestate) {case Game_menu://Menu Touch Event processing gamemenu.ontouchevent (event) according to game status;
Break
Case Gameing:break;
Case Game_pause:break;
Case Game_win:break;
Case Game_lost:break;
return true; /** * Button press Event Monitor/@Override public boolean onKeyDown (int keycode, keyevent event) {//Handle back button if (keycode = Keyevent.keycode_back) {////game wins, failures, and Defaults return menu if (gamestate = = Gameing | | gamestate = = Game_win | | gamestate = = game_l
OST) {gamestate = Game_menu;
Boss State is set to not appear Isboss = false;
Reset game initgame ();
Reset Monster Appearance enemyarrayindex = 0;
else if (gamestate = = Game_menu) {//Current game status in menu interface, default return key to Exit game MainActivity.instance.finish ();
System.exit (0);
//Indicates that the key has been processed and is no longer handed over to the system for processing,//Thus avoiding the game being cut into the background return true;
The//Key listener event function differs according to game state to monitor switch (gamestate) {case game_menu:break;
Case gameing://main character key presses the event Player.onkeydown (KeyCode, event);
Break
Case Game_pause:break;
Case Game_win:break; Case Game_losT:break;
Return Super.onkeydown (KeyCode, event); /** * Button Lift Event Monitor/@Override public boolean onKeyUp (int keycode, keyevent event) {//Process back return key if (keycode = Ke Yevent.keycode_back) {////game wins, failures, and Defaults return menu if (gamestate = = Gameing | | gamestate = = Game_win | | gamestate = = Game_los
T) {gamestate = Game_menu;
//Indicates that the key has been processed and is no longer handed over to the system for processing,//Thus avoiding the game being cut into the background return true;
The//Key listener event function differs according to game state to monitor switch (gamestate) {case game_menu:break;
Case gameing://Button lift event player.onkeyup (keycode);
Break
Case Game_pause:break;
Case Game_win:break;
Case Game_lost:break;
Return Super.onkeydown (KeyCode, event);
/** * Game logic/private void logic () {//logical handling different processing according to game state different switch (gamestate) {case game_menu:break;
Case gameing://Background logic background.logic ();
Lead 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 is constantly adding enemy aircraft, then the enemy IsdeadJudgment,//If Dead then removed from the container, the container played an optimal role; if (En.isdead) {vcenemy.removeelementat (i);
else {en.logic ();
}//Generate enemy count++; if (count% Createenemytime = 0) {for (int i = 0; i < enemyarray[enemyarrayindex].length; i++) {//Fly if en
Emyarray[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 + y));
}//Here to determine if the next group is the last group (Boss) if (Enemyarrayindex = = enemyarray.length-1) {Isboss = true;
else {enemyarrayindex++;
(int i = 0; i < vcenemy.size (); i++) {if (Player.iscollsionwith (Vcenemy.elementat (i))}}///Handle collision for the enemy //Collision, lead blood volume-1 PLAYER.SETPLAYERHP (PLAYER.GETPLAYERHP ()-1);
When the protagonist's blood volume is less than 0, determine the game failure if (PLAYER.GETPLAYERHP () <=-1) {gamestate = Game_lost;
Add an enemy bullet countenemybullet++ every 2 seconds;
if (countenemybullet% = = 0) {for (int i = 0; i < vcenemy.size (); i++) {Enemy en = Vcenemy.elementat (i);
Different types of enemy aircraft different bullets run trajectory 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 +, EN.Y +, bullettype));
}///Handling Enemy bullets logic for (int i = 0; i < vcbullet.size (); i++) {Bullet b = vcbullet.elementat (i);
if (b.isdead) {vcbullet.removeelement (b);
else {b.logic (); }///handling enemy bullets with protagonists for (int i = 0; i < vcbullet.size (); i++) {if Player.iscollsionwith (Vcbullet.elementat (i)) ) {//Collision, lead blood volume-1 player.seTPLAYERHP (PLAYER.GETPLAYERHP ()-1);
When the protagonist's blood volume is less than 0, determine the game failure if (PLAYER.GETPLAYERHP () <=-1) {gamestate = Game_lost; (int i = 0; i < vcbulletplayer.size (); i++) {//Take out each element of the lead 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 lead the bullet traversal to judge if (Vcenemy.elementat (j). Iscollsionw
ITH (Blplayer)) {Vcboom.add (new Boom (Bmpboom, Vcenemy.elementat (j). x, Vcenemy.elementat (j). Y, 7));
}}} else {//boss related logic//Add one lead per 0.5 second bullet boss.logic (); if (countplayerbullet% = = 0) {//boss The usual bullets before the madness vcbulletboss.add (New Bullet (Bmpbossbullet, boss.x +, Boss.y
+, bullet.bullet_fly));
//boss bullets 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 to the protagonist's collision for (int i = 0; i < vcbulletboss.size (); i++) {iF (Player.iscollsionwith (Vcbulletboss.elementat (i))) {//Collision, lead blood volume-1 PLAYER.SETPLAYERHP (PLAYER.GETPLAYERHP ()-1);
When the protagonist's blood volume is less than 0, determine the game failure if (PLAYER.GETPLAYERHP () <=-1) {gamestate = Game_lost; }}//boss was hit by a lead bullet and produced an explosion effect for (int i = 0; i < vcbulletplayer.size (); i++) {Bullet b = Vcbulletplayer.elem
Entat (i);
if (Boss.iscollsionwith (b)) {if (boss.hp <= 0) {//game victory gamestate = Game_win;
else {//delete the bullet of this collision in time, prevent duplicate to judge this bullet and boss collision, B.isdead = true;
Boss Blood volume minus 1 boss.sethp (boss.hp-1);
Add three boss on the boss explosion effect Vcboom.add (new Boom (Bmpboosboom, boss.x +, Boss.y + 30, 5));
Vcboom.add (New Boom (Bmpboosboom, boss.x +, Boss.y + 40, 5));
Vcboom.add (New Boom (Bmpboosboom, boss.x +, Boss.y + 50, 5));
Add one lead per 1 second bullet countplayerbullet++; if (countplayerbullet% = = 0) {vcbulletplayer.add (new Bullet) (Bmpbullet, player.x +, player.y-20, bullet.bullet_
PLAYER)); }//Process lead bullets 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) {///play completed in the calmly remove 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 () {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, t width, int height) {}/** * Surfaceview View dies, respond to this function/@Override public void SurfacedestroyeD (surfaceholder holder) {flag = false;
}
}
and victories and failures pages
Last attached 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);
Displays the custom Surfaceview view
Setcontentview (this);
@Override
protected void OnDestroy () {
//TODO auto-generated method stub
mysurfaceview.gamestate = Mysurfaceview.game_menu;
Super.ondestroy ();
}
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.