Advertisement
Kyrexar

Snake v0.1

Nov 14th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Snake extends JComponent
  6. {
  7.     private final static int ANCHO = 500, ALTO = 500, LADO = 10, VELO = 200;
  8.     private float x, y, xf, yf;
  9.     private float vx, vy;
  10.     private boolean arriba, abajo, izquierda, derecha, teclaZ, teclaX, teclaC;
  11.     private boolean pause;
  12.     private static int score=0;
  13.  
  14.     public Snake()
  15.     {
  16.         setPreferredSize(new Dimension(ANCHO, ALTO));
  17.        
  18.         addKeyListener(new KeyAdapter() {
  19.                 public void keyPressed(KeyEvent e) {
  20.                     actualiza(e.getKeyCode(), true);
  21.                 }
  22.  
  23.                 public void keyReleased(KeyEvent e) {
  24.                     actualiza(e.getKeyCode(), false);
  25.                 }
  26.  
  27.                 private void actualiza(int keyCode, boolean pressed) {
  28.                     switch (keyCode) {
  29.                         case KeyEvent.VK_UP:
  30.                             arriba = pressed;
  31.                             break;
  32.  
  33.                         case KeyEvent.VK_DOWN:
  34.                             abajo = pressed;
  35.                             break;
  36.  
  37.                         case KeyEvent.VK_LEFT:
  38.                             izquierda = pressed;
  39.                             break;
  40.  
  41.                         case KeyEvent.VK_RIGHT:
  42.                             derecha = pressed;
  43.                             break;
  44.                            
  45.                         case KeyEvent.VK_Z:
  46.                             teclaZ = pressed;
  47.                             break;
  48.                            
  49.                         case KeyEvent.VK_X:
  50.                             teclaX = pressed;
  51.                             break;
  52.                            
  53.                         case KeyEvent.VK_C:
  54.                             teclaC = pressed;
  55.                             break;
  56.                     }
  57.                 }
  58.             });
  59.         setFocusable(true);
  60.     }
  61.  
  62.     private float ajustar(float valor, float min, float max)
  63.     {
  64.         if (valor > max) return max;
  65.         if (valor < min) return min;
  66.         return valor;
  67.     }
  68.    
  69.     private boolean colision(float x, float y, float xf, float yf)
  70.     {
  71.         int a, b, c, d;
  72.        
  73.         for( a = (int) x ; a<x+LADO ; a++ )
  74.         {
  75.             for( b = (int) y ; b<=y+LADO ; b++ )
  76.             {
  77.                 for( c = (int) xf ; c<=xf+LADO ; c++ )
  78.                 {
  79.                     for( d = (int) yf ; c<=yf+LADO ; c++ )
  80.                     {
  81.                         if( a==c && b==d ) return true;
  82.                         //System.out.println( "("+a+","+b+")==("+c+","+d+")" );
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.        
  88.         return false;
  89.     }
  90.  
  91.     public void paint(Graphics g) {
  92.        
  93.         // BOX
  94.         g.setColor(Color.WHITE);
  95.         g.fillRect(0, 0, ANCHO, ALTO);
  96.        
  97.         // SCORE        
  98.         g.setColor(Color.BLACK);
  99.         g.setFont( new Font("Serif", Font.BOLD, 20) );
  100.         g.drawString( ""+score , 5, 20 );
  101.         g.drawString( "Z=PAUSA"  , 5, 40 );
  102.  
  103.         // FRUIT
  104.         g.setColor(Color.GREEN);
  105.         g.fillOval(Math.round(xf), Math.round(yf), LADO, LADO);
  106.        
  107.         // SNAKE
  108.         g.setColor(Color.RED);
  109.         g.fillRect(Math.round(x), Math.round(y), LADO, LADO);
  110.        
  111.         // PAUSE
  112.         if( pause )
  113.         {
  114.             g.setColor(Color.DARK_GRAY);
  115.             g.fillRect(ANCHO/2-20, ALTO/2, 20, 50);
  116.             g.fillRect(ANCHO/2+20, ALTO/2, 20, 50);
  117.         }
  118.     }
  119.  
  120.     private void dibuja() throws Exception {
  121.         SwingUtilities.invokeAndWait(new Runnable() {
  122.                 public void run() {
  123.                     paintImmediately(0, 0, ANCHO, ALTO);
  124.                 }
  125.             });
  126.     }
  127.  
  128.     public void fisica() throws Exception {
  129.         long tiempoViejo = System.nanoTime();
  130.        
  131.         vx = 0; vy = 0;
  132.         x = ANCHO/2-LADO/2; y = ALTO/2-LADO/2;
  133.         xf = ajustar( (float) Math.random()*ANCHO, 0, ANCHO - LADO );
  134.         yf = ajustar( (float) Math.random()*ALTO, 0, ALTO - LADO );
  135.        
  136.         while (true) {
  137.             long tiempoNuevo = System.nanoTime();
  138.             float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
  139.             tiempoViejo = tiempoNuevo;
  140.            
  141.             if( teclaZ ) { vx=0; vy=0; pause=true; }
  142.             //if( teclaX ) { score++; }
  143.             //if( teclaC ) { score=0; }
  144.            
  145.             if( arriba && vy==0 ) { vy = -VELO; vx = 0; pause=false; }
  146.             if( abajo && vy==0 ) { vy = VELO; vx = 0; pause=false; }
  147.             if( izquierda && vx==0 ) { vx = -VELO; vy = 0; pause=false; }
  148.             if( derecha && vx==0 ) { vx = VELO; vy = 0 ; pause=false; }
  149.            
  150.             x = ajustar(x + vx * dt, 0, ANCHO - LADO);
  151.             y = ajustar(y + vy * dt, 0, ALTO - LADO);            
  152.            
  153.             if( x==0 || x==ANCHO-LADO || y==0 || y==ALTO-LADO )
  154.             {
  155.                 x = ANCHO/2-LADO/2; vx = 0;
  156.                 y = ALTO/2-LADO/2; vy = 0;
  157.                
  158.                 score=0;
  159.                
  160.                 xf = ajustar( (float) Math.random()*ANCHO, 0, ANCHO - LADO );
  161.                 yf = ajustar( (float) Math.random()*ALTO, 0, ALTO - LADO );
  162.             }
  163.             else if( colision(x,y,xf,yf)==true )
  164.             {
  165.                 score++;
  166.                
  167.                 xf = ajustar( (float) Math.random()*ANCHO, 0, ANCHO - LADO );
  168.                 yf = ajustar( (float) Math.random()*ALTO, 0, ALTO - LADO );
  169.             }
  170.  
  171.             dibuja();
  172.         }
  173.     }
  174.  
  175.     public static void main(String[] args) throws Exception
  176.     {
  177.         JFrame jf = new JFrame("Snake");
  178.         jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } });
  179.         jf.setResizable(false);
  180.         Snake snake = new Snake();
  181.         jf.getContentPane().add(snake);
  182.         jf.pack();
  183.         jf.setVisible(true);
  184.         snake.fisica();
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement