【android游戏】弹弹球

转载请注明出处: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);
   }
   
}

游戏运行截图:

猜你喜欢

转载自blog.csdn.net/u011038298/article/details/84333562