gpt4 book ai didi

Java:蛇 - 改变方向

转载 作者:行者123 更新时间:2023-12-01 10:36:25 25 4
gpt4 key购买 nike

我是编程新手,并且卡在涉及蛇方向改变的特定部分上。该游戏与旧诺基亚手机上的游戏相同。很经典。

目前,每次我按 W、A、S 或 D 键,蛇都会移动 1 平方/20 像素。问题是我希望这个 Action 是连续的并对按下的其他键发出警报。例如,当我按“S”键时,蛇会向下移动(直到撞到墙壁),除非我按 W、A 或 D。在这种情况下,蛇应将其运动更改为相应的方向。我有一个小小的想法,我将不得不使用一个循环来继续移动蛇(我认为这个循环将位于方法的一个分支(if/else)内:运动)。

这是我当前开发的源代码。该代码在 Java 的“Ready to Program”上运行没有问题。如果您不写“System.out.println();”,而是写“c.println()”,这也会很有帮助(但不是必需)。这是因为我使用的是 C 控制台。

我已经导入了 KeyAdapter 和 KeyEvent。这一切都在一个名为 SnakeGame 的公共(public)类中。当我早些时候试图弄清楚这一点时, keyPressed 就出现了。我相信它仍然有用。

// STORAGE ---- Setting up the variables
public static int life = 1; // Sets how many lives the snake has: 1
public static int appleX; // The X coordinate of the apple
public static int appleY; // The Y coordinate of the apple
public static int[] jointPosX = new int[400]; // X coordinates of the snake's joints. Stores as an array
public static int[] jointPosY = new int[400]; // Y coordinates of the snake's joints. Stores as an array
public static int jointNum = 3; // How many joints the snake starts out with: 3
public static int key;
public static boolean goLeft;
public static boolean goRight;
public static boolean goUp;
public static boolean goDown;
public static char moveInput; // The character (w, a, s, d) pressed for direction of movement

public static void board() // The game board
{
c.setColor(Color.black);
c.drawRect(19, 19, 401, 401); // These 3 lines make the border thick
c.drawRect(18, 18, 403, 403);
c.drawRect(17, 17, 405, 405);
c.setColor(new Color(72, 232, 75)); // Color: Medium - Dark Green
c.fillRect(20, 20, 400, 400); // The actual area where the snake moves

c.setColor(Color.black);
for (int y = 20; y < 420; y+=20) // Moving the position of the square across the Y axis
{
for (int x = 20; x < 420; x+=20) // Moving the position of the square across the X axis
{
c.drawRect(x, y, 20, 20); // Drawing the individual squares of the grid
}
}
}
public static void apple() // Where the apple will spawn
{
Random generator = new Random(); // Setting up the randum number generator

appleX = generator.nextInt(380 - 0 + 1) + 1; // X coordinate for apple (0 - 380)
if (appleX % 20 == 0) // Divisible perfectly by 20. Each square is 20 pixels wide
{
appleX = appleX; // The number is perfect
}
else
{
int rem = appleX % 20; // The remainder is assigned a variable
appleX = appleX - rem; // The X coordinate is now divisible by 20 (Subtracts the remainder from the indivisible X coordinate)
}
appleX = appleX + 21; // Adds 21 since board starts 21 pixels to the right

appleY = generator.nextInt(380 - 0 + 1) + 1; // Y coordinate for apple (0 - 380)
if (appleY % 20 == 0) // Divisible perfectly by 20. Each square is 20 pixels tall
{
appleY = appleY; // The number is perfect
}
else
{
int rem = appleY % 20; // The remainder is assigned a variable
appleY = appleY - rem; // The Y coordinate is now divisible by 20 (Subtracts the remainder from the indivisible Y coordinate)
}
appleY = appleY + 21; // Adds 21 since board starts 21 pixels downwards
}
public void keyPressed(KeyEvent e)
{
key = e.getKeyCode();

if (key == KeyEvent.VK_W)
{
goLeft = false;
goRight = false;
goUp = true;
goDown = false;
}
if (key == KeyEvent.VK_A)
{
goLeft = true;
goRight = false;
goUp = false;
goDown = false;
}
if (key == KeyEvent.VK_S)
{
goLeft = false;
goRight = false;
goUp = false;
goDown = true;
}
if (key == KeyEvent.VK_D)
{
goLeft = false;
goRight = true;
goUp = false;
goDown = false;
}
}
public static void selfDeath() // If the snake hits itself
{
for (int i = jointNum - 1; i > 0; i--) // Variable i stands for index number
{
if (jointPosX[0] == jointPosX[i] && jointPosY[0] == jointPosY[i]) // Checks if X and Y coordinates of head of snake matches any other part of snake
{
life--; // Loses a life
}
}
}
public static void wallDeath() // If the snake hits the wall
{
if (jointPosX[0] < 20 || jointPosX[0] > 420 || jointPosY[0] < 20 || jointPosY[0] > 420) // Checks if X and Y coordinates of head of snake is outside border coordinates
{
life--; // Loses a life
}
}
public static void movement() // How the snake moves
{
c.setColor(new Color(23, 156, 26)); // Color: Dark green
c.fillRect(jointPosX[0], jointPosY[0], 19, 19); // Initial point of snake

while(life == 1) {
moveInput = c.getChar(); // Getting W, A, S, or D from user
board(); // Redrawing the board
c.setColor(Color.red); // Color: Red
c.fillOval(appleX, appleY, 19, 19); // Drawing the apple
if (moveInput == 'w')
{
for (int i = jointNum - 1; i > 0; i--) { // Variable i stands for index number
jointPosY[i] = jointPosY[i - 1]; // Changing the Y coordinates of the joints to follow
jointPosX[i] = jointPosX[i - 1]; // Changing the X ooordinates of the joints to follow
}

jointPosY[0] = jointPosY[0] - 20; // Changing the vertical position of snake head (User pressed W)

c.setColor(new Color(23, 156, 26));
for (int z = 0; z < jointNum; z++) { // Moving along the index numbers
c.fillRect(jointPosX[z], jointPosY[z], 19, 19); // Drawing each joint
}
}
if (moveInput == 'a')
{
for (int i = jointNum - 1; i > 0; i--) { // Variable i stands for index number
jointPosX[i] = jointPosX[i - 1]; // Changing the X ooordinates of the joints to follow
jointPosY[i] = jointPosY[i - 1]; // Changing the Y coordinates of the joints to follow
}

jointPosX[0] = jointPosX[0] - 20; // Changing the horizontal position of snake head (User pressed S)

c.setColor(new Color(23, 156, 26));
for (int z = 0; z < jointNum; z++) { // Moving along the index numbers
c.fillRect(jointPosX[z], jointPosY[z], 19, 19); // Drawing each joint
}
}
if (moveInput == 's')
{
for (int i = jointNum - 1; i > 0; i--) { // Variable i stands for index number
jointPosY[i] = jointPosY[i - 1]; // Changing the Y coordinates of the joints to follow
jointPosX[i] = jointPosX[i - 1]; // Changing the X ooordinates of the joints to follow
}

jointPosY[0] = jointPosY[0] + 20; // Changing the vertical position of snake head (User pressed A)

c.setColor(new Color(23, 156, 26));
for (int z = 0; z < jointNum; z++) { // Moving along the index numbers
c.fillRect(jointPosX[z], jointPosY[z], 19, 19); // Drawing each joint
}
}
if (moveInput == 'd')
{
for (int i = jointNum - 1; i > 0; i--) { // Variable i stands for index number
jointPosX[i] = jointPosX[i - 1]; // Changing the X ooordinates of the joints to follow
jointPosY[i] = jointPosY[i - 1]; // Changing the Y coordinates of the joints to follow
}

jointPosX[0] = jointPosX[0] + 20; // Changing the horizontal position of snake head (User pressed D)

c.setColor(new Color(23, 156, 26));
for (int z = 0; z < jointNum; z++) { // Moving along the index numbers
c.fillRect(jointPosX[z], jointPosY[z], 19, 19); // Drawing each joint
}
}
addJoint();
selfDeath();
wallDeath();
}
}
public static void addJoint() // Checks to add a joint to the snake
{
if (jointPosX[0] == appleX && jointPosY[0] == appleY) // If X and Y coordinates of head of snake match X and Y coordinates of apple
{
jointNum++; // Adds a joint
apple(); // Adds another place for the apple to spawn
}
}
public static void main(String[] args)
{
c = new Console(25, 75);

jointPosX[0] = 101;
jointPosY[0] = 101;
jointPosX[1] = 101;
jointPosY[1] = 101;
jointPosX[2] = 101;
jointPosY[2] = 101;
board();
apple();
movement();
}

}

最佳答案

首先,您希望每次重绘框架时都让它朝正确的方向移动一点,而不仅仅是当用户按下箭头键时。

当用户确实按下某个键时,这不是告诉它移动的时间,而是修改存储头节点当前方向的变量的时间。真正的技巧是保持蛇的形状,为了实现这一点,您不希望每个节点都朝同一方向移动,而是每个节点都移动到前一个节点的位置。

例如,如果这是你的蛇(1 是你开始吃的蛇头,7 是你最后吃的蛇的尾部)

1)The game is in the current Position
xxxxxxxxxxxxxxxxxx
x x 2) User presses the Right Arrow (=>)
x 76 x
x 543 x 3) Direction is adjusted
x 2 x Var Direction_X = 1
x 1 x Var Direction_Y = 0
x x
xxxxxxxxxxxxxxxxxx

4) Head node moves per Direction_X and Direction_Y
xxxxxxxxxxxxxxxxxx
x x
x 76 x
x 543 x
x 2 x
x 1 x
x x
xxxxxxxxxxxxxxxxxx

5) Node 2 moves to where node 1 was
xxxxxxxxxxxxxxxxxx
x x
x 76 x
x 543 x
x x
x 21 x
x x
xxxxxxxxxxxxxxxxxx

6) Node 3 moves to where node 2 was
xxxxxxxxxxxxxxxxxx
x x
x 76 x
x 54 x
x 3 x
x 21 x
x x
xxxxxxxxxxxxxxxxxx

7) Nodes 4 to 7 follow the same pattern
xxxxxxxxxxxxxxxxxx
x x
x 7 x
x 654 x
x 3 x
x 21 x
x x
xxxxxxxxxxxxxxxxxx

8) The frame is repainted with everything in the new position.

实现注意事项:

上图是我能想到的最好的方式来展示它,以明确算法的意图,但在实际编码时,您可能想从后面开始,在替换节点时(将 7 放在 6 处, 6 at 5、5 at 4、...、2 at 1,根据当前方向变量移动 1)。这是因为下一个位置已存储在您的列表中。

关于Java:蛇 - 改变方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com