Home>
First look at the effect map:
analysis:Different enemy aircraft movement logic and drawing according to enemy aircraft type
/**
* Enemy aircraft
*
* @author liuml
* @time May 31, 2016 4:14:59 PM
* /
public class enemy {
//Type of enemy aircraft
public int type;
//flies
public static final int type_fly=1;
//ducks (moving from left to right)
public static final int type_duckl=2;
//ducks (moving from right to left)
public static final int type_duckr=3;
//enemy aircraft image resources
public bitmap bmpenemy;
//enemy coordinates
public int x, y;
//The width and height of each frame of the enemy aircraft
public int framew, frameh;
//The current frame index of the enemy aircraft
private int frameindex;
//enemy's movement speed
private int speed ;;
//determine whether the enemy plane has been screened
public boolean isdead;
//Constructor of enemy aircraft
public enemy (bitmap bmpenemy, int enemytype, int x, int y) {
this.bmpenemy=bmpenemy;
framew=bmpenemy.getwidth ()/10;
frameh=bmpenemy.getheight ();
this.type=enemytype;
this.x=x;
this.y=y;
//Different types of enemy aircraft have different HP
switch (type) {
//flies
case type_fly:
speed=25;
break;
//duck
case type_duckl:
speed=3;
break;
case type_duckr:
speed=3;
break;
}
}
//enemy drawing function
public void draw (canvas canvas, paint paint) {
canvas.save ();
canvas.cliprect (x, y, x + framew, y + frameh);
canvas.drawbitmap (bmpenemy, x-frameindex * framew, y, paint);
canvas.restore ();
}
//enemy logic ai
public void logic () {
//Continuously loop the frames to form an animation
frameindex ++;
if (frameindex>= 10) {
frameindex=0;
}
//different types of enemy aircraft have different ai logic
switch (type) {
case type_fly:
if (isdead == false) {
//deceleration appears,Speed up
speed-= 1;
y +=speed;
if (y<= -200) {
isdead=true;
}
}
break;
case type_duckl:
if (isdead == false) {
//diagonal lower right corner movement
x +=speed/2;
y +=speed;
if (x>mysurfaceview.screenw) {
isdead=true;
}
}
break;
case type_duckr:
if (isdead == false) {
//oblique lower left corner movement
x-= speed/2;
y +=speed;
if (x<-50) {
isdead=true;
}
}
break;
}
}
}
Generate enemy planes 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 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;
/**
* 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);
//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);
}
} else {
//boss draws
}
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 ++;
}
}
}
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;
}
}
Impact checkingModify the player class
package com.gsf;
import android.graphics.bitmap;
import android.graphics.canvas;
import android.graphics.paint;
import android.view.keyevent;
public class player {
private int playerhp=3;
private bitmap bmpplayerhp;
//Protagonist coordinates and bitmap
private int x, y;
private bitmap bmpplayer;
//main character moving speed
private int speed=5;
//main character moving logo
private boolean isup, isdown, isleft, isright;
//The main character's constructor
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 ();
}
//Protagonist game drawing method
public void draw (canvas canvas, paint paint) {
//draw the protagonist
canvas.drawbitmap (bmpplayer, x, y, paint);
//Plot blood
for (int i=0;i<playerhp;i ++) {
canvas.drawbitmap (bmpplayerhp, i * bmpplayerhp.getwidth (), mysurfaceview.screenh-bmpplayerhp.getheight (), paint);
}
}
/**
* Key event monitoring
* /
public void onkeydown (int keycode, keyevent event) {
if (keycode == keyevent.keycode_dpad_up) {
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;
}
//determine the screen x boundary
if (x + bmpplayer.getwidth ()>= mysurfaceview.screenw) {
x=mysurfaceview.screenw-bmpplayer.getwidth ();
} else if (x<= 0) {
x=0;
}
//determine the y boundary of the screen
if (y + bmpplayer.getheight ()>= mysurfaceview.screenh) {
y=mysurfaceview.screenh-bmpplayer.getheight ();
} else if (y<= 0) {
y=0;
}
}
//Set the main character's health
public void setplayerhp (int hp) {
this.playerhp=hp;
}
//Get the main character's health
public int getplayerhp () {
return playerhp;
}
//Judgment collision (the enemy aircraft collides with the protagonist bullet)
public boolean iscollsionwith (enemy bullet) {
int x2=bullet.x;
int y2=bullet.y;
int w2=bullet.framew;
int h2=bullet.frameh;
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;
}
//Collision,Let it die
//isdead=true;
return true;
}
}
Add collision logic to mysurface
/**
* 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 ++;
}
}
//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;
}
}
}
}
break;
//timer
private int nocollisioncount=0;
//because of invincible time
private int nocollisiontime=60;
//flag for collision
private boolean iscollision;
//Judging collision (protagonist and enemy aircraft)
public boolean iscollsionwith (enemy en) {
//Whether it is invincible time
if (iscollision == false) {
int x2=en.x;
int y2=en.y;
int w2=en.framew;
int h2=en.frameh;
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;
}
//Into the invincible state after collision
iscollision=true;
return true;
//in an invincible state,Ignore collision
} else {
return false;
}
}
Modify logic method
/**
* Game logic
* /
public void logic () {
if (isup) {
y-= speed;
}
if (isdown) {
y +=speed;
}
if (isleft) {
x-= speed;
}
if (isright) {
x +=speed;
}
//determine the screen x boundary
if (x + bmpplayer.getwidth ()>= mysurfaceview.screenw) {
x=mysurfaceview.screenw-bmpplayer.getwidth ();
} else if (x<= 0) {
x=0;
}
//determine the y boundary of the screen
if (y + bmpplayer.getheight ()>= mysurfaceview.screenh) {
y=mysurfaceview.screenh-bmpplayer.getheight ();
} else if (y<= 0) {
y=0;
}
//handle invincibility
if (iscollision) {
//timer starts
nocollisioncount ++;
if (nocollisioncount>= nocollisiontime) {
//After invincible time,Contact invincible status and initialize counter
iscollision=false;
nocollisioncount=0;
}
}
}
Modify the drawing of the protagonistplayer class
//Protagonist game drawing method
public void draw (canvas canvas, paint paint) {
//draw the protagonist
//When invincible time,Make the protagonist blink
if (iscollision) {
//every 2 game loops,Draw the main character once
if (nocollisioncount%2 == 0) {
canvas.drawbitmap (bmpplayer, x, y, paint);
}
} else {
canvas.drawbitmap (bmpplayer, x, y, paint);
}
//Plot blood
for (int i=0;i<playerhp;i ++) {
canvas.drawbitmap (bmpplayerhp, i * bmpplayerhp.getwidth (), mysurfaceview.screenh-bmpplayerhp.getheight (), paint);
}
}
Related articles
- Ultimate Boss Fighting Game Android Fighting Game
- Bullet Generation and Collision and Explosion Effect for Android Combat Airplane Game (5)
- 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)
- Menu page design of Android combat aircraft game (1)
- Android implements simple operations of paper planes
Trends