codigo juego de culebrita o snake echo en java | Curso De Programacion Java Developer Free Courses Online
lindas bebes
lunes, 21 de enero de 2013

codigo juego de culebrita o snake echo en java



bueno es un programa de codigo libre gpl donde encontraremos una culebrita sencillo comiendo cajitas azules con muros al cual no podras chocar, y  comer todas las cajas azules les dejo el codigo y mas abajo el link para descargar el codigo .

primero que todo creamos un proyecto este lo llamaremos snake, luego de crear el proyecto creamos un pakete dentro del pakete snake llamado enums , en la cual quedara nombrado el pakete snake.enums
dentro de ese pakete creamos una nueva clase y la llamaremos KeyDirections  se encargara de la direccion y la la pulsacion de teclas en esa clase pondremos el siguiente codigo:




package snake.enums;
public enum KeyDirections
{
  UP(38),
  DOWN(40),
  LEFT(37),
  RIGHT(39);
  private int my_direction;
  private KeyDirections(final int the_direction)
  {
    setDirection(the_direction); 
  }
  public void setDirection(final int the_direction)
  {
    my_direction = the_direction;
  }
  public int getDirection()
  {
    return my_direction;
  }
}
luego de esto creamos un nuevo pakete en el pakete snake llamado gui el cual se encargara de la interfaz grafica entonces quedara el pakete nombrado de la siguiente forma snake.gui dentro de ese pakete crearemos las siguientes clases:
1 clase la nombraremos Level dentro de esa clase pondremos el siguiente codigo:



package snake.gui;

import java.awt.Color;

public class Level
{
  Color[][] layout = new Color[30][30];
  
  String convertFromImage;
  public Color[][] getLevel()
  {
    return null;
  }
  public Color[][] getLevel(final int levelNumber)
  {
    switch(levelNumber)
    {
      case 1:
        convertFromImage = "******************************" +
                         "*        *    s              *" +
                         "*        *    s              *" +
                         "*   f    *                   *" +
                         "*        *                   *" +
                         "*   f    *                   *" +
                         "*        *                   *" +
                         "*        *                   *" +
                         "*                            *" +
                         "*              f             *" +
                         "*                            *" +
                         "*              f             *" +
                         "*                            *" +
                         "*              f             *" +
                         "*                            *" +
                         "*              f             *" +
                         "*                            *" +
                         "*                            *" +
                         "*                            *" +
                         "*                            *" +
                         "*                            *" +
                         "*       x                    *" +
                         "*                            *" +
                         "*          x                 *" +
                         "*                            *" +
                         "*             x              *" +
                         "*                            *" +
                         "*                            *" +
                         "*                            *" +
                         "******************************";
        break;
      case 2:
        convertFromImage = "******************************" +
        "*         *   s              *" +
        "*         *   s              *" +
        "*   f     *                  *" +
        "*         *                  *" +
        "*   f     *          *       *" +
        "*         *          *       *" +
        "*         *          *       *" +
        "*         *          *       *" +
        "*         *     f    *       *" +
        "*         *          *       *" +
        "*         *     f    *       *" +
        "*         *          *       *" +
        "*         *     f    *       *" +
        "*         *          *x      *" +
        "*         *     f    *       *" +
        "*         *          *       *" +
        "*         *          *       *" +
        "*         ************       *" +
        "*                            *" +
        "*                            *" +
        "*                            *" +
        "*                            *" +
        "*               **************" +
        "*                            *" +
        "*                            *" +
        "*                            *" +
        "*                     f      *" +
        "*                            *" +
        "******************************";
        break;
      case 3:
        convertFromImage = "******************************" +
        "*             s              *" +
        "*             s              *" +
        "*                   **********" +
        "*                ***         *" +
        "* f             *       f    *" +
        "*       ********             *" +
        "*                        *   *" +
        "*                       *    *" +
        "*        ***           *     *" +
        "*           ***    *****     *" +
        "*    f         *   *         *" +
        "*          *****   *         *" +
        "*        **        *    f    *" +
        "*     ***     f    *         *" +
        "*    **           *          *" +
        "*    *           *           *" +
        "*  **       *****         ****" +
        "*  *   f   *            **   *" +
        "*  *       *        ****     *" +
        "*   *     *   f    *         *" +
        "*         *       *     f    *" +
        "*      ***       *           *" +
        "*               *            *" +
        "*              *             *" +
        "*             *              *" +
        "*                            *" +
        "*                            *" +
        "*                            *" +
        "******************************";
        break;
      case 4:
        convertFromImage = "******************************" +
        "*        f  * s    *         *" +
        "*           * s    *  f  **  *" +
        "*         f *      *  *   *  *" +
        "*****  ******      *  ********" +
        "*           *      *  *      *" +
        "*                     *   *  *" +
        "*********            f    * f*" +
        "*                    *****   *" +
        "*                f           *" +
        "*   **************           *" +
        "*                         *  *" +
        "*                         *  *" +
        "*       *                 *  *" +
        "*   *****************     *  *" +
        "*       *      *          *  *" +
        "*              *          *  *" +
        "*              *    ff    *  *" +
        "*      *       ************  *" +
        "*     * *                 *  *" +
        "*    * f                     *" +
        "*   *  f                     *" +
        "*********************        *" +
        "*              f    **       *" +
        "*        f            **     *" +
        "*     *************     **   *" +
        "*    ***           *     *   *" +
        "*     **  f     f   *        *" +
        "*                            *" +
        "******************************";

        break;
      case 5:

        convertFromImage = "******************************" +
        "*             s      *       *" +
        "*             s      *    f  *" +
        "*                    ***     *" +
        "*                            *" +
        "*       f                    *" +
        "*                    *    f  *" +
        "*   **************   *       *" +
        "*                    *xxx    *" +
        "*                    *****xx**" +
        "*     f     f                *" +
        "*                 f          *" +
        "************                 *" +
        "*          **************    *" +
        "*    f                       *" +
        "*                      ffff  *" +
        "*                        *****" +
        "**********   *************   *" +
        "*        *   ***             *" +
        "*        *   **              *" +
        "*        *   *               *" +
        "*        *   *****           *" +
        "*        *        *          *" +
        "*        *         *         *" +
        "*        *****      ******   *" +
        "*             *           *  *" +
        "*              *      f  *   *" +
        "*               *********    *" +
        "*                            *" +
        "******************************";
        break;
      case 6:
        convertFromImage = "******************************" +
        "*  f          s              *" +
        "*             s              *" +
        "*    f                       *" +
        "***************************  *" +
        "*                f           *" +
        "*                            *" +
        "*  ***************************" +
        "*                            *" +
        "***************************  *" +
        "*                            *" +
        "*  ***************************" +
        "*  *    f       f            *" +
        "*  *                         *" +
        "*  ****************          *" +
        "*  *                         *" +
        "*  *                         *" +
        "*  *                         *" +
        "*  *  *****************      *" +
        "*  *  *                      *" +
        "*  * f* f        f           *" +
        "*  *  *      *****************" +
        "*  *  *           f          *" +
        "*  *  ******************     *" +
        "*  *  *                      *" +
        "*  *  *       ****************" +
        "*  *f *                      *" +
        "*     ***************      f *" +
        "*     *    f                 *" +
        "******************************";
        break;
      case 7:
        convertFromImage = "******************************" +
        "*           * s*             *" +
        "*      ff  f* s*             *" +
        "*  ***     **  *         *****" +
        "*    *     f*       *****  f *" +
        "*    *      *                *" +
        "*    *  **  f                *" +
        "*          *            x    *" +
        "*  *        *           x    *" +
        "*  *                    x    *" +
        "*  *           x        x    *" +
        "*  *           *             *" +
        "*              *             *" +
        "*  x  x  f     *       ***   *" +
        "*   xx         *    ***   *  *" +
        "*    *             *      ** *" +
        "*     **           *       * *" +
        "*                  *       * *" +
        "*           *     *        * *" +
        "*      *    *    *           *" +
        "*     *     *    *           *" +
        "*    *     *     *           *" +
        "*   *            *           *" +
        "*    *   f       *     f     *" +
        "*     ***       *            *" +
        "* *    *       *       f     *" +
        "* *           *              *" +
        "*       f                 *  *" +
        "*                    ******* *" +
        "******************************";
        break;
      case 8:
        convertFromImage = "******************************" +
        "*             s              *" +
        "*             s              *" +
        "*                            *" +
        "***************************  *" +
        "*                            *" +
        "*                            *" +
        "*  ***************************" +
        "*                            *" +
        "*                            *" +
        "*   *xxxxx*******  *******  x*" +
        "*   *   f       *  *xxxxxx  x*" +
        "*   *    f      *  *******  x*" +
        "*   *x*****     *           x*" +
        "*  *xxx   *     *  f     f  x*" +
        "* *x*x*****     **************" +
        "*  *x*                       *" +
        "*   *                        *" +
        "*   xxxx      **********     *" +
        "*    ****     **xx   f  **   *" +
        "*     ****     **      **    *" +
        "*      ****     **    **     *" +
        "*    f  ****          **     *" +
        "*        ****       ****     *" +
        "*  *********       ****      *" +
        "*  ***    f       xxxx       *" +
        "*  **            xxxx        *" +
        "*  ***************           *" +
        "*                            *" +
        "******************************";
        break;
      case 9:

        convertFromImage = "******************************" +
        "*           * s*             *" +
        "*      ff  f* s*             *" +
        "*  **********  *  f*     *****" +
        "*    *     f*  *****      *f *" +
        "*    *      *  *    f        *" +
        "*    *  ****f  **            *" +
        "*          *    *****   x*   *" +
        "*  *        *  f*xx     xx   *" +
        "*  *  *******  **xxxx   *x   *" +
        "*  *           xxxxxx   **   *" +
        "*  *           *             *" +
        "*  xxx      *  *             *" +
        "*  xxxx  f  *  *       ***   *" +
        "*   xxxx    *  *    ***   *  *" +
        "*    **** **       *      ** *" +
        "*     ****         *       * *" +
        "*                  *       * *" +
        "*           *     *        * *" +
        "*      *    *    *     ***** *" +
        "*     *     *    *     *     *" +
        "*    *     *     *     ***** *" +
        "*   *******      *         * *" +
        "*    **x**       *     f   * *" +
        "*     ***       *          * *" +
        "* *  f *       *       f   * *" +
        "* **  *  f    *            * *" +
        "*    f             ****x*x**f*" +
        "*   ***              ******* *" +
        "******************************";
         break;
      case 10:
        convertFromImage = "******************************" +
        "*f           *s*f            *" +
        "*       **** *s* *********** *" +
        "*      *   * * * *           *" +
        "**   f**   * * * * ***********" +
        "***    *   * *f  *       f   *" +
        "*****  *   * *************** *" +
        "**  * **   *   *       *   * *" +
        "*   *f*    *** * ***** * * * *" +
        "*     *   **** * *  ** * * * *" +
        "* ***     *xx* *    *  * * * *" +
        "* *  ********* **** * ** * * *" +
        "* *     f   *     * *    *  f*" +
        "* *  ****** ***** * **********" +
        "* *  **   * ***** *          *" +
        "*f**   ** *  **** ********** *" +
        "*  *    ***  ****    *     * *" +
        "*  *f          ** *  * *** * *" +
        "* **   *****    f ** *   * * *" +
        "*f**   *   *      ** *** * * *" +
        "* **   *   *  ******   * *f  *" +
        "*  *f  *   *  *   **  *  *****" +
        "*  *   *   *  * *    *   **  *" +
        "*  **      *  *  *  *    **  *" +
        "*  *       *  *f  **      *  *" +
        "*  ****   *   *        ****  *" +
        "*   **** *    ***********x*  *" +
        "*     ***             f  **  *" +
        "* f                          *" +
        "******************************";
    }
    int y = -1;
    int z;
    for(int x = 0; x < 900; x++)
    {
      char c = convertFromImage.charAt(x);
      z = x / 30;
      if (y == 29) { y = -1; }
      y++;
      //System.out.println("" + z + ", " + y);
      if (c == '*')
      {
        layout[z][y] = Color.DARK_GRAY;  
      }
      else if (c == ' ')
      {
        layout[z][y] = Color.GREEN;
      }
      else if (c == 'f')
      {
        layout[z][y] = Color.BLUE;
      }
      else if (c == 's')
      {
        layout[z][y] = Color.YELLOW;
      }
      else if (c == 'x')
      {
        layout[z][y] = Color.RED;
      }
    }
    
    //Colors of board are correct on x and y axis here
    return layout.clone();
  }
}


perfecto ahora creamos una nueva clase llamada Points dentro de esta clase ponemos el siguiente codigo:


package snake.gui;

import java.awt.Point;

import snake.enums.KeyDirections;

public class Points
{
  /*
   * "thisPartsPoint" = the coordinates for this part of the snake's body
   * "thisPartsDirection" = the direction this part of the snake's body is facing
   */
  Point thisPartsPoint = new Point();
  KeyDirections thisPartsDirection;
  public Points(final Point the_point, final KeyDirections the_direction)
  { 
    thisPartsPoint = the_point;
    thisPartsDirection = the_direction;
  }
  public Point getPoint()
  {
    return thisPartsPoint;   
  }
}

luego de esto creamos una nueva clase llamada SnakeGUI dentro de esta clase ponemos el siguiente codigo:



package snake.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

import snake.enums.KeyDirections;

@SuppressWarnings("serial")
public class SnakeGUI extends JFrame
{
  public static final int FRAME_HEIGHT = 500;
  public static final int FRAME_WIDTH = 455;
  //private int[] my_keys;
  
  private int my_snake_direction;
  private int my_current_level;
  
  private Listener my_listener = new Listener();
  
  private MyTimer my_timer_listener = new MyTimer();
  private Timer my_timer;
  private KeyboardListener my_keyboardlistener = new KeyboardListener();
  
  private final JPanel my_main_panel = new JPanel(new BorderLayout());
 
  private final JMenuBar my_menubar = new JMenuBar();
  
  private final JMenu my_menubar_game = new JMenu("Game");
  private final JMenuItem my_menubar_game_newgame = new JMenuItem("New Game");
  private final JMenuItem my_menubar_game_pause = new JMenuItem("Pause");
  private final JMenuItem my_menubar_game_quit = new JMenuItem("Quit");
  /*******/
  private final JMenu my_menubar_help = new JMenu("Help");
  private final JMenuItem my_menubar_help_rules = new JMenuItem("Rules");
  private final JMenuItem my_menubar_help_about = new JMenuItem("About");

  private SnakePanel my_panel;
  
  private Color[][] board;
  private Level level = new Level();
  
  private List my_snake;
  private List new_snake;

  public SnakeGUI()
  {
    my_current_level = 1;
    my_timer = new Timer(1000, my_timer_listener); 

    my_snake = new ArrayList();
    new_snake = new ArrayList();
    
    my_timer.setDelay(1000);

    newGame();
    setup();
  }
  public void newGame()
  {
    my_timer.start();
    my_snake.clear();
    board = level.getLevel(my_current_level);   //set level at the same time of assigning my_current_level to 1
    my_snake.add(new Point(1, 14));
    my_snake.add(new Point(2, 14));
    my_snake_direction = 40;

  }
  public void setup()
  {
    my_panel = new SnakePanel(board);
    //my_panel.updateSnake(my_snake);    //temp
    my_main_panel.add(my_panel, BorderLayout.CENTER);
    addKeyListener(my_keyboardlistener);
    add(my_main_panel);
    setupMenuBar();
    setJMenuBar(my_menubar);
    setTitle("Snake");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setLocationRelativeTo(null);
    setVisible(true);
    //my_panel.resetCollided();
    my_panel.updateSnake(my_snake);
    my_menubar_game_newgame.addActionListener(my_listener);
    my_menubar_game_pause.addActionListener(my_listener);
    my_menubar_game_quit.addActionListener(my_listener);
    my_menubar_help_rules.addActionListener(my_listener);
    my_menubar_help_about.addActionListener(my_listener);

    
  }
  private void setupMenuBar()
  {
    my_menubar.add(my_menubar_game);
    my_menubar_game.add(my_menubar_game_newgame);
    my_menubar_game.add(my_menubar_game_pause);
    my_menubar_game.addSeparator();
    my_menubar_game.add(my_menubar_game_quit);
    my_menubar.add(my_menubar_help);
    my_menubar_help.add(my_menubar_help_rules);
    my_menubar_help.addSeparator();
    my_menubar_help.add(my_menubar_help_about);
  }
  
  private class KeyboardListener implements KeyListener
  {
    /**
     * The key that was just pressed.
     * @param the_event The event that just happened (used to acquire what key was pressed).
     */
    public void keyPressed(final KeyEvent the_event)
    {
      final int key = the_event.getKeyCode();
      if (my_timer.isRunning())
      {
        if (key == 38 && my_snake_direction != 40)
 
        {
          my_snake_direction = key;
          moveSnake(KeyDirections.UP.getDirection());
        }
        else if (key == 40 && my_snake_direction != 38)
        {
          my_snake_direction = key;
          moveSnake(KeyDirections.DOWN.getDirection());
          //System.out.println("DOWN pressed");
        }
        else if (key == 37 && my_snake_direction != 39)
        {
          my_snake_direction = key;
          moveSnake(KeyDirections.LEFT.getDirection());
          //System.out.println("LEFT pressed");
        }
        else if (key == 39 && my_snake_direction != 37)
        {
          my_snake_direction = key;
          moveSnake(KeyDirections.RIGHT.getDirection());
          //System.out.println("RIGHT pressed");
        }
      }
    }
      /**
       * The key that was just released.
       * @param the_event The event that just happened (used to acquire what key was released). 
       */
    public void keyReleased(final KeyEvent the_event)
    {
    }
    /**
     * The key that was just typed.
     * @param the_event The event that just happened (used to acquire what key was typed).
     */
    public void keyTyped(final KeyEvent the_event)
    {
    }
  }

  private void moveSnake(int direction)
  {
    //System.out.println(my_timer.getDelay());
    boolean skipit = true;
    new_snake.clear();
    my_panel.clearOldSnake(my_snake);
    
    //System.out.println(direction + "");
    //If the desired direction is UP and the snake's not going down (can't go down to up, must go left or right first)
    if (direction == 38)
    {
      //System.out.println("Going UP");
      Point newPoint = new Point(my_snake.get(my_snake.size() - 1).x - 1,
          my_snake.get(my_snake.size() - 1).y);
      my_snake.add(newPoint);
      //my_snake.remove(0);
    }
    //Want to go DOWN, but can't if going UP
    else if (direction == 40)
    {
      //System.out.println("Going DOWN");
      Point newPoint = new Point(my_snake.get(my_snake.size() - 1).x + 1,
                                 my_snake.get(my_snake.size() - 1).y);
      my_snake.add(newPoint);
      //my_snake.remove(0);
    }
    //Want to go LEFT, but can't if going RIGHT
    else if (direction == 37)
    {
      //System.out.println("Going LEFT");
      Point newPoint = new Point(my_snake.get(my_snake.size() - 1).x,
                                 my_snake.get(my_snake.size() - 1).y - 1);
      my_snake.add(newPoint);
      //my_snake.remove(0);
    }
    //Want to go RIGHT, but can't if going LEFT
    else if (direction == 39)
    {
      //System.out.println("Going RIGHT");
      Point newPoint = new Point(my_snake.get(my_snake.size() - 1).x,
                                 my_snake.get(my_snake.size() - 1).y + 1);
      my_snake.add(newPoint);
      //my_snake.remove(0);
    }
    else
    {
      skipit = false;
    }
    if (skipit)
    {
      if (my_panel.growMode())
      {
        if(my_panel.checkForLevelCompletion())
        {
          my_current_level++;
          if (my_current_level == 11)
          {
            JOptionPane.showMessageDialog(null, "Good job.  You beat the game.");
            my_current_level = 1;
          }
          
          my_timer.setDelay(my_timer.getDelay() - 80);
          
          newGame();
        }
       }
      else
      {
        my_snake.remove(0);
          
      }
      my_panel.updateSnake(my_snake);
      
      if (my_panel.isCollided())
      {
        //System.out.println("COLLIDED");
        gameOver();
      }
    }
  }
  private class MyTimer implements ActionListener
  {
    public void actionPerformed(final ActionEvent the_event)
    {
      moveSnake(my_snake_direction);
    }
  }
 
  private void gameOver()
  {
    my_timer.stop();
    //System.out.println("Game over");

    newGame();
    my_panel.resetCollided();    
  }
  
  private class Listener implements ActionListener
  {
    public void actionPerformed(final ActionEvent the_event)
    {
      final String the_action = the_event.getActionCommand();
 
      if (the_action.equals(my_menubar_game_newgame.getActionCommand()))
      {
        my_timer.setDelay(1000);

        my_current_level = 1;
        newGame();
      }
      else if (the_action.equals(my_menubar_game_quit.getActionCommand()))
      {
        System.exit(0);
      }
      else if (the_action.equals(my_menubar_help_rules.getActionCommand()))
      {
        JOptionPane.showMessageDialog(null,
            "Eric Sweeten\nSnake v1.0\n\n" + 
            "Keep the snake on the green.  Gather up all the food\n" +
            "(blue squares), and once all the food is gathered, you\n" +
            "pass the level.  There are 10 levels total.  Once you\n" + 
            "pass the 10th level, you beat the game.");
      }
      else if (the_action.equals(my_menubar_help_about.getActionCommand()))
      {
        JOptionPane.showMessageDialog(null, 
        "Eric Sweeten\nSnake v1.0\n\neric.sweeten@gmail.com");
      }
      else if (the_action.equals(my_menubar_game_pause.getActionCommand()))
      {
        togglePause();
      }
    }
  }
  private void togglePause()
  {
    if (my_timer.isRunning())
    {
      my_timer.stop();
    }
    else
    {
      my_timer.start();
    }
  }
}


perfecto luego de esto creamos una nueva clase llamada SnakePanel dentro de esta clase ponemos el siguiente codigo:



package snake.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class SnakePanel extends JPanel
{
  Color board[][] = new Color[30][30];
  private Rectangle2D[][] my_board = new Rectangle2D[30][30];
  private boolean collided;
  private boolean growMode;
  int growingThreeSpaces;
  int spacesToGrowTotal;
  int beginning;
  
  public SnakePanel(Color[][] gameBoard)
  {
    collided = false;
    growMode = false;
    beginning = 0;
    board = gameBoard;
    
    for (int x = 0; x < 30; x++)
    {
      for (int y = 0; y < 30; y++)
      {
        my_board[x][y] =
          new Rectangle2D.Double(y * 15, x * 15, 14, 14);      //Quick fix
      }
    }   //board is correct here
  }
  
  public void paintComponent(final Graphics the_graphics)
  {
    super.paintComponent(the_graphics);
    //Rectangle2D my_square;
    final Graphics2D g2d = (Graphics2D) the_graphics;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (int x = 0; x < 30; x++)
    {
      for (int y = 0; y < 30; y++)
      {
        g2d.setColor(board[x][y]);
        g2d.fill(my_board[x][y]);
        g2d.draw(my_board[x][y]);
      }
    }  
  }
  public void clearOldSnake(List points)
  {
    //System.out.println("clearOldSnake(List points (points.size() = " + points.size() + ")");
    for (int x = 0; x < points.size(); x++)
    {
      board[points.get(x).x][points.get(x).y] = Color.GREEN;
    }
  }
  public boolean growMode()
  {
    return growMode;
  }
  /*
   * This updates the position of the snake by points (positions) on the map level
   */
  public void updateSnake(List points)
  {
    if (beginning < 2) { beginning++; }
    if (growMode)
    {
      growingThreeSpaces++;
      if (growingThreeSpaces == spacesToGrowTotal)
      {
        resetGrowMode();
      }
    }

    for (int x = 0; x < points.size(); x++)
    {
      //System.out.println("Coords: " + board[points.get(x).x][points.get(x).y]);
      if (board[points.get(x).x][points.get(x).y] == Color.BLUE)
      {
        //System.out.println("COLLIDED WITH SOMETHING THAT MAKES SNAKE GROW");
        spacesToGrowTotal += 3;
        growMode = true;

      }
      
      else if (board[points.get(x).x][points.get(x).y] != Color.GREEN &&
               board[points.get(x).x][points.get(x).y] != Color.BLUE)
      {
        if (beginning > 1) { collided = true; }
      }

      board[points.get(x).x][points.get(x).y] = Color.YELLOW;
      //System.out.println("Coloring the snake yellow");
      //System.out.println(points.get(x).x + ", " + points.get(x).y);
    }
    repaint();
  }
  public boolean isCollided()
  {
    return collided;
  }
  public void resetCollided()
  {
    collided = false;
  }
  public void resetGrowMode()
  {
    spacesToGrowTotal = 0;
    growingThreeSpaces = 0;
    growMode = false;
  }
  public boolean checkForLevelCompletion()
  {
    boolean complete = true;
    for (int x = 0; x < 30; x++)
    {
      for (int y = 0; y < 30; y++)
      {
        if (board[x][y] == Color.BLUE)
        {
          complete = false;
        }
      }
    }
    return complete;
  }
}

ahora solo queda llamarlo como lo llamamos facil yo creo una nueva clase en el pakete snake llamada
SnakeMain y dentro de esta clase pongo el siguiente codigo:


package snake;

import snake.gui.SnakeGUI;

public class SnakeMain
{
/**
 * @param args
 */
  public static void main(String[] args)
  {
    SnakeGUI gui = new SnakeGUI();
    gui.newGame();
  }
}

si todo va bien quedara en netbeans como la siguiente imagen:


perfecto lo ejecutamos y a jugar :)




4 comentarios:

 
;