您的位置:首页 > 编程语言

安卓游戏开发_基础环境获取代码

2016-01-21 23:00 260 查看
public class PinBall extends Activity{

private int tableHeight;
private int tableWidth;
Random random = new Random();

//挡板大小和坐标
private final int racket_width = 70;
private final int racket_height = 20;
private int racketY;
private int racketX;

//球体大小,速度,坐标
private final int ballR = 12;
private int ballX;
private int ballY;
private int Vy = 3;
//生成一个比率(-0.5~0.5之间),用于产生随机方向
private double xyRate = random.nextDouble() - 0.5;
private int Vx = (int) (Vy * 2 * xyRate);

//游戏进行时标志
private boolean isLose = false;

@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 game = new  GameView(this);
setContentView(game);
//获取桌面大小
WindowManager manager = getWindowManager();
Display display = manager.getDefaultDisplay();
tableHeight = display.getHeight();
tableWidth = display.getWidth();
//根据获取的桌面大小初始化坐标
initXY();

//重绘
final Handler handler = new Handler(){
public void handleMessage(Message message){
if(message.what == 0x123){
game.invalidate();
}
}
};

final Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
//左右边框
if(ballX < 0 || (ballX + ballR > tableWidth)){
Vx = -Vx;
}
//超过下边界且未被球拍接住
if((ballY + ballR > racketY) && (ballX + ballR < racketX || ballX > racketX + racket_width)){
timer.cancel();
isLose = true;
}
//被球拍打回或者反弹时到达顶部
if(ballY < 0 || (ballY + ballR > racketY && ballX > racketX && ballX < racketX + racket_width )){
Vy = -Vy;
}
ballX += Vx;
ballY += Vy;
handler.sendEmptyMessage(0x123);
}
}, 0, 10);

//给游戏view加监听器
game.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int mid = tableWidth/2;
switch (x > mid ? 1 : 2) {
case 1:
if(racketX + racket_width < tableWidth){
racketX += 10;
}
break;
case 2:
if(racketX > 0){
racketX -= 10;
}
break;
}
game.invalidate();
return true;
}
});

}

private void initXY(){
racketY = tableHeight - 80;
racketX = random.nextInt(tableWidth);
ballX = random.nextInt(tableWidth);
ballY = random.nextInt(10) + 20;
}

class GameView extends View{

private static final float TEXT_SIZE = 40;

public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
setFocusable(true);
}

@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
//游戏结束
if (isLose) {
paint.setTextSize(TEXT_SIZE);
paint.setColor(Color.RED);
canvas.drawText("Game Over!", tableWidth/2 - paint.getTextSize()*2, tableHeight/2 - paint.getTextSize()/2, paint);
}
//游戏继续
else {
paint.setColor(Color.BLUE);
canvas.drawCircle(ballX, ballY, ballR, paint);
paint.setColor(Color.GREEN);
canvas.drawRect(racketX, racketY, racketX + racket_width, racketY + racket_height, paint);

}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: