1
\$\begingroup\$

I have a snake game set up in Java and I wanted to be able to toggle between the ability to go through the sides and pop out the opposite side and the classic version where you die when you hit the border. The problem is that my snake keeps going one spot after the border; how do I fix this?

This is one of the parts of my code, that acts when the right arrow key is pressed:

if(right) {
    for(int i = lengthofsnake - 1; i >= 0; i--) {
        snakeylength[i+1] = snakeylength[i];
    }

    for(int i = lengthofsnake; i >= 0; i--) {
        if(i == 0) {
            snakexlength[i] = snakexlength[i] + 25;
        }
        else {
            snakexlength[i] = snakexlength[i-1];
        }

        if(!borderGameplay) {
            if(snakexlength[i] > 850) {
                snakexlength[i] = 25;
            }
        }
        else {
            if(snakexlength[i] > 850) {
                borderDead = true;
            }
        }
    }

    repaint();
}

borderGameplay is the variable that you can toggle to have borders on and off, and borderDead is used in this if statement:

if(borderDead) {
    if(score > highScore) {
        highScore = score;
    }
    playing = false;
    dead();
    deadGraphic(g);
}

This is my deadGraphic(g): public void deadGraphic(Graphics g) { g.setColor(Color.WHITE); g.setFont(new Font("arial", Font.BOLD, 50)); g.drawString("Game Over", 300, 300); g.setFont(new Font("arial", Font.BOLD, 20)); g.drawString("Press \"Space\" To Restart And \"Enter\" To Toggle Borders", 160, 340);

    if(borderGameplay) {
        g.drawString("Borders ON", 370, 380);
    } 
    else {
        g.drawString("Borders OFF", 365, 380);
    }
 }

This is my dead():

public void dead() {
    right = false;
    left = false;
    up = false;
    down = false;
    dead = true;
    score = 0;
    moves = 0;
}

And finally, my JFrame:

JFrame obj = new JFrame("Snake");
gameplay Gameplay = new gameplay(); 
obj.setBounds(10, 10, 905, 700);
obj.setBackground(Color.DARK_GRAY);
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(Gameplay);
obj.setVisible(true);

If you need to see other parts of my code, please tell me; this question is already really long, and my total code is 550 lines.

\$\endgroup\$
2
  • \$\begingroup\$ Try changing snakexlength[i] > 850 to >= instead. \$\endgroup\$
    – Charanor
    Oct 8 '17 at 15:58
  • \$\begingroup\$ Already tried that, the problem is that they will die when they are beside the barrier and I want them to only die if they go into the barrier \$\endgroup\$
    – Eric Leus
    Oct 9 '17 at 16:17
1
\$\begingroup\$

I understand your problem to be that the snake, before it dies, can go one block into the boundary, or wall.

From the looks of it, your right boundary is at 850px on the X axis, and spans the Y axis indefinitely, giving us a nice wall.

Well, your snake travels 25 pixels per frame per second (25px/FPS); if this is wrong, I apologize, do correct me as this is what I was able to understand from your code.

When the snake reaches 850px, the IF statement does not equal true, because your operator is set as greater than. It's only that once your snake reaches 875px (after moving 25px at a time) that the IF statement is true and the snake dies while in the boundary.

Here's my suggested solution: make sure the user isn't already at the pre-defined barrier before you move them right anymore. This way, before they get the chance to move inside the of the barrier, they die instead.

\$\endgroup\$
1
  • \$\begingroup\$ Glad to hear it! No problem man \$\endgroup\$
    – Alpha
    Oct 9 '17 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .