2017-10-09 35 views
0

所以我创建了这个基于一年前的小型简介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); 
+0

难道你只需要制作一个'badCubeArr'(把它命名为你想要的) - 几乎只是一个拥有不同坐标的多个“cubeBad”对象的数组 –

+0

你的意思是确定而不是选择? –

回答

0

首先,你需要一个数组来存储所有的新敌人。

var enemies = []; 

然后,当你想创建一个新的敌人:

enemies.push(new cubebad()); 

要在工作演示观看此,随时访问一个项目,我的工作出现这种情况,处理大量的这里的敌人(小行星) - http://asteroidio.bitballoon.com/

从该游戏的第333行开始,您可以看到我有的Asteroid对象,它可以作为创建所有小行星的模板。然后,一旦游戏开始我使用:

for (let i=0; i<foodValue; i++) { 
    asteroids[i] = new Asteroids; 
} 

它创建了几百个小行星,并将它们放入一个数组中。

相关问题