Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.10 KB | None | 0 0
  1. package zavrsni;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.applet.*;
  7. import java.util.Random;
  8. import java.net.URL;
  9. import java.net.MalformedURLException;
  10.  
  11.  
  12.  
  13. /**
  14. * a helper class
  15. */
  16. class Square {
  17. int x, y, c;
  18.  
  19. Square(int x, int y, int c) {
  20. this.x = x;
  21. this.y = y;
  22. this.c = c;
  23. }
  24.  
  25. int X() {
  26. return x;
  27. }
  28.  
  29. int Y() {
  30. return y;
  31. }
  32.  
  33. int Color() {
  34. return c;
  35. }
  36.  
  37. boolean InBounds() {
  38. return (x >= 0 && x < tetris.cols &&
  39. y >= 0 && y < tetris.rows+4);
  40. }
  41.  
  42. boolean IsEqual(Square s) {
  43. return x == s.x && y == s.y && c == s.c;
  44. }
  45. }
  46.  
  47. /**
  48. * Godmar Back
  49. * University of Utah, Computer Systems Lab, 1995
  50. *
  51. * my favorite tetris in Java
  52. */
  53. public class tetris extends Applet implements Runnable, ActionListener{
  54.  
  55. static int sqlength; // length of tetris square
  56. static final int xoffset = 200; // distance from left
  57. static int cols; // columns
  58. static int rows; // rows
  59. int f[][]; // field
  60. int of[][]; // old field
  61. int what; // type of current piece
  62. Square curpiece[] = new Square[4]; // current piece
  63. boolean lost; // lost
  64. boolean neednewpiece; // need new piece
  65. Thread killme = null; // thread that does gravity
  66. Color colors[];
  67. int curval, score=0;
  68. int level; // current level of difficulty
  69. int pieces; // nr of pieces dropped
  70. String highscorehost;
  71. short highscoreport;
  72. boolean blackwhite; // BW mode
  73. boolean suspended = false; // the suggested off-switch
  74. boolean justupdating = false;
  75. final Button start_newgame_butt = new TetrisButton("Start");
  76. final Button pause_resume_butt = new TetrisButton("Pause");
  77. private GridCanvas game_grid = new GridCanvas(grid, true);
  78.  
  79.  
  80.  
  81. private class TetrisButton extends Button {
  82. public TetrisButton(String label) {
  83. super(label);
  84. }
  85. public Dimension getPreferredSize() {
  86. return new Dimension(120, super.getPreferredSize().height);
  87. }
  88. }
  89.  
  90.  
  91. // move a bunch of squares
  92. private boolean movepiece(Square from[], Square to[])
  93. {
  94. outerlabel:
  95. for (int i=0; i<to.length; i++) {
  96. if (!to[i].InBounds()) {
  97. return false;
  98. }
  99. if (f[to[i].X()][to[i].Y()] != 0) { // it's taken
  100. for (int j=0; j<from.length; j++)
  101. if(to[i].IsEqual(from[j])) {
  102. continue outerlabel; // but by a from
  103. }
  104. return false; // no can do
  105. }
  106. }
  107.  
  108. // blank old piece
  109. for (int i=0; i<from.length; i++)
  110. if(from[i].InBounds()) {
  111. f[from[i].X()][from[i].Y()] = 0;
  112. }
  113.  
  114. // mark new piece
  115. for (int i=0; i<to.length; i++)
  116. f[to[i].X()][to[i].Y()] = to[i].Color();
  117. return true;
  118. }
  119.  
  120. // throw new piece in the game
  121. private void newpiece()
  122. {
  123. Square old[] = new Square[4];
  124. old[0] = old[1] = old[2] = old[3] = new Square(-1, -1, 0);
  125.  
  126. int m = cols/2;
  127. what = (int) (Math.random()*7);
  128.  
  129. switch(what) {
  130. case 0:
  131. // XXXX red
  132. curval = 100;
  133. curpiece[0] = new Square(m-1, rows-1, 1);
  134. curpiece[1] = new Square(m-2, rows-1, 1);
  135. curpiece[2] = new Square(m, rows-1, 1);
  136. curpiece[3] = new Square(m+1, rows-1, 1);
  137. break;
  138. case 1:
  139. // X orange
  140. // XXX
  141. curval = 120;
  142. curpiece[0] = new Square(m , rows-2, 5);
  143. curpiece[1] = new Square(m , rows-1, 5);
  144. curpiece[2] = new Square(m-1, rows-2, 5);
  145. curpiece[3] = new Square(m+1, rows-2, 5);
  146. break;
  147. case 2:
  148. // XX ???
  149. // XX
  150. curval = 180;
  151. curpiece[0] = new Square(m , rows-2, 2);
  152. curpiece[1] = new Square(m-1, rows-1, 2);
  153. curpiece[2] = new Square(m , rows-1, 2);
  154. curpiece[3] = new Square(m+1, rows-2, 2);
  155. break;
  156. case 3:
  157. // XX ???
  158. // XX
  159. curval = 180;
  160. curpiece[0] = new Square(m , rows-2, 7);
  161. curpiece[1] = new Square(m+1, rows-1, 7);
  162. curpiece[2] = new Square(m , rows-1, 7);
  163. curpiece[3] = new Square(m-1, rows-2, 7);
  164. break;
  165. case 4:
  166. // XX light blue
  167. // XX
  168. curval = 100;
  169. curpiece[0] = new Square(m-1, rows-1, 3);
  170. curpiece[1] = new Square(m , rows-1, 3);
  171. curpiece[2] = new Square(m-1, rows-2, 3);
  172. curpiece[3] = new Square(m , rows-2, 3);
  173. break;
  174. case 5:
  175. // XXX
  176. // X
  177. curval = 120;
  178. curpiece[0] = new Square(m , rows-1, 6);
  179. curpiece[1] = new Square(m-1, rows-1, 6);
  180. curpiece[2] = new Square(m+1, rows-1, 6);
  181. curpiece[3] = new Square(m+1, rows-2, 6);
  182. break;
  183. case 6:
  184. // XXX yellow
  185. // X
  186. curval = 120;
  187. curpiece[0] = new Square(m , rows-1, 4);
  188. curpiece[1] = new Square(m+1, rows-1, 4);
  189. curpiece[2] = new Square(m-1, rows-1, 4);
  190. curpiece[3] = new Square(m-1, rows-2, 4);
  191. break;
  192. }
  193. lost = !movepiece(old, curpiece);
  194. }
  195.  
  196. // move current piece
  197. private synchronized boolean movecurpiece(int byx, int byy, boolean rotate)
  198. {
  199. Square newpos[] = new Square[4];
  200.  
  201. for(int i=0; i<4; i++) {
  202. if(rotate) {
  203. int dx = curpiece[i].X() - curpiece[0].X();
  204. int dy = curpiece[i].Y() - curpiece[0].Y();
  205. // 90 degree rotation: [ 0 -1]
  206. // [ 1 0]
  207. newpos[i] = new Square( curpiece[0].X() - dy,
  208. curpiece[0].Y() + dx,
  209. curpiece[i].Color());
  210. } else
  211. newpos[i] = new Square( curpiece[i].X() + byx,
  212. curpiece[i].Y() + byy,
  213. curpiece[i].Color());
  214. }
  215. if (!movepiece(curpiece, newpos))
  216. return false;
  217. curpiece = newpos;
  218. return true;
  219. }
  220.  
  221. // remove full lines
  222. private void removelines()
  223. {
  224. outerlabel:
  225. for (int j=0; j<rows; j++) {
  226. // check jth line
  227. for (int i=0; i<cols; i++)
  228. if (f[i][j] == 0)
  229. continue outerlabel;
  230.  
  231. // remove jth line
  232. for (int k=j; k<rows-1; k++)
  233. for (int i=0; i<cols; i++)
  234. f[i][k] = f[i][k+1];
  235. // recheck jth line
  236. j--;
  237. }
  238. }
  239.  
  240. private static class TetrisLabel extends Label {
  241. private final static Font LABEL_FONT = new Font("Serif", Font.BOLD, 18);
  242. private TetrisLabel(String text) {
  243. super(text);
  244. setFont(LABEL_FONT);
  245. }
  246. private void addValue(int val) {
  247. setText(Integer.toString((Integer.parseInt(getText())) + val ));
  248. }
  249. }
  250.  
  251. // applet is being loaded the first time
  252. public void init()
  253. { // applet is loaded the first time
  254.  
  255. pause_resume_butt.setEnabled(false);
  256. start_newgame_butt.addActionListener(new ActionListener() {
  257. public void actionPerformed(ActionEvent ae) {
  258. if(start_newgame_butt.getLabel().equals("Start"))
  259. startGame();
  260. else
  261. start();
  262. }
  263. });
  264. pause_resume_butt.addActionListener(new ActionListener() {
  265. public void actionPerformed(ActionEvent ae) {
  266. if(pause_resume_butt.getLabel().equals("Pause"))
  267. pauseGame();
  268. else
  269. resumeGame();
  270. }
  271. });
  272.  
  273.  
  274. String s = getParameter("squaresize");
  275. sqlength = s == null ? 20 : (new Integer(s)).intValue();
  276.  
  277. s = getParameter("columns");
  278. cols = s == null ? 10 : (new Integer(s)).intValue();
  279.  
  280. s = getParameter("rows");
  281. rows = s == null ? 20 : (new Integer(s)).intValue();
  282.  
  283. s = getParameter("blackwhite");
  284. blackwhite = s == null ? false : (new Boolean(s)).booleanValue();
  285.  
  286. highscorehost = getParameter("highscorehost");
  287.  
  288. s = getParameter("highscoreport");
  289. highscoreport = (short)(s == null ? 4711 : (new Integer(s)).intValue());
  290.  
  291. f = new int[cols][rows+4];
  292. of = new int[cols][rows+4];
  293.  
  294. resize(sqlength*cols+xoffset*2, sqlength*(rows+3));
  295. colors = new Color[8];
  296. colors[0] = new Color(40,40,40); // off color
  297.  
  298. if (blackwhite) {
  299. for(int i=1; i<8; i++)
  300. colors[i] = new Color(255, 255, 255);
  301. } else {
  302. colors[1] = new Color(255,0,0); // Default red - ok
  303. colors[2] = new Color(0,200,0); // green ?
  304. colors[3] = new Color(0,200,255); // light blue - ok
  305. colors[4] = new Color(255,255,0); // yellow - ok
  306. colors[5] = new Color(255,150,0); // orange - ok
  307. colors[6] = new Color(210,0,240); // purple ?
  308. colors[7] = new Color(40,0,240); // dark blue ?
  309.  
  310.  
  311.  
  312.  
  313. // add the key listener to all components that might get focus
  314. // so that it'll work regardless of which has focus
  315. start_newgame_butt.addActionListener(this);
  316. pause_resume_butt.addActionListener(this);
  317.  
  318. Panel right_panel = new Panel(new GridLayout(3, 1));
  319. right_panel.setBackground(Color.white);
  320.  
  321. Panel control_panel = new Panel();
  322. control_panel.add(start_newgame_butt);
  323. control_panel.add(pause_resume_butt);
  324. control_panel.setBackground(Color.white);
  325. right_panel.add(control_panel);
  326.  
  327. Panel tmp = new Panel(new BorderLayout());
  328. tmp.setBackground(Color.white);
  329. right_panel.add(tmp);
  330.  
  331. Panel stats_panel = new Panel(new GridLayout(4, 2));
  332. stats_panel.add(new TetrisLabel(" Level: " + level));
  333. stats_panel.add(new TetrisLabel(" Score: " + score));
  334. tmp = new Panel(new BorderLayout());
  335. tmp.setBackground(Color.white);
  336. tmp.add("Center", stats_panel);
  337. right_panel.add(tmp);
  338.  
  339. // finaly, add all the main panels to the applet panel
  340. this.setLayout(new GridLayout(1, 2));
  341. this.add(game_grid);
  342. this.add(right_panel);
  343. this.setBackground(Color.black);
  344. this.validate();
  345. }
  346. }
  347.  
  348.  
  349. public void actionPerformed(ActionEvent e)
  350. {
  351. String commandStr = e.getActionCommand();
  352. if (commandStr.equals("Start Game"))
  353. {
  354. start_newgame_butt.setLabel("End Game");
  355. startGame();
  356. }
  357. else if (commandStr.equals("End Game"))
  358. {
  359. start_newgame_butt.setLabel("Start Game");
  360. stop();
  361. }
  362. else if (commandStr.equals("Pause Game"))
  363. {
  364. pause_resume_butt.setLabel("Pause Game");
  365. stop();
  366. }
  367. }
  368.  
  369. // applet is started
  370. public void start()
  371. {
  372. for(int i=0; i<cols; i++)
  373. for(int j=0; j<rows+4; j++) {
  374. f[i][j] = 0;
  375. of[i][j] = -1;
  376. }
  377. level = 0;
  378. score = 0;
  379. pieces= 0;
  380. curval = -1;
  381. neednewpiece = true;
  382. lost = false;
  383.  
  384. // spawn gravity thread
  385. (killme = new Thread(this)).start();
  386. requestFocus();
  387. }
  388.  
  389. // applet is stopped
  390. public synchronized void stop()
  391. {
  392. if (killme != null)
  393. killme.stop();
  394. killme = null;
  395. }
  396.  
  397. private void startGame() {
  398. start_newgame_butt.setLabel("Start New Game");
  399. pause_resume_butt.setEnabled(true); // stays enabled from here on
  400. pause_resume_butt.setLabel("Pause");
  401. pause_resume_butt.validate();
  402. }
  403.  
  404. private void pauseGame() {
  405. pause_resume_butt.setLabel("Resume");
  406. }
  407.  
  408. private void resumeGame() {
  409. pause_resume_butt.setLabel("Pause");
  410. }
  411.  
  412. // main loop for launched thread
  413. public void run()
  414. {
  415. while (!lost) {
  416. try {
  417. int t =
  418. level == 0 ? 900 :
  419. level == 1 ? 800 :
  420. level == 2 ? 700 :
  421. level == 3 ? 600 :
  422. level == 4 ? 500 :
  423. level == 5 ? 400 :
  424. level == 6 ? 300 :
  425. level == 7 ? 250 :
  426. level == 8 ? 200 :
  427. level == 9 ? 150 :
  428. level == 10 ? 100 : 75;
  429. ;
  430. if(t == 900){
  431. int m = 1;
  432. }
  433. Thread.sleep(t);
  434. } catch (InterruptedException e){}
  435. if (neednewpiece) {
  436. if(curval > 0) {
  437. score += curval;
  438. pieces++;
  439. level = 0 + pieces/30;
  440. }
  441. removelines();
  442. newpiece();
  443. neednewpiece = false;
  444. } else {
  445. neednewpiece = !movecurpiece(0, -1, false);
  446. if(!neednewpiece)
  447. curval -= 5;
  448. }
  449. repaint();
  450. }
  451. killme = null;
  452. if (highscorehost == null)
  453. return;
  454.  
  455. System.out.println("switching to highscore...");
  456. try {
  457. /* send mail to gback@cs.utah.edu if you want to get
  458. * the highscore class and the server
  459. HighScore hs = new HighScore(this, highscorehost,
  460. highscoreport, score);
  461. */
  462. } catch(Exception e) {
  463. getAppletContext().showStatus("highscore failed: "+e.getMessage());
  464. }
  465. // this terminates second thread
  466. }
  467.  
  468. // key is pressed
  469. public boolean keyDown(java.awt.Event evt, int key)
  470. {
  471. if (key != 's' && lost || suspended)
  472. return true;
  473.  
  474. switch(key) {
  475. case 's':
  476. stop(); // stop old game
  477. start(); // start new game
  478. break;
  479.  
  480. case 'h': // left
  481. case java.awt.Event.LEFT:
  482. curval -= 5;
  483. movecurpiece(-1, 0, false);
  484. neednewpiece = false;
  485. repaint();
  486. break;
  487.  
  488. case 'k': // right
  489. case 'l':
  490. case java.awt.Event.RIGHT:
  491. curval -= 5;
  492. movecurpiece(1, 0, false);
  493. neednewpiece = false;
  494. repaint();
  495. break;
  496.  
  497. case 'j': // rotate
  498. case java.awt.Event.UP:
  499. if(what!=4) {
  500. curval -= 5;
  501. movecurpiece(0, 0, true);
  502. repaint();
  503. }
  504. break;
  505.  
  506. case ' ': // drop
  507. case java.awt.Event.DOWN:
  508. while(movecurpiece(0, -1, false))
  509. ;
  510. repaint();
  511. break;
  512. }
  513. return true;
  514. }
  515.  
  516. // we need to override this method, otherwise repaint()
  517. // will erase the whole screen, causing tremendous flickering
  518. public void update(Graphics g)
  519. {
  520. justupdating = true;
  521. // tell paint to draw only stuff that has changed
  522. paint(g);
  523. }
  524.  
  525. // paint graphics on screen
  526. public synchronized void paint(Graphics g)
  527. {
  528. g.setFont(new java.awt.Font("Helvetica", 0, 18));
  529. int gx = sqlength;
  530. int gy = sqlength*rows/4;
  531.  
  532. g.clearRect(gx, gy-25, xoffset-gx, 25);
  533. g.drawString("Score: " + score, gx, gy);
  534.  
  535. gy += 30;
  536. g.clearRect(gx, gy-25, xoffset-gx, 25);
  537. g.drawString("Level: " + level, gx, gy);
  538.  
  539. for (int i=0; i<cols; i++)
  540. for (int j=0; j<rows; j++) {
  541. if (!justupdating ||
  542. of[i][rows-1-j] == -1 ||
  543. of[i][rows-1-j] != f[i][rows-1-j]) {
  544. g.setColor(colors[f[i][rows-1-j]]);
  545. g.fill3DRect(
  546. xoffset+sqlength*i,
  547. sqlength+sqlength*j,
  548. sqlength, sqlength, true);
  549. }
  550. of[i][rows-1-j] = f[i][rows-1-j];
  551. }
  552. justupdating = false;
  553. }
  554.  
  555. public boolean mouseDown(java.awt.Event evt, int x, int y)
  556. {
  557. if (killme == null)
  558. return true;
  559.  
  560. if (suspended)
  561. killme.resume();
  562. else
  563. killme.suspend();
  564. suspended = !suspended;
  565. return true;
  566. }
  567. } // end Tetris applet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement