Advertisement
Guest User

Week 5 teenagecoders challenge (Snake)

a guest
Apr 6th, 2015
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13.  
  14.  
  15. public class Snake {
  16.     /* If you write code like this, I will come to your house and murder you */
  17.    
  18.     public static void main(String[] args) {
  19.         @SuppressWarnings("serial")
  20.        
  21.         class Panel extends JPanel implements KeyListener, Runnable {
  22.  
  23.             class Body { /* mm nested classes used like structs */
  24.                 public double x, y;
  25.  
  26.                 public Body(double x, double y) {
  27.                     this.x = x;
  28.                     this.y = y;
  29.                 }
  30.             }
  31.            
  32.             public double x, y, theta;
  33.             public int foodX, foodY;
  34.             public long time, lastTick;
  35.             public ArrayList<Integer> key;
  36.             public ArrayList<Body> body;
  37.            
  38.             public Panel() {
  39.                 this.addKeyListener(this);
  40.                 setFocusable(true);
  41.                 requestFocus();
  42.                 reset();
  43.                 new Thread(this).start();
  44.             }
  45.            
  46.             public void reset() {
  47.                 key = new ArrayList<>();
  48.                 body = new ArrayList<>();
  49.                 time = new Date().getTime();
  50.                 lastTick = time;
  51.                 x = 0;
  52.                 y = 0;
  53.                 theta = 0;
  54.                 body.add(new Body(-15, 0));
  55.                 randFood();
  56.             }
  57.            
  58.             public void randFood() {
  59.                 foodX = (int)(Math.random() * 300 - 150);
  60.                 foodY = (int)(Math.random() * 300 - 150);
  61.             }
  62.            
  63.             public int key(int k) {
  64.                 return key.contains(k) ? 1 : 0;
  65.             }
  66.            
  67.             public void tick(double delta) {
  68.                 theta -= key(KeyEvent.VK_A)*2*Math.PI*delta;
  69.                 theta += key(KeyEvent.VK_D)*2*Math.PI*delta;
  70.                        
  71.                 x += 200*Math.cos(theta)*delta;
  72.                 y += 200*Math.sin(theta)*delta;
  73.                
  74.                 for(int i = 0; i < body.size(); i++) {
  75.                     int ax = 0, ay = 0;
  76.                     if(i == 0) {
  77.                         ax = (int)(x + 15*Math.cos(theta+Math.PI));
  78.                         ay = (int)(y + 15*Math.sin(theta+Math.PI));
  79.                     } else {
  80.                         ax = (int)body.get(i-1).x;
  81.                         ay = (int)body.get(i-1).y;
  82.                        
  83.                     }
  84.                     Body b = body.get(i);
  85.                     double angle = Math.atan2(ay-b.y, ax-b.x);
  86.                     ax += Math.cos(angle+Math.PI)*16;
  87.                     ay += Math.sin(angle+Math.PI)*16;
  88.                     b.x += (ax-b.x)*0.3;
  89.                     b.y += (ay-b.y)*0.3;
  90.                 }
  91.                
  92.                 for(int i = 0; i < body.size(); i++) {
  93.                     Body a = body.get(i);
  94.                     for(int j = 0; j < body.size(); j++) {
  95.                         if(j!=i) {
  96.                             Body b = body.get(j);
  97.                             double dist = Math.hypot(a.x-b.x, a.y-b.y);
  98.                             if(dist < 16) {
  99.                                 double angle = Math.atan2(a.y-b.y, a.x-b.y);
  100.                                 a.x += Math.cos(angle)*(16-dist)/2;
  101.                                 a.y += Math.sin(angle)*(16-dist)/2;
  102.                                 b.x += Math.cos(angle+Math.PI)*(16-dist)/2;
  103.                                 b.y += Math.sin(angle+Math.PI)*(16-dist)/2;
  104.                                
  105.                             }
  106.                         }
  107.                     }
  108.                    
  109.                     if(Math.hypot(x-a.x, y-a.y) < 15) {
  110.                         reset();
  111.                         return;
  112.                     }
  113.                 }
  114.                
  115.                 if(x < -200 || x > 200 || y < -200 || y > 200) {
  116.                     reset();
  117.                     return;
  118.                 }
  119.                
  120.                 if(Math.hypot(x-foodX, y-foodY) < 15) {
  121.                     randFood();
  122.                     Body last = body.get(body.size()-1);
  123.                     body.add(new Body(last.x, last.y));
  124.                 }
  125.             }
  126.            
  127.             public void paintComponent(Graphics gr) {
  128.                 Graphics2D g = (Graphics2D)gr;
  129.                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  130.                         RenderingHints.VALUE_ANTIALIAS_ON);
  131.                
  132.                 int w = getWidth(), h = getHeight();
  133.                 g.setColor(Color.BLACK);
  134.                 g.fillRect(0, 0, w, h);
  135.                
  136.                 g.translate(w/2, h/2);
  137.                
  138.                 int[] xPoints = {
  139.                     (int)(x+14*Math.cos(theta)),
  140.                     (int)(x+14*Math.cos(theta+Math.PI*3/4)),
  141.                     (int)(x+14*Math.cos(theta+Math.PI*5/4))
  142.                 }, yPoints = {
  143.                     (int)(y+14*Math.sin(theta)),
  144.                     (int)(y+14*Math.sin(theta+Math.PI*3/4)),
  145.                     (int)(y+14*Math.sin(theta+Math.PI*5/4))
  146.                 };
  147.                
  148.                 g.setColor(Color.WHITE);
  149.                 g.setStroke(new BasicStroke(3));
  150.                 g.drawPolygon(xPoints, yPoints, 3);
  151.                
  152.                 for(int i = 0; i < body.size(); i++) {
  153.                     Body b = body.get(i);
  154.                     g.drawOval((int)b.x-8, (int)b.y-8, 16, 16);
  155.                 }
  156.                
  157.                 g.drawOval(foodX-5, foodY-5, 10, 10);
  158.             }
  159.            
  160.             public void run() {
  161.                 while(true) {
  162.                     Thread.currentThread();
  163.                     lastTick = time;
  164.                     time = new Date().getTime();
  165.                     double delta = (time-lastTick)/1000.0;
  166.                     tick(delta);
  167.                     repaint();
  168.                     try {
  169.                         Thread.sleep(20);
  170.                     } catch (InterruptedException e) {
  171.                         e.printStackTrace();
  172.                     }
  173.                 }
  174.             }
  175.  
  176.             public void keyPressed(KeyEvent e) {
  177.                 int c = e.getKeyCode();
  178.                 if(!key.contains(c))
  179.                         key.add(c);
  180.             }
  181.  
  182.             public void keyReleased(KeyEvent e) {
  183.                 int c = e.getKeyCode(), i = key.indexOf(c);
  184.                 if(i > -1) {
  185.                     key.remove(i);
  186.                 }
  187.             }
  188.  
  189.             public void keyTyped(KeyEvent arg0) {}
  190.            
  191.         }
  192.        
  193.         JFrame frame = new JFrame("Snake");
  194.         frame.setSize(400, 400);
  195.         frame.setResizable(false);
  196.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  197.         frame.setContentPane(new Panel());
  198.         frame.setVisible(true);
  199.         frame.getContentPane();
  200.     }
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement