简体   繁体   中英

Having a hard time on figuring out how to move my snake constantly

So I'm creating a java game called snake, I'm currently stuck on how to get the start of the snake to constantly moved after it is initially moved, Im trying to use the thread.sleep but I'm pretty use I'm using it in the wrong place any tips/help would be appreciated!

import java.awt.event.KeyEvent;
public class snake
{
   static double squareX = .5;
   static double squareY = .5;
   static double squareR = .02;
   static double CsquareR = .01;
   static double CsquareX = Math.random();
   static double CsquareY = Math.random();




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




   public static void updateMotion() throws InterruptedException
   {

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



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

Instead of changing the position while the keys are pressed, change the direction and update the position all the time.

   static double xMovement = 0;
   static double yMovement = 0;

   public static void updatePosition() {
       squareX += xMovement;
       squareY += yMovement;
   }

   public static void updateDirection() throws InterruptedException
   {

      if (StdDraw.isKeyPressed(KeyEvent.VK_UP))
      {
          xMovement =  0.0;
          yMovement = +0.1;
      }
      else if (StdDraw.isKeyPressed(KeyEvent.VK_DOWN))
      {
          xMovement =  0.0;
          yMovement = -0.1;
      }
      else if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT))
      {
          xMovement = -0.1;
          yMovement =  0.0;
      }
      else if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT))
      {
          xMovement = +0.1;
          yMovement =  0.0;
      }
   }

   public static void main(String[] args)  throws InterruptedException 
   {
       while(true)
       {
         snake.drawScene();
         snake.updateDirection();
         snake.updatePosition();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM