gpt4 book ai didi

javascript - 2D 平台游戏,在 html5 中制作玩家相机 View

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:34 24 4
gpt4 key购买 nike

我想修改我在 codepen 中找到的平台游戏。 http://codepen.io/loktar00/pen/JEdqD原图如下图。

enter image description here

我想把它改成:

enter image description here

enter image description here

我想放大播放器的视口(viewport)并制作一个类似相机的 View ,我可以在其中滚动 Canvas 元素中的一个级别。

喜欢这个游戏http://www.html5quintus.com/quintus/examples/platformer_full/

视口(viewport)和相机正在跟踪玩家。

这是我的代码:

(function () {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();

var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
width = 100,
height = 100,
player = {
x: width / 2,
y: height - 15,
width: 5,
height: 5,
speed: 3,
velX: 0,
velY: 0,
jumping: false,
grounded: false
},
keys = [],
friction = 0.8,
gravity = 0.3;

var boxes = [];

// player's position
var playerX = 20;
var playerY = 20;

// how far offset the canvas is
var offsetX = 0;
var offsetY = 0;

// dimensions
boxes.push({
x: 0,
y: 0,
width: 10,
height: height
});
boxes.push({
x: 0,
y: height - 2,
width: width,
height: 50
});
boxes.push({
x: width - 10,
y: 0,
width: 50,
height: height
});

boxes.push({
x: 120,
y: 10,
width: 80,
height: 80
});
boxes.push({
x: 170,
y: 50,
width: 80,
height: 80
});
boxes.push({
x: 220,
y: 100,
width: 80,
height: 80
});
boxes.push({
x: 270,
y: 150,
width: 40,
height: 40
});

canvas.width = width;
canvas.height = height;

function update() {
// check keys
if (keys[38] || keys[32] || keys[87]) {
// up arrow or space
if (!player.jumping && player.grounded) {
player.jumping = true;
player.grounded = false;
player.velY = -player.speed * 2;
}
}
if (keys[39] || keys[68]) {
// right arrow
if (player.velX < player.speed) {
player.velX++;
offsetX--;
}
}
if (keys[37] || keys[65]) {
// left arrow
if (player.velX > -player.speed) {
player.velX--;
offsetX++;
}
}

player.velX *= friction;
player.velY += gravity;

ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "black";
ctx.beginPath();

player.grounded = false;
for (var i = 0; i < boxes.length; i++) {
ctx.rect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);

var dir = colCheck(player, boxes[i]);

if (dir === "l" || dir === "r") {
player.velX = 0;
player.jumping = false;
} else if (dir === "b") {
player.grounded = true;
player.jumping = false;
} else if (dir === "t") {
player.velY *= -1;
}

}

if(player.grounded){
player.velY = 0;
}

player.x += player.velX;
player.y += player.velY;

ctx.save();
ctx.translate(offsetX, offsetY);
// clear the viewport
ctx.clearRect(-offsetX, -offsetY, 100,100);

ctx.fill();
ctx.fillStyle = "red";
ctx.fillRect(player.x, player.y, player.width, player.height);

ctx.fillRect(playerX-offsetX, playerY-offsetY, 8, 8);

// draw the other stuff
var l = boxes.length;
for (var i = 0; i < l; i++) {
// we should really only draw the things that intersect the viewport!
// but I am lazy so we are drawing everything here
var x = boxes[i][0];
var y = boxes[i][1];
ctx.fillStyle = 'lightblue';
ctx.fillRect(x, y, 8, 8);
ctx.fillStyle = 'black';
ctx.fillText(x + ', ' + y, x, y) // just to show where we are drawing these things
}

ctx.restore();

requestAnimationFrame(update);
}

function colCheck(shapeA, shapeB) {
// get the vectors to check against
var vX = (shapeA.x + (shapeA.width / 2)) - (shapeB.x + (shapeB.width / 2)),
vY = (shapeA.y + (shapeA.height / 2)) - (shapeB.y + (shapeB.height / 2)),
// add the half widths and half heights of the objects
hWidths = (shapeA.width / 2) + (shapeB.width / 2),
hHeights = (shapeA.height / 2) + (shapeB.height / 2),
colDir = null;

// if the x and y vector are less than the half width or half height, they we must be inside the object, causing a collision
if (Math.abs(vX) < hWidths && Math.abs(vY) < hHeights) {
// figures out on which side we are colliding (top, bottom, left, or right)
var oX = hWidths - Math.abs(vX),
oY = hHeights - Math.abs(vY);
if (oX >= oY) {
if (vY > 0) {
colDir = "t";
shapeA.y += oY;
} else {
colDir = "b";
shapeA.y -= oY;
}
} else {
if (vX > 0) {
colDir = "l";
shapeA.x += oX;
} else {
colDir = "r";
shapeA.x -= oX;
}
}
}
return colDir;
}

document.body.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});

document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});


window.addEventListener("load", function () {
update();
});
 <h3>A, D or Arrow keys to move, W or space to jump</h3>
<canvas id="canvas"></canvas>

但这行不通。

最佳答案

您可以使用代码笔中的相同代码,但在第一个 update() 之前的 load 事件中添加几行。

让我们这样说:

window.addEventListener("load", function () {
ctx.scale(2,2);
ctx.translate(-100,-100);
update();
});

这将放大两倍并以新坐标为中心,但是请记住,如果您想在玩家离开 View 时重新居中,则必须自己做。

作为执行此操作的部分方法,您可以使用 player.velXplayer.velY 的相反值检查玩家是否移动和平移 Canvas 。像这样的东西:

var playerMoved = false;

if (keys[38] || keys[32] || keys[87]) {
playerMoved = true;
//...Other stuff
}

if (keys[39] || keys[68]) {
playerMoved = true;
//...Other stuff
}

if (keys[37] || keys[65]) {
playerMoved = true;
//...Other stuff
}

if (playerMoved) {
// You still need to check quite a few things, like
// the player being out of bounds, the view not translating
// when x less than or bigger then a specific value, and so on
ctx.translate(-player.velX, -player.velY);
}

这不是一个完整的解决方案,因为它需要相当多的代码,但它应该能让您入门。

关于javascript - 2D 平台游戏,在 html5 中制作玩家相机 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42084499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com