簡單彈球游戲
一.小游戲功能描述
功能一:進入小游戲界面,點擊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);
}
三.效果截圖
功能一:

功能二:


功能三:

作者:鄧雅心
原文鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/244748.html
標籤:其他
上一篇:Xlua移動端控制物體旋轉和縮放
下一篇:Pygame(五)畫線
