在製作蛇遊戲的過程中,無法弄清楚如何使動作正確 (In process of making snake game, Can't figure out how to make the movement right)


問題描述

在製作蛇遊戲的過程中,無法弄清楚如何使動作正確 (In process of making snake game, Can't figure out how to make the movement right)

我目前在 jgrasp 中製作了一個蛇遊戲,只使用標準的繪製運動和靜態方法來創建這個遊戲,到目前為止,這是我擁有的代碼,因為我昨天才開始。但我目前被困在如何使蛇運動,所以它只能上下左右,並且運動是恆定的。目前我所擁有的只是箭頭鍵移動蛇,但它仍然可以對角移動並且不是恆定的

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:

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.

(by Reed SagerRyan)

參考文件

  1. In process of making snake game, Can't figure out how to make the movement right (CC BY‑SA 2.5/3.0/4.0)

#java #stdin #jgrasp






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論