Advertisement
Guest User

Untitled

a guest
Jun 8th, 2012
24,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.00 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class Tetris extends JFrame implements KeyListener {
  7. int pos[] = {0,1};
  8. boolean bottom = false;
  9. int n = 20;
  10. int m = 10;
  11. JButton b[][];
  12. Color tmp[][] = new Color[m][n];
  13. int rand = 0;
  14. int centralx = 0;
  15. int centraly = 0;
  16. int deltax = 0;
  17. int perim[][] = new int[m+4][n+4];
  18. or[][][] prof = new or[4][4][7];
  19. Color rnd[] = {Color.red, Color.yellow, Color.cyan, Color.green, Color.white, Color.blue, Color.orange};
  20. int rowsclrd = 0;
  21. public Tetris(){
  22. for(int a = 0;a<4;a++){
  23. for(int b = 0;b<4;b++){
  24. for(int c = 0;c<7;c++){
  25. prof[a][b][c] = new or();
  26. }
  27. }
  28. }
  29. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  30. prof[0][0][0].x = -1;
  31. prof[0][1][0].x = 0;
  32. prof[0][2][0].x = 0;
  33. prof[0][3][0].x = 1;
  34. prof[1][0][0].x = 0;
  35. prof[1][1][0].x = 0;
  36. prof[1][2][0].x = -1;
  37. prof[1][3][0].x = -1;
  38. prof[2][0][0].x = 1;
  39. prof[2][1][0].x = 0;
  40. prof[2][2][0].x = 0;
  41. prof[2][3][0].x = -1;
  42. prof[3][0][0].x = 0;
  43. prof[3][1][0].x = 0;
  44. prof[3][2][0].x = 1;
  45. prof[3][3][0].x = 1;
  46. prof[0][0][1].x = -1;
  47. prof[0][1][1].x = -1;
  48. prof[0][2][1].x = 0;
  49. prof[0][3][1].x = 1;
  50. prof[1][0][1].x = -1;
  51. prof[1][1][1].x = 0;
  52. prof[1][2][1].x = 0;
  53. prof[1][3][1].x = 0;
  54. prof[2][0][1].x = 1;
  55. prof[2][1][1].x = 1;
  56. prof[2][2][1].x = 0;
  57. prof[2][3][1].x = -1;
  58. prof[3][0][1].x = 1;
  59. prof[3][1][1].x = 0;
  60. prof[3][2][1].x = 0;
  61. prof[3][3][1].x = 0;
  62. prof[0][0][2].x = -1;
  63. prof[0][1][2].x = 0;
  64. prof[0][2][2].x = 1;
  65. prof[0][3][2].x = 1;
  66. prof[1][0][2].x = 0;
  67. prof[1][1][2].x = 0;
  68. prof[1][2][2].x = 0;
  69. prof[1][3][2].x = -1;
  70. prof[2][0][2].x = 1;
  71. prof[2][1][2].x = 0;
  72. prof[2][2][2].x = -1;
  73. prof[2][3][2].x = -1;
  74. prof[3][0][2].x = 0;
  75. prof[3][1][2].x = 0;
  76. prof[3][2][2].x = 0;
  77. prof[3][3][2].x = 1;
  78. prof[0][0][3].x = -1;
  79. prof[0][1][3].x = 0;
  80. prof[0][2][3].x = 0;
  81. prof[0][3][3].x = 1;
  82. prof[1][0][3].x = -1;
  83. prof[1][1][3].x = -1;
  84. prof[1][2][3].x = 0;
  85. prof[1][3][3].x = 0;
  86. prof[2][0][3].x = 1;
  87. prof[2][1][3].x = 0;
  88. prof[2][2][3].x = 0;
  89. prof[2][3][3].x = -1;
  90. prof[3][0][3].x = 1;
  91. prof[3][1][3].x = 1;
  92. prof[3][2][3].x = 0;
  93. prof[3][3][3].x = 0;
  94. prof[0][0][4].x = -1;
  95. prof[0][1][4].x = 0;
  96. prof[0][2][4].x = 0;
  97. prof[0][3][4].x = 1;
  98. prof[1][0][4].x = 0;
  99. prof[1][1][4].x = 0;
  100. prof[1][2][4].x = -1;
  101. prof[1][3][4].x = 0;
  102. prof[2][0][4].x = 1;
  103. prof[2][1][4].x = 0;
  104. prof[2][2][4].x = 0;
  105. prof[2][3][4].x = -1;
  106. prof[3][0][4].x = 0;
  107. prof[3][1][4].x = 0;
  108. prof[3][2][4].x = 1;
  109. prof[3][3][4].x = 0;
  110. prof[0][0][5].x = 0;
  111. prof[0][1][5].x = 0;
  112. prof[0][2][5].x = 1;
  113. prof[0][3][5].x = 1;
  114. prof[1][0][5].x = 0;
  115. prof[1][1][5].x = 0;
  116. prof[1][2][5].x = 1;
  117. prof[1][3][5].x = 1;
  118. prof[2][0][5].x = 0;
  119. prof[2][1][5].x = 0;
  120. prof[2][2][5].x = 1;
  121. prof[2][3][5].x = 1;
  122. prof[3][0][5].x = 0;
  123. prof[3][1][5].x = 0;
  124. prof[3][2][5].x = 1;
  125. prof[3][3][5].x = 1;
  126. prof[0][0][6].x = -1;
  127. prof[0][1][6].x = 0;
  128. prof[0][2][6].x = 1;
  129. prof[0][3][6].x = 2;
  130. prof[1][0][6].x = 0;
  131. prof[1][1][6].x = 0;
  132. prof[1][2][6].x = 0;
  133. prof[1][3][6].x = 0;
  134. prof[2][0][6].x = 1;
  135. prof[2][1][6].x = 0;
  136. prof[2][2][6].x = -1;
  137. prof[2][3][6].x = -2;
  138. prof[3][0][6].x = 0;
  139. prof[3][1][6].x = 0;
  140. prof[3][2][6].x = 0;
  141. prof[3][3][6].x = 0;
  142. prof[0][0][0].y = 0;
  143. prof[0][1][0].y = 0;
  144. prof[0][2][0].y = 1;
  145. prof[0][3][0].y = 1;
  146. prof[1][0][0].y = -1;
  147. prof[1][1][0].y = 0;
  148. prof[1][2][0].y = 0;
  149. prof[1][3][0].y = 1;
  150. prof[2][0][0].y = 0;
  151. prof[2][1][0].y = 0;
  152. prof[2][2][0].y = -1;
  153. prof[2][3][0].y = -1;
  154. prof[3][0][0].y = 1;
  155. prof[3][1][0].y = 0;
  156. prof[3][2][0].y = 0;
  157. prof[3][3][0].y = -1;
  158. prof[0][0][1].y = 0;
  159. prof[0][1][1].y = 1;
  160. prof[0][2][1].y = 0;
  161. prof[0][3][1].y = 0;
  162. prof[1][0][1].y = -1;
  163. prof[1][1][1].y = -1;
  164. prof[1][2][1].y = 0;
  165. prof[1][3][1].y = 1;
  166. prof[2][0][1].y = -1;
  167. prof[2][1][1].y = 0;
  168. prof[2][2][1].y = 0;
  169. prof[2][3][1].y = 0;
  170. prof[3][0][1].y = 1;
  171. prof[3][1][1].y = 1;
  172. prof[3][2][1].y = 0;
  173. prof[3][3][1].y = -1;
  174. prof[0][0][2].y = 0;
  175. prof[0][1][2].y = 0;
  176. prof[0][2][2].y = 0;
  177. prof[0][3][2].y = 1;
  178. prof[1][0][2].y = -1;
  179. prof[1][1][2].y = 0;
  180. prof[1][2][2].y = 1;
  181. prof[1][3][2].y = 1;
  182. prof[2][0][2].y = 0;
  183. prof[2][1][2].y = 0;
  184. prof[2][2][2].y = 0;
  185. prof[2][3][2].y = -1;
  186. prof[3][0][2].y = 1;
  187. prof[3][1][2].y = 0;
  188. prof[3][2][2].y = -1;
  189. prof[3][3][2].y = -1;
  190. prof[0][0][3].y = 1;
  191. prof[0][1][3].y = 1;
  192. prof[0][2][3].y = 0;
  193. prof[0][3][3].y = 0;
  194. prof[1][0][3].y = -1;
  195. prof[1][1][3].y = 0;
  196. prof[1][2][3].y = 0;
  197. prof[1][3][3].y = 1;
  198. prof[2][0][3].y = -1;
  199. prof[2][1][3].y = -1;
  200. prof[2][2][3].y = 0;
  201. prof[2][3][3].y = 0;
  202. prof[3][0][3].y = 1;
  203. prof[3][1][3].y = 0;
  204. prof[3][2][3].y = 0;
  205. prof[3][3][3].y = -1;
  206. prof[0][0][4].y = 0;
  207. prof[0][1][4].y = 0;
  208. prof[0][2][4].y = 1;
  209. prof[0][3][4].y = 0;
  210. prof[1][0][4].y = -1;
  211. prof[1][1][4].y = 0;
  212. prof[1][2][4].y = 0;
  213. prof[1][3][4].y = 1;
  214. prof[2][0][4].y = 0;
  215. prof[2][1][4].y = 0;
  216. prof[2][2][4].y = -1;
  217. prof[2][3][4].y = 0;
  218. prof[3][0][4].y = 1;
  219. prof[3][1][4].y = 0;
  220. prof[3][2][4].y = 0;
  221. prof[3][3][4].y = -1;
  222. prof[0][0][5].y = 0;
  223. prof[0][1][5].y = 1;
  224. prof[0][2][5].y = 0;
  225. prof[0][3][5].y = 1;
  226. prof[1][0][5].y = 0;
  227. prof[1][1][5].y = 1;
  228. prof[1][2][5].y = 0;
  229. prof[1][3][5].y = 1;
  230. prof[2][0][5].y = 0;
  231. prof[2][1][5].y = 1;
  232. prof[2][2][5].y = 0;
  233. prof[2][3][5].y = 1;
  234. prof[3][0][5].y = 0;
  235. prof[3][1][5].y = 1;
  236. prof[3][2][5].y = 0;
  237. prof[3][3][5].y = 1;
  238. prof[0][0][6].y = 0;
  239. prof[0][1][6].y = 0;
  240. prof[0][2][6].y = 0;
  241. prof[0][3][6].y = 0;
  242. prof[1][0][6].y = -1;
  243. prof[1][1][6].y = 0;
  244. prof[1][2][6].y = 1;
  245. prof[1][3][6].y = 2;
  246. prof[2][0][6].y = 0;
  247. prof[2][1][6].y = 0;
  248. prof[2][2][6].y = 0;
  249. prof[2][3][6].y = 0;
  250. prof[3][0][6].y = -1;
  251. prof[3][1][6].y = 0;
  252. prof[3][2][6].y = 1;
  253. prof[3][3][6].y = 2;
  254. for (int y = 0;y<2;y++){
  255. for (int x = 0;x<m+4;x++){
  256. perim[x][y]= 1;
  257. }}
  258. for (int y = n+2;y<n+4;y++){
  259. for (int x = 0;x<m+4;x++){
  260. perim[x][y]= 4;
  261. }}
  262. for (int y = 2;y<n+2;y++){
  263. for (int x = 0;x<2;x++){
  264. perim[x][y]= 2;
  265. }}
  266. for (int y = 2;y<n+2;y++){
  267. for (int x = m+2;x<m+4;x++){
  268. perim[x][y]= 2;
  269. }}
  270. for(int y = 0;y<n+4;y++){
  271. for (int x = 0;x<m+4;x++){
  272. System.out.print(perim[x][y]);
  273. }
  274. System.out.println("");
  275. }
  276. b = new JButton [m][n];
  277. setLayout(new GridLayout(n,m));//Grid layouts x and y are SWAPPED!
  278. for (int y = 0;y<n;y++){
  279. for (int x = 0;x<m;x++){
  280. b[x][y] = new JButton(" ");
  281. tmp[x][y] = Color.DARK_GRAY;
  282. b[x][y].setBackground(Color.DARK_GRAY);
  283. add(b[x][y]);
  284. b[x][y].setEnabled(true);
  285. }//end inner for
  286. }
  287. setFocusable(true);
  288. addKeyListener(this);
  289. pack();
  290. setVisible(true);
  291. blockgen();
  292.  
  293.  
  294. }//end constructor Mine()
  295.  
  296. class or {
  297. int x;
  298. int y;
  299. }
  300.  
  301. public void blockgen(){
  302. Component temporaryLostComponent = null;
  303. pos[0] = 0;
  304. pos[1] = 1;
  305. rand = (int) (Math.floor(Math.random()*7+1));
  306. centralx = 4;
  307. centraly = 0;
  308. System.out.print(rand);
  309. if ((b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].getBackground() == Color.DARK_GRAY) &&
  310. (b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].getBackground() == Color.DARK_GRAY) &&
  311. (b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].getBackground() == Color.DARK_GRAY) &&
  312. (b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].getBackground() == Color.DARK_GRAY)){
  313. b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
  314. b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
  315. b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
  316. b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
  317. go();
  318. } else {
  319. JOptionPane.showMessageDialog(temporaryLostComponent, "Game Over! You cleared "+rowsclrd+" rows, well done!");
  320. System.exit(0);
  321. }
  322. }
  323.  
  324. public void rotate(){
  325. if (pos[0] < 3){
  326. pos[1] = pos[0];
  327. pos[0]++;
  328. } else if (pos[0] == 3){
  329. pos[0] = 0;
  330. pos[1] = 3;
  331. } else {
  332. System.out.println("error");
  333. }
  334. if ((perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 4) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 1) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 2) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 3)
  335. && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 3)
  336. && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 3)
  337. && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 4) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 1) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 2) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 3)){
  338. b[centralx+prof[pos[1]][0][rand-1].x][centraly+prof[pos[1]][0][rand-1].y].setBackground(Color.DARK_GRAY);
  339. b[centralx+prof[pos[1]][1][rand-1].x][centraly+prof[pos[1]][1][rand-1].y].setBackground(Color.DARK_GRAY);
  340. b[centralx+prof[pos[1]][2][rand-1].x][centraly+prof[pos[1]][2][rand-1].y].setBackground(Color.DARK_GRAY);
  341. b[centralx+prof[pos[1]][3][rand-1].x][centraly+prof[pos[1]][3][rand-1].y].setBackground(Color.DARK_GRAY);
  342. b[centralx+(prof[pos[0]][0][rand-1].x)][centraly+(prof[pos[0]][0][rand-1].y)].setBackground(rnd[rand-1]);
  343. b[centralx+(prof[pos[0]][1][rand-1].x)][centraly+(prof[pos[0]][1][rand-1].y)].setBackground(rnd[rand-1]);
  344. b[centralx+(prof[pos[0]][2][rand-1].x)][centraly+(prof[pos[0]][2][rand-1].y)].setBackground(rnd[rand-1]);
  345. b[centralx+(prof[pos[0]][3][rand-1].x)][centraly+(prof[pos[0]][3][rand-1].y)].setBackground(rnd[rand-1]);
  346. } else {
  347. if (pos[1] > 0){
  348. pos[0] = pos[1];
  349. pos[1]--;
  350. } else if (pos[1] == 0){
  351. pos[0] = 0;
  352. pos[1] = 3; }
  353. }
  354. }
  355.  
  356.  
  357. public int getxs(){
  358. int xs = 0;
  359. int[] xf = {-1, -1, -1, -1};
  360. for (int d = 0;d<4;d++){
  361. if ((xf[0] != prof[pos[0]][d][rand-1].x) || (xf[1] != prof[pos[0]][d][rand-1].x) || (xf[2] != prof[pos[0]][d][rand-1].x) || (xf[3] != prof[pos[0]][d][rand-1].x)){
  362. xf[d] = prof[pos[0]][d][rand-1].x;
  363. }
  364. }
  365. for (int d = 0;d<4;d++){
  366. if (xf[d] != -1){
  367. xs++;
  368. }
  369. }
  370. return xs;
  371. }
  372.  
  373.  
  374. public void movedown(){
  375. int[] m2 = {-1, -1, -1, -1};
  376. int[] m1 = {-1, -1, -1, -1};
  377. int[] zero = {-1, -1, -1, -1};
  378. int[] one = {-1, -1, -1, -1};
  379. int[] two = {-1, -1, -1, -1};
  380. for (int d = 0;d<4;d++){
  381. if (prof[pos[0]][d][rand-1].x == -2){
  382. m2[d] = d;
  383. } else if (prof[pos[0]][d][rand-1].x == -1){
  384. m1[d] = d;
  385. } else if (prof[pos[0]][d][rand-1].x == 0){
  386. zero[d] = d;
  387. } else if (prof[pos[0]][d][rand-1].x == 1){
  388. one[d] = d;
  389. } else if (prof[pos[0]][d][rand-1].x == 2){
  390. two[d] = d;
  391. }
  392. }
  393. int tmpm2 = -5;
  394. int tmpm1 = -5;
  395. int tmpzero = -5;
  396. int tmpone = -5;
  397. int tmptwo = -5;
  398. for (int d = 0;d<4;d++){
  399. if (m2[d] != -1){
  400. if (tmpm2<prof[pos[0]][m2[d]][rand-1].y){
  401. tmpm2 = prof[pos[0]][m2[d]][rand-1].y;
  402. }
  403. }
  404. if (m1[d] != -1){
  405. if (tmpm1<prof[pos[0]][m1[d]][rand-1].y){
  406. tmpm1 = prof[pos[0]][m1[d]][rand-1].y;
  407. }
  408. }
  409. if (zero[d] != -1){
  410. if (tmpzero<prof[pos[0]][zero[d]][rand-1].y){
  411. tmpzero = prof[pos[0]][zero[d]][rand-1].y;
  412. }
  413. }
  414. if (one[d] != -1){
  415. if (tmpone<prof[pos[0]][one[d]][rand-1].y){
  416. tmpone = prof[pos[0]][one[d]][rand-1].y;
  417. }
  418. }
  419. if (two[d] != -1){
  420. if (tmptwo<prof[pos[0]][two[d]][rand-1].y){
  421. tmptwo = prof[pos[0]][two[d]][rand-1].y;
  422. }
  423. }
  424. }
  425. int total = 0;
  426. for (int d = 0;d<4;d++){
  427. if (prof[pos[0]][d][rand-1].x == -2){
  428. if (perim[2+centralx+-2][2+centraly+tmpm2+1] != 4){
  429. if(b[centralx+-2][centraly+tmpm2+1].getBackground() == Color.DARK_GRAY){
  430. total++;
  431. }}
  432. } else if (prof[pos[0]][d][rand-1].x == -1){
  433. if (perim[2+centralx+-1][2+centraly+tmpm1+1] != 4){
  434. if (b[centralx+-1][centraly+tmpm1+1].getBackground() == Color.DARK_GRAY){
  435. total++;
  436. }}
  437. } else if (prof[pos[0]][d][rand-1].x == 0){
  438. if (perim[2+centralx][2+centraly+tmpzero+1] != 4){
  439. if (b[centralx][centraly+tmpzero+1].getBackground() == Color.DARK_GRAY){
  440. total++;
  441. }}
  442. } else if (prof[pos[0]][d][rand-1].x == 1){
  443. if (perim[2+centralx+1][2+centraly+tmpone+1] != 4){
  444. if (b[centralx+1][centraly+tmpone+1].getBackground() == Color.DARK_GRAY){
  445. total++;
  446. }}
  447. } else if (prof[pos[0]][d][rand-1].x == 2){
  448. if (perim[2+centralx+2][2+centraly+tmptwo+1] != 4){
  449. if (b[centralx+2][centraly+tmptwo+1].getBackground() == Color.DARK_GRAY){
  450. total++;
  451. }}
  452. }
  453. }
  454. if (total == 4){
  455. b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);
  456. b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);
  457. b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);
  458. b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);
  459. centraly++;
  460. b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
  461. b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
  462. b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
  463. b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
  464. } else {
  465.  
  466. bottom = true;
  467. }
  468. }
  469.  
  470.  
  471.  
  472. public void go(){
  473. do{
  474. try {
  475. Thread.sleep(1000L);
  476. } catch (InterruptedException e) {
  477. // TODO Auto-generated catch block
  478. e.printStackTrace();
  479. }
  480. movedown();
  481. rowcheck();
  482. }
  483. while(bottom == false);
  484. bottom = false;
  485. blockgen();
  486. }
  487.  
  488. public void rowcheck(){
  489. int row = 0;
  490. for (int y = 0;y<20;y++){
  491. for (int x = 0;x<10;x++){
  492. if (b[x][y].getBackground() != Color.DARK_GRAY){
  493. row++;
  494. }
  495. if (row == 10){
  496. rowsclrd++;
  497. rowclear(y);
  498. }
  499. }
  500. row = 0;
  501. }
  502. }
  503.  
  504. public void rowclear(int y){
  505. int inc = 0;
  506. for (int x = 0;x<10;x++){
  507. b[x][y].setBackground(Color.DARK_GRAY);
  508. }
  509. for (int c = y-1;c>-1;c--){
  510. for (int x = 0;x<10;x++){
  511. tmp[x][y-inc] = b[x][c].getBackground();
  512. }inc++;}
  513. for (int c = y;c>-1;c--){
  514. for (int x = 0;x<10;x++){
  515. b[x][c].setBackground(tmp[x][c]);
  516. }
  517. }
  518. }
  519.  
  520. public void movelr(){
  521. int[] m2 = {-1, -1, -1, -1};
  522. int[] m1 = {-1, -1, -1, -1};
  523. int[] zero = {-1, -1, -1, -1};
  524. int[] one = {-1, -1, -1, -1};
  525. int[] two = {-1, -1, -1, -1};
  526. for (int d = 0;d<4;d++){
  527. if (prof[pos[0]][d][rand-1].y == -2){
  528. m2[d] = d;
  529. } else if (prof[pos[0]][d][rand-1].y == -1){
  530. m1[d] = d;
  531. } else if (prof[pos[0]][d][rand-1].y == 0){
  532. zero[d] = d;
  533. } else if (prof[pos[0]][d][rand-1].y == 1){
  534. one[d] = d;
  535. } else if (prof[pos[0]][d][rand-1].y == 2){
  536. two[d] = d;
  537. }
  538. }
  539. int tmpm2 = -5;
  540. int tmpm1 = -5;
  541. int tmpzero = -5;
  542. int tmpone = -5;
  543. int tmptwo = -5;
  544. if (deltax == 1){
  545. for (int d = 0;d<4;d++){
  546. if (m2[d] != -1){
  547. if (tmpm2<prof[pos[0]][m2[d]][rand-1].x){
  548. tmpm2 = prof[pos[0]][m2[d]][rand-1].x;
  549. }
  550. }
  551. if (m1[d] != -1){
  552. if (tmpm1<prof[pos[0]][m1[d]][rand-1].x){
  553. tmpm1 = prof[pos[0]][m1[d]][rand-1].x;
  554. }
  555. }
  556. if (zero[d] != -1){
  557. if (tmpzero<prof[pos[0]][zero[d]][rand-1].x){
  558. tmpzero = prof[pos[0]][zero[d]][rand-1].x;
  559. }
  560. }
  561. if (one[d] != -1){
  562. if (tmpone<prof[pos[0]][one[d]][rand-1].x){
  563. tmpone = prof[pos[0]][one[d]][rand-1].x;
  564. }
  565. }
  566. if (two[d] != -1){
  567. if (tmptwo<prof[pos[0]][two[d]][rand-1].x){
  568. tmptwo = prof[pos[0]][two[d]][rand-1].x;
  569. }
  570. }
  571. }
  572. } else if (deltax == -1){
  573. tmpm2 = 5;
  574. tmpm1 = 5;
  575. tmpzero = 5;
  576. tmpone = 5;
  577. tmptwo = 5;
  578. for (int d = 0;d<4;d++){
  579. if (m2[d] != -1){
  580. if (tmpm2>prof[pos[0]][m2[d]][rand-1].x){
  581. tmpm2 = prof[pos[0]][m2[d]][rand-1].x;
  582. }
  583. }
  584. if (m1[d] != -1){
  585. if (tmpm1>prof[pos[0]][m1[d]][rand-1].x){
  586. tmpm1 = prof[pos[0]][m1[d]][rand-1].x;
  587. }
  588. }
  589. if (zero[d] != -1){
  590. if (tmpzero>prof[pos[0]][zero[d]][rand-1].x){
  591. tmpzero = prof[pos[0]][zero[d]][rand-1].x;
  592. }
  593. }
  594. if (one[d] != -1){
  595. if (tmpone>prof[pos[0]][one[d]][rand-1].x){
  596. tmpone = prof[pos[0]][one[d]][rand-1].x;
  597. }
  598. }
  599. if (two[d] != -1){
  600. if (tmptwo>prof[pos[0]][two[d]][rand-1].x){
  601. tmptwo = prof[pos[0]][two[d]][rand-1].x;
  602. }
  603. }
  604. }
  605. }
  606. int total = 0;
  607. for (int d = 0;d<4;d++){
  608. if (prof[pos[0]][d][rand-1].y == -2){
  609. if (perim[2+centralx+deltax+tmpm2][2+centraly-2] != 2){
  610. if(b[centralx+deltax+tmpm2][centraly-2].getBackground() == Color.DARK_GRAY){
  611. total++;
  612. }}
  613. } else if (prof[pos[0]][d][rand-1].y == -1){
  614. if (perim[2+centralx+deltax+tmpm1][2+centraly-1] != 2){
  615. if (b[centralx+deltax+tmpm1][centraly-1].getBackground() == Color.DARK_GRAY){
  616. total++;
  617. }}
  618. } else if (prof[pos[0]][d][rand-1].y == 0){
  619. if (perim[2+centralx+deltax+tmpzero][2+centraly] != 2){
  620. if (b[centralx+deltax+tmpzero][centraly].getBackground() == Color.DARK_GRAY){
  621. total++;
  622. }}
  623. } else if (prof[pos[0]][d][rand-1].y == 1){
  624. if (perim[2+centralx+deltax+tmpone][2+centraly+1] != 2){
  625. if (b[centralx+deltax+tmpone][centraly+1].getBackground() == Color.DARK_GRAY){
  626. total++;
  627. }}
  628. } else if (prof[pos[0]][d][rand-1].y == 2){
  629. if (perim[2+centralx+deltax+tmptwo][2+centraly+2] != 2){
  630. if (b[centralx+deltax+tmptwo][centraly+2].getBackground() == Color.DARK_GRAY){
  631. total++;
  632. }}
  633. }
  634. } if (total == 4){
  635. b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);
  636. b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);
  637. b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);
  638. b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);
  639. centralx = centralx+deltax;
  640. b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);
  641. b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);
  642. b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);
  643. b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);
  644. }
  645.  
  646.  
  647. }
  648.  
  649. public static void main (String[] args){
  650. new Tetris();
  651. }
  652.  
  653. @Override
  654. public void keyPressed(KeyEvent e) {
  655. if (e.getKeyCode() == KeyEvent.VK_RIGHT){
  656. deltax = 1;
  657. movelr();
  658. }
  659. if (e.getKeyCode() == KeyEvent.VK_LEFT){
  660. deltax = -1;
  661. movelr();
  662. }
  663. if (e.getKeyCode() == KeyEvent.VK_UP){
  664. rotate();
  665. }
  666. if (e.getKeyCode() == KeyEvent.VK_DOWN){
  667. movedown();
  668. }
  669. }
  670.  
  671.  
  672. @Override
  673. public void keyReleased(KeyEvent e) {
  674. // TODO Auto-generated method stub
  675.  
  676. }
  677.  
  678.  
  679. @Override
  680. public void keyTyped(KeyEvent e) {
  681. // TODO Auto-generated method stub
  682.  
  683. }
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement