domingo, 17 de agosto de 2014

Descargar juego de tetris del gusano que crece en jcreator (Snake) que crece cuando come puntos




Este codigo es del juego snake me pareció muy interesante, me lo paso un amigo y lo quiero compartir con ustedes, interesante lo del punto al azar en el cuadro, funciona en jcreator y en netbeans  







Esta clase se llama Snake 



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class Snake extends JFrame implements KeyListener, Runnable {

    JPanel p1, p2;
    JButton[] lb = new JButton[200];
    JButton bonusfood;
    JTextArea t;
    int x = 500, y = 250, gu = 2, directionx = 1, directiony = 0, speed = 50, difference = 0, oldx, oldy, score = 0;
    int[] lbx = new int[300];
    int[] lby = new int[300];
    Point[] lbp = new Point[300];
    Point bfp = new Point();
    Thread myt;
    boolean food = false, runl = false, runr = true, runu = true, rund = true, bonusflag = true;
    Random r = new Random();
    JMenuBar mymbar;
    JMenu game, help, level;

    public void initializeValues() {
        gu = 3;//para tamaño inicial del gusano
        lbx[0] = 100;
        lby[0] = 150;
        directionx = 10;
        directiony = 0;
        difference = 0;
        score = 0;
        food = false;
        runl = false;
        runr = true;
        runu = true;
        rund = true;
        bonusflag = true;
    }

  Snake() {
        super("Snake");
        setSize(500, 330);
        //Create Menue bar with functions
        creatbar();
        //initialize all variables
        initializeValues();
        // Start of UI design
        p1 = new JPanel();
        p2 = new JPanel();
        // t will view the score
        t = new JTextArea("Score ==>" + score);
        t.setEnabled(false);
        t.setBackground(Color.BLACK);
        // snake have to eat bonousfood to growup
        bonusfood = new JButton();
        bonusfood.setEnabled(false);
        // will make first snake
        createFirstSnake();

        p1.setLayout(null);
        p2.setLayout(new GridLayout(0, 1));
        p1.setBounds(0, 0, x, y);
        p1.setBackground(Color.blue);
        p2.setBounds(0, y, x, 30);
        p2.setBackground(Color.RED);

        p2.add(t); // will contain score board
        // end of UI design
        getContentPane().setLayout(null);
        getContentPane().add(p1);
        getContentPane().add(p2);

        show();
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        addKeyListener(this);
        // start thread
        myt = new Thread(this);
        myt.start(); // go to run() method corremos el hilo
    }

   public void createFirstSnake() {
         // Initially the snake has small length 3
        for (int i = 0; i < 3; i++) {
            lb[i] = new JButton("lb" + i);
            lb[i].setEnabled(false);
            p1.add(lb[i]);
            lb[i].setBounds(lbx[i],lby[i], 10, 10);//los botones se muestran
// en el marco con valor inicial de lbx[0]=100 lby[0]=150 tamaño de 10x10)
            lbx[i + 1] = lbx[i] - 10;
            lby[i + 1] = lby[i];
        }
    }

    public void creatbar() {
        mymbar = new JMenuBar();

        game = new JMenu("Game");

        JMenuItem newgame = new JMenuItem("New Game");
        JMenuItem exit = new JMenuItem("Exit");

        newgame.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        reset();
                    }
                });

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        game.add(newgame);
        game.addSeparator();
        game.add(exit);

        mymbar.add(game);

        level = new JMenu("Level");

        mymbar.add(level);

        help = new JMenu("Help");

        JMenuItem creator = new JMenuItem("Creator");
        JMenuItem instruction = new JMenuItem("Instraction");

        creator.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(p2, "Name :tu ");
            }
        });

        help.add(creator);
        help.add(instruction);
        mymbar.add(help);

        setJMenuBar(mymbar);
    }

    void reset() {
        initializeValues();
        p1.removeAll();

        myt.stop();

        createFirstSnake();
        t.setText("Score==>" + score);

        myt = new Thread(this);
        myt.start();
    }

    void growup() {//crecer
        lb[gu] = new JButton();
        lb[gu].setEnabled(false);
        p1.add(lb[gu]);
//para aparecer al azar un punto en el cuadro
        int a = 10 + (10 * r.nextInt(48));//ramdom para x
        int b = 10 + (10 * r.nextInt(23));//ramdom para y
        System.out.print("\nx"+a);
        System.out.print("\ny"+b);
        lbx[gu] = a;
        lby[gu] = b;
        lb[gu].setBounds(a, b, 10, 10);

        gu++;
    }
    // this method contains the logic to move the snake. player will define the derection
    // this method just forward the snake to the right derection which deriction is pressed
    // by the player.
    void moveForward() {
        for (int i = 0; i < gu; i++) {// o123 en total 4 pociciones dependiendo si no creece gu
            lbp[i] = lb[i].getLocation();
           
           System.out.print("\nlbp i=0 1<3 i++"+lbp[i]);
           System.out.print("\nlb "+lb[i]);
        }

        lbx[0] += directionx;//incrementa de 10 en10 en 10
          System.out.print("\nlbx"+lbx[0]);
        lby[0] += directiony;
          System.out.print("\nlby"+lby[0]);
        lb[0].setBounds(lbx[0], lby[0], 10, 10);

        for (int i = 1; i < gu; i++) {
            lb[i].setLocation(lbp[i-1 ]);//quitara la ultima pocicion de las 4 botones
           System.out.print("\nlb 1<3 i++ i-1"+lb[i]);
        }
//cuando llega al final inicia en el otro lado
        if (lbx[0] == x) {
            lbx[0] = 10;
          //    System.out.print("\nvector botones llego a la pared x=500"+x);
        } else if (lbx[0] == 0) {
            lbx[0] = x - 10;
          
        } else if (lby[0] == y) {
            lby[0] = 10;
      //      System.out.print("\nvector botones llego a la pared x="+y);
        } else if (lby[0] == 0) {
            lby[0] = y - 10;
        }

        if (lbx[0] == lbx[gu - 1] && lby[0] == lby[gu - 1]) {
            food = false;
            score += 5;
            t.setText("Score==>" + score);
            if (score % 50 == 0 && bonusflag == true) {
                p1.add(bonusfood);
                bonusfood.setBounds((10 * r.nextInt(50)), (10 * r.nextInt(25)), 15, 15);
                bfp = bonusfood.getLocation();
                bonusflag = false;
            }
        }

        if (bonusflag == false) {
            if (bfp.x <= lbx[0] && bfp.y <= lby[0] && bfp.x + 10 >= lbx[0] && bfp.y + 10 >= lby[0]) {
                p1.remove(bonusfood);
                score += 100;
                t.setText("Score ==>" + score);
                bonusflag = true;
            }
        }

        if (food == false) {
            growup();
            food = true;
        } else {
            lb[gu - 1].setBounds(lbx[gu - 1], lby[gu - 1], 10, 10);
        }

        for (int i = 1; i < gu; i++) {
            if (lbp[0] == lbp[i]) {
                t.setText("GAME OVER               " + score);
                try {
                    myt.join();
                } catch (InterruptedException ie) {
                }
                break;
            }
        }


        p1.repaint();
        show();
    }

    public void keyPressed(KeyEvent e) {
        //KeyCode() == 37 cada numero significa una tecla de direccion
        // snake move to left when player pressed left arrow
        if (runl == true && e.getKeyCode() == 37) {
            directionx = -10; // means snake move right to left by 10pixels
            directiony = 0;
            runr = false;     // run right(runr) means snake cant move from left to right
            runu = true;      // run up   (runu) means snake can move from down to up
            rund = true;      // run down (run down) means snake can move from up to down
        }
        // snake move to up when player pressed up arrow
        if (runu == true && e.getKeyCode() == 38) {
            directionx = 0;
            directiony = -10; // means snake move from down to up by 10 pixel
            rund = false;     // run down (run down) means snake can move from up to down
            runr = true;      // run right(runr) means snake can move from left to right
            runl = true;      // run left (runl) means snake can move from right to left
        }
        // snake move to right when player pressed right arrow
        if (runr == true && e.getKeyCode() == 39) {
            directionx = +10; // means snake move from left to right by 10 pixel
            directiony = 0;
            runl = false;
            runu = true;
            rund = true;
        }
        // snake move to down when player pressed down arrow
        if (rund == true && e.getKeyCode() == 40) {
            directionx = 0;
            directiony = +10; // means snake move from left to right by 10 pixel
            runu = false;
            runr = true;
            runl = true;
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void run() {
        for (;;) {
            // Move the snake move forword. In the start of the game snake move left to right,
            // if player press up, down, right or left arrow snake change its direction according to
            // pressed arrow
            moveForward();
            try {
                Thread.sleep(speed);
            } catch (InterruptedException ie) {
            }
        }
    }

}


Esta esta es la clase principal para que funcione, Main.java
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Snake();
    }

}

1 comentario: