将背景图像添加到Javascript“Pong”游戏中

时间:2014-05-18 16:06:29

标签: javascript html canvas open-source pong

作为我学习的一部分,我正在修改我在网上找到的开源“Pong”游戏中的某些部分(由Kushagra Agarwal创建)。

Game's Fiddle

我正在尝试为游戏设置一个漂亮的背景图片, 这不会影响游戏体验 我尝试了很多方法,但到目前为止,我只是设法将背景颜色更改为不同的颜色,而不是实际图像:

// Function to paint canvas
function paintCanvas() {
    ctx.fillStyle = "blue/black/whatever";
    ctx.fillRect(0, 0, W, H);


试图计算ctx.fillStyle以显示图像并不顺利,因为paintCanvas()不断循环,如果获得其他值,则为“颜色”字符串(例如重500kb的背景图像)将在游戏中造成巨大延迟。

有人有想法吗?

感谢的, 罗伊

1 个答案:

答案 0 :(得分:1)

我觉得如果你只是在身体标签上放一个背景图片就好了!

<body background="someImage">
</body>

我注释掉了你的paintCanvas(),插入了一个clear,并插入了一个body标签!

http://jsfiddle.net/XLKFC/11/

// Function to paint canvas
function paintCanvas() {
    canvas.width = canvas.width; // Will clear the canvas
    //ctx.fillStyle = "blue";
    //ctx.fillRect(0, 0, W, H);
}