Android打砖块功能模块设计,android心路(四) 弹球游戏(打砖块)
粗浅的碰撞检测,这个游戏基本功能实现了,这几天太累了,休息2天准备直接看现成的框架了。import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.view.SurfaceView;//桌面弹球游戏父类public class BallComponet {// 设置x坐标p
粗浅的碰撞检测,这个游戏基本功能实现了,这几天太累了,休息2天准备直接看现成的框架了。
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.SurfaceView;
//桌面弹球游戏父类
public class BallComponet {
// 设置x坐标
private int x = -1;
// 设置y坐标
private int y = -1;
// 设置图片
private Bitmap mImg =
null;
// 设置图片速度
private int speed = 5;
public BallComponet(int path,SurfaceView
view){
super();
this.mImg = ((BitmapDrawable)
view.getResources().getDrawable(path)).getBitmap();
}
public BallComponet(int path,SurfaceView view,int
x,int y){
super();
this.mImg = ((BitmapDrawable)
view.getResources().getDrawable(path)).getBitmap();
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getSpeed() {
return this.speed;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Bitmap getImage() {
return this.mImg;
}
}
砖块类,继承自父类
import android.view.SurfaceView;
public class Brick extends BallComponet {
// 定义一个boolean变量设置本类是否有效
private boolean disable = false;
public Brick(int path, SurfaceView view,int x,
int y) {
super(path, view,x,y);
// TODO Auto-generated
constructor stub
}
public void setDisable(boolean disable) {
this.disable = disable;
}
public boolean isDisable() {
return this.disable;
}
}
//小球类,同样继承
import android.view.SurfaceView;
public class Ball extends BallComponet {
private int SpeedX = 0;
private int SpeedY = 0;
private boolean isStarted = false;
public Ball(int path, SurfaceView view, int x,
int y) {
super(path, view, x, y);
// TODO Auto-generated
constructor stub
}
public void setSpeedX(int speedX) {
this.SpeedX = speedX;
}
public int getSpeedX() {
return this.SpeedX;
}
public void setSpeedY(int speedY) {
this.SpeedY = speedY;
}
public int getSpeedY() {
return this.SpeedY;
}
public void setStarted(boolean isStarted) {
this.isStarted =
isStarted;
}
public boolean isStarted() {
return this.isStarted;
}
}
import android.view.SurfaceView;
//挡板类
public class Stick extends BallComponet {
// 定义档板移动的速度
public static final int SPEED = 20;
// 定义档板初始的长度
private int preWidth = 0;
//定义画布宽度
private int ScreenWidth = 320;
public Stick(int path, SurfaceView view,int x,
int y ) {
super(path, view,x,y);
// TODO Auto-generated
constructor stub
this.preWidth =
super.getImage().getWidth();
}
public int getPreWidth() {
return this.preWidth;
}
public void setPreWidth(int preWidth) {
this.preWidth = preWidth;
}
public void Left(){
int x = this.getX();
if(x<=0){
return;
}
this.setX(x-SPEED);
}
public void Right(){
int x = this.getX();
if(x>=ScreenWidth-this.getImage().getWidth()){
return;
}
this.setX(x+SPEED);
}
}
游戏逻辑处理以及显示类
import com.ball.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class GameSurFaceView extends SurfaceView
implements SurfaceHolder.Callback,Runnable{
//控制循环
boolean mbLoop =
false;
//刷新频率
int speed = 20;
//定义SurfaceHolder对象
SurfaceHolder mSurfaceHolder =
null;
//定义屏幕宽高
int ScreenWidth =0 ;
int ScreenHeight = 0;
//初始化砖块类
int brickSizeX = 5;
int brickSizeY = 11;
Brick[][] mybrick = new
Brick[brickSizeX][brickSizeY];
//初始化托盘类
Stick mystick = null;
//初始化小球类
Ball myball = null;
//初始化游戏状态
String gameStatu ="按中键开始游戏";
public GameSurFaceView(Context context) {
super(context);
// TODO Auto-generated
constructor stub
mSurfaceHolder =
this.getHolder();
// 添加回调
mSurfaceHolder.addCallback(this);
this.setFocusable(true);
mbLoop = true;
}
public void surfaceChanged(SurfaceHolder arg0,
int arg1, int arg2, int arg3) {
// TODO Auto-generated method
stub
}
public void surfaceCreated(SurfaceHolder arg0)
{
// TODO Auto-generated method
stub
//初始化屏幕宽高
DisplayMetrics dm = new
DisplayMetrics();
ScreenWidth = 320;
ScreenHeight = 470;
//初始化砖块
for(int
i=0;i
for(int
j=0;j
mybrick[i][j]
= new Brick(R.drawable.brick, this, 0, 0);
mybrick[i][j].setX(mybrick[i][j].getImage().getWidth()*j);
mybrick[i][j].setY(mybrick[i][j].getImage().getHeight()*i);
//设置随机砖块
boolean
isDisable = Math.random() > 0.8 ? true :
false;
mybrick[i][j].setDisable(isDisable);
}
}
mystick = new
Stick(R.drawable.stick, this, 100, 350);
myball = new
Ball(R.drawable.ball, this,0,0);
//初始化小球位置
myball.setX(mystick.getX()+mystick.getImage().getWidth()/2);
myball.setY(mystick.getY()-myball.getImage().getHeight());
//初始化球速度
myball.setSpeedX(10);
myball.setSpeedY(-5);
//绑定事件
this.setOnKeyListener(onkeylistener);
//开启绘图线程
new Thread(this).start();
//为球的运动单独开启一个线程
ballThread td1 = new
ballThread();
Thread ballTd = new
Thread(td1);
ballTd.start();
}
public void surfaceDestroyed(SurfaceHolder
arg0) {
// TODO Auto-generated method
stub
mbLoop = false;
}
class
ballThread extends Thread {
public void run(){
while (mbLoop)
{
try
{
Thread.sleep(speed);
}
catch
(Exception e)
{
}
synchronized(
mSurfaceHolder )
{
ballmove();
if(isWin()){
gameStatu="胜利啦";
}
}
}
}
}
// 绘图循环
public void run()
{
while (mbLoop)
{
try
{
Thread.sleep(speed);
}
catch
(Exception e)
{
}
synchronized(
mSurfaceHolder )
{
Draw();
}
}
}
// 绘图方法
public void Draw()
{
Canvas canvas=
mSurfaceHolder.lockCanvas();
canvas.drawColor(Color.GRAY);
for(int
i=0;i
for(int
j=0;j
if(!mybrick[i][j].isDisable()){
drawImage(canvas,
mybrick[i][j].getImage(), mybrick[i][j].getX(),
mybrick[i][j].getY());
}
}
}
drawImage(canvas,
mystick.getImage(), mystick.getX(), mystick.getY());
drawImage(canvas,
myball.getImage(), myball.getX(), myball.getY());
Paint mypt = new Paint();
mypt.setColor(Color.RED);
drawText(canvas, gameStatu, 0,
mystick.getY()+mystick.getImage().getHeight()+20, mypt);
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
//球运动函数
public void ballmove(){
if(myball.isStarted()){
//定义每次位移量
int ballPad
=2;
myball.setX(myball.getX()+myball.getSpeedX());
myball.setY(myball.getY()+myball.getSpeedY());
//检测球是否与画板边缘碰撞
int ballx =
myball.getX();
int bally =
myball.getY();
if(ballx<=0||ballx>=ScreenWidth
- myball.getImage().getWidth()){
myball.setSpeedX(-myball.getSpeedX()+ballPad);
}
if(bally<=0){
myball.setSpeedY(-myball.getSpeedY()+ballPad);
}
if(bally>=ScreenHeight){
//游戏失败 gameStatu="游戏失败";
mbLoop = true;
}
if(isHitStick(myball,
mystick)){
myball.setSpeedY(-myball.getSpeedY()+ballPad);
}
for(int
i=0;i
for(int
j=0;j
if(isHitBrick(myball,
mybrick[i][j])){
myball.setSpeedX(-myball.getSpeedX()+ballPad);
myball.setSpeedY(-myball.getSpeedY()+ballPad);
}
}
}
}
}
//监视键盘事件
public OnKeyListener onkeylistener = new
OnKeyListener() { public boolean onKey(View v,
int keyCode, KeyEvent event) {
// TODO
Auto-generated method stub
更多推荐
所有评论(0)