gpt4 book ai didi

JavaScript 游戏视口(viewport)

转载 作者:数据小太阳 更新时间:2023-10-29 05:24:17 26 4
gpt4 key购买 nike

您好,我是 JavaScript 的新手,因此决定按照 w3schools game tutorial 了解更多信息.

我决定让它成为一款平台游戏,但我一直无法弄清楚如何在 Canvas 外添加跟随玩家的视口(viewport)。

谁能帮帮我?提前致谢。

Fiddle

//Background
var Background;

//Objects
var Player;
var Obstacle;

//Mobile buttons
var UpBtn;
var DownBtn;
var LeftBtn;
var RightBtn;
//End

function startGame() {
Background = new component(656, 270, "gray", 0, 0);

Player = new component(30, 30, "blue", 200, 75);

Obstacle = new component(10,100, "red", 300, 170);

//Mobile buttons
UpBtn = new component(0, 0, "black", 50, 160);
DownBtn = new component(0, 0, "black", 50, 220);
LeftBtn = new component(30, 30, "black", 20, 200);
RightBtn = new component(30, 30, "black", 90, 200);
//End
myGameArea.start();
}

var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
//Mobile buttons
window.addEventListener('mousedown', function (e) {
myGameArea.x = e.pageX;
myGameArea.y = e.pageY;
})
window.addEventListener('mouseup', function (e) {
myGameArea.x = false;
myGameArea.y = false;
})
window.addEventListener('touchstart', function (e) {
myGameArea.x = e.pageX;
myGameArea.y = e.pageY;
})
window.addEventListener('touchend', function (e) {
myGameArea.x = false;
myGameArea.y = false;
})
//End


//Keyboard
window.addEventListener('keydown', function (e) {
myGameArea.keys = (myGameArea.keys || []);
myGameArea.keys[e.keyCode] = true;
})
window.addEventListener('keyup', function (e) {
myGameArea.keys[e.keyCode] = false;
})
//End

},
stop : function() {
clearInterval(this.interval);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
dead : function() {
myGameArea.stop();
startGame();
}
}

function component(width, height, color, x, y, type) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
this.width = width;
this.height = height;
this.x = x;
this.y = y;
speed = 4;
this.speedX = 0;
this.speedY = 0;
this.gravity = 0.175;
this.gravitySpeed = 0;
this.bounce = 1;
this.update = function() {

ctx = myGameArea.context;
if (type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = -(this.gravitySpeed * this.bounce);
}
}
//Mobile buttons
this.clicked = function() {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var clicked = true;
if ((mybottom < myGameArea.y) || (mytop > myGameArea.y) || (myright < myGameArea.x) || (myleft > myGameArea.x)) {
clicked = false;
}
return clicked;
}
//End

this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright)) {
crash = false;
}
return crash;
}
}

function updateGameArea() {

if (Player.crashWith(Obstacle)) {
myGameArea.dead();
} else {
myGameArea.clear();
Obstacle.update();
}
//Mobile buttons
if (myGameArea.x && myGameArea.y) {
/*if (myUpBtn.clicked()) {
Player.y -= this.speed;
}
if (myDownBtn.clicked()) {
Player.y += this.speed;
}*/
if (LeftBtn.clicked()) {
Player.x += -this.speed;
}
if (RightBtn.clicked()) {
Player.x += this.speed;
}
}
UpBtn.update();
DownBtn.update();
LeftBtn.update();
RightBtn.update();
Player.update();

//End

//Keyboard
Player.speedX = 0;
Player.speedY = 0;

if (myGameArea.keys && myGameArea.keys[37]) {Player.speedX = -this.speed; }
if (myGameArea.keys && myGameArea.keys[39]) {Player.speedX = this.speed; }
//if (myGameArea.keys && myGameArea.keys[38]) {Player.speedY = -this.speed; }
//if (myGameArea.keys && myGameArea.keys[40]) {Player.speedY = this.speed; }
//End
Background.newPos();
Background.update();
Player.newPos();
Player.update();

Obstacle.update();

//UpBtn.update();
//DownBtn.update();
LeftBtn.update();
RightBtn.update();
}

最佳答案

您可以创建一个变量来存储 Canvas 中的 offsetX:

var offsetX = 0;

然后,在 updateGameArea() 方法中,更新此 offsetX 以玩家 x 坐标为中心:

offsetX = Player.x - myGameArea.canvas.width/2;

另外,绘制组件时减去offsetX:

ctx.fillRect(
this.x - (this.isFixed ? 0 : offsetX), // <--- here
this.y,
this.width,
this.height);

我还创建了一个新属性来判断组件是否固定在屏幕上:

this.isFixed = false;

然后将 isFixed = true 设置为控制按钮和背景:

Background.isFixed=true;
LeftBtn.isFixed=true;
RightBtn.isFixed=true;

这是更新后的代码 (Fiddle):

https://jsfiddle.net/xkbvphjz/13/

完整代码:

//Background
var Background;

//Objects
var Player;
var Obstacle;

//Mobile buttons
var UpBtn;
var DownBtn;
var LeftBtn;
var RightBtn;
//End

var offsetX = 0;

function startGame() {
Background = new component(656, 270, "gray", 0, 0);
Background.isFixed=true;

Player = new component(30, 30, "blue", 200, 75);

Obstacle = new component(10,100, "red", 300, 170);

//Mobile buttons
UpBtn = new component(0, 0, "black", 50, 160);
DownBtn = new component(0, 0, "black", 50, 220);
LeftBtn = new component(30, 30, "black", 20, 200);
LeftBtn.isFixed=true;
RightBtn = new component(30, 30, "black", 90, 200);
RightBtn.isFixed=true;
//End
myGameArea.start();
}

var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
//Mobile buttons
window.addEventListener('mousedown', function (e) {
myGameArea.x = e.pageX;
myGameArea.y = e.pageY;
})
window.addEventListener('mouseup', function (e) {
myGameArea.x = false;
myGameArea.y = false;
})
window.addEventListener('touchstart', function (e) {
myGameArea.x = e.pageX;
myGameArea.y = e.pageY;
})
window.addEventListener('touchend', function (e) {
myGameArea.x = false;
myGameArea.y = false;
})
//End


//Keyboard
window.addEventListener('keydown', function (e) {
myGameArea.keys = (myGameArea.keys || []);
myGameArea.keys[e.keyCode] = true;
})
window.addEventListener('keyup', function (e) {
myGameArea.keys[e.keyCode] = false;
})
//End

},
stop : function() {
clearInterval(this.interval);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
dead : function() {
myGameArea.stop();
startGame();
}
}

function component(width, height, color, x, y, type, isFixed) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
this.width = width;
this.height = height;
this.x = x;
this.y = y;
speed = 4;
this.speedX = 0;
this.speedY = 0;
this.gravity = 0.175;
this.gravitySpeed = 0;
this.bounce = 1;
this.isFixed = false;
this.update = function() {

ctx = myGameArea.context;
if (type == "image") {
ctx.drawImage(this.image,
this.x - (this.isFixed ? 0 : offsetX),
this.y,
this.width, this.height);
} else {
ctx.fillStyle = color;
ctx.fillRect(
this.x - (this.isFixed ? 0 : offsetX),
this.y,
this.width,
this.height);
}
}
this.newPos = function() {
if (!this.isFixed) {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
//offsetX += this.speedX;
}
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = -(this.gravitySpeed * this.bounce);
}
}
//Mobile buttons
this.clicked = function() {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var clicked = true;
if ((mybottom < myGameArea.y) || (mytop > myGameArea.y) || (myright < myGameArea.x) || (myleft > myGameArea.x)) {
clicked = false;
}
return clicked;
}
//End

this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright)) {
crash = false;
}
return crash;
}
}

function updateGameArea() {

if (Player.crashWith(Obstacle)) {
myGameArea.dead();
} else {
myGameArea.clear();
Obstacle.update();
}
//Mobile buttons
if (myGameArea.x && myGameArea.y) {
/*if (myUpBtn.clicked()) {
Player.y -= this.speed;
}
if (myDownBtn.clicked()) {
Player.y += this.speed;
}*/
if (LeftBtn.clicked()) {
Player.x += -this.speed;
}
if (RightBtn.clicked()) {
Player.x += this.speed;
}
}
UpBtn.update();
DownBtn.update();
LeftBtn.update();
RightBtn.update();
Player.update();

offsetX = Player.x - myGameArea.canvas.width/2;

//End

//Keyboard
Player.speedX = 0;
Player.speedY = 0;

if (myGameArea.keys && myGameArea.keys[37]) {Player.speedX = -this.speed; }
if (myGameArea.keys && myGameArea.keys[39]) {Player.speedX = this.speed; }
//if (myGameArea.keys && myGameArea.keys[38]) {Player.speedY = -this.speed; }
//if (myGameArea.keys && myGameArea.keys[40]) {Player.speedY = this.speed; }
//End
Background.newPos();
Background.update();
Player.newPos();
Player.update();

Obstacle.update();

//UpBtn.update();
//DownBtn.update();
LeftBtn.update();
RightBtn.update();
}

关于JavaScript 游戏视口(viewport),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57706710/

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