Advertisement
Meowmeowcat

Snake

Mar 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.74 KB | None | 0 0
  1. package snake;
  2.  
  3.  
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.awt.geom.*;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9.  
  10. public class Snake extends JFrame implements Runnable {
  11.  
  12. static final int numRows = 31;
  13. static final int numColumns = 61;
  14. static final int XBORDER = 20;
  15. static final int YBORDER = 20;
  16. static final int YTITLE = 30;
  17. static final int WINDOW_BORDER = 8;
  18. static final int WINDOW_WIDTH = 2*(WINDOW_BORDER + XBORDER) + numColumns*30;
  19. static final int WINDOW_HEIGHT = YTITLE + WINDOW_BORDER + 2 * YBORDER + numRows*30;
  20.  
  21. boolean animateFirstTime = true;
  22. int xsize = -1;
  23. int ysize = -1;
  24. Image image;
  25. Graphics2D g;
  26.  
  27. int time;
  28. boolean game;
  29. boolean snakeSpeed;
  30. boolean twoPlayer;
  31. double fps = 60.0;
  32. double sps;
  33. double dps = 1;
  34.  
  35. int score;
  36. int highscore;
  37.  
  38. int R;
  39. int G;
  40. int B;
  41.  
  42. final int EMPTY = 0;
  43. final int SNAKE = 1;
  44. final int DEATH = 2;
  45. final int SNAKE2 = 3;
  46. int board[][];
  47.  
  48. int currentRow;
  49. int currentColumn;
  50. int rowDir;
  51. int columnDir;
  52.  
  53. int currentRow2;
  54. int currentColumn2;
  55. int rowDir2;
  56. int columnDir2;
  57.  
  58. boolean gameOver;
  59.  
  60. static Snake frame;
  61. public static void main(String[] args) {
  62. frame = new Snake();
  63. frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  64. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65. frame.setVisible(true);
  66. }
  67.  
  68. public Snake() {
  69. addMouseListener(new MouseAdapter() {
  70. public void mousePressed(MouseEvent e) {
  71. if (e.BUTTON1 == e.getButton()) {
  72. //left button
  73.  
  74. // location of the cursor.
  75. int xpos = e.getX();
  76. int ypos = e.getY();
  77.  
  78. }
  79. if (e.BUTTON3 == e.getButton()) {
  80. //right button
  81. reset();
  82. }
  83. repaint();
  84. }
  85. });
  86.  
  87. addMouseMotionListener(new MouseMotionAdapter() {
  88. public void mouseDragged(MouseEvent e) {
  89. repaint();
  90. }
  91. });
  92.  
  93. addMouseMotionListener(new MouseMotionAdapter() {
  94. public void mouseMoved(MouseEvent e) {
  95.  
  96. repaint();
  97. }
  98. });
  99.  
  100. addKeyListener(new KeyAdapter() {
  101.  
  102. public void keyPressed(KeyEvent e) {
  103. if (e.VK_W == e.getKeyCode()) {
  104. game = true;
  105. rowDir = -1;
  106. columnDir = 0;
  107. } else if (e.VK_S == e.getKeyCode()) {
  108. game = true;
  109. rowDir = 1;
  110. columnDir = 0;
  111. } else if (e.VK_A == e.getKeyCode()) {
  112. game = true;
  113. columnDir = -1;
  114. rowDir = 0;
  115. } else if (e.VK_D == e.getKeyCode()) {
  116. game = true;
  117. columnDir = 1;
  118. rowDir = 0;
  119. }
  120. else if (e.VK_UP == e.getKeyCode()) {
  121. game = true;
  122. rowDir2 = -1;
  123. columnDir2 = 0;
  124. } else if (e.VK_DOWN == e.getKeyCode()) {
  125. game = true;
  126. columnDir2 = 0;
  127. rowDir2 = 1;
  128. } else if (e.VK_RIGHT == e.getKeyCode()) {
  129. game = true;
  130. columnDir2 = 1;
  131. rowDir2 = 0;
  132. }
  133. else if (e.VK_LEFT == e.getKeyCode()) {
  134. game = true;
  135. columnDir2 = -1;
  136. rowDir2 = 0;
  137. }
  138. repaint();
  139. }
  140. });
  141. init();
  142. start();
  143. }
  144. Thread relaxer;
  145. ////////////////////////////////////////////////////////////////////////////
  146. public void init() {
  147. requestFocus();
  148. }
  149. ////////////////////////////////////////////////////////////////////////////
  150. public void destroy() {
  151. }
  152. ////////////////////////////////////////////////////////////////////////////
  153. public void paint(Graphics gOld) {
  154. if (image == null || xsize != getSize().width || ysize != getSize().height) {
  155. xsize = getSize().width;
  156. ysize = getSize().height;
  157. image = createImage(xsize, ysize);
  158. g = (Graphics2D) image.getGraphics();
  159. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  160. RenderingHints.VALUE_ANTIALIAS_ON);
  161. }
  162. //fill background
  163. g.setColor(Color.gray);
  164. g.fillRect(0, 0, xsize, ysize);
  165.  
  166. int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
  167. int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
  168. //fill border
  169. g.setColor(Color.black);
  170. g.fillPolygon(x, y, 4);
  171. // draw border
  172. g.setColor(Color.black);
  173. g.drawPolyline(x, y, 5);
  174.  
  175. if (animateFirstTime) {
  176. gOld.drawImage(image, 0, 0, null);
  177. return;
  178. }
  179.  
  180. g.setColor(Color.black);
  181. //horizontal lines
  182. for (int zi=1;zi<numRows;zi++)
  183. {
  184. g.drawLine(getX(0) ,getY(0)+zi*getHeight2()/numRows ,
  185. getX(getWidth2()) ,getY(0)+zi*getHeight2()/numRows );
  186. }
  187. //vertical lines
  188. for (int zi=1;zi<numColumns;zi++)
  189. {
  190. g.drawLine(getX(0)+zi*getWidth2()/numColumns ,getY(0) ,
  191. getX(0)+zi*getWidth2()/numColumns,getY(getHeight2()) );
  192. }
  193.  
  194. //Display the objects of the board
  195.  
  196. Color RGB = new Color(R,G,B);
  197.  
  198. for (int zrow=0;zrow<numRows;zrow++)
  199. {
  200. for (int zcolumn=0;zcolumn<numColumns;zcolumn++)
  201. {
  202. if (board[zrow][zcolumn] == SNAKE)
  203. {
  204. g.setColor(Color.green);
  205. g.fillRect(getX(0)+zcolumn*getWidth2()/numColumns,
  206. getY(0)+zrow*getHeight2()/numRows,
  207. getWidth2()/numColumns,
  208. getHeight2()/numRows);
  209. }
  210. else if (board[zrow][zcolumn] == DEATH)
  211. {
  212. g.setColor(Color.red);
  213. g.fillRect(getX(0)+zcolumn*getWidth2()/numColumns,
  214. getY(0)+zrow*getHeight2()/numRows,
  215. getWidth2()/numColumns,
  216. getHeight2()/numRows);
  217. }
  218. else if (board[zrow][zcolumn] == SNAKE2)
  219. {
  220. g.setColor(Color.yellow);
  221. g.fillRect(getX(0)+zcolumn*getWidth2()/numColumns,
  222. getY(0)+zrow*getHeight2()/numRows,
  223. getWidth2()/numColumns,
  224. getHeight2()/numRows);
  225. }
  226. }
  227. }
  228. if(gameOver){
  229. g.setFont(new Font("Arial Black",Font.PLAIN,92));
  230. g.setColor(Color.green);
  231. g.drawString("GAME OVER",getX(getWidth2()/3-10),getYNormal(getHeight2()/2));
  232. }
  233.  
  234. g.setFont(new Font("Arial",Font.PLAIN,12));
  235. g.setColor(Color.black);
  236. g.drawString("Score: " + score,getX(0),getYNormal(-15));
  237. g.drawString("Highscore: " + highscore,getX(100),getYNormal(-15));
  238.  
  239. gOld.drawImage(image, 0, 0, null);
  240. }
  241. ////////////////////////////////////////////////////////////////////////////
  242. // needed for implement runnable
  243. public void run() {
  244. while (true) {
  245. animate();
  246. repaint();
  247. double seconds = 1/fps; //time that 1 frame takes.
  248. int miliseconds = (int) (1000.0 * seconds);
  249. try {
  250. Thread.sleep(miliseconds);
  251. } catch (InterruptedException e) {
  252. }
  253. }
  254. }
  255. /////////////////////////////////////////////////////////////////////////
  256. public void reset() {
  257. //Allocate memory for the 2D array that represents the board.
  258. board = new int[numRows][numColumns];
  259. //Initialize the board to be empty.
  260. for (int zrow = 0;zrow < numRows;zrow++)
  261. {
  262. for (int zcolumn = 0;zcolumn < numColumns;zcolumn++)
  263. board[zrow][zcolumn] = EMPTY;
  264. }
  265.  
  266. time = 0;
  267. game = false;
  268. score = 0;
  269. snakeSpeed = false;
  270. twoPlayer = false;
  271.  
  272. R=0;
  273. G=0;
  274. B=0;
  275.  
  276. if(twoPlayer){
  277. currentRow = numRows/2;
  278. currentColumn = numColumns/2-1;
  279. }
  280. else
  281. currentRow = numRows/2;
  282. currentColumn = numColumns/2;
  283.  
  284. currentRow2 = numRows/2;
  285. currentColumn2 = numColumns/2+1;
  286. board[currentRow][currentColumn] = SNAKE;
  287.  
  288. if(twoPlayer)
  289. board[currentRow2][currentColumn2] = SNAKE2;
  290.  
  291. rowDir = -1;
  292. columnDir = 0;
  293. rowDir2 = -1;
  294. columnDir2 = 0;
  295.  
  296. if(snakeSpeed)
  297. sps = 5;
  298. else
  299. sps = 10;
  300.  
  301. gameOver = false;
  302. }
  303. /////////////////////////////////////////////////////////////////////////
  304. public void animate() {
  305. if (animateFirstTime) {
  306. animateFirstTime = false;
  307. if (xsize != getSize().width || ysize != getSize().height) {
  308. xsize = getSize().width;
  309. ysize = getSize().height;
  310. }
  311. reset();
  312. }
  313.  
  314. if(gameOver)
  315. return;
  316.  
  317. if(game && time % (int)(fps/sps) == (int)(fps/sps)-1){
  318. currentColumn += columnDir;
  319. currentRow += rowDir;
  320.  
  321. if(twoPlayer){
  322. currentColumn2 += columnDir2;
  323. currentRow2 += rowDir2;
  324. }
  325.  
  326. if(currentColumn == numColumns)
  327. currentColumn = 0;
  328. else if(currentColumn == -1)
  329. currentColumn = numColumns-1;
  330. else if(currentRow == numRows)
  331. currentRow = 0;
  332. else if(currentRow == -1)
  333. currentRow = numRows-1;
  334.  
  335. if(twoPlayer){
  336. if(currentColumn2 == numColumns)
  337. currentColumn2 = 0;
  338. else if(currentColumn2 == -1)
  339. currentColumn2 = numColumns-1;
  340. else if(currentRow2 == numRows)
  341. currentRow2 = 0;
  342. else if(currentRow2 == -1)
  343. currentRow2 = numRows-1;
  344. }
  345.  
  346. if(board[currentRow][currentColumn] == SNAKE){
  347. gameOver = true;
  348. if(score > highscore)
  349. highscore = score;
  350. }
  351. else if(board[currentRow][currentColumn] == DEATH){
  352. gameOver = true;
  353. if(score > highscore)
  354. highscore = score;
  355. }
  356. else if(board[currentRow][currentColumn] == SNAKE2){
  357. gameOver = true;
  358. if(score > highscore)
  359. highscore = score;
  360. }
  361. else{
  362. board[currentRow][currentColumn] = SNAKE;
  363. score++;
  364. }
  365.  
  366. if(twoPlayer){
  367. if(board[currentRow2][currentColumn2] == SNAKE){
  368. gameOver = true;
  369. if(score > highscore)
  370. highscore = score;
  371. }
  372. else if(board[currentRow2][currentColumn2] == DEATH){
  373. gameOver = true;
  374. if(score > highscore)
  375. highscore = score;
  376. }
  377. else if(board[currentRow2][currentColumn2] == SNAKE2){
  378. gameOver = true;
  379. if(score > highscore)
  380. highscore = score;
  381. }
  382. else{
  383. board[currentRow2][currentColumn2] = SNAKE2;
  384. score++;
  385. }
  386. }
  387. }
  388.  
  389. if(game && time % (int)(fps/dps) == (int)(fps/dps)-1)
  390. {
  391. death();
  392. }
  393.  
  394. if(snakeSpeed && time % 600 == 599 && sps<12)
  395. sps+=1;
  396.  
  397. time++;
  398. }
  399. ////////////////////////////////////////////////////////////////////////////
  400. public void death(){
  401. if(gameOver)
  402. return;
  403. int randomRow = (int)(Math.random()*numRows);
  404. int randomColumn = (int)(Math.random()*numColumns);
  405. if(board[randomRow][randomColumn] == EMPTY)
  406. board[randomRow][randomColumn] = DEATH;
  407. else
  408. death();
  409. }
  410. ////////////////////////////////////////////////////////////////////////////
  411. public void start() {
  412. if (relaxer == null) {
  413. relaxer = new Thread(this);
  414. relaxer.start();
  415. }
  416. }
  417. ////////////////////////////////////////////////////////////////////////////
  418. public void stop() {
  419. if (relaxer.isAlive()) {
  420. relaxer.stop();
  421. }
  422. relaxer = null;
  423. }
  424.  
  425.  
  426. /////////////////////////////////////////////////////////////////////////
  427. public int getX(int x) {
  428. return (x + XBORDER + WINDOW_BORDER);
  429. }
  430.  
  431. public int getY(int y) {
  432. return (y + YBORDER + YTITLE );
  433. }
  434.  
  435. public int getYNormal(int y) {
  436. return (-y + YBORDER + YTITLE + getHeight2());
  437. }
  438.  
  439. public int getWidth2() {
  440. return (xsize - 2 * (XBORDER + WINDOW_BORDER));
  441. }
  442.  
  443. public int getHeight2() {
  444. return (ysize - 2 * YBORDER - WINDOW_BORDER - YTITLE);
  445. }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement