"The zero-start of Android game Programming" 17. Game Development Basics (Game adaptation screen of the brief and role, let the protagonist move up)

Source: Internet
Author: User

1. Description and function of game screen

With the growing number of mobile phones installed on the market, Android phones with various resolutions and various screen sizes have emerged. In order to ensure that a game or a software can be displayed on all Android phones normally, the usual appropriate screen practices are: the use of screen height, bitmap width to set some of the location of the game elements, the font of the best way to use font graphics, so that the text will not be different from the phone resolution, After all, the size of the picture is constant.

2. Let the protagonist of the game move up
The example demonstrates the dynamic effect of a picture that consists of a multi-row, multi-column action frame.

New project, game frame for Surfaceview frame, prepare picture robot.png as follows:

Modify the Mysurfaceview class with the following code:

 Packagecom.example.ex4_13;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;Importandroid.view.KeyEvent;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceHolder.Callback;ImportAndroid.view.SurfaceView; Public classMysurfaceviewextendsSurfaceviewImplementscallback,runnable {PrivateSurfaceholder SFH; PrivateCanvas Canvas; PrivatePaint paint; Private BooleanFlag; PrivateThread th; //Robot Bitmap    PrivateBitmap Bmprobot; //the direction constants of the robot    Private Final intDir_left =0; Private Final intDir_right=1; //the current direction of the robot    Private intDIR =Dir_right; //Action Frame subscript    Private intCurrentframe; //x, y position of the robot    Private introbot_x,robot_y; //handle key card phenomenon    Private BooleanisUp, Isdown, Isleft, isright;  PublicMysurfaceview (Context context) {Super(context); SFH= This. Getholder (); Sfh.addcallback ( This); Paint=NewPaint ();        Paint.setcolor (Color.White); Paint.setantialias (true); Setfocusable (true); Bmprobot= Bitmapfactory.decoderesource ( This. Getresources (), R.drawable.robot); }    /*** Surfaceview View creation, responding to this function*/@Override Public voidsurfacecreated (Surfaceholder holder) {flag=true; //instance Threadth =NewThread ( This); //Start ThreadTh.start (); }        /*** Surfaceview view state has changed, response to this function*/@Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) {            }    /*** Response to this function when the Surfaceview view dies*/@Override Public voidsurfacedestroyed (Surfaceholder holder) {flag=false; }    /*** Draw function*/    Private voidMydraw () {Try{Canvas=Sfh.lockcanvas (); if(canvas!=NULL) {canvas.drawcolor (color.black);            Drawframe (Currentframe,canvas,paint); }        } Catch(Exception e) {//Todo:handle Exception}finally{            if(canvas!=NULL) {sfh.unlockcanvasandpost (canvas); }        }    }    /**     *      * @paramCurrentframe Drawing Frames *@paramFramew * High per frame *@paramFrameh * High per frame *@paramCanvas * Canvas instance *@paramPaint * Brush instance*/    Private voidDrawframe (intCurrentframe,canvas canvas,paint Paint) {        //width of each frame        intFramew = Bmprobot.getwidth ()/6; //high in each frame        intFrameh = Bmprobot.getheight ()/2; //get the number of columns in a bitmap        intcol = bmprobot.getwidth ()/Framew; //gets the x-coordinate of the current frame relative to the bitmap        intx = currentframe% col *Framew; //Gets the y-coordinate of the current frame relative to the bitmap        inty = Currentframe/col *Frameh;        Canvas.save (); //set a wide-height visual area equal to the size of each frame of the robotCanvas.cliprect (robot_x, robot_y, robot_x + bmprobot.getwidth ()/6, robot_y + bmprobot.getheight ()/2); if(dir = = dir_left) {//If you are moving to the left//Mirror operation-invert-changes the orientation of the robot animationCanvas.scale ( -1, 1, robot_x-x + bmprobot.getwidth ()/2, robot_y-y + bmprobot.getheight ()/2); } canvas.drawbitmap (Bmprobot, robot_x-X, Robot_y-y, paint);    Canvas.restore (); }    /*** Game Logic*/    Private voidlogic () {//control Robot Displacement Direction        if(isUp) {robot_y-= 5; }        if(isdown) {robot_y+ = 5; }        if(isleft) {robot_x-= 5; }        if(isright) {robot_x+ = 5; }        //loop control of the number of action frames, allowing its action frames to repeatedly playcurrentframe++; if(Currentframe >= 12) {Currentframe= 0; }} @Override Public voidrun () { while(flag) {LongStart =System.currenttimemillis ();            Mydraw ();            Logic (); LongEnd =System.currenttimemillis (); Try {                if(End-start < 50) {Thread.Sleep (-(End-start)); }            } Catch(interruptedexception e) {e.printstacktrace (); }        }    }    /*** Key Event monitoring*/@Override Public BooleanOnKeyDown (intKeyCode, 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; Dir=Dir_left; }        if(KeyCode = =keyevent.keycode_dpad_right) {Isright=true; Dir=Dir_right; }        return Super. OnKeyDown (KeyCode, event); } @Override Public BooleanOnKeyUp (intKeyCode, 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; }        return Super. OnKeyUp (KeyCode, event); }        }

"The zero-start of Android game Programming" 17. Game Development Basics (Game adaptation screen of the brief and role, let the protagonist move up)

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.