Search code examples
javatimerdelay

Everything in my game starts going double speed when I start a new game. Can't stop the timer


I am doing a Snake clone and everything goes fine before I start a new game. Everything works then too but everything is also double speed! I think this is because there are two timers working same time doing same functions? I don't find a way how to deal with this. This is what my timer looks like and as you can see, it is quite messy :/ Don't even try to figure out it all. My snake consists balls and most of the code tracks their movement.

public class Draw extends JPanel implements KeyListener{

static ArrayList x = new ArrayList(); //the x coordinates of balls
static ArrayList y = new ArrayList(); //the y coordinates of balls
static int x_val=15; // movement x-axis
static int y_val=0; // movement y-axis
static boolean ballCoordinatesDone = false; //initialize balls
static int score = 0; 
static boolean snakeCrashed = false; //if snake has crashed (stop)
static boolean highscoresOpened = false; //if highscores are opened already (don't repeat)

static int foodx = 0; //food x coordinate
static int foody = 0; //food y coordinate


Timer  t = new Timer(150, new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(snakeCrashed == false){
        if(ballCoordinatesDone == false){
        ballCoordinates();
        }
        ballCoordinatesDone = true;

        int lastX = Integer.parseInt((String) x.get(x.size()-1)); //gets x coodinate for first ball
        int lastY = Integer.parseInt((String) y.get(y.size()-1)); //gets y coodinate for first ball
        int a;

//checks if snake collides with itself
        for(a=0; a < x.size(); a++){

            if(lastX + x_val == Integer.parseInt((String) x.get(a)) && lastY + y_val == Integer.parseInt((String) y.get(a))){

                snakeCrashed = true;

            }
        }

//checks if snake goes out of bounds
        if(lastX + x_val < 5  || lastY + y_val < 50 || lastX + x_val >= 310  || lastY + y_val >= 355  ){

            snakeCrashed = true;

        }else{

//adds a new ball in direction of movement (new head)
        x.add("" + (lastX + x_val));
        y.add("" + (lastY + y_val));

        }

//checks if you hit a food
        if(lastX + x_val == foodx && lastY + y_val == foody){

            foodRandomizer();
            score+=10;

        }else{
 //removes the tail     
            if(snakeCrashed == false){
            x.remove(0);
            y.remove(0);
            }

        }

        repaint();

//if snakeCrashed == true   
        }else{
            if(highscoresOpened == false){ 
            CreateGUI.highscoreFrame(); 
            highscoresOpened = true;
            t.stop();


            }
        }
    }

});

Then the CreateGUI.highscoreFrame has a button called newGame that looks like this:

JButton newGame = new JButton("New Game");
    newGame.setBounds(75,300,100,40);
    newGame.setVisible(true);
    newGame.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            hsFrame.setVisible(false);
            hsFrame.dispose();
            submitHSFrame.setVisible(false);
            submitHSFrame.dispose();
            snake.setVisible(false);
            snake.dispose();
            Draw.snakeCrashed = false;
            Draw.highscoresOpened = false;
            Draw.x.clear();
            Draw.y.clear();
            Draw.score = 0;
            Draw.x_val=15;
            Draw.y_val=0;
            Draw.ballCoordinatesDone = false;
            Draw.score = 0;

            CreateGUI ng = new CreateGUI();

        }
    }); 

    hsFrame.add(newGame);

Solution

  • Every Draw object creates a new timer so you have the game running n times at the same time but the copies of the game share the state because ir is stored in static variables so the state is updated n-1 times faster than it should.

    You may stop the timer this way or you may use a static timer and schedule/cancel the task each time.