Advertisement
Guest User

Untitled

a guest
Apr 11th, 2012
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.93 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;
  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. public Tetris(){
  19. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  20. for (int y = 0;y<2;y++){
  21. for (int x = 0;x<m+4;x++){
  22. perim[x][y]= 1;
  23. }}
  24. for (int y = n+2;y<n+4;y++){
  25. for (int x = 0;x<m+4;x++){
  26. perim[x][y]= 4;
  27. }}
  28. for (int y = 2;y<n+2;y++){
  29. for (int x = 0;x<2;x++){
  30. perim[x][y]= 2;
  31. }}
  32. for (int y = 2;y<n+2;y++){
  33. for (int x = m+2;x<m+4;x++){
  34. perim[x][y]= 2;
  35. }}
  36. for(int y = 0;y<n+4;y++){
  37. for (int x = 0;x<m+4;x++){
  38. System.out.print(perim[x][y]);
  39. }
  40. System.out.println("");
  41. }
  42. b = new JButton [m][n];
  43. setLayout(new GridLayout(n,m));//Grid layouts x and y are SWAPPED!
  44. for (int y = 0;y<n;y++){
  45. for (int x = 0;x<m;x++){
  46. b[x][y] = new JButton(" ");
  47. tmp[x][y] = Color.DARK_GRAY;
  48. b[x][y].setBackground(Color.DARK_GRAY);
  49. add(b[x][y]);
  50. b[x][y].setEnabled(true);
  51. }//end inner for
  52. }
  53. setFocusable(true);
  54. addKeyListener(this);
  55. pack();
  56. setVisible(true);
  57. blockgen();
  58.  
  59.  
  60. }//end constructor Mine()
  61.  
  62. public void blockgen(){
  63. pos = 0;
  64. rand = (int) (Math.floor(Math.random()*7+1));
  65. System.out.print(rand);
  66. if (rand == 1){
  67. z();
  68. centralx = 4;
  69. centraly = 0;
  70. go();
  71. } else if (rand == 2){
  72. l();
  73. centralx = 4;
  74. centraly = 0;
  75. go();
  76. } else if (rand == 3){
  77. bl();
  78. centralx = 4;
  79. centraly = 0;
  80. go();
  81. } else if (rand == 4){
  82. s();
  83. centralx = 4;
  84. centraly = 0;
  85. go();
  86. } else if (rand == 5){
  87. t();
  88. centralx = 4;
  89. centraly = 0;
  90. go();
  91. } else if (rand == 6){
  92. sq();
  93. centralx = 4;
  94. centraly = 0;
  95. go();
  96. } else if (rand == 7){
  97. i();
  98. centralx = 4;
  99. centraly = 0;
  100. go();
  101. }
  102. }
  103.  
  104. public void rotate(){
  105. pos++;
  106. if (rand == 1){
  107.  
  108. } else if (rand == 2){
  109.  
  110. } else if (rand == 3){
  111.  
  112. } else if (rand == 4){
  113.  
  114. } else if (rand == 5){
  115.  
  116. } else if (rand == 6){
  117.  
  118. } else if (rand == 7){
  119. if (pos == 1){
  120. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  121. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  122. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  123. b[centralx+2][centraly].setBackground(Color.DARK_GRAY);
  124. b[centralx][centraly-1].setBackground(Color.orange);
  125. b[centralx][centraly].setBackground(Color.orange);
  126. b[centralx][centraly+1].setBackground(Color.orange);
  127. b[centralx][centraly+2].setBackground(Color.orange);
  128. }
  129. }
  130. }
  131.  
  132. public void movedown(){
  133. if (rand == 1){
  134. if ((perim[2+centralx-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
  135. && (perim[centralx+2][centraly+1+2] != 4)
  136. && (perim[centralx+2][centraly+1+1+2] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)
  137. && (perim[centralx+1+2][centraly+1+1+2] != 4) && (b[centralx+1][centraly+1+1].getBackground() == Color.DARK_GRAY)){
  138. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  139. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  140. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  141. b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
  142. centraly++;
  143. b[centralx-1][centraly].setBackground(Color.red);
  144. b[centralx][centraly].setBackground(Color.red);
  145. b[centralx][centraly+1].setBackground(Color.red);
  146. b[centralx+1][centraly+1].setBackground(Color.red);
  147. } else {
  148. bottom = true;
  149. }
  150. } else if (rand == 2){
  151. if ((perim[centralx+2-1][centraly+1+2] != 4)
  152. && (perim[centralx+2-1][centraly+1+2+1] != 4) && (b[centralx-1][centraly+1+1].getBackground() == Color.DARK_GRAY)
  153. && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
  154. && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)){
  155. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  156. b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
  157. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  158. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  159. centraly++;
  160. b[centralx-1][centraly].setBackground(Color.yellow);
  161. b[centralx-1][centraly+1].setBackground(Color.yellow);
  162. b[centralx][centraly].setBackground(Color.yellow);
  163. b[centralx+1][centraly].setBackground(Color.yellow);
  164. } else {
  165. bottom = true;
  166. }
  167. } else if (rand == 3){
  168. if ((perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
  169. && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
  170. && (perim[centralx+2+1][centraly+1+2] != 4)
  171. && (perim[centralx+2+1][centraly+1+2+1] != 4) && (b[centralx+1][centraly+1+1].getBackground() == Color.DARK_GRAY)){
  172. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  173. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  174. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  175. b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
  176. centraly++;
  177. b[centralx-1][centraly].setBackground(Color.cyan);
  178. b[centralx][centraly].setBackground(Color.cyan);
  179. b[centralx+1][centraly].setBackground(Color.cyan);
  180. b[centralx+1][centraly+1].setBackground(Color.cyan);
  181. } else {
  182. bottom = true;
  183. }
  184. } else if (rand == 4){
  185. if ((perim[centralx+2-1][centraly+1+2+1] != 4) && (b[centralx-1][centraly+1+1].getBackground() == Color.DARK_GRAY)
  186. && (perim[centralx+2][centraly+1+2+1] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)
  187. && (perim[centralx+2][centraly+1+2] != 4)
  188. && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)){
  189. b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
  190. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  191. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  192. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  193. centraly++;
  194. b[centralx-1][centraly+1].setBackground(Color.green);
  195. b[centralx][centraly+1].setBackground(Color.green);
  196. b[centralx][centraly].setBackground(Color.green);
  197. b[centralx+1][centraly].setBackground(Color.green);
  198. } else {
  199. bottom = true;
  200. }
  201. } else if (rand == 5){
  202. if ((perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)
  203. && (perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
  204. && (perim[centralx+2][centraly+1+2] != 4)
  205. && (perim[centralx+2][centraly+1+2+1] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)){
  206. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  207. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  208. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  209. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  210. centraly++;
  211. b[centralx-1][centraly].setBackground(Color.white);
  212. b[centralx][centraly].setBackground(Color.white);
  213. b[centralx+1][centraly].setBackground(Color.white);
  214. b[centralx][centraly+1].setBackground(Color.white);
  215. } else {
  216. bottom = true;
  217. }
  218. } else if (rand == 6){
  219. if ((perim[centralx+2][centraly+1+2] != 4)
  220. && (perim[centralx+2][centraly+1+2+1] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)
  221. && (perim[centralx+2+1][centraly+1+2] != 4)
  222. && (perim[centralx+2+1][centraly+1+2+1] != 4) && (b[centralx+1][centraly+1+1].getBackground() == Color.DARK_GRAY)){
  223. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  224. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  225. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  226. b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
  227. centraly++;
  228. b[centralx][centraly].setBackground(Color.blue);
  229. b[centralx][centraly+1].setBackground(Color.blue);
  230. b[centralx+1][centraly].setBackground(Color.blue);
  231. b[centralx+1][centraly+1].setBackground(Color.blue);
  232. } else {
  233. bottom = true;
  234. }
  235. } else if (rand == 7){
  236. if (pos == 0){
  237. if ((perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
  238. && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
  239. && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)
  240. && (perim[centralx+2+2][centraly+1+2] != 4) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)){
  241. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  242. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  243. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  244. b[centralx+2][centraly].setBackground(Color.DARK_GRAY);
  245. centraly++;
  246. b[centralx-1][centraly].setBackground(Color.orange);
  247. b[centralx][centraly].setBackground(Color.orange);
  248. b[centralx+1][centraly].setBackground(Color.orange);
  249. b[centralx+2][centraly].setBackground(Color.orange);
  250. } else {
  251. bottom = true;
  252. }
  253. } else if (pos == 1){
  254. if ((perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
  255. && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
  256. && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)
  257. && (perim[centralx+2+2][centraly+1+2] != 4) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)){
  258. b[centralx][centraly-1].setBackground(Color.DARK_GRAY);
  259. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  260. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  261. b[centralx][centraly+2].setBackground(Color.DARK_GRAY);
  262. centraly++;
  263. b[centralx][centraly-1].setBackground(Color.orange);
  264. b[centralx][centraly].setBackground(Color.orange);
  265. b[centralx][centraly+1].setBackground(Color.orange);
  266. b[centralx][centraly+2].setBackground(Color.orange);
  267. } else {
  268. bottom = true;
  269. }
  270. }
  271. }
  272.  
  273. }
  274.  
  275. public void go(){
  276. do{
  277. try {
  278. Thread.sleep(1000L);
  279. } catch (InterruptedException e) {
  280. // TODO Auto-generated catch block
  281. e.printStackTrace();
  282. }
  283. movedown();
  284. rowcheck();
  285. }
  286. while(bottom == false);
  287. bottom = false;
  288. blockgen();
  289. }
  290.  
  291. public void rowcheck(){
  292. int row = 0;
  293. for (int y = 0;y<20;y++){
  294. for (int x = 0;x<10;x++){
  295. if (b[x][y].getBackground() != Color.DARK_GRAY){
  296. row++;
  297. }
  298. if (row == 10){
  299. rowclear(y);
  300. }
  301. }
  302. row = 0;
  303. }
  304. }
  305.  
  306. public void rowclear(int y){
  307. int inc = 0;
  308. for (int x = 0;x<10;x++){
  309. b[x][y].setBackground(Color.DARK_GRAY);
  310. }
  311. for (int c = y-1;c>-1;c--){
  312. for (int x = 0;x<10;x++){
  313. tmp[x][y-inc] = b[x][c].getBackground();
  314. }inc++;}
  315. for (int c = y;c>-1;c--){
  316. for (int x = 0;x<10;x++){
  317. b[x][c].setBackground(tmp[x][c]);
  318. }
  319. }
  320. }
  321.  
  322. public void movelr(){
  323. if (rand == 1){
  324. if (((perim[centralx+deltax-1+2][centraly+2] != 1) && (perim[2+centralx+deltax-1][centraly+2] != 2) && (perim[2+centralx+deltax-1][2+centraly] != 3) && (perim[2+centralx+deltax-1][centraly+2] != 4))
  325. && ((perim[centralx+deltax+2][centraly+2] != 1) && (perim[2+centralx+deltax][centraly+2] != 2) && (perim[centralx+deltax+2][centraly+2] != 3) && (perim[centralx+deltax+2][centraly+2] != 4))
  326. && ((perim[centralx+deltax+2][centraly+1+2] != 1) && (perim[centralx+deltax+2][centraly+1+2] != 2) && (perim[centralx+deltax+2][centraly+1+2] != 3) && (perim[centralx+deltax+2][centraly+1+2] != 4))
  327. && ((perim[centralx+1+deltax+2][centraly+1+2] != 1) && (perim[centralx+deltax+1+2][centraly+1+2] != 2) && (perim[centralx+deltax+1+2][centraly+1+2] != 3) && (perim[centralx+deltax+1+2][centraly+1+2] != 4))){
  328. if (((deltax == 1) && (b[centralx+deltax+1][centraly+1].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax][centraly].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx+deltax][centraly+1].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax-1][centraly].getBackground() == Color.DARK_GRAY))){
  329. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  330. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  331. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  332. b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
  333. centralx = centralx+deltax;
  334. b[centralx-1][centraly].setBackground(Color.red);
  335. b[centralx][centraly].setBackground(Color.red);
  336. b[centralx][centraly+1].setBackground(Color.red);
  337. b[centralx+1][centraly+1].setBackground(Color.red);
  338. }
  339. }
  340. } else if (rand == 2){
  341. if (((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
  342. && ((perim[centralx+2+deltax-1][centraly+2+1] != 1) && (perim[centralx+2+deltax-1][centraly+2+1] != 2) && (perim[centralx+2+deltax-1][centraly+2+1] != 3) && (perim[centralx+2+deltax-1][centraly+2+1] != 4))
  343. && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
  344. && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))){
  345. if (((deltax == 1) && (b[centralx+deltax+1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax-1][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx+deltax-1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax-1][centraly+1].getBackground() == Color.DARK_GRAY))){
  346. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  347. b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
  348. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  349. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  350. centralx = centralx+deltax;
  351. b[centralx-1][centraly].setBackground(Color.yellow);
  352. b[centralx-1][centraly+1].setBackground(Color.yellow);
  353. b[centralx][centraly].setBackground(Color.yellow);
  354. b[centralx+1][centraly].setBackground(Color.yellow);
  355. }
  356. }
  357. } else if (rand == 3){
  358. if (((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
  359. && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
  360. && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
  361. && ((perim[centralx+2+deltax+1][centraly+2+1] != 1) && (perim[centralx+2+deltax+1][centraly+2+1] != 2) && (perim[centralx+2+deltax+1][centraly+2+1] != 3) && (perim[centralx+2+deltax+1][centraly+2+1] != 4))){
  362. if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY) && (b[centralx-2][centraly].getBackground() == Color.DARK_GRAY))){
  363. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  364. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  365. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  366. b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
  367. centralx = centralx+deltax;
  368. b[centralx-1][centraly].setBackground(Color.cyan);
  369. b[centralx][centraly].setBackground(Color.cyan);
  370. b[centralx+1][centraly].setBackground(Color.cyan);
  371. b[centralx+1][centraly+1].setBackground(Color.cyan);
  372. }
  373. }
  374. } else if (rand == 4){
  375. if (((perim[centralx+2+deltax-1][centraly+2+1] != 1) && (perim[centralx+2+deltax-1][centraly+2+1] != 2) && (perim[centralx+2+deltax-1][centraly+2+1] != 3) && (perim[centralx+2+deltax-1][centraly+2+1] != 4))
  376. && ((perim[centralx+2+deltax][centraly+2+1] != 1) && (perim[centralx+2+deltax][centraly+2+1] != 2) && (perim[centralx+2+deltax][centraly+2+1] != 3) && (perim[centralx+2+deltax][centraly+2+1] != 4))
  377. && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
  378. && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))){
  379. if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx-1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx-2][centraly+1].getBackground() == Color.DARK_GRAY))){
  380. b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
  381. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  382. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  383. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  384. centralx = centralx+deltax;
  385. b[centralx-1][centraly+1].setBackground(Color.green);
  386. b[centralx][centraly+1].setBackground(Color.green);
  387. b[centralx][centraly].setBackground(Color.green);
  388. b[centralx+1][centraly].setBackground(Color.green);
  389. }
  390. }
  391. } else if (rand == 5){
  392. if (((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
  393. && ((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
  394. && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
  395. && ((perim[centralx+2+deltax][centraly+2+1] != 1) && (perim[centralx+2+deltax][centraly+2+1] != 2) && (perim[centralx+2+deltax][centraly+2+1] != 3) && (perim[centralx+2+deltax][centraly+2+1] != 4))){
  396. if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx-2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY))){
  397. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  398. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  399. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  400. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  401. centralx = centralx+deltax;
  402. b[centralx-1][centraly].setBackground(Color.white);
  403. b[centralx][centraly].setBackground(Color.white);
  404. b[centralx+1][centraly].setBackground(Color.white);
  405. b[centralx][centraly+1].setBackground(Color.white);
  406. }
  407.  
  408. }
  409. } else if (rand == 6){
  410. if (((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
  411. && ((perim[centralx+2+deltax][centraly+2+1] != 1) && (perim[centralx+2+deltax][centraly+2+1] != 2) && (perim[centralx+2+deltax][centraly+2+1] != 3) && (perim[centralx+2+deltax][centraly+2+1] != 4))
  412. && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
  413. && ((perim[centralx+2+deltax+1][centraly+2+1] != 1) && (perim[centralx+2+deltax+1][centraly+2+1] != 2) && (perim[centralx+2+deltax+1][centraly+2+1] != 3) && (perim[centralx+2+deltax+1][centraly+2+1] != 4))){
  414. if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx-1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY))){
  415. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  416. b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
  417. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  418. b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
  419. centralx = centralx+deltax;
  420. b[centralx][centraly].setBackground(Color.blue);
  421. b[centralx][centraly+1].setBackground(Color.blue);
  422. b[centralx+1][centraly].setBackground(Color.blue);
  423. b[centralx+1][centraly+1].setBackground(Color.blue);
  424. }
  425. }
  426. } else if (rand == 7){
  427. if (((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
  428. && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
  429. && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
  430. && ((perim[centralx+2+deltax+2][centraly+2] != 1) && (perim[centralx+2+deltax+2][centraly+2] != 2) && (perim[centralx+2+deltax+2][centraly+2] != 3) && (perim[centralx+2+deltax+2][centraly+2] != 4))){
  431. if (((deltax == 1) && (b[centralx+deltax+2][centraly].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx+deltax-1][centraly].getBackground() == Color.DARK_GRAY))){
  432. b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
  433. b[centralx][centraly].setBackground(Color.DARK_GRAY);
  434. b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
  435. b[centralx+2][centraly].setBackground(Color.DARK_GRAY);
  436. centralx = centralx+deltax;
  437. b[centralx-1][centraly].setBackground(Color.orange);
  438. b[centralx][centraly].setBackground(Color.orange);
  439. b[centralx+1][centraly].setBackground(Color.orange);
  440. b[centralx+2][centraly].setBackground(Color.orange);
  441. }
  442. }
  443. }
  444. }
  445.  
  446.  
  447.  
  448. public void l(){
  449. b[3][0].setBackground(Color.yellow);
  450. b[3][1].setBackground(Color.yellow);
  451. b[4][0].setBackground(Color.yellow);
  452. b[5][0].setBackground(Color.yellow);
  453. }
  454.  
  455. public void sq(){
  456. b[4][0].setBackground(Color.blue);
  457. b[4][1].setBackground(Color.blue);
  458. b[5][0].setBackground(Color.blue);
  459. b[5][1].setBackground(Color.blue);
  460. }
  461.  
  462. public void bl(){
  463. b[3][0].setBackground(Color.cyan);
  464. b[4][0].setBackground(Color.cyan);
  465. b[5][0].setBackground(Color.cyan);
  466. b[5][1].setBackground(Color.cyan);
  467. }
  468.  
  469. public void s(){
  470. b[3][1].setBackground(Color.green);
  471. b[4][1].setBackground(Color.green);
  472. b[4][0].setBackground(Color.green);
  473. b[5][0].setBackground(Color.green);
  474. }
  475.  
  476. public void z(){
  477. b[3][0].setBackground(Color.red);
  478. b[4][0].setBackground(Color.red);
  479. b[4][1].setBackground(Color.red);
  480. b[5][1].setBackground(Color.red);
  481. }
  482.  
  483. public void i(){
  484. b[6][0].setBackground(Color.orange);
  485. b[3][0].setBackground(Color.orange);
  486. b[4][0].setBackground(Color.orange);
  487. b[5][0].setBackground(Color.orange);
  488. }
  489.  
  490. public void t(){
  491. b[3][0].setBackground(Color.white);
  492. b[4][0].setBackground(Color.white);
  493. b[4][1].setBackground(Color.white);
  494. b[5][0].setBackground(Color.white);
  495. }
  496.  
  497. public static void main (String[] args){
  498. new Tetris();
  499. }
  500.  
  501. @Override
  502. public void keyPressed(KeyEvent e) {
  503. if (e.getKeyCode() == KeyEvent.VK_RIGHT){
  504. deltax = 1;
  505. movelr();
  506. }
  507. if (e.getKeyCode() == KeyEvent.VK_LEFT){
  508. deltax = -1;
  509. movelr();
  510. }
  511. if (e.getKeyCode() == KeyEvent.VK_UP){
  512. rotate();
  513. }
  514. if (e.getKeyCode() == KeyEvent.VK_DOWN){
  515. movedown();
  516. }
  517. }
  518.  
  519.  
  520. @Override
  521. public void keyReleased(KeyEvent e) {
  522. // TODO Auto-generated method stub
  523.  
  524. }
  525.  
  526.  
  527. @Override
  528. public void keyTyped(KeyEvent e) {
  529. // TODO Auto-generated method stub
  530.  
  531. }
  532. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement