android练习:碰撞的小球

系统 1706 0
先看图:
android练习:碰撞的小球
小球碰到边上反弹,下面的横杆接住小球让其再反弹上去。很简单,大家都见过的啦。
这里主要练习SurfaceView,Callback以及在SurfaceView中使用线程,看主要的View类:
    
package com.wt.app;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.FontMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class GameView extends SurfaceView implements Callback,Runnable{
	private static final String TAG="GameView";
	SurfaceHolder holder=null;
	int x,y=0;
	int speedX=0,speedY=0;
	int ball_x,ball_y=0;
	int ball_speedX=5,ball_speedY=5;
	int r=5;
	public GameView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		holder=this.getHolder();
		holder.addCallback(this);
		this.setFocusable(true);
	}
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		x=(Consts.screenWidth-50)>>1;
		y=(Consts.screenHeight-10);
		ball_x=10;
		ball_y=10;
		new Thread(this).start();
	}
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	boolean isKeyDown=false;
	boolean isRunning=true;
	public void run() {
		// TODO Auto-generated method stub
		Paint paint=new Paint();
		while(isRunning){
			move();
			paint(paint);
			try {
				Thread.sleep(15);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void paint(Paint paint) {
		// TODO Auto-generated method stub
		Canvas canvas=holder.lockCanvas();
		
		//刷屏
		canvas.drawColor(Color.WHITE);
//		paint.setColor(Color.WHITE);
//		canvas.drawRect(0, 0, Consts.screenWidth, Consts.screenHeight, paint);
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		canvas.drawRect(x, y, x+50, y+10, paint);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(5);
		paint.setColor(Color.RED);
		canvas.drawCircle(ball_x, ball_y, r, paint);
		if(gameOver){
			paint.setStrokeWidth(1);
			paint.setColor(Color.RED);
			paint.setTextSize(25);
			FontMetrics fm=paint.getFontMetrics();
			int textHeight=(int)Math.ceil((fm.descent-fm.top));
			float textWidth = paint.measureText("Game Over");
			canvas.drawText("Game Over", (Consts.screenWidth-textWidth)/2, (Consts.screenHeight-textHeight)/2, paint);
		}
		holder.unlockCanvasAndPost(canvas);
	}

	boolean gameOver=false;
	public void move(){
		if(isKeyDown){
			x+=speedX;
			if(x<0){
				x=0;
			}
			if(x>Consts.screenWidth-50){
				x=Consts.screenWidth-50;
			}
		}
		ball_x+=ball_speedX;
		ball_y+=ball_speedY;
		if(ball_x<r) ball_speedX=-ball_speedX;
		if(ball_x>Consts.screenWidth-r) ball_speedX=-ball_speedX;
		if(ball_y<r) ball_speedY=-ball_speedY;
		
		Rect r1=new Rect();
		r1.set(x, y, x+50, y+10);
		Rect r2=new Rect();
		r2.set(ball_x-r, ball_y-r, ball_x+r, ball_y+r);
		if(Tools.isCollideWith(r1, r2)){
			ball_speedY=-ball_speedY;
		}
		if(ball_y>Consts.screenHeight){
			gameOver=true;
			isRunning=false;
		}
	}
}

  

Activity类:
    
package com.wt.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity {
    /** Called when the activity is first created. */
	private static final String TAG="GameActivity";
	GameView view;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        //窗口宽高
        DisplayMetrics dm=new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        Consts.screenWidth=dm.widthPixels;
        Consts.screenHeight=dm.heightPixels;
        
//        Consts.screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
//        Consts.screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
        
        Log.i(TAG,"Consts.screenWidth="+Consts.screenWidth);
        Log.i(TAG,"Consts.screenHeight="+Consts.screenHeight);
//        setContentView(R.layout.main);
        view=new GameView(this);
        setContentView(view);
    }
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		view.isKeyDown=true;
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			view.speedX=-5;
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			view.speedX=5;
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		view.isKeyDown=false;
		view.speedX=0;
		return super.onKeyUp(keyCode, event);
	}
    
}

  

其他辅助类如下:
    
package com.wt.app;

import android.graphics.Rect;

public class Tools {

	public static final boolean isCollideWith(int ax,int ay,int aw,int ah,int bx,int by,int bw,int bh){
		if(ay-by>bh||ax-bx>bw||by-ay>ah||bx-ax>aw){
			return false;
		}
		return true;
	}
	
	public static final boolean isCollideWith(Rect r1,Rect r2){
		if(r2.left-r1.left>=r1.width()||r1.left-r2.left>=r2.width()||r2.top-r1.top>=r1.height()||r1.top-r2.top>=r2.height()){
			return false;
		}
		return true;
	}
}

  

    
package com.wt.app;

public class Consts {

	public static int screenWidth;
	public static int screenHeight;
}

  

适合新手练习用!

android练习:碰撞的小球


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论