Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sidm.mgpgame;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.provider.Settings;
- import android.util.DisplayMetrics;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import java.util.Random;
- import java.util.Vector;
- /**
- * Created by tansiewlan on 11/23/2015.
- */
- public class GamePanelSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
- // Implement this interface to receive information about changes to the surface.
- private GameThread myThread = null; // Thread to control the rendering
- // 1b) Define Screen width and Screen height as integer
- int Screenwidth, Screenheight;
- // Define Paint Object
- Paint paint = new Paint();
- // Variables for FPS
- public float FPS;
- float deltaTime;
- long dt;
- enum GameState
- {
- GS_LOAD,
- GS_READY,
- GS_PLAY,
- GS_PAUSE,
- GS_END,
- }
- GameState state;
- //HighScore Variables
- HighScore GamePlayScore;
- //Card Variable
- Bitmap RedCard;
- Bitmap GreenCard;
- Bitmap BlueCard;
- Bitmap ClinkedCard;
- Card card[];
- //ColourBox Variables
- Bitmap RedCardHolder;
- Bitmap GreenCardHolder;
- Bitmap BlueCardHolder;
- Bitmap ClinkedCardHolder;
- CardHolder cardholder[];
- //constructor for this GamePanelSurfaceView class
- public GamePanelSurfaceView(Context context) {
- // Context is the current state of the application/object
- super(context);
- // Adding the callback (this) to the surface holder to intercept events
- getHolder().addCallback(this);
- // 1d) Set information to get screen size
- DisplayMetrics metrics = context.getResources().getDisplayMetrics();
- Screenwidth = metrics.widthPixels;
- Screenheight = metrics.heightPixels;
- // Create the game loop thread
- myThread = new GameThread(getHolder(), this);
- state = GameState.GS_PLAY;
- //Initialise GameObjects
- GamePlayScore = new HighScore(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_highscore),Screenwidth/4, Screenheight/4, true),Screenwidth - Screenwidth/8*5,Screenheight -Screenheight,0);
- RedCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_red_card),Screenwidth/8, Screenheight/10, true);
- GreenCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_green_card),Screenwidth/8, Screenheight/10, true);
- BlueCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_blue_card),Screenwidth/8, Screenheight/10, true);
- ClinkedCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.star),Screenwidth/8, Screenheight/10, true);
- card = new Card[3];
- card[0] = new Card(RedCard,GreenCard,BlueCard,ClinkedCard,Card.Color.Red,Card.Number.CLINKED,Screenwidth-Screenwidth/8 * 3, Screenheight/4 * 3, false);
- card[1] = new Card(RedCard,GreenCard,BlueCard,ClinkedCard,Card.Color.Blue,Card.Number.ONE,Screenwidth-Screenwidth/8 * 5, Screenheight/4 * 3, false);
- card[2] = new Card(RedCard,GreenCard,BlueCard,ClinkedCard,Card.Color.Green,Card.Number.NINE,Screenwidth-Screenwidth/8 * 7, Screenheight/4 * 3, false);
- RedCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_red_card_holder),Screenwidth/5, Screenheight/7, true);
- GreenCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_green_card_holder),Screenwidth/5, Screenheight/7, true);
- BlueCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_blue_card_holder),Screenwidth/5, Screenheight/7, true);
- ClinkedCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.star_card_holder), Screenwidth / 5, Screenheight / 7, true);
- cardholder = new CardHolder[3];
- cardholder[0] = new CardHolder(RedCardHolder,GreenCardHolder,BlueCardHolder,ClinkedCardHolder, CardHolder.FrameColour.Red, (Screenwidth - Screenwidth / 8 * 3), Screenheight/4,card[0]);
- cardholder[1] = new CardHolder(RedCardHolder,GreenCardHolder,BlueCardHolder,ClinkedCardHolder, CardHolder.FrameColour.Blue, Screenwidth-Screenwidth/8 * 5, Screenheight/4,card[1]);
- cardholder[2] = new CardHolder(RedCardHolder,GreenCardHolder,BlueCardHolder,ClinkedCardHolder, CardHolder.FrameColour.Green, Screenwidth-Screenwidth/8 * 7, Screenheight/4,card[2]);
- // Make the GamePanel focusable so it can handle events
- setFocusable(true);
- }
- //must implement inherited abstract methods
- public void surfaceCreated(SurfaceHolder holder) {
- // Create the thread
- if (!myThread.isAlive()) {
- myThread = new GameThread(getHolder(), this);
- myThread.startRun(true);
- myThread.start();
- }
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- // Destroy the thread
- if (myThread.isAlive()) {
- myThread.startRun(false);
- }
- boolean retry = true;
- while (retry) {
- try {
- myThread.join();
- retry = false;
- } catch (InterruptedException e) {
- }
- }
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- }
- public boolean checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
- if (x2 > x1+w1) // too right
- return false;
- if (y2+h2 < y1)
- return false; // too bottom
- if (y2 > y1+h1) //too high
- return false;
- if (x2+w2 < x1) // too left
- return false;
- return true;
- }
- public void RenderGameplay(Canvas canvas) {
- // 2) Re-draw 2nd image after the 1st image ends
- if (canvas == null) {
- return;
- }
- //Render HighScore
- //Image
- canvas.drawBitmap(GamePlayScore.texture, GamePlayScore.posX, GamePlayScore.posY, null);
- //Score
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + GamePlayScore.Score, GamePlayScore.posX + 110, GamePlayScore.posY + 250, paint);
- //Render Card
- for(int i = 0; i < 3 ; ++i) {
- switch (card[i].color) {
- case Red: {
- canvas.drawBitmap(card[i].R_texture, card[i].posX, card[i].posY, null);
- break;
- }
- case Green: {
- canvas.drawBitmap(card[i].G_texture, card[i].posX, card[i].posY, null);
- break;
- }
- case Blue: {
- canvas.drawBitmap(card[i].B_texture, card[i].posX, card[i].posY, null);
- break;
- }
- }
- //Box1 Number
- switch (card[i].number) {
- case CLINKED: {
- canvas.drawBitmap(card[i].Clinked_texture, card[i].posX, card[i].posY, null);
- break;
- }
- case ONE: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 1, card[i].posX, card[i].posY, paint);
- break;
- }
- case TWO: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 2, card[i].posX, card[i].posY, paint);
- break;
- }
- case THREE: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 3, card[i].posX, card[i].posY, paint);
- break;
- }
- case FOUR: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 4, card[i].posX, card[i].posY, paint);
- break;
- }
- case FIVE: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 5, card[i].posX, card[i].posY, paint);
- break;
- }
- case SIX: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 6, card[i].posX, card[i].posY, paint);
- break;
- }
- case SEVEN: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 7, card[i].posX, card[i].posY, paint);
- break;
- }
- case EIGHT: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 8, card[i].posX, card[i].posY, paint);
- break;
- }
- case NINE: {
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(200);
- paint.setTextSize(30);
- canvas.drawText("" + 9, card[i].posX, card[i].posY, paint);
- break;
- }
- }
- }
- //Render Card Holder
- for(int i = 0 ; i < 3; ++i)
- {
- switch(cardholder[i].framecolour)
- {
- case Red:
- {
- canvas.drawBitmap(cardholder[i].R_texture, cardholder[i].posX, cardholder[i].posY, null);
- break;
- }
- case Blue:
- {
- canvas.drawBitmap(cardholder[i].B_texture, cardholder[i].posX, cardholder[i].posY, null);
- break;
- }
- case Green:
- {
- canvas.drawBitmap(cardholder[i].G_texture, cardholder[i].posX, cardholder[i].posY, null);
- break;
- }
- }
- }
- // Bonus) To print FPS on the screen
- paint.setARGB(255, 255, 255, 255);
- paint.setStrokeWidth(100);
- paint.setTextSize(50);
- canvas.drawText("FPS =" + FPS, Screenwidth/2, Screenheight/2, paint);
- }
- //Update method to update the game play
- public void update(float dt, float fps) {
- FPS = fps;
- }
- // Rendering is done on Canvas
- public void doDraw(Canvas canvas) {
- RenderGameplay(canvas);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // 5) In event of touch on screen, the spaceship will relocate to the point of touch
- short X = (short) event.getX();
- short Y = (short) event.getY();
- switch(event.getAction())
- {
- case MotionEvent.ACTION_DOWN:
- {
- if(checkCollision(X,Y,50,50,card[0].posX,card[0].posY,150,150))
- {
- card[0].isPressed = true;
- }
- return true;
- }
- case MotionEvent.ACTION_UP:
- {
- if(checkCollision(cardholder[0].posX,cardholder[0].posY,50,50,card[0].posX,card[0].posY,150,150)) {
- }
- card[0].isPressed = false;
- card[0].posX = card[0].SpawnLocationX;
- card[0].posY = card[0].SpawnLocationY;
- return true;
- }
- case MotionEvent.ACTION_MOVE:
- {
- if(card[0].isPressed == true)
- {
- card[0].posX = X-50;
- card[0].posY = Y-50;
- }
- return true;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement