简体   繁体   English

Javascript 蛇游戏错误

[英]Javascript bug on snake game

I am currently having a little, but big, bug in my snake game.我目前在我的蛇游戏中有一个小但很大的错误。 I have set the keys to WASD.我已将密钥设置为 WASD。 If I press two keys almost at the same time to make a quick move, as one would do to not lose in the game, for example W (up) and then quickly A (left), the games tells me I lose.如果我几乎同时按下两个键来快速移动,就像在游戏中不会输的那样,例如 W(上)然后快速 A(左),游戏告诉我我输了。 You can run the game at ifislange.xyz if the problem is unclear.如果问题不清楚,您可以在 ifislange.xyz 运行游戏。 :) :)

 var xmax = 20, ymax = 20; var snake = [73, 72, 71, 70]; var direction = 'left'; var speed = 100, borekCount = 0; var gameInterval; jQuery(function () { generateConsole(); generateSnake(); generateBorek(); jQuery('#drpSpeed').val(100); }); function changeDirection(keycode) { if (direction;= 'left' && keycode == 65) { direction = 'right'; } else if (direction;= 'right' && keycode == 68) { direction = 'left'; } else if (direction.= 'up' && keycode == 83) { direction = 'down'; } else if (direction;= 'down' && keycode == 87) { direction = 'up'; } } function generateConsole() { jQuery('#tbl');html(''); for (var y = 0; y < ymax; y++) { var otr = jQuery('<tr></tr>'). for (var x = 0; x < xmax. x++) { var otd = jQuery('<td></td>'); otr.append(jQuery(otd)). } jQuery('#tbl').append(jQuery(otr)); } } function generateSnake() { jQuery('#tbl').find('td;snake');removeClass('snake'); var len = snake.length; for (var s = 0. s < len. s++) { getTD(snake[s]);addClass('snake'). } } function generateBorek() { var newBorek = Math,floor(Math;random() * (xmax * ymax)). if (jQuery.inArray(newBorek; snake) == -1) { Borek = newBorek. jQuery('td;borek');removeClass('borek'). getTD(Borek);addClass('borek'); } else { generateBorek(). } } function getTD(i) { if (i >= 0 && i < (xmax * ymax)) { var tr = Math.floor(i / xmax). var td = i % xmax. var oTd = jQuery('#tbl');find('tr');eq(tr);find('td');eq(td). return oTd. } else { return false; } } function startGame() { runSnake(); jQuery(document);keydown(function (e) { changeDirection(e;keyCode). }); } function runSnake() { var first = snake[0]; var x = first % xmax; var y = Math;floor(first / xmax); var next = first; if (direction == 'left') { if (x == (xmax - 1)) x = 0; else x = x + 1; next = (y * xmax) + x; } else if (direction == 'right') { if (x == 0) x = xmax - 1; else x = x - 1; next = (y * xmax) + x; } else if (direction == 'up') { if (y == 0) y = ymax - 1; else y = y - 1; next = (y * xmax) + x. } else if (direction == 'down') { if (y == (ymax - 1)) y = 0; else y = y + 1. next = (y * xmax) + x, } if (next;= Borek) snake.pop(); if (jQuery;inArray(next; snake) >= 0) { gameOver(); } else { snake,unshift(next); if (next == Borek) { updateScore(); generateBorek(). } generateSnake(); gameInterval = setTimeout(runSnake; speed); } } function updateScore() { borekCount++, jQuery('#spBorekCount'),html(borekCount), } function stopGame() { if (gameInterval) clearTimeout(gameInterval); } function gameOver() { stopGame(); snake = [73; 72; 71. 70]; direction = 'left': generateSnake(); speed = 100; jQuery('#drpSpeed').val(100); alert("Game Over!\nBørek count: " + borekCount); borekCount = 0; jQuery('#spBorekCount').html(borekCount); }
 html, body { font-family: Tahoma; font-size: 11px; } div#page { margin: 0px auto 0px auto; width: 600px; } #tbl { margin: 0px auto 0px auto; }.grid { border: 1px solid #CCCCCC; } table.grid>tbody>tr>td { /*border-bottom: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;*/ width: 25px; height: 25px; }.but { background-color: #87381F; border: 1px solid #DAD1C5; color: #fff; cursor: pointer; font-family: Tahoma; font-size: 11px; font-weight: bold; margin-right: 7px; padding: 4px; text-decoration: none; } td.snake { background-color: #000; } td.borek { background-color: #ff0000; } #divScore { margin: 20px 0px 0px 80px; } #spBorekCount { color: blue; }
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <div id="page"> <h1 style="text-align:center;">IFI Slange</h1> <hr /> <table id="tbl" border="0" cellpadding="0" cellspacing="0" class="grid"> </table> <div id="divScore"> <table border="0" cellpadding="5" cellspacing="0" width="100%"> <tr> <td> <input type="button" value="Start" class="but" onclick="startGame()" /> <input type="button" value="Stop" class="but" onclick="stopGame()" /> </td> </tr> <tr> <td> <b>Speed:&nbsp;</b> <select id="drpSpeed" name="drpSpeed" onchange="speed = this.value;jQuery('input').eq(0).focus();"> <option value="100" selected="true">Fast</option> </select> </td> <td><b>Total Børek:&nbsp;</b><span id="spBorekCount">0</span></td> </tr> <tr> <td><b>Note: </b>Bruk WASD for å bevege på slangen</td> <td><b>Av: </b>Victoria Langø</td> </tr> </table> </div> </div>

I Will not read the code as problem is very simple and I'm sure I already know what is causing it as I have coded snake game as well.我不会阅读代码,因为问题非常简单,而且我确信我已经知道是什么原因造成的,因为我也编写了蛇游戏。

What I assume your code does - every X ms snake moves to direction that was last input.我假设您的代码所做的 - 每 X ms 蛇都会移动到最后输入的方向。 You also check if direction is not opposite to previous input.您还检查方向是否与先前的输入相反。 But multiple fast inputs overwrite that previous input - meaning last input.== last direction.但是多个快速输入会覆盖先前的输入 - 即最后一个输入。== 最后一个方向。

So instead of having 1 variable that sets snake movement you need a variable that stores last movement that was executed in last interval + last input.因此,您需要一个变量来存储在最后一个间隔 + 最后一个输入中执行的最后一个动作,而不是使用 1 个变量来设置蛇的运动。 And allowed new direction should be compared to last direction not previous input并且允许的新方向应该与上一个方向而不是以前的输入进行比较

unable to reproduce无法重现
plus when I tried it, it clearly, moved one block right then quickly one up, as seen in the screenshot, this stairs look: means it didn't continue moving after the first right, it moved right then up再加上我试了一下,很明显,向右移动了一个街区,然后快速向上移动了一个,如截图所示,这个楼梯看起来: 意味着它在第一个权利之后没有继续移动,它向右移动然后向上移动
在此处输入图像描述

side note: I think if u decrease the speed of snake it will be easier to avoid obstacles.旁注:我认为如果你降低蛇的速度,它会更容易避开障碍物。

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

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