【Android小游戏】安卓小游戏开发(手把手教学系列三)

想起当年初学安卓时,Java还不懂,大一做类似这样的游戏的时候,居然不用线程【Android小游戏】安卓小游戏开发(手把手教学系列三)_第1张图片

想想也是醉了,现在来填几年前的坑

开始:

就像之前两篇一样,要一个游戏人物,一个游戏运行Activity

1.游戏人物:

package yyc.com.htmltextview.Plane;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import yyc.com.htmltextview.R;

/**  * Created by Administrator on 2015/10/23.  */ public class PlaneView extends View {
    public float currentX,currentY;
    Bitmap plane;

    public PlaneView(Context context){
        super(context);
        plane= BitmapFactory.decodeResource(context.getResources(),R.drawable.doge);//飞机
        setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p=new Paint();
        canvas.drawBitmap(plane,currentX,currentY,p);
    }
}

2.游戏Activity:

package yyc.com.htmltextview.Plane;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import yyc.com.htmltextview.R;

/**  * Created by Administrator on 2015/10/23.  */ public class GameView extends Activity {
    private int speed=10;
    float startx,starty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        final PlaneView pl=new PlaneView(this);
        setContentView(pl);
        pl.setBackgroundResource(R.drawable.ba);

        WindowManager windowManager=getWindowManager();
        Display display=windowManager.getDefaultDisplay();
        DisplayMetrics metrics=new DisplayMetrics();
        display.getMetrics(metrics);
        pl.currentX=metrics.widthPixels/2;
        pl.currentY=metrics.heightPixels-40;


        pl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startx = event.getX();
                        starty = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                       int offset = (int) (event.getX() - startx) / 3;
                        // Log.e("TAST", offset + "offset");
                        if (offset >50) {
                            //右
                            pl.currentX+=speed;
                        }else if(offset<-50){
                            pl.currentX-=speed;
                            //左
                            Log.e("TAG", "offset<-50");
                        }
                       float upoffset = event.getY() - starty;
                        if (upoffset> 50) {
                            //下
                            pl.currentY+=speed;
                            Log.e("TAG", "upoffset> 50");
                        }else if(upoffset<-50){
                            //上
                            pl.currentY-=speed;
                            Log.e("TAG", "upoffset<-50");
                        }

                       // invalidate();
                        pl.invalidate();
                    default:
                        break;
                }

                return true;
            }
        });
    }
}
运行结果如下(没弄GIF图,但测试可行 【Android小游戏】安卓小游戏开发(手把手教学系列三)_第2张图片

【Android小游戏】安卓小游戏开发(手把手教学系列三)_第3张图片

你可能感兴趣的:(游戏)