当前位置:操作系统 > 安卓/Android >>

【Android游戏开发之四】Android 游戏框架(一个游戏角色在屏幕行走的demo)

 

其实上一篇分析su易做图ceview的文章就是一个简单的游戏框架了,当然这里再强调一下,简单的游戏框架,所以不要高手们不要乱喷~

 

 这个Demo是给群里一童鞋写的一个对图片操作以及按键处理,游戏简单框架的一个demo,这里放出给大家分享~

 

package com.himi; 

import android.content.Context; 

import android.content.res.Resources; 

import android.graphics.Bitmap; 

import android.graphics.BitmapFactory; 

import android.graphics.Canvas; 

import android.graphics.Color; 

import android.graphics.Paint; 

import android.util.Log; 

import android.view.KeyEvent; 

import android.view.Su易做图ceHolder; 

import android.view.Su易做图ceView; 

import android.view.Su易做图ceHolder.Callback; 

public class MySu易做图ceView extends Su易做图ceView implements Callback, Runnable { 

    private Thread th = new Thread(this); 

    private Su易做图ceHolder sfh; 

    private int SH, SW; 

    private Canvas canvas; 

    private Paint p; 

    private Paint p2; 

    private Resources res; 

    private Bitmap bmp; 

    private int bmp_x = 100, bmp_y = 100; 

    private boolean UP, DOWN, LEFT, RIGHT; 

    private int animation_up[] = { 3, 4, 5 }; 

    private int animation_down[] = { 0, 1, 2 }; 

    private int animation_left[] = { 6, 7, 8 }; 

    private int animation_right[] = { 9, 10, 11 }; 

    private int animation_init[] = animation_down; 

    private int frame_count; 

    public MySu易做图ceView(Context context) { 

        super(context); 

        this.setKeepScreenOn(true); 

        res = this.getResources(); 

        bmp = BitmapFactory.decodeResource(res, R.drawable.enemy1); 

        sfh = this.getHolder(); 

        sfh.addCallback(this); 

        p = new Paint(); 

        p.setColor(Color.YELLOW); 

        p2 = new Paint(); 

        p2.setColor(Color.RED); 

        p.setAntiAlias(true); 

        setFocusable(true);  //备注1

    } 

    public void su易做图ceCreated(Su易做图ceHolder holder) { 

        SH = this.getHeight(); 

        SW = this.getWidth(); 

        th.start(); 

    } 

    public void draw() { 

        canvas = sfh.lockCanvas(); 

        canvas.drawRect(0, 0, SW, SH, p);   //备注2

        canvas.save();   //备注3

        canvas.drawText("Himi", bmp_x-2, bmp_y-10, p2); 

        canvas.clipRect(bmp_x, bmp_y, bmp_x + bmp.getWidth() / 13, bmp_y+bmp.getHeight()); 

        if (animation_init == animation_up) { 

            canvas.drawBitmap(bmp, bmp_x - animation_up[frame_count] * (bmp.getWidth() / 13), bmp_y, p); 

        } else if (animation_init == animation_down) { 

            canvas.drawBitmap(bmp, bmp_x - animation_down[frame_count] * (bmp.getWidth() / 13), bmp_y, p); 

        } else if (animation_init == animation_left) { 

            canvas.drawBitmap(bmp, bmp_x - animation_left[frame_count] * (bmp.getWidth() / 13), bmp_y, p); 

        } else if (animation_init == animation_right) { 

            canvas.drawBitmap(bmp, bmp_x - animation_right[frame_count] * (bmp.getWidth() / 13), bmp_y, p); 

        } 

        canvas.restore();  //备注3

        sfh.unlockCanvasAndPost(canvas); 

    } 

    public void cycle() { 

        if (DOWN) { 

            bmp_y += 5; 

        } else if (UP) { 

            bmp_y -= 5; 

        } else if (LEFT) { 

            bmp_x -= 5; 

        } else if (RIGHT) { 

            bmp_x += 5; 

        } 

        if (DOWN || UP || LEFT || RIGHT) { 

            if (frame_count < 2) { 

                frame_count++; 

            } else { 

                frame_count = 0; 

            } 

        } 

        if (DOWN == false && UP == false && LEFT == false && RIGHT == false) { 

            frame_count = 0; 

        } 

    } 

    @Override 

    public boolean onKeyDown(int key, KeyEvent event) { 

        if (key == KeyEvent.KEYCODE_DPAD_UP) { 

            if (UP == false) { 

                animation_init = animation_up; 

            } 

            UP = true; 

    &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,