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;
}
}
}
}