Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.ArrayList;
- import java.util.Date;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Snake {
- /* If you write code like this, I will come to your house and murder you */
- public static void main(String[] args) {
- @SuppressWarnings("serial")
- class Panel extends JPanel implements KeyListener, Runnable {
- class Body { /* mm nested classes used like structs */
- public double x, y;
- public Body(double x, double y) {
- this.x = x;
- this.y = y;
- }
- }
- public double x, y, theta;
- public int foodX, foodY;
- public long time, lastTick;
- public ArrayList<Integer> key;
- public ArrayList<Body> body;
- public Panel() {
- this.addKeyListener(this);
- setFocusable(true);
- requestFocus();
- reset();
- new Thread(this).start();
- }
- public void reset() {
- key = new ArrayList<>();
- body = new ArrayList<>();
- time = new Date().getTime();
- lastTick = time;
- x = 0;
- y = 0;
- theta = 0;
- body.add(new Body(-15, 0));
- randFood();
- }
- public void randFood() {
- foodX = (int)(Math.random() * 300 - 150);
- foodY = (int)(Math.random() * 300 - 150);
- }
- public int key(int k) {
- return key.contains(k) ? 1 : 0;
- }
- public void tick(double delta) {
- theta -= key(KeyEvent.VK_A)*2*Math.PI*delta;
- theta += key(KeyEvent.VK_D)*2*Math.PI*delta;
- x += 200*Math.cos(theta)*delta;
- y += 200*Math.sin(theta)*delta;
- for(int i = 0; i < body.size(); i++) {
- int ax = 0, ay = 0;
- if(i == 0) {
- ax = (int)(x + 15*Math.cos(theta+Math.PI));
- ay = (int)(y + 15*Math.sin(theta+Math.PI));
- } else {
- ax = (int)body.get(i-1).x;
- ay = (int)body.get(i-1).y;
- }
- Body b = body.get(i);
- double angle = Math.atan2(ay-b.y, ax-b.x);
- ax += Math.cos(angle+Math.PI)*16;
- ay += Math.sin(angle+Math.PI)*16;
- b.x += (ax-b.x)*0.3;
- b.y += (ay-b.y)*0.3;
- }
- for(int i = 0; i < body.size(); i++) {
- Body a = body.get(i);
- for(int j = 0; j < body.size(); j++) {
- if(j!=i) {
- Body b = body.get(j);
- double dist = Math.hypot(a.x-b.x, a.y-b.y);
- if(dist < 16) {
- double angle = Math.atan2(a.y-b.y, a.x-b.y);
- a.x += Math.cos(angle)*(16-dist)/2;
- a.y += Math.sin(angle)*(16-dist)/2;
- b.x += Math.cos(angle+Math.PI)*(16-dist)/2;
- b.y += Math.sin(angle+Math.PI)*(16-dist)/2;
- }
- }
- }
- if(Math.hypot(x-a.x, y-a.y) < 15) {
- reset();
- return;
- }
- }
- if(x < -200 || x > 200 || y < -200 || y > 200) {
- reset();
- return;
- }
- if(Math.hypot(x-foodX, y-foodY) < 15) {
- randFood();
- Body last = body.get(body.size()-1);
- body.add(new Body(last.x, last.y));
- }
- }
- public void paintComponent(Graphics gr) {
- Graphics2D g = (Graphics2D)gr;
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- int w = getWidth(), h = getHeight();
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, w, h);
- g.translate(w/2, h/2);
- int[] xPoints = {
- (int)(x+14*Math.cos(theta)),
- (int)(x+14*Math.cos(theta+Math.PI*3/4)),
- (int)(x+14*Math.cos(theta+Math.PI*5/4))
- }, yPoints = {
- (int)(y+14*Math.sin(theta)),
- (int)(y+14*Math.sin(theta+Math.PI*3/4)),
- (int)(y+14*Math.sin(theta+Math.PI*5/4))
- };
- g.setColor(Color.WHITE);
- g.setStroke(new BasicStroke(3));
- g.drawPolygon(xPoints, yPoints, 3);
- for(int i = 0; i < body.size(); i++) {
- Body b = body.get(i);
- g.drawOval((int)b.x-8, (int)b.y-8, 16, 16);
- }
- g.drawOval(foodX-5, foodY-5, 10, 10);
- }
- public void run() {
- while(true) {
- Thread.currentThread();
- lastTick = time;
- time = new Date().getTime();
- double delta = (time-lastTick)/1000.0;
- tick(delta);
- repaint();
- try {
- Thread.sleep(20);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public void keyPressed(KeyEvent e) {
- int c = e.getKeyCode();
- if(!key.contains(c))
- key.add(c);
- }
- public void keyReleased(KeyEvent e) {
- int c = e.getKeyCode(), i = key.indexOf(c);
- if(i > -1) {
- key.remove(i);
- }
- }
- public void keyTyped(KeyEvent arg0) {}
- }
- JFrame frame = new JFrame("Snake");
- frame.setSize(400, 400);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setContentPane(new Panel());
- frame.setVisible(true);
- frame.getContentPane();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement