1 Star 0 Fork 0

Shadow / code-snippet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.html 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
Shadow 提交于 2022-04-27 17:57 . webpack笔记
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="player"></div>
<button id="replay">replay</button>
<style>
.player {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
background: red;
}
</style>
<script>
let prevTime = Date.now();
class Player {
constructor(dom, speed = 2) {
this.dom = dom;
this.speed = speed;
this.reset();
}
reset() {
this.dom.style.left = 0;
this.dom.style.top = 0;
}
moveLeft() {
const left = parseInt(this.dom.style.left);
console.log(left);
this.dom.style.left = left - this.speed + 'px';
}
moveRight() {
const left = parseInt(this.dom.style.left);
this.dom.style.left = left + this.speed + 'px';
}
moveUp() {
const top = parseInt(this.dom.style.top);
this.dom.style.top = top - this.speed + 'px';
}
moveDown() {
const top = parseInt(this.dom.style.top);
this.dom.style.top = top + this.speed + 'px'
}
}
const player = new Player(document.querySelector('.player'), 10);
const commands = {
w: 'moveUp',
a: 'moveLeft',
s: 'moveDown',
d: 'moveRight',
}
const commandStack = [];
document.addEventListener('keypress', event => {
const command = player[commands[event.key]].bind(player);
if(command) {
command();
const now = Date.now();
commandStack.push({
command,
timeout: now - prevTime,
})
prevTime = now;
}
})
let promise = Promise.resolve();
replay.addEventListener("click", () => {
player.reset();
commandStack.forEach(current => {
promise = promise.then(() => {
return new Promise(resolve => {
setTimeout(() => {
current.command();
resolve();
}, current.timeout)
})
})
})
})
</script>
</body>
</html>
1
https://gitee.com/ytiona/code-snippet.git
git@gitee.com:ytiona/code-snippet.git
ytiona
code-snippet
code-snippet
master

搜索帮助