Android开发学习——游戏开发小demo

 

public class MainActivity extends Activity {

    private GameUI gameUI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameUI = new GameUI(getApplicationContext());
        setContentView(gameUI);
    }
    
    //处理屏幕的点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        gameUI.handleTouch(event);
        return super.onTouchEvent(event);
    }

    
}

 

public class GameUI extends SurfaceView implements SurfaceHolder.Callback{
    boolean flag ;
    private RenderThread thread;
    private SurfaceHolder h;
//-----三个对象------------------
    private Man man;
    private List falist;
    private MyButton btn;

    public GameUI(Context context) {
        super(context);

        h = getHolder();
        h.addCallback(this);
    }
    
    public  class RenderThread extends Thread{
        public void run(){
            while(flag){
                try{
                drawUI();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        
//------------------------------------------------------------------------
        
        
       //绘制界面
        private void drawUI() {
            // TODO Auto-generated method stub
            Canvas lockCanvas = h.lockCanvas();
//--------------------绘制了一个矩形---------------------------------------            
            Paint paint = new Paint();
            paint.setColor(Color.GRAY);
//----------绘制屏幕大小的矩形,把之前绘制的全部盖在下边---------------------
            lockCanvas.drawRect(0, 0,getWidth(),getHeight(),paint);
//------------------------------------------------------------    -----------        
            man.drawSelf(lockCanvas);//把男孩绘制到屏幕上
//            if(fa != null){
//                fa.drawSelf(lockCanvas);//把笑脸绘制在界面上    
//                fa.move();
//            }
            for(face fa : falist){
                fa.drawSelf(lockCanvas);//把笑脸绘制在界面上    
                fa.move();
                if(fa.position.x<0||fa.position.x>getWidth()||fa.position.y<0||fa.position.y>getHeight()){
                    falist.remove(fa);
                }
            }
            
            btn.drawSelf(lockCanvas);//把按钮绘制在界面上
            
            
            h.unlockCanvasAndPost(lockCanvas);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        
    }

    @Override
    public void surfaceCreated(SurfaceHolder h) {
        
        Bitmap manbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar_boy);
        man = new Man(manbitmap, new Point(0,0));//创建了 小人,还没有绘制到界面上
        falist = new CopyOnWriteArrayList();//可以在集合遍历的过程中增加或者删除元素。堆内存的开销大,对数组进行了一次复制!
        
//------------现在不在这个位置位置绘制笑脸,在小人的最上绘制------
//        Bitmap facebitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rating_small);
//        fa = new face(facebitmap,new Point(0,0));//创建了笑脸
//-----------------按钮---------------------------------------------------------        
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bottom_default);
        Bitmap pressbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bottom_press);
        btn = new MyButton(bitmap, new Point(100,getHeight()-100),pressbitmap);//创建了一个button,还没有绘制到屏幕上
        btn.setClicklistener(new MyButton.OnClickListener() {
            
            @Override
            public void click() {
                // TODO Auto-generated method stub
                man.move(Man.DOWN);
            }
        });
        
        thread = new RenderThread();
        flag = true;
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        //一般停止线程,都是控制线程的循环
        flag = false;
    }    
    
    public void handleTouch(MotionEvent e){
        switch(e.getAction()){
        //case MotionEvent.ACTION_MOVE:
        
        //当按下的时候,绘制笑脸
        case MotionEvent.ACTION_DOWN:
            //拿到按下的xy坐标
            int x = (int) e.getRawX();
            int y = (int) e.getRawY();
            if(btn.isclick(new Point(x,y))){
                //man.move(Man.DOWN);        
                btn.click();
            }else{//按下的时候不显示笑脸
                face f = man.createface(getContext(),new Point(x,y));
                falist.add(f);
            }           
            break;
        case MotionEvent.ACTION_UP:
            btn.setIsClick(false);//当手指弹起的时候,让按钮点击的状态改为false
            break;            
        default:
            break;        
        }
    }
}

 

//精灵,所有显示界面的一个基类。
public abstract class Sprite {
    private Bitmap defaultBitmap;//默认显示的图片
    protected Point position;//位置
    
    //放置 图片和位置 为空
    public Sprite(Bitmap defaultBitmap, Point position) {
        super();
        this.defaultBitmap = defaultBitmap;
        this.position = position;
    }


    //绘制自身 就是把图片绘制到位置上
    public void drawSelf(Canvas c){
        c.drawBitmap(defaultBitmap, position.x,position.y, null);
    }

}
public class Man extends Sprite {

    public static final int DOWN=0;
    public Man(Bitmap defaultBitmap, Point position) {
        super(defaultBitmap, position);
        // TODO Auto-generated constructor stub
    }
    
    //特性方法,创建笑脸
    public face createface(Context c,Point touchpoint){
        
        Bitmap facebitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.rating_small);
        face fa = new face(facebitmap,new Point(position.x+50,position.y+50),touchpoint);//创建了笑脸,在小人的坐标上刚好到嘴的地方绘制笑脸。
        
        
        return fa;
        
    }
    
    public void move(int d){
        if(d == DOWN){
            this.position.y += 6;
        }
    }

}
public class face extends Sprite {

    //移动的距离是一个固定的值
    int speed = 6;
    private int tx;
    private int ty;
    public face(Bitmap defaultBitmap, Point position,Point touch) {
        super(defaultBitmap, position);
        // TODO Auto-generated constructor stub
        int x = touch.x - position.x;
        int y = touch.y - position.y;
        int d = (int) Math.sqrt(x*x+y*y);
        tx = speed*x/d;
        ty = speed*y/d;
        
        
        
    }
    
    //笑脸移动的方法
    public void move(){
        this.position.x += tx;
        this.position.y += ty;
    }

}
public class MyButton extends Sprite {

    private OnClickListener clicklistener;
    public interface OnClickListener{
        void click();
    }
    
    public void setClicklistener(OnClickListener clicklistener) {
        this.clicklistener = clicklistener;
    }
    
    //当按钮被点击的时  调用此方法
    public void click(){
        if(clicklistener!=null){
            clicklistener.click();
        }
    }

    private boolean isclick;//按下的状态
    private Bitmap pressBit;
    public MyButton(Bitmap defaultBitmap, Point position,Bitmap pressBit) {
        super(defaultBitmap, position);
        // TODO Auto-generated constructor stub
        this.pressBit = pressBit;
    }
    
    
    
    
    public void setIsClick(boolean isClick){
        this.isclick = isClick;
    }

    public void drawSelf(Canvas c){
        if(isclick){
            //绘制一个按下的图片
            c.drawBitmap(pressBit, position.x,position.y, null);
        }else{
            super.drawSelf(c);//绘制默认的图片
        }
    }
    
    //判断 手指按下的点  是否 在 当前按钮的  图片之中
    public boolean isclick(Point touchpoint){
        //创建了一个矩形,当前按钮的矩形
        Rect rect = new Rect(position.x,position.y,position.x+pressBit.getWidth(),position.y+pressBit.getHeight());    
        //如果包含,就为点击
        isclick = rect.contains(touchpoint.x,touchpoint.y );
        return isclick;        
    }

}

 

Android开发学习——游戏开发小demo_第1张图片

 

转载于:https://www.cnblogs.com/mengxiao/p/6612714.html

你可能感兴趣的:(移动开发)