我正在努力在谷歌上找到我的问题的答案,所以认为 id post。
我只使用 html5、javascript 和 jquery 构建了一个自上而下的基于瓷砖的 rpg 游戏。
我已经在数组中设置了图像。我已经在一个数组中设置了我的地图,然后循环遍历它以在屏幕上显示图块。
我希望在画布上显示一个 750px x 600px 的大地图部分,当玩家移动到 4 个边界之一时,地图将更新并显示地图的下一部分,或者如果没有可用的地图保持原样。
我将如何实现这个?
如果您不明白我在问什么,请尝试更好地解释:p,但这是我的代码:
$(document).ready(function(){
var canvas = $("#TBG");
var context = canvas.get(0).getContext("2d");
var canvasWidth = canvas.width();
var canvasHeight = canvas.height();
//Player Controls -------------------------------------------------------------------------
$(window).keydown(function(e){
var keyCode = e.keyCode;
if (keyCode == arrowRight){
Player.moveRight = true;
}
else if (keyCode == arrowLeft){
Player.moveLeft = true;
}
else if (keyCode == arrowUp){
Player.moveUp = true;
}
else if (keyCode == arrowDown) {
Player.moveDown = true;
}
});
$(window).keyup(function(e){
var keyCode = e.keyCode;
if (keyCode == arrowRight){
Player.moveRight = false;
}
else if (keyCode == arrowLeft){
Player.moveLeft = false;
}
else if (keyCode == arrowUp){
Player.moveUp = false;
}
else if (keyCode == arrowDown) {
Player.moveDown = false;
}
});
function player() {
this.x = 100;
this.y = 100;
this.vX = 0;
this.vY = 0;
var moveRight = false;
var moveLeft = false;
var moveUp = false;
var moveDown = false;
}
var Player = new player();
var arrowLeft = 37;
var arrowUp = 38;
var arrowRight = 39;
var arrowDown = 40;
//End Player controls ------------------------------------------------------------------------------
//Map initialisation -------------------------------------------------------------------------------------
var images = new Array(3);
images[0] = new Image();
images[0].src = "images/player.png";
images[1] = new Image();
images[1].src = "images/wall.png";
images[2] = new Image();
images[2].src = "images/grass.png";
images[3] = new Image();
images[3].src = "images/floor.png";
var tileMap = [
[3,1,3,3,3,3,3,3,3,3],
[3,1,1,2,2,2,2,2,2,3],
[3,1,1,1,2,2,2,2,2,3],
[3,2,1,1,1,2,2,2,2,3],
[3,2,2,1,1,1,2,2,2,3],
[3,2,2,2,1,1,1,1,1,3],
[3,2,2,2,2,1,1,1,1,3],
[3,2,2,2,2,1,1,1,1,3],
[3,2,2,2,2,1,1,1,1,3],
[3,3,3,3,3,3,3,3,3,3]
];
//Animate Loop -------------------------------------------------------------------------------------------
function animate () {
context.clearRect(0,0,canvasWidth,canvasHeight);
Player.vX = 0;
Player.vY = 0;
//Player Animation
if (Player.moveRight) {
Player.vX =2;
};
if (Player.moveUp) {
Player.vY = -2;
};
if (Player.moveDown) {
Player.vY = 2;
};
if (Player.moveLeft) {
Player.vX = -2;
};
Player.x += Player.vX;
Player.y += Player.vY;
if (Player.x < 0){
Player.x = 0;
Player.vX *= -2;
}
else if (Player.x + 64 > canvasWidth) {
Player.x = canvasWidth - 64;
Player.vX *= -2;
};
if (Player.y < 0){
Player.y = 0;
Player.vY *= -2;
}
else if (Player.y + 64 > canvasHeight) {
Player.y = canvasWidth - 64;
Player.vY *= -2;
};
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++){
var tileId = tileMap[i][j];
var tileWidth = 32;
var tileHeight = 32;
context.drawImage(images[tileId],i * tileWidth, j * tileHeight);
}
}
context.drawImage(images[0], Player.x, Player.y);
setTimeout(animate, 0);
}
// End Loop ------------------------------------------------------------------------------------------------
animate();
});
我不擅长 Javascript,但学习并了解有关编程的大量知识。
提前谢谢汤姆