klasscho

GameBoard class

May 26th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package com.packag;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.image.BufferedImage;
  6.  
  7. public class GameBoard {
  8.     public static final int ROWS = 4;
  9.     public static final int COLS = 4;
  10.  
  11.     private final int startingTiles = 2;
  12.     private Tile[][] board;
  13.     private boolean dead;
  14.     private boolean won;
  15.     private BufferedImage gameBoard;
  16.     private BufferedImage finalBoard;
  17.     private int x;
  18.     private int y;
  19.  
  20.     private static int SPACING = 10;
  21.     public static int BOARD_WIDTH = (COLS +1) * SPACING + COLS * Tile.WIDTH;
  22.     public static int BOARD_HEIGHT = (ROWS +1) * SPACING + ROWS* Tile.HEIGHT;
  23.  
  24.     private boolean hasStarted;
  25.  
  26.     public GameBoard(int x, int y){
  27.         this.x = x;
  28.         this.y = y;
  29.         board = new Tile[ROWS][COLS];
  30.         gameBoard = new BufferedImage(BOARD_WIDTH, BOARD_HEIGHT, BufferedImage.TYPE_INT_RGB);
  31.         finalBoard = new BufferedImage(BOARD_WIDTH, BOARD_HEIGHT, BufferedImage.TYPE_INT_RGB);
  32.  
  33.         creatBoardImage();
  34.     }
  35.     private void creatBoardImage(){
  36.         Graphics2D g = (Graphics2D) gameBoard.getGraphics();
  37.         g.setColor(Color.darkGray);
  38.         g.fillRect(0,0, BOARD_WIDTH, BOARD_HEIGHT);
  39.         g.setColor(Color.lightGray);
  40.  
  41.         for(int row = 0; row < ROWS; row++){
  42.             for (int col = 0; col < COLS; col++){
  43.                 int x = SPACING + SPACING * col + Tile.WIDTH * col;
  44.                 int y = SPACING + SPACING * row + Tile.HEIGHT * row;
  45.                 g.fillRoundRect(x, y, Tile.WIDTH, Tile.HEIGHT, Tile.ARC_WIDTH, Tile.ARC_HEIGHT);
  46.             }
  47.         }
  48.     }
  49.  
  50.     public void render ( Graphics2D g){
  51.         Graphics2D g2d = (Graphics2D)finalBoard.getGraphics();
  52.         g2d.drawImage(gameBoard, 0, 0, null);
  53.  
  54.         //draw tiles
  55.  
  56.         g.drawImage(finalBoard, x, y, null);
  57.         g2d.dispose();
  58.     }
  59.  
  60.     public void  update(){
  61.         checkKeys();
  62.     }
  63.  
  64.     private void checkKeys(){
  65.         if (Keyboard.typed(KeyEvent.VK_LEFT)){
  66.             // move tiles left
  67.             if (!hasStarted) hasStarted = true;
  68.         }
  69.         if (Keyboard.typed(KeyEvent.VK_RIGHT)){
  70.             // move tiles right
  71.             if (!hasStarted) hasStarted = true;
  72.         }
  73.         if (Keyboard.typed(KeyEvent.VK_UP)){
  74.             // move tiles up
  75.             if (!hasStarted) hasStarted = true;
  76.         }
  77.         if (Keyboard.typed(KeyEvent.VK_DOWN)){
  78.             // move tiles down
  79.             if (!hasStarted) hasStarted = true;
  80.         }
  81.     }
  82. }
Add Comment
Please, Sign In to add comment