对不起,如果标题不清楚。我正在制作一个简单的HTML/JS/CSS游戏,并且我使用正方形作为玩家精灵。我想知道是否有可能让用户在玩游戏之前选择其正方形的颜色,以便可以对其进行自定义。我目前会粘贴我的代码。我还想知道是否有可能为游戏制作起始屏幕吗?页面加载时没有加载它。
var myGamePiece;
var myObstacles = [];
var myMusic;
function startGame() {
myGamePiece = new component(30, 30, "purple", 10, 120);
myObstacles = [];
myObstacle = new component(10, 200, "green", 300, 120);
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
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.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
stop : function() {
clearInterval(this.interval);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.x += this.speedX;
this.y += this.speedY;
}
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() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
myGameArea.stop();
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].newPos();
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
function restart() {
myGameArea.stop();
myGameArea.clear();
startGame();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function moveup() {
myGamePiece.speedY = -1;
}
function movedown() {
myGamePiece.speedY = 1;
}
function moveleft() {
myGamePiece.speedX = -1;
}
function moveright() {
myGamePiece.speedX = 1;
}
function clearmove() {
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
}
p {
font-size: 20px;
}
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
<!DOCTYPE html>
<html>
<head>
<title>Blockbound</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body onload="startGame()">
<div style="text-align:center;width:480px;">
<button onmousedown="moveup()" onmouseup="clearmove()" ontouchstart="moveup()">UP</button><br><br>
<button onmousedown="moveleft()" onmouseup="clearmove()" ontouchstart="moveleft()">LEFT</button>
<button onmousedown="moveright()" onmouseup="clearmove()" ontouchstart="moveright()">RIGHT</button><br><br>
<button onmousedown="movedown()" onmouseup="clearmove()" ontouchstart="movedown()">DOWN</button>
<div style="text-align:right;width:480px;">
<button onmousedown="restart()">RESTART</button>
</div>
</div>
<p>Welcome to Blockbound!</p>
<p>Instructions: Use the buttons to control the purple square. Get the highest score you can without colliding with the obstacles!</p>
<p>Game made by Sebastian Latham</p>
<p>HIGH SCORES:</p>
<p>Broteam123: 13978</p>
</body>
</html>
您在这里问了2个问题。
关于精灵颜色,我认为了解JavaScript函数参数将非常有价值。这是有关该信息的一些信息
您可以看到游戏件精灵是在 mygamepiece 变量中确定的,该变量具有5个参数。
myGamePiece = new component(30, 30, 'purple', 10, 120);
此变量在startGame((函数中,因此,如果将颜色的字符串参数传递给了StartGame函数,并将其引用到新组件中,则可以看到它可以工作。我将将开始游戏函数从没有参数更改为:
function startGame(chosencolour) {
myGamePiece = new component(30, 30, chosencolour, 10, 120);
myObstacles = [];
myObstacle = new component(10, 200, "green", 300, 120);
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
myGameArea.start();
}
证明这一点的一种简单方法是使用启动游戏的按钮,而不是当前如何通过HTML主体启动它。
<button onClick="startGame('red')">Red Sprite</button>
这将使您朝着使用户能够使用自己的精灵的正确方向。
关于您有关起始屏幕的第二个问题,请从HTML中的身体上删除开始游戏功能,创建一个新的Div,例如
<div id="startscreen"></div>
使用颜色精灵选项,然后更改启动屏幕div的样式以显示:在StartGame函数中无。
document.getElementById('startscreen').style.display = 'none';
从您启动的代码中学习很多东西,因此请考虑查找有关JavaScript函数的更多信息。
这两个问题绝对是可能的。它需要您设计启动屏幕本身,并在该启动屏幕上制作一个调用加载游戏函数的按钮或某些内容。
为了使玩家能够选择自己的颜色,它需要您将游戏的一部分(可能在设置中(指定为他们可以打开HTML色轮的地方,或者您选择显示颜色。然后,使其保存播放器选择的内容到其设置文件中(可能是您保存其用户名或拥有的内容(,并在关闭设置或击中设置后,然后将其Square Color Load作为新颜色作为新颜色。