如何解决如何在我的JavaScript游戏中添加计数器?
我用Javascript编写了这个游戏:
window.onload = function() {
canv = document.getElementById("gc");
ctx = canv.getContext("2d");
document.addEventListener("keydown",keyPush);
setInterval(game,1000 / 7);
}
px = py = 10;
gs = tc = 27;
ax = ay = 15;
xv = yv = 0;
trail = [];
tail = 2;
function game () {
px += xv;
py += yv;
if (px < 0) {
px = tc - 1;
}
if (px > tc - 1) {
px = 0;
}
if (py < 0) {
py = tc-1;
}
if (py > tc - 1) {
py = 0;
}
ctx.fillStyle = "black";
ctx.fillRect(0,canv.width,canv.height);
ctx.fillStyle = "lime";
for(var i = 0; i < trail.length; i++) {
ctx.fillRect(trail[i].x * gs,trail[i].y * gs,gs - 2,gs - 2);
if (trail[i].x == px && trail[i].y == py) {
tail = 2;
}
}
trail.push({ x: px,y: py });
while(trail.length > tail) {
trail.shift();
}
if (ax == px && ay == py) {
tail++;
ax = Math.floor(Math.random() * tc);
ay = Math.floor(Math.random() * tc);
}
ctx.fillStyle = "red";
ctx.fillRect(ax * gs,ay * gs,gs - 2);
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
xv = -1; yv = 0;
break;
case 38:
xv = 0; yv = -1;
break;
case 39:
xv = 1; yv = 0;
break;
case 40:
xv = 0; yv = 1;
break;
}
}
<canvas id="gc" width="729" height="729"></canvas>
我想在页面上的任何地方添加一个计数器,以便计算“尾巴”的长度。
我自己做了一些尝试,但似乎没有用,有什么想法应该怎么做?
还有另一个问题...如何更改网页上的按钮或文本字段的代码?例如更改:
setInterval(game,1000/7);
到
setInterval(game,1000/9);
使用按钮或文本字段,您可以在其中键入数字并将其粘贴到代码中?
解决方法
要在setInterval计时器中使用预定义的值,您要做的就是找到获取用户输入的最佳方法,例如使用input box
。然后,您可以使用javascript
例如
[HTML]
<input type="number" value="500" id="num">
[JS]
let num = document.getElementById('num').value;
num = +num; //cast the value to number since its returned as a string.
setInterval(game,num/nth); //nth is any value of your choice,or you can also grab from user input
```
For your first question,getting how long the tail is depends on how you want it.
you can use a variable and always increment it,each time your code condition is met.
,
为了在画布上记录路径长度,我添加了您的代码
ctx.fillStyle = "blue";
ctx.fillText(trail.length,100,100);
ctx.font = "bold 20px sans-serif";
ctx.textBaseline = "bottom";
那样,它将不断在左上角打印路径长度。 关于第二个问题,我在全局范围内添加了intervalTime变量,以便您可以更改它,然后下一个间隔将是您输入该变量的时间。
var intervalTime = 1000/7;
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
setInterval(game,intervalTime);
}
px=py=10;
gs=tc=27;
ax=ay=15;
xv=yv=0;
trail=[];
tail = 2;
function game() {
px+=xv;
py+=yv;
if(px<0) {
px= tc-1;
}
if(px>tc-1) {
px= 0;
}
if(py<0) {
py= tc-1;
}
if(py>tc-1) {
py= 0;
}
ctx.fillStyle="black";
ctx.fillRect(0,canv.width,canv.height);
ctx.fillStyle = "blue";
ctx.fillText(trail.length,100);
ctx.font = "bold 20px sans-serif";
ctx.textBaseline = "bottom";
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) {
tail = 2;
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) {
tail++;
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2);
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
xv=-1;yv=0;
break;
case 38:
xv=0;yv=-1;
break;
case 39:
xv=1;yv=0;
break;
case 40:
xv=0;yv=1;
break;
}
}
<canvas id="gc" width="729" height="729"></canvas>
,
我想在页面上的任何地方添加一个计数器,所以它计算 长“尾巴”是。我自己做了一点尝试,但似乎没有 工作,我有什么想法吗?
对于这个问题,您可以在页面上添加一个元素,然后在每次尾部更改长度时都使用javascript更新它的文本(使用元素的innerText
属性)。
示例html元素:
<!-- inside the body element -->
<div>
<span>Tail Count: </span>
<span id="tail-counter">2</span>
</div>
和javascript:
while (trail.length > tail) {
trail.shift();
document.getElementById("tail-counter").innerText = tail;
}
if (ax == px && ay == py) {
tail++;
document.getElementById("tail-counter").innerText = tail;
ax = Math.floor(Math.random() * tc);
ay = Math.floor(Math.random() * tc);
}
另外一个问题...我该如何使用按钮或文本更改代码 网页上的字段?例如改变 setInterval(game,1000/7);设置setInterval(game,1000/9);用一个按钮 或文本字段,您可以在其中键入数字并将其粘贴 进入代码?
对于此问题,要基于文本字段更改代码,您需要首先将文本字段添加到页面:
<div>
<span>Set Game Speed: </span>
<input id="game-speed" type="number" value="7" />
</div>
然后,您可以使用javascript获取文本字段的值并将其用于代码中
game_speed = Number.parseInt(document.getElementById("game-speed").value);
interval = setInterval(game,1000 / game_speed);
现在全部在一起(请注意,该代码允许您在玩游戏时更改游戏速度。这是通过清除已经用clearInterval
设置的间隔,然后用新的游戏速度设置新的间隔来实现的)
<canvas id="gc" width="729" height="729"></canvas>
<body style="overflow-x: hidden; overflow-y: hidden;">
<div>
<span>Tail Count: </span>
<span id="tail-counter">2</span>
</div>
<div>
<span>Set Game Speed: </span>
<input id="game-speed" type="number" value="7" />
</div>
</body>
<script>
px = py = 10;
gs = tc = 27;
ax = ay = 15;
xv = yv = 0;
trail = [];
tail = 2;
game_speed = 7;
interval = {};
window.onload = function () {
canv = document.getElementById("gc");
ctx = canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game_speed = Number.parseInt(document.getElementById("game-speed").value);
interval = setInterval(game,1000 / game_speed);
}
function game() {
let new_speed = Number.parseInt(document.getElementById("game-speed").value);
if (new_speed !== game_speed) {
clearInterval(interval);
game_speed = new_speed;
interval = setInterval(game,1000 / game_speed);
}
px += xv;
py += yv;
if (px < 0) {
px = tc - 1;
}
if (px > tc - 1) {
px = 0;
}
if (py < 0) {
py = tc - 1;
}
if (py > tc - 1) {
py = 0;
}
ctx.fillStyle = "black";
ctx.fillRect(0,canv.height);
ctx.fillStyle = "lime";
for (var i = 0; i < trail.length; i++) {
ctx.fillRect(trail[i].x * gs,trail[i].y * gs,gs - 2,gs - 2);
if (trail[i].x == px && trail[i].y == py) {
tail = 2;
}
}
trail.push({
x: px,y: py
});
while (trail.length > tail) {
trail.shift();
document.getElementById("tail-counter").innerText = tail;
}
if (ax == px && ay == py) {
tail++;
document.getElementById("tail-counter").innerText = tail;
ax = Math.floor(Math.random() * tc);
ay = Math.floor(Math.random() * tc);
}
ctx.fillStyle = "red";
ctx.fillRect(ax * gs,ay * gs,gs - 2);
}
function keyPush(evt) {
switch (evt.keyCode) {
case 37:
xv = -1;
yv = 0;
break;
case 38:
xv = 0;
yv = -1;
break;
case 39:
xv = 1;
yv = 0;
break;
case 40:
xv = 0;
yv = 1;
break;
}
}
</script>
<html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。