首页 > 代码库 > Android游戏开发:物理游戏之重力系统开发--圆形自由落体Demo

Android游戏开发:物理游戏之重力系统开发--圆形自由落体Demo

    本节为大家提供有关物理游戏的知识,讲解了一个简单的圆形自由落体Demo的编写。。
Java代码
package com.himi;   
import java.util.Random;   
import java.util.Vector;   
import android.content.Context;   
import android.graphics.Canvas;   
import android.graphics.Color;   
import android.graphics.Paint;   
import android.util.Log;   
import android.view.KeyEvent;   
import android.view.SurfaceHolder;   
import android.view.SurfaceView;   
import android.view.SurfaceHolder.Callback;   
public class MySurfaceViee extends SurfaceView implements Callback, Runnable {   
    private Thread th;   
    private SurfaceHolder sfh;   
    private Canvas canvas;   
    private Paint paint;   
    private boolean flag;   
    public static int screenW, screenH;   
    private Vector<MyArc> vc;//这里定义装我们自定义圆形的容器   
    private Random ran;//随即库   
    public MySurfaceViee(Context context) {   
        super(context);   
        this.setKeepScreenOn(true);   
        vc = new Vector<MyArc>();   
        ran = new Random();//备注1   
        sfh = this.getHolder();   
        sfh.addCallback(this);   
        paint = new Paint();   
        paint.setAntiAlias(true);   
        setFocusable(true);   
    }   
    public void surfaceCreated(SurfaceHolder holder) {   
        flag = true;//这里都是上一篇刚讲过的。。。   
        th = new Thread(this);   
        screenW = this.getWidth();   
        screenH = this.getHeight();   
        th.start();   
    }   
    public void draw() {   
        try {   
            canvas = sfh.lockCanvas();   
            canvas.drawColor(Color.BLACK);   
            if (vc != null) {//当容器不为空,遍历容器中所有圆形画方法   
                for (int i = 0; i < vc.size(); i++) {   
                    vc.elementAt(i).drawMyArc(canvas, paint);   
                }   
            }   
        } catch (Exception e) {   
            // TODO: handle exception   
        } finally {   
            try {   
                if (canvas != null)   
                    sfh.unlockCanvasAndPost(canvas);   
            } catch (Exception e2) {   
            }   
        }   
    }   
    private void logic() {//主逻辑   
        if (vc != null) {//当容器不为空,遍历容器中所有圆形逻辑   
            for (int i = 0; i < vc.size(); i++) {   
                vc.elementAt(i).logic();   
            }   
        }   
    }   
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {   
        //当按键事件响应,我们往容器中仍个我们的圆形实例   
        vc.addElement(new MyArc(ran.nextInt(this.getWidth()), ran.nextInt(100), ran.nextInt(50)));   
        return true;   
    }   
    public void run() {   
        // TODO Auto-generated method stub   
        while (flag) {   
            logic();   
            draw();   
            try {   
                Thread.sleep(100);   
            } catch (Exception ex) {   
            }   
        }   
    }   
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {   
        Log.v("Himi", "surfaceChanged");   
    }   
    public void surfaceDestroyed(SurfaceHolder holder) {   
        flag = false;   
    }   
}  
       OK,代码都很简单,也很清晰! 稍微说一句:像MyArc里面也有类似MysurfaceView中一样的方法 logic() 以及draw(),这样能更好的管理我们的代码结构,思路清晰,各尽其责,避免混乱。

 

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399184163&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3706290.html&dt=1399184167002&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740&correlator=1399184166863&frm=20&ga_vid=429972749.1397695120&ga_sid=1399178814&ga_hid=704768994&ga_fc=1&u_tz=480&u_his=256&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=0&ady=2454&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=p9Jb6IwPsR&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399184163&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3706290.html&dt=1399184167052&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740%2C8660799060&correlator=1399184166863&frm=20&ga_vid=429972749.1397695120&ga_sid=1399178814&ga_hid=704768994&ga_fc=1&u_tz=480&u_his=256&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=306&ady=2704&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=At67hciQjp&p=http%3A//www.cnblogs.com&dtd=53" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>