I am interested in this piece of the game, just to learn Android, so I learned to do the next Android game
SOURCE Location
Link: http://pan.baidu.com/s/1eQ4gdI6 Password: g58e
This article is my own study and record, like people can read, write bad don't spray
At first, the basics of Android and the environment are not in the detail, the basic analysis of the game:
Required elements: Money and money Bag Board
Little game, just a few tests of the function, so did not do well,
The reason that the individual likes to play the game is because the game and the real world are very close, can use the realistic thought to think the game
For example, the game: first of all, money is definitely the default in the case of non-stop movement, and then the money bag, by the player to control, can also move, when the money and money bag contact, to make extra points, and the money disappears, is such a simple idea
Main activity
Package Com.rp;import Android.app.activity;import Android.os.bundle;import android.view.window;import Android.view.windowmanager;public class Mainactivity extends Activity {@Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);//Set Full screen This.getwindow (). SetFlags ( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); Requestwindowfeature ( Window.feature_no_title);//Display Custom Surfaceview view Setcontentview (new Mysurfaceview (This));}}
Money This class
Package Com.rp;import Android.graphics.canvas;import Android.graphics.paint;import android.graphics.rect;public Class Money {//define coin private Rect money;public int moneyx=0,moneyy=0;//wallet coordinates public int speed=2;//The falling speed of the coin public int height; Private Boolean isdeath=false;private int cnt;//fractional public money (int moneyx,int speed,int height) {This.moneyx = Moneyx;this.speed = Speed;this.height = height;this.cnt = speed;} public void Drawmoney (Canvas canvas,paint Paint) {money = new Rect (Moneyx, Moneyy, moneyx+10, moneyy+20); Canvas.drawrect ( Money, paint);} The logic of Money/*** * Free fall When cross bounds when re-movement */public void logic () {if (Isdeath ()) {moneyy = 0;} Else{moneyy +=speed;}} /*** * Death function * When the money is over the bounds, or the money bag is met death */public Boolean Isdeath () {if (this.moneyy>=this.height) {Isdeath = true;} Else{isdeath = false;} return Isdeath;} public int getcnt () {return CNT;} public void setcnt (int cnt) {this.cnt = cnt;}}
A rocker system has been added here, no special effect, good-looking
Package Com.rp;import Android.graphics.canvas;import Android.graphics.paint;public class Yaogan {// Defines the center point coordinates of two circles with radius public float Smallcenterx = Smallcentery = 490, Smallcenterr = 20;public float Bigcenterx = +, bigcent ErY = 490, Bigcenterr = 40;public void Drawyaogan (Canvas canvas,paint paint) {//Draw great circle Canvas.drawcircle (Bigcenterx, Bigcentery, Bigcenterr, paint);//Draw Small Circle canvas.drawcircle (Smallcenterx, Smallcentery, Smallcenterr, paint);} /** * Small Circle for circular motion of the circle, set the coordinate position of the center point of the small circle * @param CenterX * Round (large circle) center point x Coordinate * @param centery * Around the circle (great circle) Center point y Coordinate * @param R * ROUND (circle) Radius * @param rad * rotational radian */public void setsmallcirclexy (float CenterX, float CenterY, float R, double rad) {//Get the x-coordinate of the circular motion Smallcenterx = (float) (R * Math.Cos (RAD)) + centerx;//get the y-coordinate of the circular motion Smallcent ErY = (float) (R * Math.sin (RAD)) + CenterY;} /** * Gets the radian between two points * @param px1 The X coordinate of the first point * @param py1 the Y coordinate of the first point @param px2 the X coordinate of the second point @param py2 the y-coordinate of the second point * @ret Urn */public double Getrad (fLoat px1, float py1, float px2, float py2) {//Get two points x distance float x = px2-px1;//get two points y distance float y = py1-py2;//calculate bevel length Float hypotenuse = (float) math.sqrt (Math.pow (x, 2) + Math.pow (Y, 2));//Get the cosine of this angle (through the theorem in the trigonometric function: adjacent edge/hypotenuse = angle cosine) Float Cosangle = x /hypotenuse;//obtains the radian of its angle by the inverse cosine theorem float rad = (float) math.acos (cosangle);//When the position of the touchscreen y-coordinate < The y-coordinate of the joystick we want to take the inverse value -0~-180 if (Py2 < PY1) {rad =-rad;} return rad;}}
Primary Invocation class
Package Com.rp;import Java.util.arraylist;import Android.content.context;import android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.region;import Android.media.mediaplayer;import Android.util.log;import Android.view.keyevent;import android.view.MotionEvent; Import Android.view.surfaceholder;import android.view.surfaceholder.callback;import android.view.SurfaceView;/** * * @author Sunzhiyan * @version 0.1 * */public class Mysurfaceview extends Surfaceview implements Callback, Runnable {Priva Te surfaceholder sfh;private Paint paint;private Thread th;private boolean flag;private Canvas canvas;private int Screenw, screenh=532;private int x=100,y=400;//Finger touch screen coordinates private Yaogan yaogan;//define a music player private MediaPlayer mediaplay;//****** Private Rect rect;private Money;private region reg;//Define a protagonist//define a set of moneyCurrency private arraylist<money> ArrayList = new arraylist<money> (); private int mx=0,my=0;private int count;/** * Surfaceview initialization function */public Mysurfaceview (context context) {super (context); SFH = This.getholder (); Sfh.addcallback (this);p aint = new paint ();p aint.setcolor (color.red);p Aint.setantialias (True) ;//Call Game Initialization initgame (context); setfocusable (true);} Initialize game function public void Initgame (context context) {//Instantiate a remote lever Yaogan = new Yaogan ();//Call a music file Mediaplay = Mediaplayer.create (context, r.raw.bgmusic);//Set Loop to play mediaplay.setlooping (true);//music to play again Mediaplay.start ();//define a set of coin money money01 = New Money ((mx+0) *50,5,screenh); Money money02 = new Money ((mx+1) *50,15,screenh); Money money03 = new Money ((mx+2) *50,10,screenh); Money money04 = new Money ((mx+4) *50,5,screenh); Money money05 = new Money ((mx+5) *50,5,screenh); Money money06 = new Money ((mx+6) *50,5,screenh); Arraylist.add (MONEY01); Arraylist.add (money02); Arraylist.add (money03 ); Arraylist.add (money04); Arraylist.add (money05); Arraylist.add (MONEY06); }/** * Surfaceview view created, in response to this function */@Overridepublic void surfacecreated (Surfaceholder holder) {flag = true;//instance thread th = new Thread (th is);//Start thread Th.start ();} /** * Game drawing */public void Mydraw () {try {canvas = Sfh.lockcanvas (), if (canvas! = null) {Canvas.drawcolor (color.white);//Draw back King//bitmap background = Bitmapfactory.decoderesource (This.getresources (), r.drawable.background);// Canvas.drawbitmap (background,0,0, paint);p Aint.setalpha (0x77);//Draw a coin//get the center point of the mouse to draw the protagonist Rect = new Rect (x-20, y-20, x+20 , y+20), Reg = new region (rect), for (int i = 0; i < arraylist.size (); i++) {//money.logic (); Arraylist.get (i). Drawmoney (canvas, paint), if (Reg.contains (Arraylist.get (i). Moneyx, Arraylist.get (i). Moneyy)) {count = count+ Arraylist.get (i). GETCNT ()//target death Arraylist.get (i). Moneyy = 0;paint.setcolor (Color.Blue);}} /*for (int i = 0; i < 7; i++) {money = new money (); Drawmoney (canvas, Paint, (mx+i) *50,i+2); Arraylist.add (money);} *//*if (Reg.contains (Moneyx, moneyy)) {Paint.setcolor (color.blue);} else{}*///Judging Collision Canvas.draWrect (rect, paint);//Remote Sensing Yaogan. Drawyaogan (canvas,paint);//constant monitoring of coordinates canvas.drawtext ("x=" +x+ "y=" +y, 480, and paint);//canvas.drawtext ("Your points are" + Count, N, v, Paint);}} catch (Exception e) {//Todo:handle Exception} finally {if (canvas! = NULL) sfh.unlockcanvasandpost (canvas);}} /** * Touch Screen Event Listener */@Overridepublic Boolean ontouchevent (Motionevent event) {//When the user's finger is lifted, the small circle should be restored to the initial position if (event.getaction () = = MOTIONEVENT.ACTION_UP) {Yaogan.smallcenterx = Yaogan. Bigcenterx;yaogan.smallcentery = Yaogan. Bigcentery;} else {int pointx = (int) event.getx (), int pointy = (int) event.gety (), x = Pointx;y = pointy;//Determines whether the user's clicked position is within the great circle if (math.sqr T (Math.pow (Yaogan. Bigcenterx-(int) event.getx ()), 2) + Math.pow ((Yaogan. Bigcentery-(int) event.gety ()), 2)) <= Yaogan. Bigcenterr) {//Let the small circle follow the user contact position Move Yaogan.smallcenterx = Pointx;yaogan.smallcentery = pointy;} else {yaogan.setsmallcirclexy (Yaogan. Bigcenterx, Yaogan. Bigcentery,yaogan. Bigcenterr, Yaogan.getrad (Yaogan. Bigcenterx, Yaogan. Bigcentery, Pointx, Pointy));}} Return true;} /** * Key Event Listener */@Overridepublic boolean onKeyDown (int keycode, keyevent event) {return Super.onkeydown (KeyCode, event);} @Overridepublic void Run () {while (flag) {Long start = System.currenttimemillis (); Mydraw (); logic (); Long end = System.curr Enttimemillis (); try {if (End-start <) {Thread.Sleep (-(End-start))}} catch (Interruptedexception e) {E.print StackTrace ();}}} /** * Surfaceview view state changed, response to this function */@Overridepublic void surfacechanged (surfaceholder holder, int format, int width, int hei ght) {}/** * Surfaceview view is extinct, respond to this function */@Overridepublic void surfacedestroyed (Surfaceholder holder) {flag = false;} /** * Game logic */private void logic () {for (int i = 0; i < arraylist.size (); i++) {//money.logic (); Arraylist.get (i). Logic () ;}}}
The basic code has been given, there is no material, the code himself to test the success of the late has the possibility of planning to improve, of course, it is necessary to need a art to help
Collect money bags for Android games (homemade)