简体   繁体   English

很难弄清楚如何不断移动我的蛇

[英]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!所以我正在创建一个名为蛇的 Java 游戏,我目前在如何让蛇的起点在最初移动后不断移动,我正在尝试使用 thread.sleep 但我很用我'我在错误的地方使用它,任何提示/帮助将不胜感激!

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();
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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