2048在javascript:更新画布 [英] 2048 in javascript: updating the canvas

查看:84
本文介绍了2048在javascript:更新画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的高中在一个计算机科学课,对于我的最后一个项目,我的合作伙伴和我决定做2048.我们不会有瓷砖移动,但相反,4×4板具有瓦片,其具有在板上出现的瓦片的数量和颜色的属性。我们能够获得图块的x和y坐标改变,但画布不会更新。我们已经有这个问题两个星期了,现在已经尝试了一切,但没有什么工作。我们的代码如下。
请记住,因为我们只使用javascript几个月,只做了很简单的事情,我们的理解是有限的。感谢您的帮助。

I am in a computer science class at my high school, and for my final project, my partner and I have decided to make 2048. We are not going to have the tiles "move", but instead, the 4x4 board will have "tiles" that are have properties of the number and color of the "tile" as it appears on the board. We are able to get the x and y coordinates of a tile to change, but the canvas will not update. We have had this problem for two weeks now, and have tried everything, but nothing has worked. Our code is posted below. Please keep in mind that as we have only used javascript for a couple months, and only to do very simple things, our understanding is limited. Thank you for your help.

<!DOCTYPE html>
<html lang = "en">
<body style="background-color:lightgrey;">
<title>2048</title>
<canvas id="myCanvas" width="410" height="410" style="border:1px solid #d3d3d3;">
</canvas>
<script>
var x = 10;
var y = 10;
document.addEventListener('keydown', function(event)
{
    if(event.keyCode == 37)//left
    {
        x -= 100;
        console.log("x:"+ x);
    }
    else if(event.keyCode == 38)//up
    {
        y -= 100;
        console.log("y:"+ y);
    }
    else if(event.keyCode == 39)//right
    {
        x += 100;
        console.log("x:"+ x);
    }
    else if(event.keyCode == 40)//down
    {
        y += 100;
        console.log("y:"+ y);
    }
});
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// grey rectangle
ctx.beginPath();
ctx.lineWidth = "2";
ctx.strokeStyle = "grey";
ctx.rect(5, 5, 400, 400);
ctx.stroke();

// blue rectangle
ctx.beginPath();
ctx.lineWidth = "5";
ctx.strokeStyle = "blue";
ctx.rect(x, y, 100, 100);
ctx.stroke();
ctx.fillStyle = "blue";
ctx.fill();  
</script> 

</body>
</html>


推荐答案

你的游戏需要的是一个游戏循环。因此,绘图需要持续发生,在开始时只绘制一次。

What your game needs is a game loop. So the drawing needs to happen continously, at the moment you draw only one time at the beginning.

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

function draw() {
    // clear the canvas so the old rectangles are gone
    ctx.clearRect(0, 0, c.width, c.height);

    // grey rectangle
    ctx.beginPath();
    ctx.lineWidth = "2";
    ctx.strokeStyle = "grey";
    ctx.rect(5, 5, 400, 400);
    ctx.stroke();

    // blue rectangle
    ctx.beginPath();
    ctx.lineWidth = "5";
    ctx.strokeStyle = "blue";
    ctx.rect(x, y, 100, 100);
    ctx.stroke();
    ctx.fillStyle = "blue";
    ctx.fill();
}

// repeat game loop function forever, 30 times per second
window.setInterval(draw, 1000.0 / 30.0)

请注意 window.setInterval ctx.clearRect

Notice the use of window.setInterval and ctx.clearRect.

这篇关于2048在javascript:更新画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆