所以我的这部分js正在移动蛇。
//adding main image and mask
fabric.Image.fromURL(IMAGE_URL_HERE, function(pic) {
canvas.add(pic);
//positioning image, if needed
pic.centerH().setCoords();
//adding path, may be a heart or any other in your case
var path = new fabric.Path('M1.398,84.129c3.305,20.461,9.73,50.635,13.591,67.385c2.354,10.212,12.549,23.734,18.51,30.02c4.923,5.191,27.513,23.343,38.34,27.589c18.604,7.295,33.984,4.187,46.012-8.306c12.028-12.493,25.595-34.78,27.954-43.994c2.587-10.105,5.065-26.842,4.313-37.243c-1.036-14.316-14.224-69.332-16.806-79.55c-4.48-17.735-26.246-48.821-80.609-37.668C-1.66,13.514-1.879,63.844,1.398,84.129z');
//positioning path, if needed
path.set({
originX: 'center',
originY: 'center',
});
//masking img with path
pic.set({
clipTo: function(ctx) {
path.render(ctx);
}
});
canvas.renderAll();
});

当我在分数超过10时尝试将其递增2时,它会破坏它的身体。
答案 0 :(得分:0)
处理newX
溢出边界(我假设为10)的情况:
newX = (newX + 2 < 10) ? newX + 2 : 10;
这将检查向newX
添加2是否会使其超过10.如果它没有,那么它将添加2.如果是,那么它将只设置newX
到10。
答案 1 :(得分:0)
正如@MikeC建议的那样,只需修改你的速度。您可以定义此功能:
function runGame(newSpeed) {
if(typeof loop_game !== "undefined")
clearInterval(loop_game);
loop_game = setInterval(render, newSpeed);
}
然后在游戏开始时,你调用这个函数:
function startGame(){
.
.
.
runGame(speed);
}
以后,当你需要提高速度时:
if (speed>10) {
speed -= 10;
runGame(speed);
}
http://codepen.io/anon/pen/xZJRdQ
我修改了你的codepen以使其工作并获得了18分:)你能打败我吗?