一、游戏界面展示
二、项目介绍
1、游戏中包括元素:Ball、Bat、Brick、Cell、Table,每一个元素对应一个类,GameView类,实现将所有元素在界面中的显示效果;
2、可通过手机的状态改变,通过判断旋转矢量传感器的值来改变Bat的状态。
三、代码实现
1、Ball类(球)
//绘制球体
public Ball() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mCenter = new Point(INIT_POS_CX, INIT_POS_CY);
mRadius = RADIUS;
mSpeed = new Point(0, 0);
}
//发射坐标
public void shot(int x, int y) {
mSpeed.x = x;
mSpeed.y = y;
}
//绘制圆
public void draw(Canvas canvas) {
canvas.drawCircle(mCenter.x, mCenter.y, mRadius, mPaint);
mCenter.offset(mSpeed.x, mSpeed.y);
}
//设置绘制位置
public void setPosition(int x, int y) {
mCenter.x = x;
mCenter.y = y;
}
2、Bat类(板)
public Bat() {
mPaint = new Paint();
mPaint.setColor(Color.MAGENTA);
mSpeed = DEFAULT_SPEED;
mWidth = DEFAULT_WIDTH;
}
//左移
public void moveLeft() {
mBody.offset(-mSpeed, 0);
}
//右移
public void moveRight() {
mBody.offset(mSpeed, 0);
}
//绘制板
public void draw(Canvas canvas) {
canvas.drawRect(mBody, mPaint);
}
//设置位置
public void setBodyPosition(Rect body) {
mBody = body;
}
3、Brick类(砖块)
//砖块颜色
private static int[] sBloodColors = {
Color.RED, Color.YELLOW, Color.GREEN
};
//定义砖块
public Brick(int row, int col, int width, int height, int blood) {
int left = col * width + BRICK_GAP / 2;
int right = left + width - 3 * BRICK_GAP / 2;
int top = row * height + BRICK_GAP;
int bottom = top + height - BRICK_GAP;
mBody = new Rect(left, top, right, bottom);
mBlood = blood;
this.row = row;
this.col = col;
}
//砖块绘制
@Override
public void draw(Canvas canvas) {
mPaint.setColor(sBloodColors[mBlood]);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(mBody, mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(mBody.left + BRICK_BORDER,
mBody.top + BRICK_BORDER,
mBody.right - BRICK_BORDER,
mBody.bottom - BRICK_BORDER,
mPaint);
}
//判断砖块数(碰撞则减一)
@Override
public boolean hit() {
mBlood--;
return mBlood < 0;
}
4、Table类
包含了音乐的加载,当球体与砖块碰撞时的音效设置
//碰撞点常量标志
private static final int HIT_NONE = 0;
private static final int HIT_TOP = 1;
private static final int HIT_RIGHT = 2;
private static final int HIT_BOTTOM = 4;
private static final int HIT_LEFT = 8;
//界面定义
public Table(Context context, Rect boundary) {
mPaintBoundary = new Paint();
mPaintBoundary.setStrokeWidth(6);
mPaintBoundary.setStyle(Paint.Style.STROKE);
mPaintBoundary.setColor(Color.GREEN);
mPaintGameOver = new Paint();
mPaintGameOver.setStyle(Paint.Style.FILL);
mPaintGameOver.setTextSize(78);
mPaintGameOver.setColor(Color.RED);
mBoundary = boundary;
mBoundaryPath = new Path();
mBoundaryPath.moveTo(boundary.left, boundary.bottom);
mBoundaryPath.lineTo(boundary.left, boundary.top);
mBoundaryPath.lineTo(boundary.right, boundary.top);
mBoundaryPath.lineTo(boundary.right, boundary.bottom);
loadSound(context);
}
//音乐加载
private void loadSound(Context context) {
mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
mAssetManager = context.getAssets();
try {
String[] filenames = mAssetManager.list("sounds");
for (String filename : filenames) {
AssetFileDescriptor fd = mAssetManager.openFd("sounds/" + filename);
int soundId = mSoundPool.load(fd, 0);
mSounds.add(soundId);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//界面绘制
public void draw(Canvas canvas) {
canvas.drawColor(Color.LTGRAY);
if (mShowGameOver) {
canvas.drawText("Game Over!", mBoundary.centerX() - 218, mBoundary.centerY(), mPaintGameOver);
} else if (mShowGamePass) {
canvas.drawText("过关了!", mBoundary.centerX() - 168, mBoundary.centerY(), mPaintGameOver);
}
// 绘制边界
canvas.drawPath(mBoundaryPath, mPaintBoundary);
// 绘制砖块
for (int row = 0; row < ROW_NUM; row++) {
for (int col = 0; col < COL_NUM; col++) {
Cell cell = mCells[row][col];
if (cell != null) {
cell.draw(canvas);
}
}
}// 判断球是否和边界碰撞(碰撞则改变移动的x.y为相反值)
int hitType = getHitType();
if ((hitType & (HIT_TOP | HIT_BOTTOM)) > 0) {
mBall.reverseYSpeed();
}
if ((hitType & (HIT_LEFT | HIT_RIGHT)) > 0) {
mBall.reverseXSpeed();
}
if (isBatHit() && mBall.isToBottom()) {
mBall.reverseYSpeed();
}
mBall.draw(canvas);
moveBat();
mBat.draw(canvas);
//获取球与球板的碰撞类型
private int getHitType() {
int type = HIT_NONE;
Point c = mBall.getCenter();
float r = mBall.getRadius();
int row = c.y / mCellHeight;
int col = c.x / mCellWidth;
Cell cell = null;
boolean hitCell = false;
Rect body = null;
boolean ballInTable = mBoundary.contains(c.x, c.y);
// 判断撞头
if (ballInTable && row > 0) {
cell = mCells[row - 1][col];
if (cell != null) {
body = cell.getBody();
hitCell = c.y > body.bottom && c.y - r <= body.bottom;
if (hitCell) {
playHitBrickSound(cell);
if (cell.hit()) {
mCells[cell.row][cell.col] = null;
}
}
}
}
if (mBall.isToTop() && (c.y - r <= 0 || hitCell)) {
type |= HIT_TOP;
}
// 判断撞右边
hitCell = false;
if (ballInTable && col < COL_NUM - 1) {
cell = mCells[row][col + 1];
if (cell != null) {
body = cell.getBody();
hitCell = c.x < body.left && c.x + r >= body.left;
if (hitCell) {
playHitBrickSound(cell);
if (cell.hit()) {
mCells[cell.row][cell.col] = null;
}
}
}
}
if (mBall.isToRight() &&
(c.x + r >= mBoundary.right && c.y < mBoundary.bottom || hitCell)) {
type |= HIT_RIGHT;
}
// 判断撞左边
hitCell = false;
if (ballInTable && col > 0) {
cell = mCells[row][col - 1];
if (cell != null) {
body = cell.getBody();
hitCell = c.x > body.right && c.x - r <= body.right;
if (hitCell) {
playHitBrickSound(cell);
if (cell.hit()) {
mCells[cell.row][cell.col] = null;
}
}
}
}
if (mBall.isToLeft() &&
((c.x - r <= 0 && c.y < mBoundary.bottom) || hitCell)) {
type |= HIT_LEFT;
}
// 判断撞下边
if (ballInTable && row < ROW_NUM - 1) {
cell = mCells[row + 1][col];
if (cell != null) {
body = cell.getBody();
hitCell = c.y < body.top && c.y + r >= body.top;
if (hitCell) {
playHitBrickSound(cell);
if (cell.hit()) {
mCells[cell.row][cell.col] = null;
}
}
}
}
if (mBall.isToBottom() && hitCell) {
type |= HIT_BOTTOM;
}
return type;
}
//通过触摸事件移动板
public void startBatMove(MotionEvent e) {
if (mBoundary.contains((int) e.getX(), (int) e.getY())) {
isBatMoving = true;
if (e.getX() > mBoundary.centerX()) { // move right
if (mBat.getBody().right < mBoundary.right) isBatMoveToLeft = false;
} else {
if (mBat.getBody().left > mBoundary.left) isBatMoveToLeft = true;
}
}
}
// 通过倾斜度改变板的形状
public void changeBatBody(double pitch) {
Rect body = mBat.getBody();
boolean wider = body.width() == mBoundary.width(); //板和边界宽度是否一致
boolean higher = body.height() > mNormalBatBody.height();//板是否比正常板的高度高
if (wider) {
if (pitch > -25) {//倾斜度判断
body.left = mNormalBatBody.left;
body.right = mNormalBatBody.right;
}
} else {
if (pitch < -30) {
body.left = mBoundary.left;
body.right = mBoundary.right;
}
}
if (higher) {
if (pitch < 10) {
body.top = mNormalBatBody.top;
}
} else {
if (pitch > 15) {
body.top = body.bottom - 10 * body.height();
}
}
}
public void stopBatMove() {
isBatMoving = false;
}
5、GameView类
第一步:获得传感器管理器
//实例化传感器管理对象
mSensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE);
第二步:为具体的传感器注册监听器
//监听传感器改变的采样率是否为适合游戏的速率
boolean ok = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
第三步:实现具体的监听方法
//监听传感器的值变化
@Override
public void onSensorChanged(SensorEvent event) {
//
if (!mIsRunning || mState != STATE_PLAY) return;
int sensorType = event.sensor.getType(); //存储传感器类型
float[] rotationMatrix;
switch (sensorType) {
case Sensor.TYPE_ROTATION_VECTOR: //为旋转矢量传感器(代表设备的方向)
rotationMatrix = new float[16];
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
float[] orientationValues = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationValues);
//倾斜度获取
double pitch = Math.toDegrees(orientationValues[1]);
double roll = Math.toDegrees(orientationValues[2]);
Log.e("mytag", "pitch = " + pitch + ", roll = " + roll);
//球板移动
mTable.startBatMove(roll);
//改变球板大小
mTable.changeBatBody(pitch);
break;
}
}
//判断触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //单点触碰
case MotionEvent.ACTION_MOVE: //触摸点移动动作
if (mIsRunning && mState == STATE_PLAY) {
//当状态为STATE_PLAY球开始移动(mIsRunning mState同为1)
mTable.startBatMove(event);
}
break;
case MotionEvent.ACTION_UP: //单点触摸离开动作
mTable.stopBatMove();
break;
}
mGestureDetector.onTouchEvent(event);
return true;
}
private class GameGestureDetector extends GestureDetector.SimpleOnGestureListener {
//点击界面,游戏开始或重置
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (mIsRunning) {
synchronized (mTable) {
if (mState == STATE_READY) {
mTable.shotBall();
mState = STATE_PLAY;
} else if (mState == STATE_OVER || mState == STATE_PASS) {
mState = STATE_READY;
//界面重置
mTable.reset();
}
}
}
return true;
}
}