Advertisement
Guest User

Mnchngrngs tetris game

a guest
Oct 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.10 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Random;
  9. import java.io.*;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13. public class Tetris extends JPanel {
  14.  
  15. private static final long serialVersionUID = -8715353373678321308L;
  16.  
  17. private final Point[][][] Tetraminos = {
  18. // I-Piece
  19. {
  20. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
  21. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
  22. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
  23. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
  24. },
  25.  
  26. // J-Piece
  27. {
  28. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
  29. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
  30. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
  31. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
  32. },
  33.  
  34. // L-Piece
  35. {
  36. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
  37. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
  38. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
  39. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
  40. },
  41.  
  42. // O-Piece
  43. {
  44. { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
  45. { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
  46. { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
  47. { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
  48. },
  49.  
  50. // S-Piece
  51. {
  52. { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
  53. { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
  54. { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
  55. { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
  56. },
  57.  
  58. // T-Piece
  59. {
  60. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
  61. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
  62. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
  63. { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
  64. },
  65.  
  66. // Z-Piece
  67. {
  68. { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
  69. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
  70. { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
  71. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
  72. }
  73. };
  74.  
  75. private final Color[] tetraminoColors = {
  76. Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
  77. };
  78.  
  79. private Point pieceOrigin;
  80. private int currentPiece;
  81. private int rotation;
  82. private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
  83. private int nextPiece1;
  84. private int nextPiece2;
  85. private int nextPiece3;
  86. private int holdPiece;
  87. private long score;
  88. private long highScore;
  89. private long totalScore;
  90. private long lines;
  91. //private long totalLines;
  92. private int level = 0;
  93. private int linesToNext;
  94. private Color[][] well;;
  95.  
  96. public void save(){
  97.  
  98. try{ // Catch errors in I/O if necessary.
  99. //Open a file to write to, named SavedObj.sav.
  100. FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
  101. // Create an ObjectOutputStream to put objects into save file.
  102. ObjectOutputStream save = new ObjectOutputStream(saveFile);
  103.  
  104. // Now we do the save.
  105. save.writeObject(highScore);
  106. save.writeObject(totalScore);
  107. //save.writeObject(totalLines);
  108. // Close the file.
  109. save.close(); // This also closes saveFile.
  110. }
  111. catch(Exception exc){
  112. exc.printStackTrace(); // If there was an error, print the info.
  113. }
  114.  
  115. }
  116.  
  117. public void load(){
  118. try{
  119. FileInputStream saveFile = new FileInputStream("SaveObj.sav");
  120. ObjectInputStream save = new ObjectInputStream(saveFile);
  121. totalScore = (Long) save.readObject();
  122. highScore = (Long) save.readObject();
  123. }
  124. catch(Exception exc){
  125. exc.printStackTrace(); // If there was an error, print the info.
  126. }
  127. }
  128.  
  129. // Creates a border around the well and initializes the dropping piece
  130. private void init() {
  131. Random rand = new Random();
  132. load();
  133. well = new Color[12][24];
  134. for (int i = 0; i < 12; i++) {
  135. for (int j = 0; j < 23; j++) {
  136. if (i == 0 || i == 11 || j == 22) {
  137. well[i][j] = Color.GRAY;
  138. } else {
  139. well[i][j] = Color.BLACK;
  140. }
  141. }
  142. }
  143. totalScore += score; // total score counter
  144. score = 0; //score
  145. lines = 0; //counts how many lines have been cleared
  146. level = 1; //affects the drop speed and multiplier
  147. linesToNext = 10; //how many lines until the next level
  148. nextPiece1 = rand.nextInt(6); //sets next piece 1 to a random piece
  149. nextPiece2 = rand.nextInt(6); //sets next piece 2 to a random piece
  150. nextPiece3 = rand.nextInt(6); //sets next piece 3 to a random piece
  151. holdPiece = 7; //sets hold piece to a non piece value
  152. newPiece();
  153. }
  154.  
  155. // Put a new, random piece into the dropping position
  156. public void newPiece() {
  157. Random rand = new Random();
  158. pieceOrigin = new Point(5, 2);
  159. rotation = 0;
  160. currentPiece = nextPiece1;
  161. nextPiece1 = nextPiece2;
  162. nextPiece2 = nextPiece3;
  163. nextPiece3 = rand.nextInt(6);
  164. if (collidesAt (pieceOrigin.x, pieceOrigin.y, rotation))
  165. {
  166. save();
  167. if (score>=highScore)
  168. {
  169. highScore = score;
  170. }
  171. init();
  172. }
  173. }
  174. // Collision test for the dropping piece
  175. private boolean collidesAt(int x, int y, int rotation) {
  176. for (Point p : Tetraminos[currentPiece][rotation]) {
  177. if (well[p.x + x][p.y + y] != Color.BLACK) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183.  
  184. // Rotate the piece clockwise or counterclockwise
  185. public void rotate(int i) {
  186. int newRotation = (rotation + i) % 4;
  187. if (newRotation < 0) {
  188. newRotation = 3;
  189. }
  190. if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
  191. rotation = newRotation;
  192. }
  193. repaint();
  194. }
  195. //Holds a piece for you
  196. public void hold()
  197. {
  198. int n;
  199. if (holdPiece == 7)
  200. {
  201. holdPiece = currentPiece;
  202. newPiece();
  203. }
  204. else
  205. {
  206. n = holdPiece;
  207. holdPiece = currentPiece;
  208. currentPiece = n;
  209. pieceOrigin = new Point(5, 2);
  210. rotation = 0;
  211. }
  212. }
  213. // Move the piece left or right
  214. public void move(int i) {
  215. if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
  216. pieceOrigin.x += i;
  217. }
  218. repaint();
  219. }
  220.  
  221. // Drops the piece one line or fixes it to the well if it can't drop
  222. public void dropDown() {
  223. if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
  224. pieceOrigin.y += 1;
  225. } else {
  226. fixToWell();
  227. }
  228. repaint();
  229. }
  230.  
  231. // Make the dropping piece part of the well, so it is available for
  232. // collision detection.
  233. public void fixToWell() {
  234. for (Point p : Tetraminos[currentPiece][rotation]) {
  235. well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
  236. }
  237. clearRows();
  238. newPiece();
  239. }
  240.  
  241. public boolean bottom(boolean bottom)
  242. {
  243. if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
  244. return false;
  245. } else {
  246. return true;
  247. }
  248. }
  249.  
  250. public void deleteRow(int row) {
  251. for (int j = row-1; j > 0; j--) {
  252. for (int i = 1; i < 11; i++) {
  253. well[i][j+1] = well[i][j];
  254. }
  255. }
  256. }
  257.  
  258. // Draw the falling piece
  259. private void drawPiece(Graphics g) {
  260. g.setColor(tetraminoColors[currentPiece]);
  261. for (Point p : Tetraminos[currentPiece][rotation]) {
  262. g.fillRect((p.x + pieceOrigin.x) * 26+6*24,
  263. (p.y + pieceOrigin.y) * 26,
  264. 25, 25);
  265. }
  266.  
  267. g.setColor(tetraminoColors[nextPiece1]);
  268. for (Point p : Tetraminos[nextPiece1][0]) {
  269. g.fillRect((p.x)*26+28*12+6*24,
  270. (p.y)*26+8*12,
  271. 26, 26);
  272. }
  273.  
  274. g.setColor(tetraminoColors[nextPiece2]);
  275. for (Point p : Tetraminos[nextPiece2][0]) {
  276. g.fillRect((p.x)*26+28*12+6*24,
  277. (p.y)*26+15*12,
  278. 26, 26);
  279. }
  280.  
  281. g.setColor(tetraminoColors[nextPiece3]);
  282. for (Point p : Tetraminos[nextPiece3][0]) {
  283. g.fillRect((p.x)*26 + 28*12+6*24,
  284. (p.y)*26 + 22*12,
  285. 26, 26);
  286. }
  287.  
  288. if (holdPiece != 7)
  289. {
  290. g.setColor(tetraminoColors[holdPiece]);
  291. for (Point p : Tetraminos[holdPiece][0]) {
  292. g.fillRect((p.x)*26 + 3*12,
  293. (p.y)*26 + 5*12,
  294. 26, 26);
  295. }
  296. }
  297. }
  298. // Clear completed rows from the field and award score according to
  299. // the number of simultaneously cleared rows.
  300. public void clearRows() {
  301. boolean gap;
  302. int numClears = 0;
  303.  
  304. for (int j = 21; j > 0; j--) {
  305. gap = false;
  306. for (int i = 1; i < 11; i++) {
  307. if (well[i][j] == Color.BLACK) {
  308. gap = true;
  309. break;
  310. }
  311. }
  312. if (!gap) {
  313. deleteRow(j);
  314. j += 1;
  315. numClears += 1;
  316. }
  317. }
  318.  
  319. switch (numClears) {
  320. case 1:
  321. score += 100*level;
  322. lines += 1;
  323. linesToNext -= 1;
  324. break;
  325. case 2:
  326. score += 300*level;
  327. lines += 2;
  328. linesToNext -= 2;
  329. break;
  330. case 3:
  331. score += 500*level;
  332. lines += 3;
  333. linesToNext -= 3;
  334. break;
  335. case 4:
  336. score += 800*level;
  337. lines += 4;
  338. linesToNext -= 4;
  339. break;
  340. }
  341. if (linesToNext<=0)
  342. {
  343. level ++;
  344. linesToNext += level*5;
  345. }
  346. }
  347.  
  348. public int findLevel(int ha)
  349. {
  350. ha = level;
  351. return ha;
  352. }
  353.  
  354. @Override
  355. public void paintComponent(Graphics g)
  356. {
  357. g.setColor(Color.BLACK); //sets paint to black
  358. g.fillRect(0,0,24*32,23*26); //paints the background
  359. // Paint the well
  360. for (int i = 0; i < 12; i++) {
  361. for (int j = 0; j < 23; j++) {
  362. g.setColor(well[i][j]);
  363. g.fillRect(26*i+24*6, 26*j, 25, 25);
  364.  
  365. }
  366. }
  367. g.setColor(Color.BLACK);
  368. g.fillRect(12*26+24*6,0,12*46+12*6,23*26);
  369. // Display the score
  370. g.setColor(Color.WHITE);
  371. g.drawString("score " + score, 28*12+6*24, 2*12); // write current score
  372. g.drawString("level " + level, 28*12+6*24, 4*12); // display current level
  373. g.drawString("high score " + highScore, 28*12+6*24, 3*12); //display highest score achieved
  374. g.drawString("lines to next Level " + linesToNext, 28*12+6*24, 5*12); // display lines until next level
  375. g.drawString("NEXT",28*12+6*24,7*12);
  376. g.drawString("Hold",5*12,3*12);
  377. // Draw the currently falling piece
  378. drawPiece(g);
  379. }
  380.  
  381. public static void main(String[] args) {
  382. JFrame f = new JFrame("Tetris");
  383. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  384. f.setSize(12*52+10, 26*23+25);
  385. f.setVisible(true);
  386. final Tetris game = new Tetris();
  387. game.init();
  388. f.add(game);
  389. // Keyboard controls
  390. f.addKeyListener(new KeyListener()
  391.  
  392. {
  393. public void keyTyped(KeyEvent e) {
  394. }
  395.  
  396. public void keyPressed(KeyEvent e) {
  397. switch (e.getKeyCode()) {
  398. case KeyEvent.VK_C:
  399. game.hold();
  400. break;
  401. case KeyEvent.VK_UP:
  402. game.rotate(+1);
  403. break;
  404. case KeyEvent.VK_Z:
  405. game.rotate(-1);
  406. break;
  407. case KeyEvent.VK_SPACE:
  408. while (!game.bottom(false) )
  409. {
  410. game.dropDown(); //drops to the bottom
  411. game.score += 2*game.findLevel(1); //adds 1 point per level
  412. }
  413. game.dropDown();
  414. break;
  415. case KeyEvent.VK_LEFT:
  416. game.move(-1); //moves left 1
  417. break;
  418. case KeyEvent.VK_RIGHT:
  419. game.move(+1); //moves right 1
  420. break;
  421. case KeyEvent.VK_DOWN: //if you press down
  422. game.dropDown(); // goes down 1 square
  423. game.score += 1*game.findLevel(1); //adds 1 point
  424. break;
  425. }
  426. }
  427.  
  428. public void keyReleased(KeyEvent e) {
  429. }
  430. });
  431.  
  432. // Make the falling piece drop every second
  433. new Thread()
  434. {
  435. @Override public void run() {
  436. while (true) {
  437. try {
  438. Thread.sleep(1000-(10*game.findLevel(1)));
  439. game.dropDown();
  440. } catch ( InterruptedException e ) {}
  441. }
  442. }
  443. }
  444. .start();
  445. }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement