Advertisement
Guest User

tst

a guest
Feb 6th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 12.81 KB | None | 0 0
  1. package com.sidm.mgpgame;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Paint;
  9. import android.provider.Settings;
  10. import android.util.DisplayMetrics;
  11. import android.view.MotionEvent;
  12. import android.view.SurfaceHolder;
  13. import android.view.SurfaceView;
  14.  
  15. import java.util.Random;
  16. import java.util.Vector;
  17.  
  18. /**
  19.  * Created by tansiewlan on 11/23/2015.
  20.  */
  21.  
  22. public class GamePanelSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
  23.     // Implement this interface to receive information about changes to the surface.
  24.  
  25.     private GameThread myThread = null; // Thread to control the rendering
  26.  
  27.     // 1b) Define Screen width and Screen height as integer
  28.     int Screenwidth, Screenheight;
  29.  
  30.     // Define Paint Object
  31.     Paint paint = new Paint();
  32.  
  33.     // Variables for FPS
  34.     public float FPS;
  35.     float deltaTime;
  36.     long dt;
  37.  
  38.     enum GameState
  39.     {
  40.         GS_LOAD,
  41.         GS_READY,
  42.         GS_PLAY,
  43.         GS_PAUSE,
  44.         GS_END,
  45.     }
  46.     GameState state;
  47.  
  48.     //HighScore Variables
  49.     HighScore GamePlayScore;
  50.  
  51.     //Card Variable
  52.     Bitmap RedCard;
  53.     Bitmap GreenCard;
  54.     Bitmap BlueCard;
  55.     Bitmap ClinkedCard;
  56.     Card card[];
  57.  
  58.     //ColourBox Variables
  59.     Bitmap RedCardHolder;
  60.     Bitmap GreenCardHolder;
  61.     Bitmap BlueCardHolder;
  62.     Bitmap ClinkedCardHolder;
  63.  
  64.     CardHolder cardholder[];
  65.  
  66.     //constructor for this GamePanelSurfaceView class
  67.     public GamePanelSurfaceView(Context context) {
  68.         // Context is the current state of the application/object
  69.         super(context);
  70.  
  71.         // Adding the callback (this) to the surface holder to intercept events
  72.         getHolder().addCallback(this);
  73.  
  74.         // 1d) Set information to get screen size
  75.         DisplayMetrics metrics = context.getResources().getDisplayMetrics();
  76.         Screenwidth = metrics.widthPixels;
  77.         Screenheight = metrics.heightPixels;
  78.  
  79.         // Create the game loop thread
  80.         myThread = new GameThread(getHolder(), this);
  81.  
  82.  
  83.  
  84.         state = GameState.GS_PLAY;
  85.  
  86.         //Initialise GameObjects
  87.         GamePlayScore = new HighScore(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_highscore),Screenwidth/4, Screenheight/4, true),Screenwidth - Screenwidth/8*5,Screenheight -Screenheight,0);
  88.  
  89.  
  90.         RedCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_red_card),Screenwidth/8, Screenheight/10, true);
  91.         GreenCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_green_card),Screenwidth/8, Screenheight/10, true);
  92.         BlueCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_blue_card),Screenwidth/8, Screenheight/10, true);
  93.         ClinkedCard = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.star),Screenwidth/8, Screenheight/10, true);
  94.  
  95.         card = new Card[3];
  96.         card[0]  = new Card(RedCard,GreenCard,BlueCard,ClinkedCard,Card.Color.Red,Card.Number.CLINKED,Screenwidth-Screenwidth/8 * 3, Screenheight/4 * 3, false);
  97.         card[1]  = new Card(RedCard,GreenCard,BlueCard,ClinkedCard,Card.Color.Blue,Card.Number.ONE,Screenwidth-Screenwidth/8 * 5, Screenheight/4 * 3, false);
  98.         card[2]  = new Card(RedCard,GreenCard,BlueCard,ClinkedCard,Card.Color.Green,Card.Number.NINE,Screenwidth-Screenwidth/8 * 7, Screenheight/4 * 3, false);
  99.  
  100.  
  101.         RedCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_red_card_holder),Screenwidth/5, Screenheight/7, true);
  102.         GreenCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_green_card_holder),Screenwidth/5, Screenheight/7, true);
  103.         BlueCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gp_blue_card_holder),Screenwidth/5, Screenheight/7, true);
  104.         ClinkedCardHolder = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.star_card_holder), Screenwidth / 5, Screenheight / 7, true);
  105.  
  106.         cardholder = new CardHolder[3];
  107.         cardholder[0] = new CardHolder(RedCardHolder,GreenCardHolder,BlueCardHolder,ClinkedCardHolder, CardHolder.FrameColour.Red, (Screenwidth - Screenwidth / 8 * 3), Screenheight/4,card[0]);
  108.         cardholder[1] = new CardHolder(RedCardHolder,GreenCardHolder,BlueCardHolder,ClinkedCardHolder, CardHolder.FrameColour.Blue, Screenwidth-Screenwidth/8 * 5, Screenheight/4,card[1]);
  109.         cardholder[2] = new CardHolder(RedCardHolder,GreenCardHolder,BlueCardHolder,ClinkedCardHolder, CardHolder.FrameColour.Green, Screenwidth-Screenwidth/8 * 7, Screenheight/4,card[2]);
  110.  
  111.         // Make the GamePanel focusable so it can handle events
  112.         setFocusable(true);
  113.     }
  114.  
  115.     //must implement inherited abstract methods
  116.     public void surfaceCreated(SurfaceHolder holder) {
  117.         // Create the thread
  118.         if (!myThread.isAlive()) {
  119.             myThread = new GameThread(getHolder(), this);
  120.             myThread.startRun(true);
  121.             myThread.start();
  122.         }
  123.     }
  124.  
  125.     public void surfaceDestroyed(SurfaceHolder holder) {
  126.         // Destroy the thread
  127.         if (myThread.isAlive()) {
  128.             myThread.startRun(false);
  129.  
  130.  
  131.         }
  132.         boolean retry = true;
  133.         while (retry) {
  134.             try {
  135.                 myThread.join();
  136.                 retry = false;
  137.             } catch (InterruptedException e) {
  138.             }
  139.         }
  140.     }
  141.  
  142.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  143.  
  144.     }
  145.  
  146.     public boolean checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
  147.  
  148.         if (x2 > x1+w1) // too right
  149.             return false;
  150.  
  151.         if (y2+h2 < y1)
  152.             return false; // too bottom
  153.  
  154.         if (y2 > y1+h1) //too high
  155.             return false;
  156.  
  157.         if (x2+w2 < x1) // too left
  158.             return false;
  159.  
  160.         return true;
  161.     }
  162.  
  163.      public void RenderGameplay(Canvas canvas) {
  164.          // 2) Re-draw 2nd image after the 1st image ends
  165.          if (canvas == null) {
  166.              return;
  167.          }
  168.  
  169.          //Render HighScore
  170.  
  171.          //Image
  172.          canvas.drawBitmap(GamePlayScore.texture, GamePlayScore.posX, GamePlayScore.posY, null);
  173.  
  174.          //Score
  175.          paint.setARGB(255, 255, 255, 255);
  176.          paint.setStrokeWidth(200);
  177.          paint.setTextSize(30);
  178.          canvas.drawText("" + GamePlayScore.Score, GamePlayScore.posX + 110, GamePlayScore.posY + 250, paint);
  179.  
  180.          //Render Card
  181.          for(int i = 0; i < 3 ; ++i) {
  182.              switch (card[i].color) {
  183.                  case Red: {
  184.                      canvas.drawBitmap(card[i].R_texture, card[i].posX, card[i].posY, null);
  185.                      break;
  186.                  }
  187.                  case Green: {
  188.                      canvas.drawBitmap(card[i].G_texture, card[i].posX, card[i].posY, null);
  189.                      break;
  190.                  }
  191.                  case Blue: {
  192.                      canvas.drawBitmap(card[i].B_texture, card[i].posX, card[i].posY, null);
  193.                      break;
  194.                  }
  195.              }
  196.              //Box1 Number
  197.              switch (card[i].number) {
  198.                  case CLINKED: {
  199.                      canvas.drawBitmap(card[i].Clinked_texture, card[i].posX, card[i].posY, null);
  200.                      break;
  201.                  }
  202.                  case ONE: {
  203.                      paint.setARGB(255, 255, 255, 255);
  204.                      paint.setStrokeWidth(200);
  205.                      paint.setTextSize(30);
  206.                      canvas.drawText("" + 1, card[i].posX, card[i].posY, paint);
  207.                      break;
  208.                  }
  209.                  case TWO: {
  210.                      paint.setARGB(255, 255, 255, 255);
  211.                      paint.setStrokeWidth(200);
  212.                      paint.setTextSize(30);
  213.                      canvas.drawText("" + 2, card[i].posX, card[i].posY, paint);
  214.                      break;
  215.                  }
  216.                  case THREE: {
  217.                      paint.setARGB(255, 255, 255, 255);
  218.                      paint.setStrokeWidth(200);
  219.                      paint.setTextSize(30);
  220.                      canvas.drawText("" + 3, card[i].posX, card[i].posY, paint);
  221.                      break;
  222.                  }
  223.                  case FOUR: {
  224.                      paint.setARGB(255, 255, 255, 255);
  225.                      paint.setStrokeWidth(200);
  226.                      paint.setTextSize(30);
  227.                      canvas.drawText("" + 4, card[i].posX, card[i].posY, paint);
  228.                      break;
  229.                  }
  230.                  case FIVE: {
  231.                      paint.setARGB(255, 255, 255, 255);
  232.                      paint.setStrokeWidth(200);
  233.                      paint.setTextSize(30);
  234.                      canvas.drawText("" + 5, card[i].posX, card[i].posY, paint);
  235.                      break;
  236.                  }
  237.                  case SIX: {
  238.                      paint.setARGB(255, 255, 255, 255);
  239.                      paint.setStrokeWidth(200);
  240.                      paint.setTextSize(30);
  241.                      canvas.drawText("" + 6, card[i].posX, card[i].posY, paint);
  242.                      break;
  243.                  }
  244.                  case SEVEN: {
  245.                      paint.setARGB(255, 255, 255, 255);
  246.                      paint.setStrokeWidth(200);
  247.                      paint.setTextSize(30);
  248.                      canvas.drawText("" + 7, card[i].posX, card[i].posY, paint);
  249.                      break;
  250.                  }
  251.                  case EIGHT: {
  252.                      paint.setARGB(255, 255, 255, 255);
  253.                      paint.setStrokeWidth(200);
  254.                      paint.setTextSize(30);
  255.                      canvas.drawText("" + 8, card[i].posX, card[i].posY, paint);
  256.                      break;
  257.                  }
  258.                  case NINE: {
  259.                      paint.setARGB(255, 255, 255, 255);
  260.                      paint.setStrokeWidth(200);
  261.                      paint.setTextSize(30);
  262.                      canvas.drawText("" + 9, card[i].posX, card[i].posY, paint);
  263.                      break;
  264.                  }
  265.              }
  266.          }
  267.  
  268.          //Render Card Holder
  269.          for(int i = 0 ; i < 3; ++i)
  270.          {
  271.              switch(cardholder[i].framecolour)
  272.              {
  273.                  case Red:
  274.                  {
  275.                      canvas.drawBitmap(cardholder[i].R_texture, cardholder[i].posX, cardholder[i].posY, null);
  276.                     break;
  277.                  }
  278.                  case Blue:
  279.                   {
  280.                  canvas.drawBitmap(cardholder[i].B_texture, cardholder[i].posX, cardholder[i].posY, null);
  281.                  break;
  282.                   }
  283.                  case Green:
  284.                  {
  285.                      canvas.drawBitmap(cardholder[i].G_texture, cardholder[i].posX, cardholder[i].posY, null);
  286.                      break;
  287.                  }
  288.              }
  289.          }
  290.  
  291.          // Bonus) To print FPS on the screen
  292.          paint.setARGB(255, 255, 255, 255);
  293.          paint.setStrokeWidth(100);
  294.          paint.setTextSize(50);
  295.          canvas.drawText("FPS =" + FPS, Screenwidth/2, Screenheight/2, paint);
  296.  
  297.      }
  298.  
  299.     //Update method to update the game play
  300.     public void update(float dt, float fps) {
  301.         FPS = fps;
  302.     }
  303.  
  304.     // Rendering is done on Canvas
  305.     public void doDraw(Canvas canvas) {
  306.         RenderGameplay(canvas);
  307.     }
  308.  
  309.     @Override
  310.     public boolean onTouchEvent(MotionEvent event) {
  311.  
  312.         // 5) In event of touch on screen, the spaceship will relocate to the point of touch
  313.         short X = (short) event.getX();
  314.         short Y = (short) event.getY();
  315.  
  316.         switch(event.getAction())
  317.         {
  318.             case MotionEvent.ACTION_DOWN:
  319.             {
  320.                 if(checkCollision(X,Y,50,50,card[0].posX,card[0].posY,150,150))
  321.                 {
  322.                     card[0].isPressed = true;
  323.                 }
  324.                 return true;
  325.             }
  326.             case MotionEvent.ACTION_UP:
  327.             {
  328.                 if(checkCollision(cardholder[0].posX,cardholder[0].posY,50,50,card[0].posX,card[0].posY,150,150)) {
  329.  
  330.                 }
  331.  
  332.                 card[0].isPressed = false;
  333.                 card[0].posX = card[0].SpawnLocationX;
  334.                 card[0].posY = card[0].SpawnLocationY;
  335.                 return true;
  336.             }
  337.             case MotionEvent.ACTION_MOVE:
  338.             {
  339.                 if(card[0].isPressed == true)
  340.                 {
  341.                     card[0].posX = X-50;
  342.                     card[0].posY = Y-50;
  343.                 }
  344.                 return true;
  345.             }
  346.         }
  347.         return true;
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement