【Android小游戏】安卓小游戏开发(直播手把手系列二)

上篇说到绘制游戏背景图,接下来绘制游戏人物。

2.绘制游戏人物:

首先我们创建一个元素(即一个人物类,其中编写他的属性和方法,也就是说写一个对象)

如下:

package yyc.com.mysurface;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;

/**  * Created by Administrator on 2015/9/19.  */ public class Man {

    private static final float RADIO_POS_HEIGHT = 2 / 3F;
    private static final int BIRD_SIZE = 30;
    private int x;
    private int y;
    private int mWidth;
    private int mHeight;
    private Bitmap bitmap;
    private RectF rect = new RectF();
    public Man(Context context,int gameWidth,int gameHeight,Bitmap bitmap){
        this.bitmap = bitmap;
        //鸟的位置
        x = gameWidth / 2 - bitmap.getWidth() / 2;
        y = (int) (gameHeight * RADIO_POS_HEIGHT);

        // 计算鸟的宽度和高度
        mWidth = Util.dp2px(context, BIRD_SIZE);
        mHeight = (int) (mWidth * 1.0f / bitmap.getWidth() * bitmap.getHeight());
    }

    public void draw(Canvas canvas)
    {
        rect.set(x, y, x + mWidth, y + mHeight);
        canvas.drawBitmap(bitmap, null, rect, null);

    }
    public int getY()
    {
        return y;
    }

    public void setY(int y)
    {
        this.y = y;
    }

    public int getWidth()
    {
        return mWidth;
    }

    public int getHeight()
    {
        return mHeight;
    }

}
主要需要注意:draw函数;是游戏绘制人物的核心。

紧接着,开始把人物放入背景图内容中。

修改MySurface 并将模块加入其中

package yyc.com.mysurface;

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.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**  * Created by Administrator on 2015/9/18.  */ public class MySurface extends SurfaceView implements SurfaceHolder.Callback,Runnable {


    private Man man;
    private Bitmap bitmap;

    private Rect gameRect=new Rect();

    private Bitmap mBg;
    private SurfaceHolder  mHolder;

    private Canvas mCanvas;

    private Thread thread;

    private boolean r;

    private void initBitmap(){
        bitmap=loadImageByResId(R.drawable.doge);
    }

    private void drawMan(){
        man.draw(mCanvas);
    }

    public MySurface(Context context) {
        super(context);
         mp=new Paint();
        initBitmap();
        mp.setAntiAlias(true);
        mp.setDither(true);
        mHolder=getHolder();
        mHolder.addCallback(this);
        setZOrderOnTop(true);
        mHolder.setFormat(PixelFormat.TRANSLUCENT);

        setFocusable(true);
        setFocusableInTouchMode(true);

        this.getKeepScreenOn();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
         r=true;
        thread=new Thread(this);
        thread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        gameRect.set(0, 0, width, height);
        man=new Man(getContext(),width,height,bitmap);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
                   r=false;
    }

    @Override
    public void run() {
        while (r)
        {
            long start = System.currentTimeMillis();
            draw();
            long end = System.currentTimeMillis();

            try
            {
                if (end - start < 50)
                {
                    Thread.sleep(50 - (end - start));
                }
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }

        }
    }

    Paint mp;
    private void draw()
    {
        try
        {
            mCanvas=mHolder.lockCanvas();
            mBg=loadImageByResId(R.drawable.bg);
            mCanvas.drawBitmap(mBg, null, gameRect, null);
            drawMan();
            if (mCanvas != null)
            {
//               mp.setColor(Color.BLUE);
//                mCanvas.drawRect(20, 30, 50, 50, mp);
//
//                mBg=loadImageByResId(R.drawable.bg);
//                mCanvas.drawBitmap(mBg, null, gameRect, null);
             //   mp.set
            }
        } catch (Exception e)
        {
        } finally
        {
            if (mCanvas != null)
                mHolder.unlockCanvasAndPost(mCanvas);
        }
    }
    private Bitmap loadImageByResId(int id){
        return BitmapFactory.decodeResource(getResources(), id);
    }
}

得出以下结果

【Android小游戏】安卓小游戏开发(直播手把手系列二)_第1张图片

人物已加入

你可能感兴趣的:(【Android小游戏】安卓小游戏开发(直播手把手系列二))