0

Im currently making a snake game in jgrasp only using the standard draw movement and static methods to create this game, So far this is the code I have because i just started yesterday. But I'm currently stuck on how to make the snakes movements so it can only be up and down and side to side and for the movement to be constant. At the moment all i have is that the arrow keys move the snake but it can still move diagonally and isn't constant

import java.awt.event.KeyEvent;
public class snake
{
   static double squareX = .5;
   static double squareY = .5;
   static double squareR = .02;



   public static void drawScene() 
   {
      StdDraw.clear();
      StdDraw.filledSquare(squareX, squareY, squareR);
      StdDraw.show(1000/24);

   }

   public static void updateMotion()
   {

      if (StdDraw.isKeyPressed(KeyEvent.VK_UP))
      {
         squareY += .01;
      }
      if (StdDraw.isKeyPressed(KeyEvent.VK_DOWN))
      {
         squareY -= .01;
      }
      if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT))
      {
         squareX -= .01;
      }
      if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT))
      {
         squareX += .01;

      }
   }



   public static void main(String[] args)
   {
       while(true)
       {
         snake.drawScene();
         snake.updateMotion();
         if (squareX + squareR >= 1 )
         {
            //TODO: show "you lose" message / stop on edge of square
            break;
         }
         if (squareX - squareR <= 0)
         {
            //TODO: show "you win" message / stop on edge of square
            break;
         }   
         if (squareY + squareR >= 1 )
         {
            //TODO: show "you lose" message / stop on edge of square
            break;
         }
         if (squareY - squareR <= 0)
         {
            //TODO: show "you win" message / stop on edge of square
            break;
         }   


       }
   }
}

1 Answer 1

0

In this method, changing your if statements to else ifs should fix your issue.

  if (StdDraw.isKeyPressed(KeyEvent.VK_UP)){
     squareY += .01;
  }else if (StdDraw.isKeyPressed(KeyEvent.VK_DOWN)){
     squareY -= .01;
  }else if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT)){
     squareX -= .01;
  }else if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT)){
     squareX += .01;
  }

What seemed to be your problem from what you described was the snake was traveling diagonally. This would occur from your program taking in a vertical and horizontal input. This segment of code will now only take in one direction per frame making it so the snake can only travel up, down, left, and right.

To make your snakes movement speed constant you need to limit the frames per sec. You can do this with the simplest Thread.sleep(10); call in each of your while loops or a swing timer. Your issue isn't your movement speed, its the fluctuating frame rate.

public static void main(String[] args) throws InterruptedException
{
    while(true)
    {
     snake.drawScene();
     snake.updateMotion();
     if (squareX + squareR >= 1 )
     {
        //TODO: show "you lose" message / stop on edge of square
        break;
     }
     if (squareX - squareR <= 0)
     {
        //TODO: show "you win" message / stop on edge of square
        break;
     }   
     if (squareY + squareR >= 1 )
     {
        //TODO: show "you lose" message / stop on edge of square
        break;
     }
     if (squareY - squareR <= 0)
     {
        //TODO: show "you win" message / stop on edge of square
        break;
     }   
     Thread.sleep(20);
   }
}

adding a line to limit your frames like this will make your snake move at a constant speed. As a note, there are different ways as apposed to using Thread.sleep() like swing timers that you can look into, but this should fix your general problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that fixed my problem on the diagonal problem, The next one is the constant problem, I tried to put while(true) inside the else if thinking that would make it constant but nothing happened.
Tried that and still nothing happened, does it have to do with the fact i'm using std draw window to do all of this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.