Advertisement
Guest User

Snake

a guest
Dec 29th, 2012
1,927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.95 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. //we need the applet methods and the MouseMotionListener interface
  7. //(used for the human controlled paddle)
  8.  
  9. public class snakeMain extends Applet implements KeyListener,
  10. ActionListener
  11. {
  12. // a font used to display the score
  13. Font newFont = new Font("sansserif", Font.BOLD, 20);
  14. //The image is going to be double buffered to avoid flicker
  15. Graphics bufferGraphics;
  16. //the Image will contain everything drawn on bufferGraphics
  17. Image offscreen;
  18. //variables used to set the width and height of the applet
  19. final int WIDTH = 800, HEIGHT = 800, BORDER = 10, GRIDSIZE = 50, CELLSIZE = 10, INITIALLENGTH = 3, INITIALSNAKEX = 25, INITIALSNAKEY = 25, INITIALSPEED = 50;
  20.  
  21. private boolean gameOver = false;
  22. private boolean keyPressedThisTick = false;
  23. private boolean wrapAround = true;
  24.  
  25. //snake variables
  26. private int snakeLength = INITIALLENGTH;
  27. private int snakeX = INITIALSNAKEX, snakeY = INITIALSNAKEY;
  28. private int snakeUpDown = 0, snakeLeftRight = -1;
  29. private int frameCounter = 0;
  30. private int speed = INITIALSPEED;
  31. private int score = 0;
  32.  
  33. //cherry variables
  34. private int cherryX, cherryY;
  35.  
  36. //blue cherry variables
  37. private int blueCherryX, blueCherryY;
  38. private int blueCherryCountdown = 0;
  39. private int blueCherryCooldown = 500;
  40. private boolean blueCherryOn = false;
  41.  
  42. //declare the array
  43. int[][] grid = new int[GRIDSIZE][GRIDSIZE];
  44.  
  45. //I am going to use a timer to do a certain list of tasks
  46. //every 15 milliseconds (67 FPS)
  47. Timer time = new Timer(15, this);
  48.  
  49. public void init()
  50. {
  51. //set the applet to be 500*300
  52. setSize(WIDTH, HEIGHT);
  53.  
  54. gameOver = false;
  55. grid[snakeY][snakeX] = snakeLength;
  56. newCherry();
  57.  
  58. //mouseMotionListener allows the player to control their paddle
  59. addKeyListener(this);
  60. setBackground(Color.black);
  61.  
  62. //create offscreen image to draw on
  63. offscreen = createImage(WIDTH, HEIGHT);
  64. bufferGraphics = offscreen.getGraphics();
  65.  
  66. //this is the game loop
  67. time.start();
  68.  
  69. }
  70.  
  71. public void reset()
  72. {
  73. snakeLength = INITIALLENGTH;
  74. snakeX = INITIALSNAKEX;
  75. snakeY = INITIALSNAKEY;
  76. snakeUpDown = 0;
  77. snakeLeftRight = -1;
  78. frameCounter = 0;
  79. speed = INITIALSPEED;
  80. score = 0;
  81. gameOver = false;
  82. wrapAround = true;
  83. keyPressedThisTick = false;
  84. blueCherryOn = false;
  85. blueCherryCooldown = 500;
  86. blueCherryCountdown = 0;
  87. newCherry();
  88.  
  89. for (int i = 0; i < GRIDSIZE; i++)
  90. {
  91. for (int j = 0; j < GRIDSIZE; j++)
  92. {
  93. grid[i][j] = 0;
  94. }
  95. }
  96.  
  97. grid[snakeY][snakeX] = snakeLength;
  98.  
  99. time.restart();
  100. }
  101.  
  102. //every 15 milliseconds the timer triggers the actionPerformed method
  103. public void actionPerformed(ActionEvent arg0)
  104. {
  105. if(gameOver == true)
  106. {
  107. //after the game needs to end we stop the timer,
  108. time.stop();
  109. repaint();
  110. }
  111. else
  112. {
  113. frameCounter = frameCounter + 10;
  114.  
  115. if (frameCounter >= speed)
  116. {
  117. checkCollision();
  118.  
  119. snakeX = snakeX + snakeLeftRight;
  120. snakeY = snakeY + snakeUpDown;
  121.  
  122. if (snakeX == 0)
  123. {
  124. if(!wrapAround)
  125. {
  126. gameOver = true;
  127. }
  128. snakeX = (GRIDSIZE - 2);
  129. }
  130.  
  131. else if (snakeX == (GRIDSIZE - 1))
  132. {
  133. if(!wrapAround)
  134. {
  135. gameOver = true;
  136. }
  137. snakeX = 1;
  138. }
  139.  
  140. if (snakeY == 0)
  141. {
  142. if(!wrapAround)
  143. {
  144. gameOver = true;
  145. }
  146. snakeY = (GRIDSIZE - 2);
  147. }
  148.  
  149. else if (snakeY == (GRIDSIZE - 1))
  150. {
  151. if(!wrapAround)
  152. {
  153. gameOver = true;
  154. }
  155. snakeY = 1;
  156. }
  157.  
  158. for (int i = 0; i < GRIDSIZE; i++)
  159. {
  160. for (int j = 0; j < GRIDSIZE; j++)
  161. {
  162. if (grid[i][j] > 0)
  163. {
  164. grid[i][j]--;
  165. }
  166. }
  167. }
  168.  
  169. grid[snakeX][snakeY] = snakeLength;
  170.  
  171. frameCounter = 0;
  172. if(blueCherryCountdown > 0)
  173. {
  174. blueCherryCountdown--;
  175. }
  176. else
  177. {
  178. blueCherryOn = false;
  179. }
  180.  
  181. if(blueCherryCooldown > 0)
  182. {
  183. blueCherryCooldown--;
  184. }
  185.  
  186. if(!blueCherryOn && blueCherryCooldown == 0)
  187. {
  188. newBlueCherry();
  189. }
  190. keyPressedThisTick = false;
  191.  
  192. }
  193. else
  194. {
  195.  
  196. }
  197. //repaints the applet
  198. repaint();
  199. }
  200. }
  201.  
  202. public void checkCollision()
  203. {
  204. //checks if the snakes next position is already occupied
  205. int snakeNextX = snakeX + snakeLeftRight;
  206. int snakeNextY = snakeY + snakeUpDown;
  207. if(grid[snakeNextX][snakeNextY] > 0)
  208. {
  209. gameOver = true;
  210. }
  211.  
  212. if(snakeNextX == cherryX && snakeNextY == cherryY)
  213. {
  214. snakeLength++;
  215. score++;
  216. newCherry();
  217. if(speed > 20)
  218. {
  219. //speed--;
  220. }
  221. else
  222. {
  223.  
  224. }
  225. }
  226.  
  227. if(snakeNextX == blueCherryX && snakeNextY == blueCherryY)
  228. {
  229. score = score + 10;
  230. blueCherryOn = false;
  231. blueCherryCountdown = 0;
  232. }
  233. }
  234.  
  235.  
  236. public void paint(Graphics g)
  237. {
  238. // first clear off the image
  239. bufferGraphics.clearRect(0,0,WIDTH,HEIGHT);
  240.  
  241. //Display the score in white
  242. bufferGraphics.setColor(Color.white);
  243.  
  244. //draws the counter between ticks
  245. bufferGraphics.drawString("Score: " + score, BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 10);
  246. bufferGraphics.drawString("Controls:", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 34);
  247. bufferGraphics.drawString("ArrowKeys - Change Direction", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 46);
  248. bufferGraphics.drawString("F - Faster", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 58);
  249. bufferGraphics.drawString("S - Slower", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 70);
  250. bufferGraphics.drawString("W - Toggle Wrap-Around ", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 82);
  251. bufferGraphics.drawString("L - Longer ", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 94);
  252. bufferGraphics.drawString("K - Shorter ", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 106);
  253.  
  254. //Now draw the board in white if the edges are not solid, red if they are
  255. if(wrapAround)
  256. {
  257. bufferGraphics.setColor(Color.white);
  258. }
  259. else
  260. {
  261. bufferGraphics.setColor(Color.red);
  262. }
  263.  
  264. //draws the edge of the game
  265. bufferGraphics.drawLine(BORDER - 1 + CELLSIZE, BORDER - 1 + CELLSIZE, GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE, BORDER - 1 + CELLSIZE);
  266. bufferGraphics.drawLine(BORDER - 1 + CELLSIZE, BORDER - 1 + CELLSIZE, BORDER - 1 + CELLSIZE, GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE);
  267. bufferGraphics.drawLine(GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE,BORDER - 1 + CELLSIZE, GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE, GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE);
  268. bufferGraphics.drawLine(BORDER - 1 + CELLSIZE,GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE,GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE,GRIDSIZE*CELLSIZE + BORDER + 1 - CELLSIZE);
  269.  
  270. //draws the cherry in white
  271. bufferGraphics.setColor(Color.white);
  272. bufferGraphics.fillRect(((cherryX*CELLSIZE) + BORDER),((cherryY*CELLSIZE) + BORDER),CELLSIZE,CELLSIZE);
  273.  
  274. //draws the blue cherry
  275. if(blueCherryOn)
  276. {
  277. bufferGraphics.setColor(Color.blue);
  278. bufferGraphics.fillRect(((blueCherryX*CELLSIZE) + BORDER),((blueCherryY*CELLSIZE) + BORDER),CELLSIZE,CELLSIZE);
  279. }
  280.  
  281. //draws the snake white if alive, red if dead
  282. if(gameOver)
  283. {
  284. bufferGraphics.setColor(Color.red);
  285. bufferGraphics.drawString("GAME OVER. Press R to reset", BORDER + CELLSIZE*GRIDSIZE + 20, BORDER + CELLSIZE + 22);
  286. }
  287. else
  288. {
  289. bufferGraphics.setColor(Color.white);
  290. }
  291.  
  292. for (int i = 0; i < GRIDSIZE; i++)
  293. {
  294. for (int j = 0; j < GRIDSIZE; j++)
  295. {
  296. if(grid[i][j] > 0)
  297. {
  298. bufferGraphics.fillRect(((i*CELLSIZE) + BORDER),((j*CELLSIZE) + BORDER),CELLSIZE,CELLSIZE);
  299. }
  300. }
  301. }
  302.  
  303. //finally draw the offscreen image to the applet
  304. g.drawImage(offscreen,0,0,this);
  305.  
  306. //this line makes sure all the monitors are up to date before proceeding
  307. Toolkit.getDefaultToolkit().sync();
  308. }
  309.  
  310. public void update(Graphics g)
  311. {
  312. paint(g);
  313. }
  314.  
  315. public void keyPressed(KeyEvent e)
  316. {
  317. if(keyPressedThisTick == true)
  318. {
  319.  
  320. }
  321.  
  322. else
  323. {
  324. if (e.getKeyCode() == KeyEvent.VK_RIGHT && !gameOver)
  325. {
  326. if(snakeUpDown == 0)
  327. {
  328. }
  329. else
  330. {
  331. snakeUpDown = 0;
  332. snakeLeftRight = 1;
  333. keyPressedThisTick = true;
  334. }
  335. }
  336.  
  337. else if (e.getKeyCode() == KeyEvent.VK_LEFT && !gameOver)
  338. {
  339. if(snakeUpDown == 0)
  340. {
  341. }
  342. else
  343. {
  344. snakeUpDown = 0;
  345. snakeLeftRight = -1;
  346. keyPressedThisTick = true;
  347. }
  348. }
  349.  
  350. else if (e.getKeyCode() == KeyEvent.VK_UP && !gameOver)
  351. {
  352. if(snakeLeftRight == 0)
  353. {
  354. }
  355. else
  356. {
  357. snakeLeftRight = 0;
  358. snakeUpDown = -1;
  359. keyPressedThisTick = true;
  360. }
  361. }
  362.  
  363. else if (e.getKeyCode() == KeyEvent.VK_DOWN && !gameOver)
  364. {
  365. if(snakeLeftRight == 0)
  366. {
  367. }
  368. else
  369. {
  370. snakeLeftRight = 0;
  371. snakeUpDown = 1;
  372. keyPressedThisTick = true;
  373. }
  374. }
  375.  
  376. else if (e.getKeyCode() == KeyEvent.VK_W && !gameOver)
  377. {
  378. if(wrapAround)
  379. {
  380. wrapAround = false;
  381. }
  382. else
  383. {
  384. wrapAround = true;
  385. }
  386. }
  387.  
  388. else if (e.getKeyCode() == KeyEvent.VK_F && !gameOver)
  389. {
  390. speed = speed - 10;
  391. if(speed < 10)
  392. {
  393. speed = 10;
  394. }
  395. }
  396.  
  397. else if (e.getKeyCode() == KeyEvent.VK_S && !gameOver)
  398. {
  399. speed = speed + 10;
  400. }
  401.  
  402. else if (e.getKeyCode() == KeyEvent.VK_L && !gameOver)
  403. {
  404. snakeLength++;
  405. }
  406.  
  407. else if (e.getKeyCode() == KeyEvent.VK_K && !gameOver)
  408. {
  409. snakeLength--;
  410. }
  411.  
  412. else if (e.getKeyCode() == KeyEvent.VK_R )
  413. {
  414. reset();
  415. }
  416. }
  417. }
  418.  
  419. public void keyTyped(KeyEvent e)
  420. {
  421. //this is placeholder
  422. }
  423.  
  424. public void keyReleased(KeyEvent e)
  425. {
  426. //this is placeholder
  427. }
  428.  
  429. public int getGridSize()
  430. {
  431. return GRIDSIZE;
  432. }
  433.  
  434. public void newCherry()
  435. {
  436. cherryX = 1 + (int)(Math.random() * ((GRIDSIZE - 2 - 1) + 1));
  437. cherryY = 1 + (int)(Math.random() * ((GRIDSIZE - 2 - 1) + 1));
  438. }
  439.  
  440. public void newBlueCherry()
  441. {
  442. if(1 + (int)(Math.random() * ((100 - 1) + 1)) == 99)
  443. {
  444. blueCherryOn = true;
  445. blueCherryX = 1 + (int)(Math.random() * ((GRIDSIZE - 2 - 1) + 1));
  446. blueCherryY = 1 + (int)(Math.random() * ((GRIDSIZE - 2 - 1) + 1));
  447. blueCherryCountdown = 100;
  448. blueCherryCooldown = 500;
  449. }
  450. }
  451.  
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement