Advertisement
Guest User

Untitled

a guest
Jun 24th, 2012
8,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.80 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.GridLayout;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8.  
  9. import javax.swing.BorderFactory;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class laser extends JFrame implements KeyListener {
  15.  
  16. public static void main (String[] args){
  17. System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
  18. new laser();
  19. }
  20. static int score = 0;
  21. static boolean fire2 = false;
  22. static boolean wmd = false;
  23. static int deltax[] = {0,-1, 1, 0};
  24. static int deltay[] = {-1, 0, 0, 1};
  25. static int rand[] = { (int) (Math.floor(Math.random()*8+1)), (int) (Math.floor(Math.random()*8+1)), (int) (Math.floor(Math.random()*8+1)), (int) (Math.floor(Math.random()*8+1)), (int) (Math.floor(Math.random()*8+1)), (int) (Math.floor(Math.random()*8+1)), (int) Math.floor(Math.random()*8+1), (int) Math.floor(Math.random()*8+1), (int) (Math.floor(Math.random()*8+1)) };
  26. static Color[] colours = new Color[9];
  27. static Color[] colour = {Color.BLACK, Color.blue, Color.WHITE, Color.magenta, (new Color (255,36,0)), Color.green, Color.cyan, (new Color (66,19,130)), Color.yellow};
  28. static Color[][] grid = new Color[14][23];
  29. static int energy = 0;
  30. static double pulse = 0;
  31. static double bullet = 0;
  32. static double clock = 0;
  33. static boolean fire = false;
  34. static boolean up = false;
  35. static boolean down = false;
  36. static boolean left = false;
  37. static boolean right = false;
  38. static int m = 16;
  39. static int n = 27;
  40. JLabel b[][];
  41. static int health = 15;
  42. static int centrex = 7;
  43. static int centrey = 19;
  44. static int ammo = 15;
  45. static boolean alive = true;
  46. static int num = 0;
  47. static Color[] bc = new Color[25];
  48. static int[] bx = new int[25];
  49. static int[] by = new int[25];
  50. static int tmp[][] = new int[14][23];
  51. static int count = 0;
  52. public laser(){
  53. for (int x = 0;x<25;x++){
  54. bx[x] = -5;
  55. by[x] = -5;
  56. }
  57. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  58. b = new JLabel [m][n];
  59. setLayout(new GridLayout(n,m));
  60. for (int y = 0;y<n;y++){
  61. for (int x = 0;x<m;x++){
  62. b[x][y] = new JLabel( );
  63. b[x][y].setBackground(Color.BLACK);
  64. add(b[x][y]);
  65. b[x][y].setEnabled(true);
  66. b[x][y].setOpaque(true);
  67. b[x][y].setBorder(BorderFactory.createLineBorder(Color.BLACK));
  68. b[x][y].setPreferredSize(new Dimension(50,25));
  69. }
  70. }
  71. setFocusable(true);
  72. addKeyListener(this);
  73. pack();
  74. setVisible(true);
  75. for (int y = 0;y<23;y++){
  76. grid[0][y] = Color.gray;
  77. grid[13][y] = Color.gray;
  78. }
  79. for (int x = 0;x<14;x++){
  80. grid[x][0] = Color.gray;
  81. grid[x][22] = Color.gray;
  82. }
  83. go();
  84. }
  85.  
  86. public void colours(){
  87. int random = (int) (Math.floor(Math.random()*8+1));
  88. colours[8] = colours[7];
  89. colours[7] = colours[6];
  90. colours[6] = colours[5];
  91. colours[5] = colours[4];
  92. colours[4] = colours[3];
  93. colours[3] = colours[2];
  94. colours[2] = colours[1];
  95. colours[1] = colours[0];
  96. colours[0] = colour[random];
  97. for (int y = 0;y<9;y++){
  98. b[0][(y)*3].setBackground(colours[y]);
  99. b[0][(y)*3+1].setBackground(colours[y]);
  100. b[0][(y)*3+2].setBackground(colours[y]);
  101. }
  102. }
  103.  
  104. public void combo(){
  105. for (int x = 0;x<12;x++){
  106. for (int y = 0;y<21;y++){
  107. if (grid[x+1][y+1] != Color.BLACK){
  108. count = 0;
  109. combochk(x, y, grid[x+1][y+1]);
  110. for (int x2 = 0;x2<12;x2++){
  111. for (int y2 = 0;y2<21;y2++){
  112. if (count > 3){
  113. if (tmp[x2+1][y2+1] == 1){
  114. grid[x2+1][y2+1] = Color.BLACK;
  115. }
  116. }
  117. tmp[x2+1][y2+1] = 0;
  118. }
  119. }
  120. if (count > 3){
  121. score++;
  122. energy+=6;
  123. }
  124. }}
  125. }
  126. }
  127.  
  128. public void combochk(int x, int y, Color c){
  129. for (int a = 0;a<4;a++){
  130. if ((grid[x+1+deltax[a]][y+1+deltay[a]] == c) && (tmp[x+deltax[a]+1][y+deltay[a]+1] == 0)){
  131. tmp[x+deltax[a]+1][y+deltay[a]+1] = 1;
  132. count++;
  133. combochk(x+deltax[a], y+deltay[a], c);
  134. }}}
  135.  
  136. public void power(){
  137. for (int y = 0;y<27;y++){
  138. if ((27-energy) <= y){
  139. b[15][y].setBackground(Color.green);
  140. } else {
  141. b[15][y].setBackground(Color.red);
  142. }
  143. }
  144. }
  145.  
  146. public void gen(){
  147. int randi;
  148. int geni;
  149. for (int x = 0;x<12;x++){
  150. for (int y = 19;y>=0;y--){
  151. grid[x+1][y+1+1] = grid[x+1][y+1];
  152. }
  153. }
  154. for (int x = 0; x<12;x++){
  155. geni = (int) (Math.floor(Math.random()*100+1));
  156. if (geni<50){
  157. randi = (int) (Math.floor(Math.random()*8+1));
  158. } else {
  159. randi = 0;
  160. }
  161. grid [x+1][1] = colour[randi];
  162. }
  163. }
  164.  
  165. public void match(){
  166. for (int x = 0;x<12;x++){
  167. for (int y = 0;y<21;y++){
  168. b[x+2][y].setBackground(grid[x+1][y+1]);
  169. }
  170. }
  171. }
  172.  
  173. public void go(){
  174. setup();
  175. int time = 4000;
  176. clock = System.currentTimeMillis();
  177. bullet = System.currentTimeMillis();
  178. pulse = System.currentTimeMillis();
  179. do{
  180. if (health < 0){
  181. Component temporaryLostComponent = null;
  182. JOptionPane.showMessageDialog(temporaryLostComponent, "Game Over! You scored: "+score+". Well done!");
  183. System.exit(0);
  184. }
  185. ship();
  186. if (System.currentTimeMillis()-pulse > time){
  187. if (time>1000){
  188. time-=10;
  189. }
  190. gen();
  191. match();
  192. pulse = System.currentTimeMillis();
  193. }
  194. if (System.currentTimeMillis()-clock > 300){
  195. regen();
  196. clock = System.currentTimeMillis();
  197. }
  198. if (System.currentTimeMillis()-bullet > 30){
  199. health();
  200. power();
  201. combo();
  202. projectile();
  203. bullet = System.currentTimeMillis();
  204. }
  205. if (up == true){
  206. up();
  207. up = false;
  208. }
  209. if (down == true){
  210. down();
  211. down = false;
  212. }
  213. if (left == true){
  214. left();
  215. left = false;
  216. }
  217. if (right == true){
  218. right();
  219. right = false;
  220. }
  221. if (fire == true){
  222. if ((ammo>0) && (num<25)){
  223. fire();
  224. colours();
  225. projectile();
  226. }
  227. }
  228. if (fire2 == true){
  229. wmd();
  230. fire2 = false;
  231. }
  232. fire = false;
  233. }while (alive == true);
  234. }
  235.  
  236. public void wmd(){
  237. energy = 0;
  238. for (int x = 0;x<12;x++){
  239. for (int y = 0;y<21;y++){
  240. grid[x+1][y+1] = Color.BLACK;
  241. }
  242. }
  243. }
  244.  
  245. public void regen(){
  246. if (ammo<12){
  247. ammo+=1;
  248. ammo(Color.blue);
  249. } else {
  250. ammo = 15;
  251. ammo(Color.blue);
  252. }
  253. }
  254.  
  255. public void setup(){
  256. for (int x = 1;x<13;x++){
  257. for(int y = 1;y<22;y++){
  258. grid[x][y] = Color.BLACK;
  259. }
  260. }
  261. for (int x = 0;x<9;x++){
  262. colours[x] = colour[rand[x]];
  263. }
  264. for (int y = 0;y<9;y++){
  265. b[0][(y)*3].setBackground(colours[y]);
  266. b[0][(y)*3+1].setBackground(colours[y]);
  267. b[0][(y)*3+2].setBackground(colours[y]);
  268. }
  269. power();
  270. ship();
  271. health();
  272. ammo(Color.blue);
  273. gen();
  274. match();
  275. }
  276.  
  277. public void ammo(Color l){
  278. for (int x = 1;x<ammo;x++){
  279. b[x][26].setBackground(l);
  280. b[x][25].setBackground(l);
  281. }
  282. }
  283. public void health(){
  284. for (int x = 1;x<15;x++){
  285. b[x][24].setBackground(Color.BLACK);
  286. }
  287. for (int x = 1;x<health;x++){
  288. if (x<3){
  289. b[x][24].setBackground(Color.red);
  290. } else {
  291. b[x][24].setBackground(Color.green);
  292. }
  293. }
  294. }
  295.  
  296. public void ship(){
  297. b[centrex+1][centrey].setBackground(Color.WHITE);
  298. b[centrex-1][centrey].setBackground(Color.WHITE);
  299. b[centrex][centrey-1].setBackground(Color.WHITE);
  300. b[centrex][centrey-2].setBackground(Color.WHITE);
  301. b[centrex+1][centrey-1].setBackground(Color.WHITE);
  302. b[centrex-1][centrey-1].setBackground(Color.WHITE);
  303. }
  304.  
  305. public void fire(){
  306. num++;
  307. bx[num-1] = centrex;
  308. by[num-1] = centrey-3;
  309. bc[num-1] = colours[8];
  310. ammo(Color.orange);
  311. if (ammo>-1){
  312. ammo-=6;
  313. }
  314. ammo(Color.BLUE);
  315. }
  316.  
  317. public void projectile(){
  318. boolean hit = false;
  319. Color tmpbc[] = new Color[25];
  320. int tmpbx[] = new int[25];
  321. int tmpby[] = new int[25];
  322. for (int y = 0;y<25;y++){
  323. tmpbc[y] = bc[y];
  324. tmpbx[y] = bx[y];
  325. tmpby[y] = by[y];
  326. }
  327. for (int x = 0;x<num;x++){
  328. if ((by[x] > 0) && (checkhit(bx[x],by[x]) == false)){
  329. b[bx[x]][by[x]].setBackground(Color.BLACK);
  330. } else {
  331. hit = true;
  332. if (hit == true){
  333. grid[bx[x]-2+1][by[x]+1] = bc[x];
  334. if(x < 25){
  335. if (x != 24){
  336. if (tmpbx[x+1] == -5){
  337. tmpbc[x] = null;
  338. tmpbx[x] = -5;
  339. tmpby[x] = -5;
  340. } else {
  341. for (int c = x;c<num;c++){
  342. tmpbc[c] = tmpbc[c+1];
  343. tmpbx[c] = tmpbx[c+1];
  344. tmpby[c] = tmpby[c+1];
  345. }
  346. }
  347. } else {
  348. tmpbc[x] = null;
  349. tmpbx[x] = -5;
  350. tmpby[x] = -5;
  351. }
  352. num--;
  353. }
  354. x--;
  355. for (int y = 0;y<25;y++){
  356. bc[y] = tmpbc[y];
  357. bx[y] = tmpbx[y];
  358. by[y] = tmpby[y];
  359. }
  360. }
  361. }
  362. }
  363. try {
  364. Thread.sleep(30L);
  365. } catch (InterruptedException e) {
  366. // TODO Auto-generated catch block
  367. e.printStackTrace();
  368. }
  369. for (int x = 0;x<num;x++){
  370. if ((by[x] > 0) && (checkhit(bx[x],by[x]) == false)){
  371. by[x]--;
  372. b[bx[x]][by[x]].setBackground(bc[x]);
  373. }
  374. }
  375. }
  376.  
  377. public boolean checkhit(int bx, int by){
  378. if(b[bx][by-1].getBackground() == Color.BLACK){
  379. return false;
  380. } else{
  381. return true;
  382. }
  383. }
  384.  
  385.  
  386. public void right(){
  387. if ((b[centrex+1+1][centrey].getBackground() != Color.BLACK) ||
  388. (b[centrex+1+1][centrey-1].getBackground() != Color.BLACK) ||
  389. (b[centrex+1][centrey-1-1].getBackground() != Color.BLACK)){
  390. health--;
  391. }
  392. b[centrex+1][centrey].setBackground(Color.BLACK);
  393. b[centrex-1][centrey].setBackground(Color.BLACK);
  394. b[centrex][centrey-1].setBackground(Color.BLACK);
  395. b[centrex][centrey-2].setBackground(Color.BLACK);
  396. b[centrex+1][centrey-1].setBackground(Color.BLACK);
  397. b[centrex-1][centrey-1].setBackground(Color.BLACK);
  398. centrex++;
  399. b[centrex+1][centrey].setBackground(Color.WHITE);
  400. b[centrex-1][centrey].setBackground(Color.WHITE);
  401. b[centrex][centrey-1].setBackground(Color.WHITE);
  402. b[centrex][centrey-2].setBackground(Color.WHITE);
  403. b[centrex+1][centrey-1].setBackground(Color.WHITE);
  404. b[centrex-1][centrey-1].setBackground(Color.WHITE);
  405. }
  406.  
  407. public void left(){
  408. if ((b[centrex-1-1][centrey].getBackground() != Color.BLACK) ||
  409. (b[centrex-1][centrey-2].getBackground() != Color.BLACK) ||
  410. (b[centrex-1-1][centrey-1].getBackground() != Color.BLACK)){
  411. health--;
  412. }
  413. b[centrex+1][centrey].setBackground(Color.BLACK);
  414. b[centrex-1][centrey].setBackground(Color.BLACK);
  415. b[centrex][centrey-1].setBackground(Color.BLACK);
  416. b[centrex][centrey-2].setBackground(Color.BLACK);
  417. b[centrex+1][centrey-1].setBackground(Color.BLACK);
  418. b[centrex-1][centrey-1].setBackground(Color.BLACK);
  419. centrex--;
  420. b[centrex+1][centrey].setBackground(Color.WHITE);
  421. b[centrex-1][centrey].setBackground(Color.WHITE);
  422. b[centrex][centrey-1].setBackground(Color.WHITE);
  423. b[centrex][centrey-2].setBackground(Color.WHITE);
  424. b[centrex+1][centrey-1].setBackground(Color.WHITE);
  425. b[centrex-1][centrey-1].setBackground(Color.WHITE);
  426. }
  427.  
  428. public void up(){
  429. if ((b[centrex+1][centrey-1-1].getBackground() != Color.BLACK) ||
  430. (b[centrex-1][centrey-1-1].getBackground() != Color.BLACK) ||
  431. (b[centrex][centrey-2-1].getBackground() != Color.BLACK)){
  432. health--;
  433. }
  434. b[centrex+1][centrey].setBackground(Color.BLACK);
  435. b[centrex-1][centrey].setBackground(Color.BLACK);
  436. b[centrex][centrey-1].setBackground(Color.BLACK);
  437. b[centrex][centrey-2].setBackground(Color.BLACK);
  438. b[centrex+1][centrey-1].setBackground(Color.BLACK);
  439. b[centrex-1][centrey-1].setBackground(Color.BLACK);
  440. centrey--;
  441. b[centrex+1][centrey].setBackground(Color.WHITE);
  442. b[centrex-1][centrey].setBackground(Color.WHITE);
  443. b[centrex][centrey-1].setBackground(Color.WHITE);
  444. b[centrex][centrey-2].setBackground(Color.WHITE);
  445. b[centrex+1][centrey-1].setBackground(Color.WHITE);
  446. b[centrex-1][centrey-1].setBackground(Color.WHITE);
  447. }
  448.  
  449. public void down(){
  450. if ((b[centrex][centrey].getBackground() != Color.BLACK) ||
  451. (b[centrex-1][centrey+1].getBackground() != Color.BLACK) ||
  452. (b[centrex+1][centrey+1].getBackground() != Color.BLACK)){
  453. health--;
  454. }
  455. b[centrex+1][centrey].setBackground(Color.BLACK);
  456. b[centrex-1][centrey].setBackground(Color.BLACK);
  457. b[centrex][centrey-1].setBackground(Color.BLACK);
  458. b[centrex][centrey-2].setBackground(Color.BLACK);
  459. b[centrex+1][centrey-1].setBackground(Color.BLACK);
  460. b[centrex-1][centrey-1].setBackground(Color.BLACK);
  461. centrey++;
  462. b[centrex+1][centrey].setBackground(Color.WHITE);
  463. b[centrex-1][centrey].setBackground(Color.WHITE);
  464. b[centrex][centrey-1].setBackground(Color.WHITE);
  465. b[centrex][centrey-2].setBackground(Color.WHITE);
  466. b[centrex+1][centrey-1].setBackground(Color.WHITE);
  467. b[centrex-1][centrey-1].setBackground(Color.WHITE);
  468. }
  469.  
  470. public boolean checkmove(int x, int y, int dx, int dy){
  471. if (((x-1+dx) > 0) && ((x+1+dx) < 15) && ((y-2+dy) > -1) && ((y+dy) < 24)){
  472. return true;
  473. } else {
  474. return false;
  475. }
  476. }
  477.  
  478. @Override
  479. public void keyPressed(KeyEvent e) {
  480. if (e.getKeyCode() == KeyEvent.VK_ENTER){
  481. if (energy > 25){
  482. fire2 = true;
  483. }
  484. } else if (e.getKeyCode() == KeyEvent.VK_UP){
  485. if (checkmove(centrex, centrey, 0,-1) == true){
  486. up = true;
  487. }
  488. } else if (e.getKeyCode() ==
  489.  
  490. KeyEvent.VK_RIGHT){
  491. if (checkmove(centrex, centrey, 1,0) == true){
  492. right = true;
  493. }
  494. } else if (e.getKeyCode() ==
  495.  
  496. KeyEvent.VK_LEFT){
  497. if (checkmove(centrex, centrey, -1,0) == true){
  498. left = true;
  499. }
  500. } else if (e.getKeyCode() ==
  501.  
  502. KeyEvent.VK_DOWN){
  503. if (checkmove(centrex, centrey, 0,1) == true){
  504. down = true;
  505. }
  506. }
  507.  
  508. }
  509.  
  510. @Override
  511. public void keyReleased(KeyEvent e) {
  512. if (e.getKeyCode() == KeyEvent.VK_SPACE){
  513. fire = true;
  514. }
  515. }
  516.  
  517. @Override
  518. public void keyTyped(KeyEvent e) {
  519.  
  520. }
  521.  
  522. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement