공 튀김 미니 게임 (사용자 정의view 응용 프로그램)

7065 단어
다음 프로그램은 작은 공과 라켓을 각각 원형 구역과 직사각형 구역으로 대체하는 간단한 볼 게임을 개발했다.
마스터 프로그램:
package com.example.clipdrawabledemo;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity {

	private int tableHight;
	private int tableWidth;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//   
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);//  
		final GameView gView=new GameView(this);
		setContentView(gView);
		
		
		WindowManager windowManager=getWindowManager();
		Display display=windowManager.getDefaultDisplay();
		DisplayMetrics metrics=new DisplayMetrics();
		display.getMetrics(metrics);
		tableHight=metrics.heightPixels;//       
		tableWidth=metrics.widthPixels;//       
		gView.setRacketY(tableHight-20);//       
		
		//      
		gView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				// TODO Auto-generated method stub
				float x=event.getX();
				float y=event.getY();
				switch(event.getAction()){
					case MotionEvent.ACTION_MOVE:
						gView.setRacketX((int) x);
						//gView.setRacketY((int) y);//            
						break;
				}
				gView.invalidate();//   
				return true;
			}
		});
		
		final Handler mHandler=new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if(msg.what==0x123){
					gView.invalidate();
				}
			}
			
		};
		
		final Timer time=new Timer();
		time.schedule(new TimerTask() {
			
			@Override
			public void run() {
				System.out.println("BallX()="+gView.getBallX()+";BallY()="+gView.getBallY());
				System.out.println("Racketx()="+gView.getRacketX()+";getRacketY()="+gView.getRacketY());
				// TODO Auto-generated method stub
				//                ,      
				if((gView.getBallX()-gView.getBall_size()<=0)||(gView.getBallX()+gView.getBall_size()>=tableWidth)){
					gView.setXspeed(-gView.getXspeed());
					gView.setYspeed(new Random().nextInt(15));
				}
				//                        ,    
				if(((gView.getBallY()+gView.getBall_size())>gView.getRacketY())&&(
						((gView.getBallX()+gView.getBall_size())(gView.getRacketX()+gView.getRacket_width()))
						)
						){
					time.cancel();
					gView.setGame_str("    ");
					gView.setIsLose(true);
				}
				//          ,       ,    
				else if((gView.getBallY()<=0)||
						(((gView.getBallY()+gView.getBall_size())>=gView.getRacketY())&&
						((gView.getBallX()+gView.getBall_size())>=gView.getRacketX())&&
						((gView.getBallX()-gView.getBall_size())<=(gView.getRacketX()+gView.getRacket_width()))
						)
						){
					gView.setYspeed(-gView.getYspeed());
					gView.setXspeed(new Random().nextInt(15));
					/*//         
					if(gView.getYspeed()<0){
						gView.setYspeed(gView.getYspeed()-5);
					}
					if(gView.getXspeed()<0){
						gView.setXspeed(gView.getXspeed()-5);
					}*/
				}
				//    
				gView.setBallY(gView.getBallY()+gView.getYspeed());
				gView.setBallX(gView.getBallX()+gView.getXspeed());
				
				mHandler.sendEmptyMessage(0x123);
				
				/*if((Math.abs(gView.getBallX()+gView.getBall_size())>tableWidth)||(Math.abs(gView.getBallY()+gView.getBall_size())>tableHight)){
					time.cancel();
					gView.setGame_str("    ");
					gView.setIsLose(true);
				}*/
			}
		},0,100);		
	}	
}

사용자 정의view:
package com.example.clipdrawabledemo;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class GameView extends View {

	Paint paint=new Paint();
	Random rand=new Random();
	private int ballX=rand.nextInt(468);//  x       ,          -       ,     800*480
	private int ballY=rand.nextInt(20);//  y       ,          -       ,     800*480
	private int ball_size=12;//    
	private int yspeed=10;//         
	private double xyRate=rand.nextDouble()-0.5;
	private int xspeed=(int) (yspeed*xyRate*2);//         
	
	private int racketX=rand.nextInt(200);//    x  
	private int racketY;//    y  
	private int racket_hight=20;//    
	private int racket_width=480;//    
	
	Boolean isLose=false;//      
	private String game_str;
	public GameView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setFocusable(true);//   View        
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		if(isLose){//     ,  
			paint.setColor(Color.rgb(255,0,0));
			paint.setTextSize(40);//      
			canvas.drawText(game_str,180,400, paint);//       
		}
		paint.setStyle(Paint.Style.FILL);//    
		paint.setAntiAlias(true);//   
		paint.setColor(Color.rgb(0,0,255));//      
		canvas.drawCircle(ballX, ballY, ball_size, paint);//  
		paint.setColor(Color.rgb(0, 255,0));//    
		canvas.drawRect(racketX, racketY,racketX+racket_width,racketY+racket_hight, paint);//   
		
	}
	
	public int getBallX() {
		return ballX;
	}

	public void setBallX(int ballX) {
		this.ballX = ballX;
	}

	public int getBallY() {
		return ballY;
	}

	public void setBallY(int ballY) {
		this.ballY = ballY;
	}

	public int getBall_size() {
		return ball_size;
	}

	public void setBall_size(int ball_size) {
		this.ball_size = ball_size;
	}
	
	public int getRacketX() {
		return racketX;
	}

	public void setRacketX(int racketX) {
		this.racketX = racketX;
	}

	public int getRacketY() {
		return racketY;
	}

	public void setRacketY(int racketY) {
		this.racketY = racketY;
	}

	public int getRacket_hight() {
		return racket_hight;
	}

	public void setRacket_hight(int racket_hight) {
		this.racket_hight = racket_hight;
	}

	public int getRacket_width() {
		return racket_width;
	}

	public void setRacket_width(int racket_width) {
		this.racket_width = racket_width;
	}
	public int getYspeed() {
		return yspeed;
	}

	public void setYspeed(int yspeed) {
		this.yspeed = yspeed;
	}

	public int getXspeed() {
		return xspeed;
	}

	public void setXspeed(int xspeed) {
		this.xspeed = xspeed;
	}

	public Boolean getIsLose() {
		return isLose;
	}

	public void setIsLose(Boolean isLose) {
		this.isLose = isLose;
	}
	
	public String getGame_str() {
		return game_str;
	}

	public void setGame_str(String game_str) {
		this.game_str = game_str;
	}
}

좋은 웹페이지 즐겨찾기