简易弹球游戏 (2)(1)
public int baffleWith;
//每份宽
int perWidth = 0;
//间隔宽
int InterWidth = 0;
//每份高
int perHeight = 0;
//间隔高
int InterHeight = 0;
//挡板层数
public int baffleLev = 8;
//每层挡板数
public int baffleNum = 5;
//挡板颜色
public int[] colors = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW};
//挡板总数
public int baffleTotle = baffleLev * baffleNum;
//障碍物 X 坐标
int baffleX [] = null;
//障碍物 Y 坐标
int baffleY [] = null;
//障碍物标签
int baffleArr[][] = null;
/*画障碍物/
public void drawBaffle(Canvas canvas){
//屏幕宽高
screenWidth = screenWidth != 0 ? screenWidth : MainActivity.tableWidth;
screenHeight = screenHeight != 0 ? screenHeight: MainActivity.tableHeight;
//宽
perWidth = perWidth != 0 ? perWidth : screenWidth/(baffleNum+2);
//间隔宽
InterWidth = InterWidth != 0 ? InterWidth : perWidth*2/(baffleNum+1);
int tempLev = baffleLev/2;
//每份高
perHeight = perHeight != 0 ? perHeight : screenHeight/2/(colors.length+tempLev+2);
//间隔高
InterHeight = InterHeight !=0 ? InterHeight : perHeight/2;
int tempHeight = 0;
int tempH = 0;
int tempWidht = 0;
int tempW = 0;
//屏幕宽
public static int tableWidth;
//屏幕高
public static int tableHeight;
//常亮属性
PowerManager powerManager = null;
WakeLock wakeLock = null;
MediaPlayer media = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置常亮
this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
//获取窗口管理器
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
//获取屏幕宽和高
tableWidth = metrics.widthPixels;
tableHeight = metrics.heightPixels;
2.设置按钮属性
//按钮宽和高
int botwidth = (int) ((int) tableWidth/2.5);
int botheight = (int) ((int) tableHeight/8);
//设置 play
ImageButton playbot =(ImageButton) findViewById(R.id.play);
ViewGroup.LayoutParams playbotn = (ViewGroup.LayoutParams) playbot.getLayoutParams();
playbotn.width = botwidth;
playbotn.height = botheight;
playbot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View source) {
Intent intent = new Intent(MainActivity.this, PlayActivity.class);
startActivity(intent);
}
});
//设置 exit
ImageButton exitbot =(ImageButton) findViewById(R.id.exit);
ViewGroup.LayoutParams exitbotn = (ViewGroup.LayoutParams) exitbot.getLayoutParams();
exitbotn.width = botwidth;
exitbotn.height = botheight;
3.设置游戏
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//结束判断
if( (playView.circleY + playView.radius) > (playView.paiY + playView.paiHeight) ){
timer.cancel();
pl
ayView.isOver = true;
}else{
//碰到墙壁时 X 的改变
if( playView.circleX <= playView.radius
|| playView.circleX >= playView.screenWidth - playView.radius ){
playView.speedX = -playView.speedX;
}
//碰到墙壁时 Y 的改变
if( playView.circleY <= playView.radius
|| ( playView.circleX >= playView.paiX
&& playView.circleX <= (playView.paiX + playView.paiWidth)
&& (playView.circleY + playView.radius) >= playView.paiY
&& (playView.circleY + playView.radius) <= playView.paiY + playView.paiHeight) ){
playView.speedY = -playView.speedY;
}
//初始化后才判断
if( playView.baffleX != null ){
//碰到障碍时 X 的改变
for( int i = 0; i < playView.baffleLev; i++ ){
int tag = 0;
if( playView.circleY >= playView.baffleY[i]
&& playView.circleY <= (playView.baffleY[i] + playView.perHeight) ){
for( int j = 0; j < playView.baffleNum; j++ ){
if( playView.baffleArr[j][i] == 0 ){
if( playView.circleX >= (playView.baffleX[j] - playView.radius)
&& playView.circleX <= (playView.baffleX[j] + playView.perWidth + playView.radius) ){
playView.speedX = -playView.speedX;
playView.baffleArr[j][i] = 1;
评论