【android遊戲】彈彈球
阿新 • • 發佈:2018-12-05
轉載請註明出處:https://blog.csdn.net/u011038298/article/details/84333562
1.首先建立一個繼承activity視窗的類,並且設定該視窗顯示的是自定義檢視
import android.os.Bundle; import android.view.Window; import android.app.Activity; import android.view.Display; import android.view.WindowManager; import android.app.ActivityManager; import android.content.pm.ActivityInfo; public class MainActivity extends Activity { public static int SCREEN_WIDTH; public static int SCREEN_HEIGHT; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 設定豎屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 隱藏標題 requestWindowFeature(Window.FEATURE_NO_TITLE); // 設定全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 獲取螢幕寬高 WindowManager wm = getWindowManager(); Display display = wm.getDefaultDisplay(); SCREEN_WIDTH = display.getWidth(); SCREEN_HEIGHT = display.getHeight(); // 設定視窗展示的介面檢視 setContentView(new GameView(this)); } }
2.遊戲的核心實現,自定義遊戲檢視。首先通過SurfaceHolder物件來拿到畫布,再啟動一個執行緒去繪製遊戲介面,接著我們又來現實onTouchEvent回撥函式,玩家通過點選螢幕來移動座標
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Paint; import android.content.Context; import android.graphics.Canvas; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; public class GameView extends SurfaceView implements Runnable, Callback { private SurfaceHolder holder = null; private boolean subThread = true; private Ball ball; private Player player; private int mark = 0; private Bitmap imgGameover; private boolean gameOver = false; private boolean clockwise = true; private int orientationState = 1; public GameView(Context context) { super(context); initData(); holder = getHolder(); holder.addCallback(this); setKeepScreenOn(true); requestFocusFromTouch(); } private void initData() { ball = new Ball(getContext()); player = new Player(getContext()); imgGameover = BitmapFactory.decodeResource(getResources(), R.drawable.gameover); } private void drawGameView() { Canvas canvas = null; if (!gameOver) { mark += 1; canvas = holder.lockCanvas(); if (canvas != null) { canvas.drawColor(Color.WHITE); ball.drawBall(canvas); player.drawBall(canvas); ballMoveLocation(); judgeBallImpactScreen(canvas); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setTextSize(15); paint.setColor(Color.GREEN); canvas.drawText("分數值: "+mark, 50, 20, paint); holder.unlockCanvasAndPost(canvas); } } } private void ballMoveLocation() { int ballSpeedX = 3; int ballSpeedY = 3; switch (orientationState) { case 1: ball.ballX += ballSpeedX; ball.ballY += ballSpeedY; break; case 2: ball.ballX -= ballSpeedX; ball.ballY += ballSpeedY; break; case 3: ball.ballX -= ballSpeedX; ball.ballY -= ballSpeedY; break; case 4: ball.ballX += ballSpeedX; ball.ballY -= ballSpeedY; break; case 5: ball.ballX -= ballSpeedX; ball.ballY += ballSpeedY; break; case 6: ball.ballX -= ballSpeedX; ball.ballY -= ballSpeedY; break; case 7: ball.ballX += ballSpeedX; ball.ballY -= ballSpeedY; break; case 8: ball.ballX += ballSpeedX; ball.ballY += ballSpeedY; break; } } private void judgeBallImpactScreen(Canvas canvas) { int random = (int)(Math.random()*10); if (clockwise) { if (ball.ballY <= 0) { orientationState = 1; if(random<=4) { clockwise = false; } } else if (ball.ballX + ball.imgBallX > getWidth()) { orientationState = 2; if(random<=4) { clockwise = false; } } else if (ball.ballY + ball.imgBallY >= player.playerY && ball.ballY + ball.imgBallY<=player.playerY+5) { if ((ball.ballX+ball.imgBallX >= player.playerX) && ball.ballX <= (player.playerX + player.imgPlayerW)) { orientationState = 3; if(random<=4) { clockwise = false; } } } else if (ball.ballX <= 0) { orientationState = 4; if(random<=4) { clockwise = false; } } if(ball.ballY>= getHeight()){ canvas.drawBitmap(imgGameover, 50, 150, null); canvas.save(); gameOver = true; } } else if (!clockwise) { if (ball.ballY <= 0) { orientationState = 5; if(random>4) { clockwise = true; } } else if (ball.ballX + ball.imgBallX > getWidth()) { orientationState = 6; if(random>4) { clockwise = true; } } else if (ball.ballY + ball.imgBallY >= player.playerY && ball.ballY + ball.imgBallY<=player.playerY+5) { if ((ball.ballX+ball.imgBallX >= player.playerX) && ball.ballX <= (player.playerX + player.imgPlayerW)) { orientationState = 7; if(random>4) { clockwise = true; } } } else if (ball.ballX <= 0) { orientationState = 8; if(random>4) { clockwise = true; } } if(ball.ballY>= getHeight()){ canvas.drawBitmap(imgGameover, 50, 150, null); canvas.save(); gameOver = true; } } } public void run() { while (subThread) { drawGameView(); try { Thread.sleep(10); } catch (InterruptedException e) { } } } @Override public boolean onTouchEvent(MotionEvent event) { float touchX = event.getX(); float touchY = event.getY(); if ((event.getAction() == MotionEvent.ACTION_DOWN) && touchY >= player.playerY) { if (touchX >= (player.playerX + player.imgPlayerW)) { player.playerX += 5; } else if (touchX <= player.playerX) { player.playerX -= 5; } } return super.onTouchEvent(event); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start(); } public void surfaceDestroyed(SurfaceHolder holder) { subThread = false; } }
3.建立球類
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.BitmapFactory; public class Ball { private Context context; public int ballX; public int ballY; public Bitmap imgBall; public int imgBallX; public int imgBallY; public Ball(Context c) { context = c; ballX = 100; ballY = 100; imgBall = BitmapFactory.decodeResource(context.getResources(), R.drawable.ball); imgBallX = imgBall.getWidth(); imgBallY = imgBall.getHeight(); } public void drawBall(Canvas canvas) { canvas.drawBitmap(imgBall, ballX, ballY, null); } }
4.建立玩家類
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class Player {
private Context context;
public int playerX;
public int playerY;
public Bitmap imgPlayer;
public int imgPlayerW;
public int imgPlayerH;
public Player(Context c) {
context = c;
imgPlayer = BitmapFactory.decodeResource(context.getResources(), R.drawable.player);
imgPlayerW = imgPlayer.getWidth();
imgPlayerH = imgPlayer.getHeight();
playerX = (int)((Main.SCREEN_WIDTH-imgPlayerW)/2);
playerY = (int)(Main.SCREEN_HEIGHT-imgPlayerH-10);
}
public void drawBall(Canvas canvas) {
canvas.drawBitmap(imgPlayer, playerX, playerY, null);
}
}
遊戲執行截圖: