Android弹球小游戏

Android弹球小游戏_第1张图片

package com.example.gamingball;


import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;


public class MainActivity extends Activity {


//判断游戏是否结束
private boolean isLost=false;


//定义球的坐标
private int ballX=(int)(Math.random()*200)+10;
private int ballY=(int)(Math.random()*10)+10;
private final int BALL_SIZE=10;
//定义球在x,y方向上的速度
private int ballXSpeed=22;
private int ballYSpeed=22;



//定义方块的坐标、宽度和高度
private int rectX;
private int rectY;
private final int RECT_WITH=90;
private final int RECT_HEIGHT=70;

private int screenWidth;
private int screenHeight;

//定义砖块一次移动的距离
private final int BLOCK_STEP=10;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉窗口标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//获取窗口管理器
WindowManager manager=getWindowManager();
Display display=manager.getDefaultDisplay();

screenWidth=display.getWidth();
screenHeight=display.getHeight();

rectX=screenWidth/2-RECT_WITH/2;
rectY=screenHeight-25;

//创建GameView组件
final GameView gameView=new GameView(MainActivity.this);


setContentView(gameView);


//创建Handler
@SuppressWarnings("unused")
final Handler handler=new Handler()
{


@Override
public void handleMessage(Message msg) {
if(msg.what==0x123){
gameView.invalidate();
}
}

};



//为GameView添加事件监听
gameView.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View source, int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
//挡板左移
case KeyEvent.KEYCODE_A:
if(rectX>BLOCK_STEP)
rectX-=BLOCK_STEP;
break;
case KeyEvent.KEYCODE_D:
if(rectXrectX+=BLOCK_STEP;
break;
}

gameView.invalidate();
return true;
}
});



//设置监听器
final Timer timer=new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
//如果小球碰到了左右边框
if(ballX<0||ballX>screenWidth-BALL_SIZE){
ballXSpeed=-ballXSpeed;
}
//如果小球碰到了上下边框
if(ballY<0){
ballYSpeed=-ballYSpeed;
}
//如果小球碰到了挡板
if(ballY+BALL_SIZE>rectY&&ballX+BALL_SIZE>rectX&&ballX<(rectX+RECT_WITH)&&ballY<(rectY+RECT_HEIGHT)){
ballYSpeed=-ballYSpeed;
}
else if(ballY>screenHeight-BALL_SIZE){
isLost=true;
}

//小球坐标增加
ballX+=ballXSpeed;
ballY+=ballYSpeed;

//发送消息
handler.sendEmptyMessage(0x123);
}
},0,100);

}


class GameView extends View{


Paint paint=new Paint();

public GameView(Context context) {
super(context);
setFocusable(true);
}


@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
//去锯齿
paint.setAntiAlias(true);
//如果游戏已经结束
if(isLost){
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("游戏已结束", 50, 200, paint);
}
//如果游戏还未结束
else {
paint.setColor(Color.rgb(240, 240, 80));
canvas.drawCircle(ballX, ballY, BALL_SIZE, paint);

paint.setColor(Color.rgb(80, 80, 200));
canvas.drawRect(rectX, rectY, rectX+RECT_WITH, rectY+RECT_HEIGHT, paint);
}
}
}
}

你可能感兴趣的:(Android)