Home>
The goal of this article is to control the small aircraft to move left and right, avoid bullets, and hit bosses.
This section implementsStart menu interface
1. First, copy the resource files
2. Divide the game state
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;
Define five states
After defining the method, in the drawing method, press the entity key, lift the method,Touch screen monitoring,Logical method,switch
//Add this in those methods
switch (gamestate) {
case game_menu:
break;
case gameing:
break;
case game_win:
break;
case game_lost:
break;
case game_pause:
break;
default:
break;
}
Let's declare some more
//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
Initialization game
/**
* 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 gamemenu
Menu
Includes initial draw button and background image
package com.gsf;
import android.graphics.bitmap;
import android.graphics.canvas;
import android.graphics.paint;
import android.view.motionevent;
/**
*
* @author liuml
* @time May 27, 2016 5:43:34 PM
* /
public class gamemenu {
//menu background image
private bitmap bmpmenu;
//button image resource (pressed and not clicked)
private bitmap bmpbutton, bmpbuttonpress;
//button coordinates
private int btnx, btny;
//Whether the button is pressed
private boolean ispress;
//menu initialization
public gamemenu (bitmap bmpmenu, bitmap bmpbutton, bitmap bmpbuttonpress) {
this.bmpmenu=bmpmenu;
this.bmpbutton=bmpbutton;
this.bmpbuttonpress=bmpbuttonpress;
//center x, y immediately at the bottom of the screen
btnx=mysurfaceview.screenw/2-bmpbutton.getwidth ()/2;
btny=mysurfaceview.screenh-bmpbutton.getheight ();
ispress=false;
}
public void draw (canvas canvas, paint paint) {
//draw the menu background
canvas.drawbitmap (bmpmenu, 0, 0, paint);
if (ispress) {
canvas.drawbitmap (bmpbuttonpress, btnx, btny, paint);
} else {
canvas.drawbitmap (bmpbutton, btnx, btny, paint);
}
}
public void ontouchevent (motionevent event) {
//Get the current touch position
int pointx=(int) event.getx ();
int pointyy=(int) event.gety ();
//when the user is pressing and moving
if (event.getaction () == motionevent.action_down
|| event.getaction () == motionevent.action_move) {
//determine whether the user clicked the button
if (pointx>btnx&&pointx<btnx + bmpbutton.getwidth ()) {
if (pointyy>btny&&pointyy<btny + bmpbutton.getheight ()) {
ispress=true;
} else {
ispress=false;
}
} else {
ispress=false;
}
//when used for lifting action
} else if (event.getaction () == motionevent.action_up) {
//determine whether to click the button when lifting,Prevent users from moving elsewhere
if (pointx>btnx&&pointx<btnx + bmpbutton.getwidth ()) {
if (pointyy>btny&&pointyy<btny + bmpbutton.getheight ()) {
ispress=false;//Reset after lifting Restore the button state to not pressed
//Change the current game state to start the game
mysurfaceview.gamestate=mysurfaceview.gameing;
}
}
}
}
}
</p>
<p>
Then use gamemenu in mysurfaceview using the menu class
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;
//
private gamemenu gamemenu;
/**
* 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);
}
/**
* 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:
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:
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:
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;
}
}
Effect picture:
Related articles
- Ultimate Boss Fighting Game Android Fighting Game
- Bullet Generation and Collision and Explosion Effect for Android Combat Airplane Game (5)
- Realization of the monster (enemy) class of Android combat aircraft game (4)
- Background image of the infinite loop of Android combat aircraft game (2)
- The main character and related elements of the Android combat aircraft game (3)
- Android implements simple operations of paper planes
Trends