- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是编程新手,并且卡在涉及蛇方向改变的特定部分上。该游戏与旧诺基亚手机上的游戏相同。很经典。
目前,每次我按 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/
我是编程新手,并且卡在涉及蛇方向改变的特定部分上。该游戏与旧诺基亚手机上的游戏相同。很经典。 目前,每次我按 W、A、S 或 D 键,蛇都会移动 1 平方/20 像素。问题是我希望这个 Action
如果您熟悉任何梦幻体育选秀,选秀顺序网格如下所示: EXAMPLE 1 (3-teams): Round Team 1 Team 2 Team 3 1 1 (1.1) 2 (
我从这里得到了蛇算法的代码(在MatLab中实现) http://www.mathworks.com/matlabcentral/fileexchange/28109-snakes-active-co
我遵循了这个 link 中的示例.然而,轮廓从初始点开始收缩。是否可以做展开的轮廓?我想要像显示的图像那样的东西。左边的图像是它的样子,右边的图像是我想要的样子——向外扩展而不是收缩。红色圆圈为起点,
我正在编写一款类似于贪吃蛇的游戏。目前,我正在努力编写 move() 和 Growth() 方法。这个游戏的运作方式是,蠕虫从 1 block 开始,每 move 一步就增加 1 block ,直到达
说明 我目前正在使用 Pygame 开发蛇游戏,但我遇到了一个问题,因为我的蛇目前仅由方 block 组成,但如果蛇包含蛇头、 body 、尾部的绘制 25x25 图片,我会发现它会更好对于弯曲的 b
我是一名优秀的程序员,十分优秀!