Advertisement
Guest User

Untitled

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