Preface
As an Android developer, I often want to develop a small game to entertain you, today, how I write a simple "aircraft war".
Experience Address: Http://www.wandoujia.com/apps/edu.njupt.zhb.planegame
Game analysis
As you know, the main "characters" in aircraft wars are:
1. Player aircraft
2. Enemy aircraft
3. Bullets sent by player aircraft
4. Bullets sent by enemy boss aircraft
What we need to control is :
1. Draw the characters in the screen
2. Control the logic of the role, for example: collision detection between enemy aircraft and our aircraft, collision detection between the bullets fired by our aircraft and enemy aircraft, and direct collision detection of bullets fired by enemy boss planes and our aircraft.
Resources:
To complete a game, but also to have the load of resources, such as airplanes, bullets and other pictures loading, such as the loading of sound effects.
Drawing of the game background
In fact, it is a picture, this picture can be end-to-end, that is, "scroll", the principle is carmack scroll algorithm principle.
The
following analysis code area :
In fact, aside from the Android platform, any platform, to do such a game, all need these logic. For Android platforms, let's take a look at the Surfaceview drawing frame. Direct Sticker Code:
Package edu.njupt.zhb.game.view;/** * * @author Zheng Haibo * @webset: Http://www.mobctrl.net * @android Development Alliance QQ Group: 272209595 */public class Planeview extends Surfaceview implements Callback, Runnable {private Surfaceholder surfaceholder;private L Ong Sleep_time = 16;//Draw period private int screenheight;private int screenwidth;private Thread thread;private Canvas CANVAS;PR Ivate Paint paint;private gamescreen currentscreen;private int level = 0;private int backgroundspeed = 1;public PlaneView ( Context context) {super (context); System.out.println ("Debug:planeview ()"); Surfaceholder = This.getholder (); Surfaceholder.addcallback (this); Surfaceholder.setformat (pixelformat.translucent);p aint = new paint ();p Aint.setantialias (True);p Aint.setdither ( true);} @Overridepublic void surfacecreated (Surfaceholder holder) {System.out.println ("debug:surfacecreated"); Setzorderontop (false); isgameover = False;if (ispause) {return;} ScreenHeight = This.getheight (); screenwidth = This.getwidth (); Initplane (); thread = new Thread(this); Thread.Start ();} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {System.out.println ("debug: Surfacechanged ");} @Overridepublic void surfacedestroyed (Surfaceholder holder) {System.out.println ("debug:surfacedestroyed"); if (lift > 0) {planeviewcallback.ongamepause ();} Ispause = true;} @Overridepublic void Run () {while (!isgameover) {//controls the drawing period if (Ispause) {try {thread.sleep (sleep_time);} catch ( Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Continue;} Long starttime = System.currenttimemillis ();d rawscreen (), Long time = System.currenttimemillis ()-Starttime;if (Time < Sleep_time) {try {thread.sleep (sleep_time-time);} catch (Interruptedexception e) {e.printstacktrace ();}}}} /** * plot scene */private void Drawscreen () {canvas = Surfaceholder.lockcanvas (); if (null = = Canvas) {return;} Clear Canvas.drawcolor (Color.White, PorterDuff.Mode.CLEAR);p Aint.setalpha (255); Gamelogic (); Gamedraw (); if (null! = Canvas) {SurfaceholdEr.unlockcanvasandpost (canvas);}} /** * game logic */private void Gamelogic () {//to do control game logic ...} private void Gamedraw () {//First draw game background drawbackground (Backgroundspeed * frameseq); if (Currentscreen = = Gamescreen.normal) { Synchronized (planes) {drawplanes ();d rawbullets ();d rawmasterplane ();}} else if (Currentscreen = = Gamescreen.boss) {drawbullets ();d rawbossplane ();d rawbossbullets ();d rawmasterplane ();}} private void Drawbossplane () {if (null! = Bossplane) {if (bossplane.isclicked ()) {//Draw blast Imgbossplane.onblastdraw (c Anvas, Paint), if (Bossplane.isblastframeend ()) {bossplane.setclicked (false);}} Bossplane.ondraw (canvas, Paint);}} @Overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method Stubsuper.ondraw (canvas); System.out.println ("Debug:ondraw");} @Overrideprotected void Ondetachedfromwindow () {Super.ondetachedfromwindow (); System.out.println ("Debug:ondetachedfromwindow ..."); ispause = False;isgameover = true;//release resource for (Planeres plane: Planesres) {Plane.getbitmap (). Recycle ();} For (Bulletres bulletres:bulletsres) {bulletres.getbitmap (). Recycle ();}} /*** user Interaction */@Overridepublic Boolean ontouchevent (motionevent e) {int x = (int) e.getx (), int y = (int) e.gety (), switch (e.ge Taction ()) {Case MotionEvent.ACTION_DOWN:if (masterplane.iscontainpoint (x, y)) {ismove = true;} Break;case MotionEvent.ACTION_MOVE:if (Ismove) {synchronized (Masterplane) {masterplane.updateposition (x, y);// Controls the movement of the player aircraft}}break;case MotionEvent.ACTION_UP:isMove = false;break;} return true;}}
For the background of the drawing, is actually a loop to draw a picture: The game's drawing logic:
private void Drawbackground (int yoffset) {yoffset%= screenheight;if (Yoffset = = 0) {canvas.drawbitmap (backgroundbmp, 0, 0 , paint);} else {Canvas.drawbitmap (backgroundbmp, New rect (0,screenheight-yoffset, ScreenWidth, ScreenHeight), new rect (0, 0, ScreenWidth, Yoffset + 1), paint); Canvas.drawbitmap (Backgroundbmp, New Rect (0, 0, screenwidth,screenheight-yoffset), NE W Rect (0, Yoffset, screenwidth,screenheight), paint);}}
Then we just need to add the Planeview to a layout:
Such as:
Planeview = new Planeview (this);p the Laneview.setplaneviewcallback (this);p laneview.setgameovercallback (this); Planeview.ismediaopen = This.ismediaopen; Layoutparams LP = new Layoutparams (layoutparams.match_parent,layoutparams.match_parent); Rl_plane.addView (PlaneView , LP);
To be continued .....