所以我创建了这个基于一年前的小型简介youtube系列的小型“游戏”,最近我发现了我的旧程序并想知道如何扩展它。我的一个想法是,有一个名为“badcube”的变量,如果玩家要触摸它,它会重置游戏/分数。我想知道如何设置它,以便每隔5点左右向坏的多维数据集添加相同的变量,从而使游戏变得更加困难。
每次变量达到某个数字时,我如何添加变量/函数?
编辑/ UPDATE:所以我加了基于从贾里德 - 布莱索响应一些线条, 我加入了可变var cubebadary = [];
和我说if语句:if (score % 5 == 0){cubebadary.push(new cubebad());}
我说这个,你会本身的作用下贴码更新。但是当我尝试通过一个简单的html页面运行它时,我检查了控制台,它带有错误:
image of error 这是什么意思,我该如何修改/修复此问题?
方案:
var canvas = document.getElementById("maincanvas");
var context = canvas.getContext("2d");
var keys = [];
var width = 1920,
height = 1080,
speed = 10;
var score = 0;
var player = {
x: 10,
y: 10,
width: 20,
height: 20
};
var cubegood = {
x: Math.random() * (width - 10),
y: Math.random() * (height - 10),
width: 10,
height: 10
};
var cubebad = {
x: Math.random() * (width - 20),
y: Math.random() * (height - 20),
width: 20,
height: 20
};
var cubebadary= [];
window.addEventListener("keydown", function(e) {
keys[e.keyCode] = true;
}, false);
window.addEventListener("keyup", function(e) {
delete keys[e.keyCode];
}, false);
/*
up - 38
down - 40
left - 37
right - 39
*/
function game() {
update();
render();
}
function update() {
if (keys[38]) player.y -= speed;
if (keys[40]) player.y += speed;
if (keys[37]) player.x -= speed;
if (keys[39]) player.x += speed;
if (player.x < 0) player.x = 0;
if (player.x >= width - player.width) player.x = width - player.width;
if (player.y < 0) player.y = 0;
if (player.y >= height - player.height) player.y = height - player.height;
if (collision(player, cubegood)) processgood();
if (collision(player, cubebad)) processbad();
if (score & 5 == 0)
{
cubebadary.push(new cubebad());
}
}
function render() {
context.clearRect(0, 0, width, height)
context.fillStyle = "blue";
context.fillRect(player.x, player.y, player.width, player.height);
context.fillStyle = "red";
context.fillRect(cubegood.x, cubegood.y, cubegood.width, cubegood.height);
context.fillStyle = "black"
context.fillRect(cubebad.x, cubebad.y, cubebad.width, cubebad.height);
context.font = "bold 30px helvetica";
context.fillText(score, 30, 30);
}
function processgood(){
score++;
cubegood.x = Math.random() * (width-10);
cubegood.y = Math.random() * (height-10);
}
function processbad(){
location.reload(true);
}
function collision(first, second) {
return !(first.x > second.x + second.width ||
first.x + first.width < second.x ||
first.y > second.y + second.height ||
first.y + first.height < second.y);
}
setInterval(function() {
game();
}, 1000/30);
难道你只需要制作一个'badCubeArr'(把它命名为你想要的) - 几乎只是一个拥有不同坐标的多个“cubeBad”对象的数组 –
你的意思是确定而不是选择? –