简体   繁体   English

如何在我的 javascript 游戏中添加计数器?

[英]How do I add a counter in my javascript game?

I got this game coded in 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, 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, 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>

And I want to add a counter anywhere on the page, so it counts how long the "tail" is.我想在页面上的任何地方添加一个计数器,以便计算“尾巴”的长度。
I have tried a little myself but it doesn't seem to work, any ideas how I should do?我自己尝试了一点,但似乎不起作用,我应该怎么做?

Also another question... how do I change the code with a button or text field on a webpage?还有另一个问题...如何使用网页上的按钮或文本字段更改代码? Like for example changing:例如改变:
setInterval(game,1000/7);设置间隔(游戏,1000/7);
to
setInterval(game,1000/9);设置间隔(游戏,1000/9);
with a button or a text field, where you can type the numbers and it gets pasted into the code?使用按钮或文本字段,您可以在其中键入数字并将其粘贴到代码中?

To use predefined value as you setInterval timer, all you need to do is to find your best method of getting user inputs, eg using an input box .要在 setInterval 计时器时使用预定义值,您需要做的就是找到获取用户输入的最佳方法,例如使用input box Then you can grab the value with javascript然后你可以用javascript获取值

Eg例如

[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.

In order to write the trail length in the canvas, I added to your code为了在画布中写出路径长度,我添加到您的代码中

ctx.fillStyle = "blue";
ctx.fillText(trail.length, 100, 100);
ctx.font = "bold 20px sans-serif";
ctx.textBaseline = "bottom";

that way it constantly prints the trail length at the top left corner.这样它就会不断地在左上角打印轨迹长度。 and as for your second question, I added the intervalTime variable at the global scope so you could change it and then the next interval will be in the time you entered to that variable.至于您的第二个问题,我在全局范围内添加了 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,0,canv.width,canv.height); ctx.fillStyle = "blue"; ctx.fillText(trail.length, 100, 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,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>

And i want to add a counter anywhere on the page, so it counts how long the "tail" is.我想在页面上的任何地方添加一个计数器,以便计算“尾巴”的长度。 I have tried a little myself but it dosen't seem to work, any ideas how i do?我自己尝试了一点,但它似乎不起作用,我有什么想法吗?

For this question, you can add an element to the page and then update it's text in javascript each time the tail changes length (using the innerText property of the element).对于这个问题,您可以向页面添加一个元素,然后在每次尾部更改长度时(使用元素的innerText属性)在 javascript 中更新它的文本。

Sample html element:示例 html 元素:

<!-- inside the body element -->
<div>
    <span>Tail Count: </span>
    <span id="tail-counter">2</span>
</div>

And the javascript:和 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);
}

Also another queston... how do i change the code with a button or text field on a webpage?还有另一个问题...如何使用网页上的按钮或文本字段更改代码? Like for example changing setInterval(game,1000/7);例如改变 setInterval(game,1000/7); to setInterval(game,1000/9);设置间隔(游戏,1000/9); with a button or a text field, where you can type the numbers and it gets pasted into the code?使用按钮或文本字段,您可以在其中键入数字并将其粘贴到代码中?

For this question, to change the code based on a text field, you need to first add the text field to the page:对于这个问题,要根据文本字段更改代码,需要先将文本字段添加到页面中:

<div>
    <span>Set Game Speed: </span>
    <input id="game-speed" type="number" value="7" />
</div>

Then you can use javascript to get the value of the text field and use it in your code然后你可以使用javascript来获取文本字段的值并在你的代码中使用它

game_speed = Number.parseInt(document.getElementById("game-speed").value);
interval = setInterval(game, 1000 / game_speed);

Now all together (note that this code allows you to change the game speed while playing. This is done by clearing the interval you already made with clearInterval and then setting a new interval with the new 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, 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(); 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, 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>

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

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