Header Ads

[Android] Lập trình game Android (Code demo)

package com.example.game;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class GFXSurface extends Activity implements OnTouchListener {

    ThanhNguyenSurface ourSurfaceView;
    float x, y;
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ourSurfaceView =  new ThanhNguyenSurface(this);
        ourSurfaceView.setOnTouchListener(this);
        x = 0; y = 0;
        setContentView(ourSurfaceView);
    
    }
    /* (non-Javadoc)
     * @see android.app.Activity#onPause()
     */
    @Override
    protected void onPause() {
        
        super.onPause();
        ourSurfaceView.pause();
    }
    /* (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        
        super.onResume();
        ourSurfaceView.resume();
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        
        x = event.getX();
        y = event.getY();
        
        
        
        return true;
    }






 class ThanhNguyenSurface extends  SurfaceView implements Runnable{

    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;
    public ThanhNguyenSurface(Context context)
    {
        super(context);
        ourHolder = getHolder();
        ourThread  = new Thread(this);
        ourThread.start();
    }
    
    
    public void pause()
    {
        isRunning = false;
        while(true)
        {
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }
    
    
    public void resume()
    {
        isRunning = true;
        ourThread  = new Thread(this);
        ourThread.start();
    }
    
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        while(isRunning)
        {
            if(!ourHolder.getSurface().isValid())
                continue;
            
            Canvas canvas =  ourHolder.lockCanvas();
            canvas.drawRGB(02, 02, 150);
            if(x != 0 && y != 0 )
            {
                Bitmap test = BitmapFactory.decodeResource(getResources(                 ), R.drawable.greenball);
                //Bitmap test2 = BitmapFactory.decodeResource(getResourc                 es(), R.drawable.ruqa);
                canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.g                 etHeight()/2), null);
                //canvas.drawBitmap(test2, x, y, null);
                
            }
            
            
            ourHolder.unlockCanvasAndPost(canvas);
        }
        
    }
}
}

No comments:

Powered by Blogger.