gpt4 book ai didi

javascript - 对象的移动,不受任何操作系统的干扰?

转载 作者:可可西里 更新时间:2023-11-01 13:03:15 25 4
gpt4 key购买 nike

有一个测试代码给你:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
bri {
position: absolute;
width: 20px;
height: 20px;
}
.dirt {
background-color:brown;
}
.gracz {
background-color:red
}
</style>
<script >
function makeLevel() {
width = 30
heigth = 30

var bricks = document.getElementById('bricks')
bricks.style.width = 20 * heigth + 'px'
bricks.style.height = 20 * width + 'px'
bricks.innerHTML = ''

for (var i = 0; i < width; i++) {
for (var j = 0; j < heigth; j++) {
var bri = document.createElement("bri");
bricks.appendChild(bri)
bri.style.top = i * 20 + "px"
bri.style.left = j * 20 + "px"
bri.id = i + "_" + j
bri.className = 'dirt'
}
}
pos_player_x = Math.floor(Math.random() * width)
pos_player_y = Math.floor(Math.random() * heigth)
bri = document.getElementById(pos_player_x + '_' + pos_player_y);
bri.className = 'gracz'
}

function moving(event) {
var key = event.which
//87 - w , 83 -s, 65 -a, 68 - d
switch (key) {
case 87:
var new_x = pos_player_x - 1;
var new_y = pos_player_y - 0;
break;
case 83:
var new_x = pos_player_x + 1;
var new_y = pos_player_y - 0;
break;
case 65:
var new_x = pos_player_x - 0;
var new_y = pos_player_y - 1;
break;
case 68:
var new_x = pos_player_x - 0;
var new_y = pos_player_y + 1;
break;
}
var new_pos = document.getElementById(new_x + '_' + new_y)
var old_pos = document.getElementById(pos_player_x + '_' + pos_player_y)
if (new_pos != null) {
new_pos.className = 'gracz';
old_pos.className = 'clean';
pos_player_y = new_y;
pos_player_x = new_x;
}
}
</script>

</head>
<body onload="makeLevel()" onkeydown="moving(event)">
<div id="game">
<div id="bricks" draggable="false"></div>
</div>
</body>
</html>

而我们需要专注于称为“移动”的功能。如果我们按住“WASD”按钮之一,我们的播放器将遵循此方向。但是,第一秒他移动了一次,之后他们会重复移动直到他到达其中一堵墙。

问题来了。如果我们按住其中一个按钮,如何消除这个小停顿?

这是我的第一篇文章,如有错误请见谅。

最佳答案

目前,您的 Action 与收到的按键次数有关。这是有问题的,不仅是因为您感知到的操作系统问题(即它会在多长时间内开始发送重复键),而且还因为如果有人比基本重复更快地按下键(“垃圾邮件”),它允许更快的移动按下按钮的速度。

相反,您的游戏应该有一个内部“滴答声”,它可以量化运动。当按下和释放键时,您会将这些更改传播到一个内部变量,并且每次您的游戏决定该移动时,都会根据该变量的当前状态移动。这意味着无论某人是按住按键还是发送垃圾邮件,无论操作系统发送重复信号的速度如何,方 block 都将按照您的游戏允许的速度移动。

编辑:这是您的代码的调整版本。注意新引入的startGameLoop功能,以及我如何使用 onkeydownonkeyup事件。在这个版本中,蛇在按下一个键时以稳定的速度移动,并且在释放后不再移动。

HTML:<body onload="makeLevel()" onkeydown="registerKey(event)" onkeyup="releaseKey()">

Javascript:

    var currentlyPressedKey;

function makeLevel() {
width = 30
heigth = 30

var bricks = document.getElementById('bricks')
bricks.style.width = 20 * heigth + 'px'
bricks.style.height = 20 * width + 'px'
bricks.innerHTML = ''

for (var i = 0; i < width; i++) {
for (var j = 0; j < heigth; j++) {
var bri = document.createElement("bri");
bricks.appendChild(bri)
bri.style.top = i * 20 + "px"
bri.style.left = j * 20 + "px"
bri.id = i + "_" + j
bri.className = 'dirt'
}
}
pos_player_x = Math.floor(Math.random() * width)
pos_player_y = Math.floor(Math.random() * heigth)
bri = document.getElementById(pos_player_x + '_' + pos_player_y);
bri.className = 'gracz';
startGameLoop();
}

function startGameLoop(){
setInterval(move,250);
}

function registerKey(event){
currentlyPressedKey = event.which;
}

function releaseKey(){
currentlyPressedKey = null;
}

function move() {
var key = currentlyPressedKey;
//87 - w , 83 -s, 65 -a, 68 - d
switch (key) {
case 87:
var new_x = pos_player_x - 1;
var new_y = pos_player_y - 0;
break;
case 83:
var new_x = pos_player_x + 1;
var new_y = pos_player_y - 0;
break;
case 65:
var new_x = pos_player_x - 0;
var new_y = pos_player_y - 1;
break;
case 68:
var new_x = pos_player_x - 0;
var new_y = pos_player_y + 1;
break;
}
var new_pos = document.getElementById(new_x + '_' + new_y)
var old_pos = document.getElementById(pos_player_x + '_' + pos_player_y)
if (new_pos != null) {
new_pos.className = 'gracz';
old_pos.className = 'clean';
pos_player_y = new_y;
pos_player_x = new_x;
}
}

关于javascript - 对象的移动,不受任何操作系统的干扰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36515528/

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