Android_ (game) Jerk off 03: Control player aircraft

Source: Internet
Author: User
Tags gety truncated

Android_ (game) Jerk off 01: Preface Portal

Android_ (game) Jerk off 02: Game Background Scrolling portal

Android_ (game) Jerk off 03: Control player Aircraft portal

Control player aircraft effects

package com.example.administrator.myapplication;import android.content.Context; Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.rect;import Android.view.motionevent;import Android.view.surfaceholder;import Android.view.surfaceview;import Android.view.view;import Android.view.windowmanager;import java.util.arraylist;import java.util.list;/** * Created by Administrator on 2018/8/ 9. */public Class Dafeijigameview extends Surfaceview implements Surfaceholder.callback,runnable,android.view.view. ontouchlistener{Private Bitmap my;//own private Bitmap baozha;//explosion private Bitmap bg;//background private Bitmap Diren    ;//enemy private Bitmap zidan;//bullet private Bitmap erjihuancun;//level two cache private WindowManager windowmanager;//get interface length and width high  private int display_w;  The wide private int display_h of the interface; High private List  of the interface<Gameimage> Gameimage = new ArrayList ();        Public Dafeijigameview (Context context) {super (context);        Getholder (). Addcallback (this);    Event Registration This.setontouchlistener (this);        } private void Init () {//Load photo my= Bitmapfactory.decoderesource (getresources (), r.drawable.my);        Baozha= Bitmapfactory.decoderesource (Getresources (), R.drawable.baozha);        Bg= Bitmapfactory.decoderesource (Getresources (), r.drawable.bg);        Diren= Bitmapfactory.decoderesource (Getresources (), R.drawable.diren);        Zidan= Bitmapfactory.decoderesource (Getresources (), R.drawable.zidan);        Erjihuancun=bitmap.createbitmap (Display_w,display_h, Bitmap.Config.ARGB_8888);    Gameimage.add (New Beijingimage (BG));    First add background photo gameimage.add (my);    } feijiimage Selectfeiji; @Override public boolean OnTouch (View V, motionevent event) {//hand near screen-generated event if (Event.getaction () ==motionev Ent. Action_down) {for (Gameimage Game:gamEimage) {if (game instanceof feijiimage) {feijiimage Feiji = (feijiimage) game; Select the aircraft if the condition is true (Feiji.getx ()<Event. GetX () && feiji.gety () <event.gety () && FEIJI.G EtX () +feiji.getwidth ( )>Event.getx () && feiji.gety () +feiji.getheigth () >event.gety ()) {                    Selectfeiji=feiji;                    }else{Selectfeiji=null;                } break; }}}else if (Event.getaction () ==motionevent.action_move) {//Move mouse if (selectfeiji!=                NULL) {selectfeiji.setx (int) event.getx ()-selectfeiji.getwidth ()/2);            Selectfeiji.sety ((int) event.gety ()-selectfeiji.getheigth ()/2);        }}else if (Event.getaction () ==motionevent.action_up) {//release mouse selectfeiji=null;    } return true;        } Private interface gameimage{public Bitmap getbitmap ();        public int GetX ();    public int GetY ();        } Private class Feijiimage implements gameimage{private Bitmap my;        private int x;        private int y;        private int width; private int HeigTh        private int getwidth () {return width;        } private int Getheigth () {return heigth; } Private List<Bitmap>bitmaps = new ArrayList<Bitmap>();            Private Feijiimage (Bitmap my) {this.my=my;            Bitmaps.add (Bitmap.createbitmap (My,0,0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4), 0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4) *2,0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4) *3,0,my.getwidth ()/4,my.getheight ());            Get the fighter's high and wide Width=my.getwidth ()/4;            Heigth=my.getheight ();            X= (Display_w-my.getwidth ()/4)/2;        Y=display_h-my.getheight ()-30;        } private int index=0;        private int num = 0;            @Override public Bitmap Getbitmap () {Bitmap Bitmap = bitmaps.get (index);               Control the frequency of switching four aircraft if (num = = ten) {index++;               if (index = = bitmaps.size ()) {index=0;           } num=0; } num++;        return bitmap;        } @Override public int GetX () {return x;        } @Override public int GetY () {return y;        } public void sety (int y) {this.y=y;        } public void SetX (int x) {this.x=x;        }}//responsible for background photo processing private class Beijingimage implements gameimage{private Bitmap BG;            Private Beijingimage (Bitmap BG) {THIS.BG=BG;        Newbitmap = Bitmap.createbitmap (Display_w,display_h, Bitmap.Config.ARGB_8888);        } private Bitmap newbitmap = null;        private int height = 0;            Public Bitmap Getbitmap () {Paint p = new Paint ();            Canvas canvas = new canvas (NEWBITMAP); Canvas.drawbitmap (BG, New Rect (0,0,bg.getwidth (), Bg.getheight ()), New Rect (0,height,            display_w,display_h+height), p); Canvas.drawbitmap (BG, new Rect (0,0,bg.getwidTh (), Bg.getheight ()), New Rect (0,-display_h+height,display_w,height), p);            height++;            if (height==display_h) {height=0;        } return Newbitmap;        } public int GetX () {return 0;        } public int GetY () {return 0;    }} private Boolean state = false;    Private Surfaceholder holder;        Painting Center public void Run () {Paint P1 = new paint ();                try{while (true) {Canvas Newcanvas = new canvas (Erjihuancun);                for (Gameimage image:gameimage) {Newcanvas.drawbitmap (Image.getbitmap (), Image.getx (), Image.gety (), p1);                Canvas canvas = Holder.lockcanvas ();                Canvas.drawbitmap (ERJIHUANCUN,0,0,P1);                Holder.unlockcanvasandpost (canvas);            Thread.Sleep (10); }}catch (Exception e) {}} @Override public void SURFACECReated (Surfaceholder holder) {} @Override public void surfacedestroyed (Surfaceholder holder) {state = FA    Lse        } @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {//Get screen width and height        Display_w=width;        Display_h=height;        Init ();        This.holder=holder;        state = true;    New Thread (This). Start (); }}
Dafeijigameview.java

Package Com.example.administrator.myapplication;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;public class Mainactivity extends appcompatactivity{    @Override    protected void OnCreate ( Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Remove title       //Requestwindowfeature (window.feature_no_title);          Dafeijigameview view =  new Dafeijigameview (this);        Setcontentview (view);}    }
Mainactivity.java

Implementation process

Aircraft class

Private class Feijiimage implements gameimage{private Bitmap my;        private int x;        private int y;        private int width;        private int heigth;        private int getwidth () {return width;        } private int Getheigth () {return heigth; } Private List<Bitmap>bitmaps = new ArrayList<Bitmap>();            Private Feijiimage (Bitmap my) {this.my=my;            Bitmaps.add (Bitmap.createbitmap (My,0,0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4), 0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4) *2,0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4) *3,0,my.getwidth ()/4,my.getheight ());            Get the fighter's high and wide Width=my.getwidth ()/4;            Heigth=my.getheight ();            X= (Display_w-my.getwidth ()/4)/2;        Y=display_h-my.getheight ()-30;        } private int index=0;        private int num = 0;            @Override public Bitmap Getbitmap () {Bitmap Bitmap = bitmaps.get (index);               Control the frequency of switching four aircraft if (num = = ten) {index++;               if (index = = bitmaps.size ()) {index=0;           } num=0; } num++;        return bitmap;        } @Override public int GetX () {return x;        } @Override public int GetY () {return y;        } public void sety (int y) {this.y=y;        } public void SetX (int x) {this.x=x; }    }
Feijiimage.class

  

 Drawing airplane animations

Private List<Bitmap> bitmaps = new ArrayList<Bitmap> ();

Add four images to the list

   Private  Feijiimage (Bitmap my) {            this.my=my;            Bitmaps.add (Bitmap.createbitmap (My,0,0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4), 0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4) *2,0,my.getwidth ()/4,my.getheight ());            Bitmaps.add (Bitmap.createbitmap (My, (My.getwidth ()/4) *3,0,my.getwidth ()/4,my.getheight ());            Get the fighter's high and wide            width=my.getwidth ()/4;            Heigth=my.getheight ();            X= (Display_w-my.getwidth ()/4)/2;            Y=display_h-my.getheight () -30;        }

Public static Bitmap CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
cutting an image from the original bitmap is an advanced way. You can use matrix to achieve advanced methods such as rotation
Parameter Description: Portal
Bitmap Source: The original bitmap from which to
int x: starting x coordinate
int y: start y coordinate
int Width: The width of the graph to be truncated
int Height: The width of the graph to be truncated
bitmap.config Config: A configuration of an enumeration type that can define the quality of the new bitmap that is truncated
Return value: Returns a cut-bitmap

 

 

draw an airplane flight animation (constantly switch four pictures, look like a plane fly)

Public Bitmap Getbitmap () {            Bitmap Bitmap = bitmaps.get (index);            Control the frequency of switching four aircraft           if (num = = ten) {               index++;               if (index = = bitmaps.size ()) {                   index=0;               }               num=0;           }            num++;            return bitmap;        }

For non-conflicting background movement and airplane flight speed, the airplane picture toggles 10 times times slower background image movement

Registering an airplane in a game event follows its own finger movement event

    Public Dafeijigameview (Context context) {        super (context);        Getholder (). Addcallback (this);        Event Registration        This.setontouchlistener (this);    }

  Realizing that the airplane follows its own finger movement

Feijiimage Selectfeiji; @Override public boolean OnTouch (View V, motionevent event) {//hand near screen-generated event if (Event.getaction () ==motionev Ent.                    Action_down) {for (Gameimage game:gameimage) {if (game instanceof Feijiimage) {                    Feijiimage Feiji = (feijiimage) game; Select the aircraft if the condition is true (Feiji.getx ()<Event. GetX () && feiji.gety () <event.gety () && FEIJI.G EtX () +feiji.getwidth ( )>event.getx () && feiji.gety () +feiji.getheigth () >event.gety ())                    {Selectfeiji=feiji;                    }else{Selectfeiji=null;                } break; }}}else if (Event.getaction () ==motionevent.action_move) {//Move mouse if (selectfeiji!=                NULL) {selectfeiji.setx (int) event.getx ()-selectfeiji.getwidth ()/2);            Selectfeiji.sety ((int) event.gety ()-selectfeiji.getheigth ()/2);        }}else if (Event.getaction () ==motionevent.action_up) {//release mouse selectfeiji=null;    } return true; }

Android_ (game) Jerk off 03: Control player aircraft

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.