nickwl23 0 Newbie Poster

Hi experts,

I have done a very simple snake game but there are some error..
I would like to make it continue with 3 lifes before game over but I fail.
Did any1 can help??

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.Random;

public class Main extends javax.swing.JApplet
{
    private char map[][];
    private Timer timer_snake;
    private String map_string[] = new String[25];
    private String map_matrix = "";
    private clsSnake Snake = new clsSnake();

    private int Snake_PositionX [] = new int[300];
    private int Snake_PositionY [] = new int[300];
    private int t_delay;    // 200ms per timer expire event
    private int point, life;

    private boolean GameOver = false;
    public Graphics G;
    private AudioClip starter, normal, expert, lose;
    //private AudioClip normal,nightmare,hell,lose;

    /** Initializes the applet MainApplet */
     public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {                 
                    initComponents();

                    button_Start.grabFocus();
                    expert = getAudioClip(getCodeBase(),"Stage_1.au");
                    normal = getAudioClip(getCodeBase(),"Stage_2.au");
                    starter = getAudioClip(getCodeBase(),"Stage_4.au");
                    lose = getAudioClip(getCodeBase(),"Life_Lost.au");
                    map = new char[25][50];

                    int x,y;
                    for (x=0; x<50; x++)
                    {
                        for (y=0; y<25; y++)
                        {
                            map[y][x] = ' ';
                        }
                    }
                    y = 0;
                    for (x=0; x<50; x++)
                    {
                        map[y][x] = '#';
                    }
                    y = 24;
                    for (x=0; x<50; x++)
                    {
                        map[y][x] = '#';
                    }
                    x = 0;
                    for (y=0; y<25; y++)
                    {
                        map[y][x] = '#';
                    }
                    x = 49;
                    for (y=0; y<25; y++)
                    {
                        map[y][x] = '#';
                    }


                    timer_snake = new Timer(t_delay, new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {

                           map_matrix = "";
                           char next = ' ';
                           repaint();

                           //direction
                           switch (Snake.getDirection())
                           {
                               case 1:  next = map [Snake_PositionY [0]] [Snake_PositionX [0] - 1];
                                        break;
                               case 2:  next = map [Snake_PositionY [0] + 1] [Snake_PositionX [0]];
                                        break;
                               case 3:  next = map [Snake_PositionY [0]] [Snake_PositionX [0] + 1];
                                        break;
                               case 5:  next = map [Snake_PositionY [0] - 1] [Snake_PositionX [0]];
                                        break;
                           }


                            if (next == '#' || next == '*') {
                                GameOver = true;
                            } else if (next == ' ') {
                                map [Snake_PositionY [Snake.getLength()]] [Snake_PositionX [Snake.getLength()]] = ' ';
                                Snake.Move();
                            } else if (next == '$') {
                                Snake.setLength(Snake.length + 1);
                                point += 10;
                                label_GameScore.setText(Integer.toString(point));
                                Snake.Move();

                                int NewApple_X;
                                int NewApple_Y;
                                do{
                                    NewApple_X = random(49);
                                    NewApple_Y = random(24);
                                }
                                while (map [NewApple_Y][NewApple_X] == '*' || map [NewApple_Y][NewApple_X] == '#');
                                map [NewApple_Y][NewApple_X] = '$';
                            }


                           //crash the wall-- game over
                           if (GameOver) {
                                label_GameLife.setText(Integer.toString(2-life));
                                //life++;
                                //if (life==4){

                                JOptionPane.showConfirmDialog(null, "GG.com.. Game Over!!!" + "\nYour Score: " +
                                Integer.toString(point), "Game Over",JOptionPane.DEFAULT_OPTION);
                                timer_snake.stop();
                                starter.stop();
                                normal.stop();
                                expert.stop();
                                lose.play();

                                //}else{

                                map = new char[25][50];

                    //reconstruc the map
                    int x, y;
                    for (x=0; x<50; x++)
                    {
                        for (y=0; y<25; y++)
                        {
                            map[y][x] = ' ';
                        }
                    }
                    y = 0;
                    for (x=0; x<50; x++)
                    {
                        map[y][x] = '#';
                    }
                    y = 24;
                    for (x=0; x<50; x++)
                    {
                        map[y][x] = '#';
                    }
                    x = 0;
                    for (y=0; y<25; y++)
                    {
                        map[y][x] = '#';
                    }
                    x = 49;
                    for (y=0; y<25; y++)
                    {
                        map[y][x] = '#';
                    }


                                }  //}

                                 for (int z = 0; z<25; z++)
                                {
                                // Converting one line at a time, 25 horizontal
                                // lines in total
                                map_string[z] = String.valueOf(map[z]);
                                map_matrix += map_string[z] + "\n";
                                }
                                jta_map.setText(map_matrix);
                                jta_map.setFont(new Font ("Courier New", Font.PLAIN, 12));




                    }

                    });
                    }




            });

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")

    private void button_StartActionPerformed(java.awt.event.ActionEvent evt) {                                             

        int Apple_X;
        int Apple_Y;

        timer_snake.start();
        Snake_PositionY[0] = 5;
        Snake_PositionX[0] = 10;
        map [Snake_PositionY[0]] [Snake_PositionX[0]] = '*';

        //replace the apple
        do{
            Apple_X = random(49);
            Apple_Y = random(24);
        }while ( (map [Apple_Y][Apple_X] == '*' || map [Apple_Y][Apple_X] == '#'));
        map [Apple_Y][Apple_X] = '$';

        if (combobox_level.getSelectedItem().toString().trim() == "Starter") {
            timer_snake.setDelay(400);
            starter.loop();
        } else if (combobox_level.getSelectedItem().toString().trim() == "Normal") {
            timer_snake.setDelay(200);
            normal.loop();
        } else if (combobox_level.getSelectedItem().toString().trim() == "Expert") {
            timer_snake.setDelay(50);
            expert.loop();
        }
        jta_map.grabFocus();
}                                            

    private void jta_mapKeyPressed(java.awt.event.KeyEvent evt) {                                   
        //set direction
        if (evt.getKeyCode() == 37)
            Snake.setDirection(1);
        else if (evt.getKeyCode() == 38)
            Snake.setDirection(5);
        else if (evt.getKeyCode() == 39)
            Snake.setDirection(3);
        else if (evt.getKeyCode() == 40)
            Snake.setDirection(2);
}                                  

class clsSnake
    {
    private int length;
    private int direction;

    public clsSnake()
    {
        length = 0;
        direction = 0;
    }

    // This is the GETter method for length
    public int getLength()
    {
        return length;
    }

    // This is the SETter method for length
    public void setLength(int length)
    {
        this.length = length;
        // The object's length is set to the length variable that is passed in
    }

    public int getDirection()
    {
        return this.direction;
    }

    public void setDirection(int direction)
    {
        // We perform a check before we change the snake's direction
        // A setter method allows you to perform checks on attributes of snake object

        if (this.direction == 1 && direction == 3)
        {
            direction = 1;
        } else if (this.direction == 3 && direction == 1)
        {
            direction = 3;
        } else if (this.direction == 2 && direction == 5)
        {
            direction = 2;
        } else if (this.direction == 5 && direction == 2)
        {
            direction = 5;
        }
        this.direction = direction;
     }

    public void Move(){
         for (int j=0; j<length; j++) {


              Snake_PositionY  [length-j] = Snake_PositionY [length-j-1];
              Snake_PositionX [length-j] = Snake_PositionX [length-j-1];
         }

         switch (direction){
             case 1: Snake_PositionX [0] -= 1;
             break;
             case 2: Snake_PositionY [0] += 1;
             break;
             case 3: Snake_PositionX [0] += 1;
             break;
             case 5: Snake_PositionY [0] -= 1;
             break;
         }
         map [Snake_PositionY [0]] [Snake_PositionX [0]] = '*';
    }
    }

    int random (int n) {
        Random RD = new Random();

        if (RD.nextInt(n)<1){
            RD.nextInt(n);
        }
        return RD.nextInt(n);
    }


    // Variables declaration - do not modify
    private javax.swing.JButton button_Start;
    private javax.swing.JComboBox combobox_level;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jta_map;
    private javax.swing.JLabel label_GameLife;
    private javax.swing.JLabel label_GameScore;
    private javax.swing.JLabel label_life;
    private javax.swing.JLabel label_score;
    // End of variables declaration

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.