Android: How to use a day to write "aircraft war" this game! (No frame-surfaceview drawing)

Source: Internet
Author: User

Preface
As an Android developer, I often want to develop a small game to entertain you, today, how I write a simple "aircraft war".

Body Test Address: Http://www.wandoujia.com/apps/edu.njupt.zhb.planegame

Source:Https://github.com/nuptboyzhb/newplanegame

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 are :
1. Draw the characters in the screen
2. Control the logic of the role. For example, a collision between an enemy aircraft and our aircraft, a collision between a bullet from our aircraft and an enemy aircraft, an enemy boss plane firing a bullet and a direct collision with our plane, and so on.
Resources:
To complete a game, but also to have the load of resources. such as airplanes, bullets and other pictures, such as loading, sound loading.
Drawing of the game background
In fact, it is a picture that can be connected to one another. Also is the "scroll", the principle is carmack scroll algorithm principle.
The following analysis code area :

In fact, throw away the Android platform, no matter what platform, to do such a game. All need these logic. For Android platforms. Let's take a look at the drawing frame of the Surfaceview.

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;}}

The drawing for the background. The fact is that the loop draws a picture: the drawing logic of this game:

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 and we can:

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);

Not to be continued.

。。



Android: How to use a day to write "aircraft war" this game! (No frame-surfaceview drawing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.