問題描述
在製作蛇遊戲的過程中,無法弄清楚如何使動作正確 (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 Sager、Ryan)