簡單彈球遊戲
一.小遊戲功能描述
功能一:進入小遊戲界面,點擊play,遊戲開始,點擊exit可繼續上一局遊戲。
功能二:小球和球拍分別以圓形區域和矩形區域代替,小球開始以隨機速度向下運動,遇到上方的障礙或下方的球拍時小球反彈,球拍由用戶控制,球拍按左右鍵時,球拍會左右移動。
功能三:若沒有接住球,遊戲失敗,則屏幕顯示goodbye。
二.過程及重要代碼
1.定義屏幕、球拍、球、障礙並設置屬性
//屏幕寬高
int screenWidth = 0;
int screenHeight = 0;
//障礙擋板寬
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();
playView.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;
playView.baffleTotle--;
tag = 1;
break;
}
}
}
if( tag == 1 ) break;
}
}
//碰到障礙時Y的改變
for( int i = 0; i < playView.baffleNum; i++ ){
int tag = 0;
if( playView.circleX >= playView.baffleX[i]
&& playView.circleX <= ( playView.baffleX[i] + playView.perWidth ) ){
for( int j = 0; j < playView.baffleLev; j++ ){
if( playView.baffleArr[i][j] == 0 ){
if( playView.circleY >= (playView.baffleY[j] - playView.radius)
&& playView.circleY <= (playView.baffleY[j] + playView.perHeight+ playView.radius) ){
playView.speedY = -playView.speedY;
playView.baffleArr[i][j] = 1;
playView.baffleTotle--;
tag = 1;
break;
}
}
}
if( tag == 1) break;
}
}
}
playView.circleX += playView.speedX;
playView.circleY += playView.speedY;
}
handler.sendEmptyMessage(0x123);
}
}, 0, 10);
}
三.效果截圖
功能一:
功能二:
功能三:
作者:鄧雅心
原文鏈接