├── .classpath ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── bin └── main │ ├── Board$1.class │ ├── Board$GameLooper.class │ ├── Board.class │ ├── ImageLoader.class │ ├── Shape.class │ ├── Title$1.class │ ├── Title.class │ └── Window.class ├── src └── main │ ├── Board.java │ ├── ImageLoader.java │ ├── Shape.java │ ├── Title.java │ └── Window.java └── textures ├── Pause.png ├── Thumbs.db ├── Title.png ├── arrow.png ├── background.png ├── music.wav ├── play.png ├── refresh.png └── tiles.png /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Tetris 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /bin/main/Board$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Board$1.class -------------------------------------------------------------------------------- /bin/main/Board$GameLooper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Board$GameLooper.class -------------------------------------------------------------------------------- /bin/main/Board.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Board.class -------------------------------------------------------------------------------- /bin/main/ImageLoader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/ImageLoader.class -------------------------------------------------------------------------------- /bin/main/Shape.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Shape.class -------------------------------------------------------------------------------- /bin/main/Title$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Title$1.class -------------------------------------------------------------------------------- /bin/main/Title.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Title.class -------------------------------------------------------------------------------- /bin/main/Window.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/bin/main/Window.class -------------------------------------------------------------------------------- /src/main/Board.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import java.awt.BasicStroke; 4 | import java.awt.Color; 5 | import java.awt.Font; 6 | import java.awt.Graphics; 7 | import java.awt.Graphics2D; 8 | import java.awt.Rectangle; 9 | import java.awt.event.ActionEvent; 10 | import java.awt.event.ActionListener; 11 | import java.awt.event.KeyEvent; 12 | import java.awt.event.KeyListener; 13 | import java.awt.event.MouseEvent; 14 | import java.awt.event.MouseListener; 15 | import java.awt.event.MouseMotionListener; 16 | import java.awt.image.BufferedImage; 17 | 18 | import javax.imageio.ImageIO; 19 | import javax.sound.sampled.AudioSystem; 20 | import javax.sound.sampled.Clip; 21 | import javax.sound.sampled.LineUnavailableException; 22 | import javax.swing.JPanel; 23 | import javax.swing.Timer; 24 | 25 | 26 | 27 | 28 | public class Board extends JPanel implements KeyListener, MouseListener, MouseMotionListener{ 29 | 30 | //Assets 31 | 32 | /** 33 | * 34 | */ 35 | private static final long serialVersionUID = 1L; 36 | 37 | private Clip music; 38 | 39 | private BufferedImage blocks, background, pause, refresh; 40 | 41 | //board dimensions (the playing area) 42 | 43 | private final int boardHeight = 20, boardWidth = 10; 44 | 45 | // block size 46 | 47 | private final int blockSize = 30; 48 | 49 | // field 50 | 51 | private int[][] board = new int[boardHeight][boardWidth]; 52 | 53 | // array with all the possible shapes 54 | 55 | private Shape[] shapes = new Shape[7]; 56 | 57 | // currentShape 58 | 59 | private static Shape currentShape, nextShape; 60 | 61 | // game loop 62 | 63 | private Timer looper; 64 | 65 | private int FPS = 60; 66 | 67 | private int delay = 1000/FPS; 68 | 69 | // mouse events variables 70 | 71 | private int mouseX, mouseY; 72 | 73 | private boolean leftClick = false; 74 | 75 | private Rectangle stopBounds, refreshBounds; 76 | 77 | private boolean gamePaused = false; 78 | 79 | private boolean gameOver = false; 80 | 81 | // buttons press lapse 82 | 83 | private Timer buttonLapse = new Timer(300, new ActionListener(){ 84 | 85 | @Override 86 | public void actionPerformed(ActionEvent e) { 87 | buttonLapse.stop(); 88 | }}); 89 | 90 | // score 91 | 92 | private int score = 0; 93 | 94 | 95 | public Board(){ 96 | // load Assets 97 | blocks = ImageLoader.loadImage("/tiles.png"); 98 | 99 | background = ImageLoader.loadImage("/background.png"); 100 | pause = ImageLoader.loadImage("/pause.png"); 101 | refresh = ImageLoader.loadImage("/refresh.png"); 102 | 103 | music = ImageLoader.LoadSound("/music.wav"); 104 | 105 | 106 | 107 | music.loop(Clip.LOOP_CONTINUOUSLY); 108 | 109 | 110 | 111 | mouseX = 0; 112 | mouseY = 0; 113 | 114 | stopBounds = new Rectangle(350, 500, pause.getWidth(), pause.getHeight() + pause.getHeight()/2); 115 | refreshBounds = new Rectangle(350, 500 - refresh.getHeight() - 20,refresh.getWidth(), 116 | refresh.getHeight() + refresh.getHeight()/2); 117 | 118 | // create game looper 119 | 120 | looper = new Timer(delay, new GameLooper()); 121 | 122 | // create shapes 123 | 124 | shapes[0] = new Shape(new int[][]{ 125 | {1, 1, 1, 1} // I shape; 126 | }, blocks.getSubimage(0, 0, blockSize, blockSize), this, 1); 127 | 128 | shapes[1] = new Shape(new int[][]{ 129 | {1, 1, 1}, 130 | {0, 1, 0}, // T shape; 131 | }, blocks.getSubimage(blockSize, 0, blockSize, blockSize), this, 2); 132 | 133 | shapes[2] = new Shape(new int[][]{ 134 | {1, 1, 1}, 135 | {1, 0, 0}, // L shape; 136 | }, blocks.getSubimage(blockSize*2, 0, blockSize, blockSize), this, 3); 137 | 138 | shapes[3] = new Shape(new int[][]{ 139 | {1, 1, 1}, 140 | {0, 0, 1}, // J shape; 141 | }, blocks.getSubimage(blockSize*3, 0, blockSize, blockSize), this, 4); 142 | 143 | shapes[4] = new Shape(new int[][]{ 144 | {0, 1, 1}, 145 | {1, 1, 0}, // S shape; 146 | }, blocks.getSubimage(blockSize*4, 0, blockSize, blockSize), this, 5); 147 | 148 | shapes[5] = new Shape(new int[][]{ 149 | {1, 1, 0}, 150 | {0, 1, 1}, // Z shape; 151 | }, blocks.getSubimage(blockSize*5, 0, blockSize, blockSize), this, 6); 152 | 153 | shapes[6] = new Shape(new int[][]{ 154 | {1, 1}, 155 | {1, 1}, // O shape; 156 | }, blocks.getSubimage(blockSize*6, 0, blockSize, blockSize), this, 7); 157 | 158 | 159 | } 160 | 161 | private void update(){ 162 | if(stopBounds.contains(mouseX, mouseY) && leftClick && !buttonLapse.isRunning() && !gameOver) 163 | { 164 | buttonLapse.start(); 165 | gamePaused = !gamePaused; 166 | } 167 | 168 | if(refreshBounds.contains(mouseX, mouseY) && leftClick) 169 | startGame(); 170 | 171 | if(gamePaused || gameOver) 172 | { 173 | return; 174 | } 175 | currentShape.update(); 176 | } 177 | 178 | 179 | public void paintComponent(Graphics g){ 180 | super.paintComponent(g); 181 | 182 | g.drawImage(background, 0, 0, null); 183 | 184 | 185 | for(int row = 0; row < board.length; row++) 186 | { 187 | for(int col = 0; col < board[row].length; col ++) 188 | { 189 | 190 | if(board[row][col] != 0) 191 | { 192 | 193 | g.drawImage(blocks.getSubimage((board[row][col] - 1)*blockSize, 194 | 0, blockSize, blockSize), col*blockSize, row*blockSize, null); 195 | } 196 | 197 | } 198 | } 199 | for(int row = 0; row < nextShape.getCoords().length; row ++) 200 | { 201 | for(int col = 0; col < nextShape.getCoords()[0].length; col ++) 202 | { 203 | if(nextShape.getCoords()[row][col] != 0) 204 | { 205 | g.drawImage(nextShape.getBlock(), col*30 + 320, row*30 + 50, null); 206 | } 207 | } 208 | } 209 | currentShape.render(g); 210 | 211 | if(stopBounds.contains(mouseX, mouseY)) 212 | g.drawImage(pause.getScaledInstance(pause.getWidth() + 3, pause.getHeight() + 3, BufferedImage.SCALE_DEFAULT) 213 | , stopBounds.x + 3, stopBounds.y + 3, null); 214 | else 215 | g.drawImage(pause, stopBounds.x, stopBounds.y, null); 216 | 217 | if(refreshBounds.contains(mouseX, mouseY)) 218 | g.drawImage(refresh.getScaledInstance(refresh.getWidth() + 3, refresh.getHeight() + 3, 219 | BufferedImage.SCALE_DEFAULT), refreshBounds.x + 3, refreshBounds.y + 3, null); 220 | else 221 | g.drawImage(refresh, refreshBounds.x, refreshBounds.y, null); 222 | 223 | 224 | if(gamePaused) 225 | { 226 | String gamePausedString = "GAME PAUSED"; 227 | g.setColor(Color.WHITE); 228 | g.setFont(new Font("Georgia", Font.BOLD, 30)); 229 | g.drawString(gamePausedString, 35, Window.HEIGHT/2); 230 | } 231 | if(gameOver) 232 | { 233 | String gameOverString = "GAME OVER"; 234 | g.setColor(Color.WHITE); 235 | g.setFont(new Font("Georgia", Font.BOLD, 30)); 236 | g.drawString(gameOverString, 50, Window.HEIGHT/2); 237 | } 238 | g.setColor(Color.WHITE); 239 | 240 | g.setFont(new Font("Georgia", Font.BOLD, 20)); 241 | 242 | g.drawString("SCORE", Window.WIDTH - 125, Window.HEIGHT/2); 243 | g.drawString(score+"", Window.WIDTH - 125, Window.HEIGHT/2 + 30); 244 | 245 | Graphics2D g2d = (Graphics2D)g; 246 | 247 | g2d.setStroke(new BasicStroke(2)); 248 | g2d.setColor(new Color(0, 0, 0, 100)); 249 | 250 | for(int i = 0; i <= boardHeight; i++) 251 | { 252 | g2d.drawLine(0, i*blockSize, boardWidth*blockSize, i*blockSize); 253 | } 254 | for(int j = 0; j <= boardWidth; j++) 255 | { 256 | g2d.drawLine(j*blockSize, 0, j*blockSize, boardHeight*30); 257 | } 258 | } 259 | 260 | public void setNextShape(){ 261 | int index = (int)(Math.random()*shapes.length); 262 | nextShape = new Shape(shapes[index].getCoords(), shapes[index].getBlock(), this, shapes[index].getColor()); 263 | } 264 | 265 | public void setCurrentShape(){ 266 | currentShape = nextShape; 267 | setNextShape(); 268 | 269 | for(int row = 0; row < currentShape.getCoords().length; row ++) 270 | { 271 | for(int col = 0; col < currentShape.getCoords()[0].length; col ++) 272 | { 273 | if(currentShape.getCoords()[row][col] != 0) 274 | { 275 | if(board[currentShape.getY() + row][currentShape.getX() + col] != 0) 276 | gameOver = true; 277 | } 278 | } 279 | } 280 | 281 | } 282 | 283 | 284 | public int[][] getBoard(){ 285 | return board; 286 | } 287 | 288 | @Override 289 | public void keyPressed(KeyEvent e) { 290 | if(e.getKeyCode() == KeyEvent.VK_UP) 291 | currentShape.rotateShape(); 292 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 293 | currentShape.setDeltaX(1); 294 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 295 | currentShape.setDeltaX(-1); 296 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 297 | currentShape.speedUp(); 298 | } 299 | @Override 300 | public void keyReleased(KeyEvent e) { 301 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 302 | currentShape.speedDown(); 303 | } 304 | 305 | @Override 306 | public void keyTyped(KeyEvent e) { 307 | 308 | } 309 | 310 | public void startGame(){ 311 | stopGame(); 312 | setNextShape(); 313 | setCurrentShape(); 314 | gameOver = false; 315 | looper.start(); 316 | 317 | } 318 | public void stopGame(){ 319 | score = 0; 320 | 321 | for(int row = 0; row < board.length; row++) 322 | { 323 | for(int col = 0; col < board[row].length; col ++) 324 | { 325 | board[row][col] = 0; 326 | } 327 | } 328 | looper.stop(); 329 | } 330 | 331 | 332 | class GameLooper implements ActionListener{ 333 | 334 | @Override 335 | public void actionPerformed(ActionEvent e) { 336 | update(); 337 | repaint(); 338 | } 339 | 340 | } 341 | 342 | 343 | @Override 344 | public void mouseDragged(MouseEvent e) { 345 | mouseX = e.getX(); 346 | mouseY = e.getY(); 347 | } 348 | 349 | @Override 350 | public void mouseMoved(MouseEvent e) { 351 | mouseX = e.getX(); 352 | mouseY = e.getY(); 353 | } 354 | 355 | @Override 356 | public void mouseClicked(MouseEvent e) { 357 | 358 | } 359 | 360 | @Override 361 | public void mousePressed(MouseEvent e) { 362 | if(e.getButton() == MouseEvent.BUTTON1) 363 | leftClick = true; 364 | } 365 | 366 | @Override 367 | public void mouseReleased(MouseEvent e) { 368 | if(e.getButton() == MouseEvent.BUTTON1) 369 | leftClick = false; 370 | } 371 | 372 | @Override 373 | public void mouseEntered(MouseEvent e) { 374 | 375 | } 376 | 377 | @Override 378 | public void mouseExited(MouseEvent e) { 379 | 380 | } 381 | 382 | public void addScore(){ 383 | score ++; 384 | } 385 | 386 | } 387 | -------------------------------------------------------------------------------- /src/main/ImageLoader.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.io.IOException; 5 | 6 | import javax.imageio.ImageIO; 7 | import javax.sound.sampled.AudioSystem; 8 | import javax.sound.sampled.Clip; 9 | 10 | 11 | public class ImageLoader { 12 | 13 | public static BufferedImage loadImage(String path){ 14 | try { 15 | return ImageIO.read(ImageLoader.class.getResource(path)); 16 | } catch (IOException e) { 17 | e.printStackTrace(); 18 | System.exit(1); 19 | } 20 | return null; 21 | 22 | } 23 | public static Clip LoadSound(String direction){ 24 | try{ 25 | Clip clip = AudioSystem.getClip(); 26 | clip.open(AudioSystem.getAudioInputStream(ImageLoader.class.getResource(direction))); 27 | return clip; 28 | }catch(Exception e){ 29 | e.printStackTrace(); 30 | } 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/Shape.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import java.awt.Graphics; 4 | import java.awt.image.BufferedImage; 5 | 6 | public class Shape { 7 | 8 | private int color; 9 | 10 | private int x, y; 11 | 12 | private long time, lastTime; 13 | 14 | private int normal = 600, fast = 50; 15 | 16 | private int delay; 17 | 18 | private BufferedImage block; 19 | 20 | private int[][] coords; 21 | 22 | private int[][] reference; 23 | 24 | private int deltaX; 25 | 26 | private Board board; 27 | 28 | private boolean collision = false, moveX = false; 29 | 30 | public Shape(int[][] coords, BufferedImage block, Board board, int color){ 31 | this.coords = coords; 32 | this.block = block; 33 | this.board = board; 34 | this.color = color; 35 | deltaX = 0; 36 | x = 4; 37 | y = 0; 38 | delay = normal; 39 | time = 0; 40 | lastTime = System.currentTimeMillis(); 41 | reference = new int[coords.length][coords[0].length]; 42 | 43 | System.arraycopy(coords, 0, reference, 0, coords.length); 44 | 45 | } 46 | 47 | public void update(){ 48 | moveX = true; 49 | time += System.currentTimeMillis() - lastTime; 50 | lastTime = System.currentTimeMillis(); 51 | 52 | if(collision) 53 | { 54 | for(int row = 0; row < coords.length; row ++) 55 | { 56 | for(int col = 0; col < coords[0].length; col ++) 57 | { 58 | if(coords[row][col] != 0) 59 | board.getBoard()[y + row][x + col] = color; 60 | } 61 | } 62 | checkLine(); 63 | board.addScore(); 64 | board.setCurrentShape(); 65 | } 66 | 67 | if(!(x + deltaX + coords[0].length > 10) && !(x + deltaX < 0)) 68 | { 69 | 70 | for(int row = 0; row < coords.length; row++) 71 | { 72 | for(int col = 0; col < coords[row].length; col ++) 73 | { 74 | if(coords[row][col] != 0) 75 | { 76 | if(board.getBoard()[y + row][x + deltaX + col] != 0) 77 | { 78 | moveX = false; 79 | } 80 | 81 | } 82 | } 83 | } 84 | 85 | if(moveX) 86 | x += deltaX; 87 | 88 | } 89 | 90 | if(!(y + 1 + coords.length > 20)) 91 | { 92 | 93 | for(int row = 0; row < coords.length; row++) 94 | { 95 | for(int col = 0; col < coords[row].length; col ++) 96 | { 97 | if(coords[row][col] != 0) 98 | { 99 | 100 | if(board.getBoard()[y + 1 + row][x + col] != 0) 101 | { 102 | collision = true; 103 | } 104 | } 105 | } 106 | } 107 | if(time > delay) 108 | { 109 | y++; 110 | time = 0; 111 | } 112 | }else{ 113 | collision = true; 114 | } 115 | 116 | deltaX = 0; 117 | } 118 | 119 | public void render(Graphics g){ 120 | 121 | for(int row = 0; row < coords.length; row ++) 122 | { 123 | for(int col = 0; col < coords[0].length; col ++) 124 | { 125 | if(coords[row][col] != 0) 126 | { 127 | g.drawImage(block, col*30 + x*30, row*30 + y*30, null); 128 | } 129 | } 130 | } 131 | 132 | for(int row = 0; row < reference.length; row ++) 133 | { 134 | for(int col = 0; col < reference[0].length; col ++) 135 | { 136 | if(reference[row][col] != 0) 137 | { 138 | g.drawImage(block, col*30 + 320, row*30 + 160, null); 139 | } 140 | 141 | } 142 | 143 | } 144 | 145 | } 146 | 147 | private void checkLine(){ 148 | int size = board.getBoard().length - 1; 149 | 150 | for(int i = board.getBoard().length - 1; i > 0; i--) 151 | { 152 | int count = 0; 153 | for(int j = 0; j < board.getBoard()[0].length; j++) 154 | { 155 | if(board.getBoard()[i][j] != 0) 156 | count++; 157 | 158 | board.getBoard()[size][j] = board.getBoard()[i][j]; 159 | } 160 | if(count < board.getBoard()[0].length) 161 | size --; 162 | } 163 | } 164 | 165 | public void rotateShape() 166 | { 167 | 168 | int[][] rotatedShape = null; 169 | 170 | rotatedShape = transposeMatrix(coords); 171 | 172 | rotatedShape = reverseRows(rotatedShape); 173 | 174 | if((x + rotatedShape[0].length > 10) || (y + rotatedShape.length > 20)) 175 | { 176 | return; 177 | } 178 | 179 | for(int row = 0; row < rotatedShape.length; row++) 180 | { 181 | for(int col = 0; col < rotatedShape[row].length; col ++) 182 | { 183 | if(rotatedShape[row][col] != 0) 184 | { 185 | if(board.getBoard()[y + row][x + col] != 0) 186 | { 187 | return; 188 | } 189 | } 190 | } 191 | } 192 | coords = rotatedShape; 193 | } 194 | 195 | 196 | private int[][] transposeMatrix(int[][] matrix){ 197 | int[][] temp = new int[matrix[0].length][matrix.length]; 198 | for (int i = 0; i < matrix.length; i++) 199 | for (int j = 0; j < matrix[0].length; j++) 200 | temp[j][i] = matrix[i][j]; 201 | return temp; 202 | } 203 | 204 | 205 | 206 | private int[][] reverseRows(int[][] matrix){ 207 | 208 | int middle = matrix.length/2; 209 | 210 | 211 | for(int i = 0; i < middle; i++) 212 | { 213 | int[] temp = matrix[i]; 214 | 215 | matrix[i] = matrix[matrix.length - i - 1]; 216 | matrix[matrix.length - i - 1] = temp; 217 | } 218 | 219 | return matrix; 220 | 221 | } 222 | 223 | 224 | public int getColor(){ 225 | return color; 226 | } 227 | 228 | public void setDeltaX(int deltaX){ 229 | this.deltaX = deltaX; 230 | } 231 | 232 | public void speedUp(){ 233 | delay = fast; 234 | } 235 | 236 | public void speedDown(){ 237 | delay = normal; 238 | } 239 | 240 | public BufferedImage getBlock(){ 241 | return block; 242 | } 243 | 244 | public int[][] getCoords(){ 245 | return coords; 246 | } 247 | 248 | public int getX(){ 249 | return x; 250 | } 251 | 252 | public int getY(){ 253 | return y; 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /src/main/Title.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import java.awt.Color; 4 | import java.awt.Graphics; 5 | import java.awt.Rectangle; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.MouseEvent; 9 | import java.awt.event.MouseListener; 10 | import java.awt.event.MouseMotionListener; 11 | import java.awt.image.BufferedImage; 12 | import java.io.IOException; 13 | 14 | import javax.imageio.ImageIO; 15 | import javax.swing.JPanel; 16 | import javax.swing.Timer; 17 | 18 | public class Title extends JPanel implements MouseListener, MouseMotionListener{ 19 | 20 | /** 21 | * 22 | */ 23 | private static final long serialVersionUID = 1L; 24 | private int mouseX, mouseY; 25 | private Rectangle bounds; 26 | private boolean leftClick = false; 27 | private BufferedImage title, instructions, play; 28 | private Window window; 29 | private BufferedImage[] playButton = new BufferedImage[2]; 30 | private Timer timer; 31 | 32 | 33 | public Title(Window window){ 34 | try { 35 | title = ImageIO.read(Board.class.getResource("/Title.png")); 36 | instructions = ImageIO.read(Board.class.getResource("/arrow.png")); 37 | play = ImageIO.read(Board.class.getResource("/play.png")); 38 | } catch (IOException e) { 39 | e.printStackTrace(); 40 | } 41 | timer = new Timer(1000/60, new ActionListener(){ 42 | 43 | @Override 44 | public void actionPerformed(ActionEvent e) { 45 | repaint(); 46 | } 47 | 48 | }); 49 | timer.start(); 50 | mouseX = 0; 51 | mouseY = 0; 52 | 53 | playButton[0] = play.getSubimage(0, 0, 100, 80); 54 | playButton[1] = play.getSubimage(100, 0, 100, 80); 55 | 56 | bounds = new Rectangle(Window.WIDTH/2 - 50, Window.HEIGHT/2 - 100, 100, 80); 57 | this.window = window; 58 | 59 | 60 | 61 | } 62 | 63 | public void paintComponent(Graphics g){ 64 | super.paintComponent(g); 65 | 66 | if(leftClick && bounds.contains(mouseX, mouseY)) 67 | window.startTetris(); 68 | 69 | g.setColor(Color.BLACK); 70 | 71 | g.fillRect(0, 0, Window.WIDTH, Window.HEIGHT); 72 | 73 | g.drawImage(title, Window.WIDTH/2 - title.getWidth()/2, Window.HEIGHT/2 - title.getHeight()/2 - 200, null); 74 | g.drawImage(instructions, Window.WIDTH/2 - instructions.getWidth()/2, 75 | Window.HEIGHT/2 - instructions.getHeight()/2 + 150, null); 76 | 77 | if(bounds.contains(mouseX, mouseY)) 78 | g.drawImage(playButton[0], Window.WIDTH/2 - 50, Window.HEIGHT/2 - 100, null); 79 | else 80 | g.drawImage(playButton[1], Window.WIDTH/2 - 50, Window.HEIGHT/2 - 100, null); 81 | 82 | 83 | } 84 | 85 | @Override 86 | public void mouseClicked(MouseEvent e) { 87 | } 88 | 89 | @Override 90 | public void mousePressed(MouseEvent e) { 91 | if(e.getButton() == MouseEvent.BUTTON1) 92 | leftClick = true; 93 | } 94 | 95 | @Override 96 | public void mouseReleased(MouseEvent e) { 97 | if(e.getButton() == MouseEvent.BUTTON1) 98 | leftClick = false; 99 | } 100 | 101 | @Override 102 | public void mouseEntered(MouseEvent e) { 103 | 104 | } 105 | 106 | @Override 107 | public void mouseExited(MouseEvent e) { 108 | } 109 | 110 | @Override 111 | public void mouseDragged(MouseEvent e) { 112 | mouseX = e.getX(); 113 | mouseY = e.getY(); 114 | } 115 | 116 | @Override 117 | public void mouseMoved(MouseEvent e) { 118 | mouseX = e.getX(); 119 | mouseY = e.getY(); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/Window.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import javax.swing.JFrame; 4 | 5 | public class Window{ 6 | //413 7 | public static final int WIDTH = 445, HEIGHT = 629; 8 | 9 | private Board board; 10 | private Title title; 11 | private JFrame window; 12 | 13 | public Window(){ 14 | 15 | window = new JFrame("Tetris"); 16 | window.setSize(WIDTH, HEIGHT); 17 | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 | window.setLocationRelativeTo(null); 19 | window.setResizable(false); 20 | 21 | board = new Board(); 22 | title = new Title(this); 23 | 24 | window.addKeyListener(board); 25 | window.addMouseMotionListener(title); 26 | window.addMouseListener(title); 27 | 28 | window.add(title); 29 | 30 | window.setVisible(true); 31 | } 32 | public void startTetris(){ 33 | window.remove(title); 34 | window.addMouseMotionListener(board); 35 | window.addMouseListener(board); 36 | window.add(board); 37 | board.startGame(); 38 | window.revalidate(); 39 | } 40 | 41 | public static void main(String[] args) { 42 | new Window(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /textures/Pause.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/Pause.png -------------------------------------------------------------------------------- /textures/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/Thumbs.db -------------------------------------------------------------------------------- /textures/Title.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/Title.png -------------------------------------------------------------------------------- /textures/arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/arrow.png -------------------------------------------------------------------------------- /textures/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/background.png -------------------------------------------------------------------------------- /textures/music.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/music.wav -------------------------------------------------------------------------------- /textures/play.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/play.png -------------------------------------------------------------------------------- /textures/refresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/refresh.png -------------------------------------------------------------------------------- /textures/tiles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoshuaHernandezMartinez/Tetris---Java/e7891ddd817abdda3c91e542eafabbdeb28a5ff6/textures/tiles.png --------------------------------------------------------------------------------