공 튀김 미니 게임 (사용자 정의view 응용 프로그램)
마스터 프로그램:
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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.