Advertisement
Guest User

snake

a guest
Apr 28th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package snake;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.awt.RenderingHints;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.KeyListener;
  14. import java.util.ArrayList;
  15. import java.util.Random;
  16.  
  17. import javax.swing.JFrame;
  18. import javax.swing.Timer;
  19.  
  20.  
  21. public class Snake implements ActionListener, KeyListener
  22. {
  23.  
  24. public static Snake snake;
  25.  
  26. public int width = 700, height = 700;
  27.  
  28. public JFrame jframe;
  29.  
  30. public RenderPanel renderPanel;
  31.  
  32. public Timer timer = new Timer(20, this);
  33.  
  34. public ArrayList<Point> snakeParts = new ArrayList<Point>();
  35.  
  36. public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
  37.  
  38. public int ticks = 0, direction = DOWN, score, tailLength = 10, time;
  39.  
  40. public Point head, cherry;
  41.  
  42. public Random random;
  43.  
  44. public boolean bot = false, selectingDifficulty;
  45.  
  46. public boolean over = false, paused;
  47.  
  48. public int gameStatus = 0;
  49.  
  50. public Dimension dim;
  51.  
  52. public Snake()
  53. {
  54. dim = Toolkit.getDefaultToolkit().getScreenSize();
  55. jframe = new JFrame("Snake");
  56. jframe.setVisible(true);
  57. jframe.setSize(805, 700);
  58. jframe.setResizable(false);
  59. jframe.setLocation(dim.width / 2 - jframe.getWidth() / 2, dim.height / 2 - jframe.getHeight() / 2);
  60. jframe.add(renderPanel = new RenderPanel());
  61. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. jframe.addKeyListener(this);
  63. startGame();
  64. }
  65.  
  66. public void startGame()
  67. {
  68. over = false;
  69. paused = false;
  70. time = 0;
  71. score = 0;
  72. tailLength = 14;
  73. ticks = 0;
  74. direction = DOWN;
  75. head = new Point(0, -1);
  76. random = new Random();
  77. snakeParts.clear();
  78. cherry = new Point(random.nextInt(79), random.nextInt(66));
  79. timer.start();
  80. }
  81.  
  82.  
  83. public void actionPerformed(ActionEvent arg0)
  84. {
  85. renderPanel.repaint();
  86. ticks++;
  87.  
  88. if (ticks % 2 == 0 && head != null && !over && !paused)
  89. {
  90. time++;
  91.  
  92. snakeParts.add(new Point(head.x, head.y));
  93.  
  94. if (direction == UP)
  95. {
  96. if (head.y - 1 >= 0 && noTailAt(head.x, head.y - 1))
  97. {
  98. head = new Point(head.x, head.y - 1);
  99. }
  100. else
  101. {
  102. over = true;
  103.  
  104. }
  105. }
  106.  
  107. if (direction == DOWN)
  108. {
  109. if (head.y + 1 < 67 && noTailAt(head.x, head.y + 1))
  110. {
  111. head = new Point(head.x, head.y + 1);
  112. }
  113. else
  114. {
  115. over = true;
  116. }
  117. }
  118.  
  119. if (direction == LEFT)
  120. {
  121. if (head.x - 1 >= 0 && noTailAt(head.x - 1, head.y))
  122. {
  123. head = new Point(head.x - 1, head.y);
  124. }
  125. else
  126. {
  127. over = true;
  128. }
  129. }
  130.  
  131. if (direction == RIGHT)
  132. {
  133. if (head.x + 1 < 80 && noTailAt(head.x + 1, head.y))
  134. {
  135. head = new Point(head.x + 1, head.y);
  136. }
  137. else
  138. {
  139. over = true;
  140. }
  141. }
  142.  
  143. if (snakeParts.size() > tailLength)
  144. {
  145. snakeParts.remove(0);
  146.  
  147. }
  148.  
  149. if (cherry != null)
  150. {
  151. if (head.equals(cherry))
  152. {
  153. score += 10;
  154. tailLength++;
  155. cherry.setLocation(random.nextInt(79), random.nextInt(66));
  156. }
  157. }
  158. }
  159. }
  160. public void render(Graphics2D g)
  161. {
  162. g.setColor(Color.BLACK);
  163. g.fillRect(0, 0, width, height);
  164. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  165.  
  166. if (gameStatus == 0)
  167. {
  168. g.setColor(Color.WHITE);
  169. g.setFont(new Font("Arial", 1, 50));
  170.  
  171. g.drawString("SNAKE", width / 2 - 75, 50);
  172.  
  173. if (!selectingDifficulty)
  174. {
  175. g.setFont(new Font("Arial", 1, 30));
  176.  
  177. g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  178. g.drawString("Press ESC to EXIT", width / 2 - 200, height / 2 + 25);
  179.  
  180. }
  181.  
  182.  
  183. }
  184. }
  185.  
  186.  
  187.  
  188. public boolean noTailAt(int x, int y)
  189. {
  190. for (Point point : snakeParts)
  191. {
  192. if (point.equals(new Point(x, y)))
  193. {
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199.  
  200. public static void main(String[] args)
  201. {
  202. snake = new Snake();
  203. }
  204.  
  205.  
  206. public void keyPressed(KeyEvent e)
  207. {
  208. int i = e.getKeyCode();
  209.  
  210. if ((i == KeyEvent.VK_A || i == KeyEvent.VK_LEFT) && direction != RIGHT)
  211. {
  212. direction = LEFT;
  213. }
  214.  
  215. if ((i == KeyEvent.VK_D || i == KeyEvent.VK_RIGHT) && direction != LEFT)
  216. {
  217. direction = RIGHT;
  218. }
  219.  
  220. if ((i == KeyEvent.VK_W || i == KeyEvent.VK_UP) && direction != DOWN)
  221. {
  222. direction = UP;
  223. }
  224.  
  225. if ((i == KeyEvent.VK_S || i == KeyEvent.VK_DOWN) && direction != UP)
  226. {
  227. direction = DOWN;
  228. }
  229.  
  230. if (i == KeyEvent.VK_SPACE)
  231. {
  232. if (over)
  233. {
  234. startGame();
  235. }
  236. else
  237. {
  238. paused = !paused;
  239. }
  240. }
  241. }
  242.  
  243. @Override
  244. public void keyReleased(KeyEvent e)
  245. {
  246. }
  247.  
  248. @Override
  249. public void keyTyped(KeyEvent e)
  250. {
  251. }
  252.  
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement