Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. package tetres;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13.  
  14. public class Tetris extends JPanel {
  15.  
  16. private static final long serialVersionUID = -8715353373678321308L;
  17.  
  18. private final Point[][][] Tetraminos = {
  19. // I-Piece
  20. {
  21. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
  22. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
  23. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
  24. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
  25. },
  26.  
  27. // J-Piece
  28. {
  29. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
  30. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
  31. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
  32. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
  33. },
  34.  
  35. // L-Piece
  36. {
  37. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
  38. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
  39. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
  40. { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
  41. },
  42.  
  43. // O-Piece
  44. {
  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. { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
  49. },
  50.  
  51. // S-Piece
  52. {
  53. { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
  54. { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
  55. { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
  56. { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
  57. },
  58.  
  59. // T-Piece
  60. {
  61. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
  62. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
  63. { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
  64. { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
  65. },
  66.  
  67. // Z-Piece
  68. {
  69. { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
  70. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) },
  71. { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
  72. { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }
  73. }
  74. };
  75.  
  76. private final Color[] tetraminoColors = {
  77. Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
  78. };
  79.  
  80. private Point pieceOrigin;
  81. private int currentPiece;
  82. private int rotation;
  83. private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
  84.  
  85. private long score;
  86. private Color[][] well;
  87. private long Geschwindigkeit;
  88.  
  89. // Creates a border around the well and initializes the dropping piece
  90. private void init() {
  91. well = new Color[12][24];
  92. for (int i = 0; i < 12; i++) {
  93. for (int j = 0; j < 23; j++) {
  94. if (i == 0 || i == 11 || j == 22) {
  95. well[i][j] = Color.GRAY;
  96. } else {
  97. well[i][j] = Color.BLACK;
  98. }
  99. }
  100. }
  101. newPiece();
  102. }
  103.  
  104. // Put a new, random piece into the dropping position
  105. public void newPiece() {
  106. pieceOrigin = new Point(5, 2);
  107. rotation = 0;
  108. if (nextPieces.isEmpty()) {
  109. Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
  110. Collections.shuffle(nextPieces);
  111. }
  112. currentPiece = nextPieces.get(0);
  113. nextPieces.remove(0);
  114. }
  115.  
  116. // Collision test for the dropping piece
  117. private boolean collidesAt(int x, int y, int rotation) {
  118. for (Point p : Tetraminos[currentPiece][rotation]) {
  119. if (well[p.x + x][p.y + y] != Color.BLACK) {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125.  
  126. // Rotate the piece clockwise or counterclockwise
  127. public void rotate(int i) {
  128. int newRotation = (rotation + i) % 4;
  129. if (newRotation < 0) {
  130. newRotation = 3;
  131. }
  132. if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
  133. rotation = newRotation;
  134. }
  135. repaint();
  136. }
  137.  
  138. // Move the piece left or right
  139. public void move(int i) {
  140. if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
  141. pieceOrigin.x += i;
  142. }
  143. repaint();
  144. }
  145.  
  146. // Drops the piece one line or fixes it to the well if it can't drop
  147. public void dropDown() {
  148. if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
  149. pieceOrigin.y += 1;
  150. } else {
  151. fixToWell();
  152. }
  153. repaint();
  154. }
  155.  
  156. // Make the dropping piece part of the well, so it is available for
  157. // collision detection.
  158. public void fixToWell() {
  159. for (Point p : Tetraminos[currentPiece][rotation]) {
  160. well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
  161. }
  162. clearRows();
  163. newPiece();
  164. }
  165.  
  166. public void deleteRow(int row) {
  167. for (int j = row-1; j > 0; j--) {
  168. for (int i = 1; i < 11; i++) {
  169. well[i][j+1] = well[i][j];
  170. }
  171. }
  172. }
  173.  
  174. // Clear completed rows from the field and award score according to
  175. // the number of simultaneously cleared rows.
  176. public void clearRows() {
  177. boolean gap;
  178. int numClears = 0;
  179.  
  180. for (int j = 21; j > 0; j--) {
  181. gap = false;
  182. for (int i = 1; i < 11; i++) {
  183. if (well[i][j] == Color.BLACK) {
  184. gap = true;
  185. break;
  186. }
  187. }
  188. if (!gap) {
  189. deleteRow(j);
  190. j += 1;
  191. numClears += 1;
  192. }
  193. }
  194.  
  195. switch (numClears) {
  196. case 1:
  197. score += 100;
  198. break;
  199. case 2:
  200. score += 300;
  201. break;
  202. case 3:
  203. score += 500;
  204. break;
  205. case 4:
  206. score += 800;
  207. break;
  208. }
  209. }
  210.  
  211. //checks for the theoretical y position of the gray Tetramino
  212. public int checkTheoreticalPos(){
  213. ArrayList<Integer> theor = new ArrayList<>();
  214. for (Point p : Tetraminos[currentPiece][rotation]) {
  215. int theorVal = 0;
  216. for(int j = p.y + pieceOrigin.y + 1; j < 22; j++){
  217. if(well[p.x + pieceOrigin.x][j]==Color.BLACK){
  218. theorVal++;
  219. }else break;
  220. }
  221. theor.add(theorVal);
  222. }
  223. return Collections.min(theor) + pieceOrigin.y;
  224. }
  225.  
  226. // Draw the falling piece
  227. private void drawPiece(Graphics g) {
  228. //paints the theoretical gray Tetramino
  229. g.setColor(Color.GRAY);
  230. for (Point p : Tetraminos[currentPiece][rotation]) {
  231. g.fillRect((p.x + pieceOrigin.x) * 26,
  232. (p.y + checkTheoreticalPos()) * 26,
  233. 25, 25);
  234. }
  235.  
  236. g.setColor(tetraminoColors[ currentPiece]);
  237. for (Point p : Tetraminos[currentPiece][rotation]) {
  238.  
  239. g.fillRect((p.x + pieceOrigin.x) * 26,
  240. (p.y + pieceOrigin.y) * 26,
  241. 25, 25);
  242. }
  243.  
  244. }
  245. @Override
  246. public void paintComponent(Graphics g)
  247. {
  248. // Paint the well
  249. g.fillRect(0, 0, 26*12, 26*23);
  250. for (int i = 0; i < 12; i++) {
  251. for (int j = 0; j < 23; j++) {
  252. g.setColor(well[i][j]);
  253. g.fillRect(26*i, 26*j, 25, 25);
  254. }
  255. }
  256.  
  257. // Display the score
  258. g.setColor(Color.WHITE);
  259. g.drawString("" + score, 19*12, 25);
  260.  
  261. // Draw the currently falling piece
  262. drawPiece(g);
  263. }
  264.  
  265. public static void main(String[] args) {
  266. JFrame f = new JFrame("Tetris");
  267. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  268. f.setSize(12*26+10, 26*23+25);
  269. f.setVisible(true);
  270.  
  271. final Tetris game = new Tetris();
  272. game.init();
  273. f.add(game);
  274.  
  275. // Keyboard controls
  276. f.addKeyListener(new KeyListener() {
  277. public void keyTyped(KeyEvent e) {
  278. }
  279.  
  280. public void keyPressed(KeyEvent e) {
  281. switch (e.getKeyCode()) {
  282. case KeyEvent.VK_UP:
  283. game.rotate(-1);
  284. break;
  285. case KeyEvent.VK_DOWN:
  286. game.rotate(+1);
  287. break;
  288. case KeyEvent.VK_LEFT:
  289. game.move(-1);
  290. break;
  291. case KeyEvent.VK_RIGHT:
  292. game.move(+1);
  293. break;
  294. case KeyEvent.VK_SPACE:
  295. game.dropDown();
  296. game.score += 1;
  297. break;
  298. case KeyEvent.VK_ESCAPE:
  299. if (game.Geschwindigkeit != 0){
  300. game.Geschwindigkeit = 0;
  301. }else if(game.score > 300){
  302. game.Geschwindigkeit = game.score/300;
  303. }else{
  304. game.Geschwindigkeit = 1;
  305. }
  306. break;
  307. }
  308. }
  309.  
  310. public void keyReleased(KeyEvent e) {
  311. }
  312. });
  313.  
  314. // Make the falling piece drop every second
  315. new Thread() {
  316. @Override public void run() {
  317. while (true) {
  318. try {
  319. if (game.Geschwindigkeit != 0){
  320. if (game.score > 300){
  321. game.Geschwindigkeit = game.score/300;
  322. }
  323. game.dropDown();
  324. Thread.sleep(1000/game.Geschwindigkeit);
  325. }
  326. } catch ( InterruptedException e ) {}
  327. }
  328. }
  329. }.start();
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement