PlaneGame--飞机小游戏案例

PlaneGame--飞机小游戏案例_第1张图片
PlaneView负责绘制游戏的的飞机
package com.eson.planegame ;

import android.content.Context ;
import android.graphics.Bitmap ;
import android.graphics.BitmapFactory ;
import android.graphics.Canvas ;
import android.graphics.Paint ;
import android.view.View ;

/**
* Created by Eson on 2016/5/18.
*/
public class PlaneView extends View {
    public float currentX ;
    public float currentY ;
    Bitmap plane ;

    public PlaneView(Context context) {

        super (context) ;
        //定义飞机图片
        plane =BitmapFactory. decodeResource(context.getResources() ,R.drawable. plane );
        setFocusable( true );
    }

    @Override
    protected void onDraw (Canvas canvas) {
        super .onDraw(canvas) ;
        //创建画笔
        Paint p= new Paint();
        //绘制飞机
        canvas.drawBitmap( plane ,currentX , currentY, p) ;
    }
}

将直接使用PlaneGame作为Activity显示的内容
package com.eson.planegame ;

import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.util.DisplayMetrics ;
import android.view.Display ;
import android.view.KeyEvent ;
import android.view.View ;
import android.view.Window ;
import android.view.WindowManager ;

public class PlaneGame extends AppCompatActivity {
    //定义飞机的移动速度
    private int speed =10 ;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        //去掉窗后标题
        requestWindowFeature(Window. FEATURE_NO_TITLE );
        super.onCreate(savedInstanceState) ;
        //全屏显示
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,WindowManager.LayoutParams. FLAG_FULLSCREEN );
        //创建PlaneView组件
        final PlaneView planeView= new PlaneView( this );
        setContentView(planeView) ;
        planeView.setBackgroundResource(R.drawable. back );
        //获取窗口管理器
        WindowManager windowManager=getWindowManager() ;
        Display display=windowManager.getDefaultDisplay() ;
        DisplayMetrics metrics= new DisplayMetrics();
        //获取屏幕宽和高
        display.getMetrics(metrics) ;
        //设置飞机的初始位置
        planeView. currentX =metrics.widthPixels / 2;
        planeView. currentY =metrics.heightPixels / 2;
        //为draw组件键盘事件绑定监听器
        planeView.setOnKeyListener( new View.OnKeyListener() {
            @Override
            public boolean onKey (View v, int keyCode , KeyEvent event) {

                //获取由哪个键触发的事件
                switch (event.getKeyCode()){

                    //控制飞机下移
                    case KeyEvent. KEYCODE_S :
                        planeView .currentY += speed;
                        break;
                    //控制飞机上移
                    case KeyEvent. KEYCODE_W :
                        planeView .currentY -= speed;
                        break;
                    //控制飞机左移
                    case  KeyEvent. KEYCODE_A :
                        planeView .currentX -= speed;
                        break;
                    case  KeyEvent. KEYCODE_D:
                        planeView .currentX += speed;
                        break;
                }
                //通知planeView组件重绘
                planeView.invalidate() ;
                return true;
            }
        });
    }
}

你可能感兴趣的:(PlaneGame--飞机小游戏案例)