Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Snake extends JComponent
- {
- private final static int ANCHO = 500, ALTO = 500, LADO = 10, VELO = 200;
- private float x, y, xf, yf;
- private float vx, vy;
- private boolean arriba, abajo, izquierda, derecha, teclaZ, teclaX, teclaC;
- private boolean pause;
- private static int score=0;
- public Snake()
- {
- setPreferredSize(new Dimension(ANCHO, ALTO));
- addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- actualiza(e.getKeyCode(), true);
- }
- public void keyReleased(KeyEvent e) {
- actualiza(e.getKeyCode(), false);
- }
- private void actualiza(int keyCode, boolean pressed) {
- switch (keyCode) {
- case KeyEvent.VK_UP:
- arriba = pressed;
- break;
- case KeyEvent.VK_DOWN:
- abajo = pressed;
- break;
- case KeyEvent.VK_LEFT:
- izquierda = pressed;
- break;
- case KeyEvent.VK_RIGHT:
- derecha = pressed;
- break;
- case KeyEvent.VK_Z:
- teclaZ = pressed;
- break;
- case KeyEvent.VK_X:
- teclaX = pressed;
- break;
- case KeyEvent.VK_C:
- teclaC = pressed;
- break;
- }
- }
- });
- setFocusable(true);
- }
- private float ajustar(float valor, float min, float max)
- {
- if (valor > max) return max;
- if (valor < min) return min;
- return valor;
- }
- private boolean colision(float x, float y, float xf, float yf)
- {
- int a, b, c, d;
- for( a = (int) x ; a<x+LADO ; a++ )
- {
- for( b = (int) y ; b<=y+LADO ; b++ )
- {
- for( c = (int) xf ; c<=xf+LADO ; c++ )
- {
- for( d = (int) yf ; c<=yf+LADO ; c++ )
- {
- if( a==c && b==d ) return true;
- //System.out.println( "("+a+","+b+")==("+c+","+d+")" );
- }
- }
- }
- }
- return false;
- }
- public void paint(Graphics g) {
- // BOX
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, ANCHO, ALTO);
- // SCORE
- g.setColor(Color.BLACK);
- g.setFont( new Font("Serif", Font.BOLD, 20) );
- g.drawString( ""+score , 5, 20 );
- g.drawString( "Z=PAUSA" , 5, 40 );
- // FRUIT
- g.setColor(Color.GREEN);
- g.fillOval(Math.round(xf), Math.round(yf), LADO, LADO);
- // SNAKE
- g.setColor(Color.RED);
- g.fillRect(Math.round(x), Math.round(y), LADO, LADO);
- // PAUSE
- if( pause )
- {
- g.setColor(Color.DARK_GRAY);
- g.fillRect(ANCHO/2-20, ALTO/2, 20, 50);
- g.fillRect(ANCHO/2+20, ALTO/2, 20, 50);
- }
- }
- private void dibuja() throws Exception {
- SwingUtilities.invokeAndWait(new Runnable() {
- public void run() {
- paintImmediately(0, 0, ANCHO, ALTO);
- }
- });
- }
- public void fisica() throws Exception {
- long tiempoViejo = System.nanoTime();
- vx = 0; vy = 0;
- x = ANCHO/2-LADO/2; y = ALTO/2-LADO/2;
- xf = ajustar( (float) Math.random()*ANCHO, 0, ANCHO - LADO );
- yf = ajustar( (float) Math.random()*ALTO, 0, ALTO - LADO );
- while (true) {
- long tiempoNuevo = System.nanoTime();
- float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
- tiempoViejo = tiempoNuevo;
- if( teclaZ ) { vx=0; vy=0; pause=true; }
- //if( teclaX ) { score++; }
- //if( teclaC ) { score=0; }
- if( arriba && vy==0 ) { vy = -VELO; vx = 0; pause=false; }
- if( abajo && vy==0 ) { vy = VELO; vx = 0; pause=false; }
- if( izquierda && vx==0 ) { vx = -VELO; vy = 0; pause=false; }
- if( derecha && vx==0 ) { vx = VELO; vy = 0 ; pause=false; }
- x = ajustar(x + vx * dt, 0, ANCHO - LADO);
- y = ajustar(y + vy * dt, 0, ALTO - LADO);
- if( x==0 || x==ANCHO-LADO || y==0 || y==ALTO-LADO )
- {
- x = ANCHO/2-LADO/2; vx = 0;
- y = ALTO/2-LADO/2; vy = 0;
- score=0;
- xf = ajustar( (float) Math.random()*ANCHO, 0, ANCHO - LADO );
- yf = ajustar( (float) Math.random()*ALTO, 0, ALTO - LADO );
- }
- else if( colision(x,y,xf,yf)==true )
- {
- score++;
- xf = ajustar( (float) Math.random()*ANCHO, 0, ANCHO - LADO );
- yf = ajustar( (float) Math.random()*ALTO, 0, ALTO - LADO );
- }
- dibuja();
- }
- }
- public static void main(String[] args) throws Exception
- {
- JFrame jf = new JFrame("Snake");
- jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } });
- jf.setResizable(false);
- Snake snake = new Snake();
- jf.getContentPane().add(snake);
- jf.pack();
- jf.setVisible(true);
- snake.fisica();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement