Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.32 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.awt.*;
  6. import javax.swing.*;
  7.  
  8. public class Snake extends JPanel implements KeyListener,ActionListener{
  9.  
  10.         int[][] grid;
  11.        
  12.         int direction;
  13.         int snakeSize;
  14.         int history;    
  15.         int head_x;
  16.         int head_y;
  17.        
  18.         int food_x;
  19.         int food_y;
  20.        
  21.         boolean gameover;
  22.        
  23.         Timer t;
  24.         Points p;
  25.        
  26.         public Snake(int width, int height) {
  27.                 grid=new int[width][height];
  28.                 setSize(width*25,height*25);
  29.                 p=new Points(this);
  30.                 add(p);
  31.                 gameover=true;
  32.         }
  33.        
  34.         public void start() {
  35.                 gameover=false;
  36.                 p=new Points(this);
  37.                 head_x=0;
  38.                 head_y=0;
  39.                 snakeSize=1;
  40.                 direction=1;
  41.                 history=1;
  42.                 for(int i=0;i<grid.length;i++)
  43.                         for(int j=0;j<grid[i].length;j++)
  44.                                 grid[i][j]=0;
  45.                 grid[0][0]=1;
  46.                 food_x=grid.length/2;
  47.                 food_y=grid[0].length/2;
  48.                 t=new Timer(250,this);
  49.                 t.start();
  50.         }
  51.        
  52.         public boolean snakeMove() {
  53.                 switch(direction) {
  54.                 case 0:
  55.                         if(head_y-1<0||grid[head_x][head_y-1]>history-snakeSize)
  56.                                 return false;
  57.                         else {
  58.                                 head_y--;
  59.                                 history++;
  60.                                 if(head_x==food_x&&head_y==food_y)
  61.                                         eat();
  62.                                 grid[head_x][head_y]=history;
  63.                                 return true;
  64.                         }
  65.                 case 1:
  66.                         if(head_x+1>=grid.length||grid[head_x+1][head_y]>history-snakeSize)
  67.                                 return false;
  68.                         else {
  69.                                 head_x++;
  70.                                 history++;
  71.                                 if(head_x==food_x&&head_y==food_y)
  72.                                         eat();
  73.                                 grid[head_x][head_y]=history;
  74.                                 return true;
  75.                         }
  76.                 case 2:
  77.                         if(head_y+1>=grid[head_x].length||grid[head_x][head_y+1]>history-snakeSize)
  78.                                 return false;
  79.                         else {
  80.                                 head_y++;
  81.                                 history++;
  82.                                 if(head_x==food_x&&head_y==food_y)
  83.                                         eat();
  84.                                 grid[head_x][head_y]=history;
  85.                                 return true;
  86.                         }
  87.                 case 3:
  88.                         if(head_x-1<0||grid[head_x-1][head_y]>history-snakeSize)
  89.                                 return false;
  90.                         else {
  91.                                 head_x--;
  92.                                 history++;
  93.                                 if(head_x==food_x&&head_y==food_y)
  94.                                         eat();
  95.                                 grid[head_x][head_y]=history;
  96.                                 return true;
  97.                         }
  98.                 default:
  99.                         return false;
  100.                 }              
  101.         }
  102.        
  103.         public void eat() {
  104.                 snakeSize++;
  105.                 int rand_x = (int)(Math.random()*grid.length);
  106.                 int rand_y = (int)(Math.random()*grid[0].length);
  107.                 while(grid[rand_x][rand_y]>history-snakeSize) {
  108.                         rand_x = (int)(Math.random()*grid.length);
  109.                         rand_y = (int)(Math.random()*grid[0].length);
  110.                 }
  111.                 food_x=rand_x;
  112.                 food_y=rand_y;
  113.                 if(t.getDelay()>=70)
  114.                         t.setDelay(t.getDelay()-5);
  115.         }
  116.        
  117.         public void tick() {
  118.                 if(!snakeMove())  {
  119.                         t.stop();
  120.                         gameover=true;
  121.                 }
  122.                        
  123.                 repaint();
  124.         }
  125.        
  126.         public void pauseToggle() {
  127.                 if(t.isRunning())
  128.                         t.stop();
  129.                 else if(!gameover)
  130.                         t.start();
  131.         }
  132.        
  133.         public static void main(String[] args) {
  134.                 //System.out.println("Go.");
  135.                 JFrame frame = new JFrame("Snake");
  136.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  137.                 Snake s = new Snake(25,25);
  138.                 frame.addKeyListener(s);
  139.                 frame.setLayout(null);
  140.                 frame.setContentPane(s);
  141.                 frame.setUndecorated(true);
  142.                 frame.setSize(s.getSize().width,s.getSize().height);
  143.                 frame.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-frame.getWidth()/2,Toolkit.getDefaultToolkit().getScreenSize().height/2-frame.getHeight()/2);
  144.                
  145.                 JLabel info = new JLabel("<html>SPACE to pause<br>ESC to reset</html>");
  146.                 info.setFont(new Font("Arial",Font.PLAIN,10));
  147.                 info.setForeground(Color.gray);
  148.                 info.setBounds(frame.getWidth()-85,0,100,30);
  149.                
  150.                 s.add(info);
  151.                
  152.                 s.setLayout(null);            
  153.                 frame.setVisible(true);
  154.                 s.start();
  155.  
  156.         }
  157.        
  158.         public void paintComponent(Graphics g) {
  159.                 for(int x=0;x<grid[0].length;x++)
  160.                         for(int y=0;y<grid[x].length;y++) {
  161.                                 if(x==food_x&&y==food_y)
  162.                                         g.setColor(Color.red);
  163.                                 else if(grid[x][y]>history-snakeSize) {
  164.                                         if(x==head_x&&y==head_y)
  165.                                                 g.setColor(Color.green);
  166.                                         else
  167.                                                 g.setColor(Color.blue);
  168.                                 }
  169.                                        
  170.                                 else
  171.                                         g.setColor(Color.black);
  172.                                 g.fillRect(x*25,y*25,25,25);
  173.                         }
  174.                 p.paintComponent(g);
  175.                 //g.setColor(Color.yellow);
  176.                 //g.fillRect(100,100,100,100);
  177.         }
  178.  
  179.        
  180.         //public void paintSquare(Graphics g, int x, int y) {
  181.         //      g.fillRect(x,y,10,10);//x+10,y+10);
  182.         //}
  183.  
  184.         @Override
  185.         public void keyPressed(KeyEvent k) {
  186.                 /*
  187.                 if(Character.toLowerCase(k.getKeyChar())=='w')
  188.                         direction=0;
  189.                 if(Character.toLowerCase(k.getKeyChar())=='d')
  190.                         direction=1;
  191.                 if(Character.toLowerCase(k.getKeyChar())=='s')
  192.                         direction=2;
  193.                 if(Character.toLowerCase(k.getKeyChar())=='a')
  194.                         direction=3;
  195.                         */
  196.                 if(k.getKeyCode()==KeyEvent.VK_LEFT||k.getKeyCode()==KeyEvent.VK_A)
  197.                         if(dirChange(3))direction=3;
  198.                 if(k.getKeyCode()==KeyEvent.VK_DOWN||k.getKeyCode()==KeyEvent.VK_S)
  199.                         if(dirChange(2))direction=2;
  200.                 if(k.getKeyCode()==KeyEvent.VK_RIGHT||k.getKeyCode()==KeyEvent.VK_D)
  201.                         if(dirChange(1))direction=1;
  202.                 if(k.getKeyCode()==KeyEvent.VK_UP||k.getKeyCode()==KeyEvent.VK_W)
  203.                         if(dirChange(0))direction=0;
  204.                 if(k.getKeyCode()==KeyEvent.VK_SPACE)
  205.                         pauseToggle();
  206.                 if(k.getKeyCode()==KeyEvent.VK_ESCAPE) {
  207.                         t.stop();
  208.                         start();
  209.                 }
  210.                        
  211.         }
  212.  
  213.         public void keyReleased(KeyEvent k) {
  214.                 // TODO Auto-generated method stub
  215.                
  216.         }
  217.  
  218.         public void keyTyped(KeyEvent k) {
  219.                 /*
  220.                 //System.out.println("Key typed: "+k);
  221.                 if(Character.toLowerCase(k.getKeyChar())=='w') {
  222.                         //System.out.print("UP ");
  223.                         if(dirChange(0))direction=0;
  224.                         //System.out.println(direction);
  225.                 }                      
  226.                 if(Character.toLowerCase(k.getKeyChar())=='d'){
  227.                         //System.out.print("RIGHT ");
  228.                         if(dirChange(1))direction=1;
  229.                         //System.out.println(direction);
  230.                 }
  231.                 if(Character.toLowerCase(k.getKeyChar())=='s'){
  232.                         //System.out.print("DOWN ");
  233.                         if(dirChange(2))direction=2;
  234.                         //System.out.println(direction);
  235.                 }
  236.                 if(Character.toLowerCase(k.getKeyChar())=='a'){
  237.                         //System.out.print("LEFT ");
  238.                         if(dirChange(3))direction=3;
  239.                         //System.out.println(direction);
  240.                 }
  241.                 */
  242.                
  243.         }
  244.        
  245.         public boolean dirChange(int newdir) {
  246.                 switch(newdir) {
  247.                 case 0:
  248.                         if(head_y-1<0)  //allow failure into wall
  249.                                 return true;
  250.                         if(grid[head_x][head_y-1]==history-1)
  251.                                 return false;
  252.                         return true;
  253.                 case 1:
  254.                         if(head_x+1==grid.length)  //allow failure into wall
  255.                                 return true;
  256.                         if(grid[head_x+1][head_y]==history-1)
  257.                                 return false;
  258.                         return true;
  259.                 case 2:
  260.                         if(head_y+1==grid[0].length)  //allow failure into wall
  261.                                 return true;
  262.                         if(grid[head_x][head_y+1]==history-1)
  263.                                 return false;
  264.                         return true;
  265.                 case 3:
  266.                         if(head_x-1<0)  //allow failure into wall
  267.                                 return true;
  268.                         if(grid[head_x-1][head_y]==history-1)
  269.                                 return false;
  270.                         return true;
  271.                 }
  272.                 return false;
  273.         }
  274.  
  275.         @Override
  276.         public void actionPerformed(ActionEvent e) {
  277.                 tick();
  278.         }
  279.  
  280. }
  281.  
  282. class Points extends JLabel{
  283.         Snake s;
  284.        
  285.         Points(Snake snake) {
  286.                 s=snake;
  287.                 setText("0");
  288.         }
  289.        
  290.         public void paintComponent(Graphics g) {
  291.                 setText(String.valueOf(s.snakeSize*10-10));
  292.                 g.setColor(Color.white);
  293.                 g.drawString(getText(),5,15);
  294.         }
  295. }
Add Comment
Please, Sign In to add comment