我有一颗流星,当只有一颗流星时,碰撞起作用,但是当我增加一颗流星时,碰撞停止起作用,这会阻止减去健康状况。
我试图在Internet上搜索,但是没有得到任何好的答案。我还问了一些表格,他们说我必须创建一个存储所有流星的地方,然后检查是否有碰撞。
let x = 200;
let x1 = 258;
let score = 0;
let health = 5;
let meteors = [];
let ecllipseMeteors = [];
let meteor;
let ecllipseMeteor;
let spaceShipimg;
let meteorimg;
let levels = 5;
function Meteor() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.speed = random(3, 7);
}
};
this.show = function() { image(meteorimg,this.x,this.y, 40, 40) };
}
function meteormodel() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.speed = random(3, 7);
}
};
this.show = function() { ellipse(this.x,this.y, 20, 20) };
}
function level() {
if (score == 10){
levels = 25;
}
}
function setup() {
createCanvas(600, 400);
timer = createP('');
for (let i = 0; i<5; i++) {
meteors[i] = new Meteor();
ecllipseMeteors[i] = new meteormodel();
}
meteor = new Meteor();
ecllipseMeteor = new meteormodel();
interval = setInterval(scoreCount, 500);
}
function gameOver() {
textSize(20);
text("GAME OVER YOUR SCORE: " + score, 200, 200);
fill(255);
}
function preload() {
spaceShipimg = loadImage('assets/spaceShip.png');
meteorimg = loadImage('assets/meteor.png');
}
function scoreCount() {
score++;
}
function draw() {
background(11, 72, 170);
//console.log(meteor)
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
meteor.y = height+1;
if (health == 0) {
gameOver();
noLoop();
}
}
if (keyIsDown(LEFT_ARROW) && x > -46) {
x -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x < 508) {
x += 5;
}
if (keyIsDown(LEFT_ARROW) && x1 > 9) {
x1 -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x1 < 565) {
x1 += 5;
}
rect(x1, 335, 20, 30)
image(spaceShipimg,x,260,120,120)
for (let meteor of meteors) {
meteor.fall();
meteor.show();
}
textSize(20);
text("Health: " + health, 10, 20);
fill(255);
textSize(20);
text("Score: " + score, 10, 40);
fill(255);
}
我期望流星之间的碰撞就像一艘只有一颗流星的飞船。
答案 0 :(得分:2)
您不必循环检查与meteor
的碰撞,而必须与循环meteors
的流星检查碰撞:
function draw() {
// [...]
for (let i = 0; i<meteors.length; i++) {
hit = collideRectCircle(x1, 335, 20, 30, meteors[i].x, meteors[i].y, 40);
if(hit == true) {
health -= 1;
meteors[i].y = height+1;
if (health == 0) {
gameOver();
noLoop();
}
}
}
// [...]
}
meteor
不再需要,它由数组meteors
代替,因此您可以从整个程序中删除meteor
。
此外,我建议在生成新流星时将其x坐标随机化:
function Meteor() {
// [...]
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.x = random(0,600); // <------ random x coordinate
this.speed = random(3, 7);
}
};
// [...]
}
let x = 200;
let x1 = 258;
let score = 0;
let health = 5;
let meteors = [];
let ecllipseMeteors = [];
let spaceShipimg;
let meteorimg;
let levels = 5;
function collideRectCircle(rx, ry, rw, rh, cx, cy, cr)
{
return rx+rw > cx-cr && cx+cr > rx && ry+rh > cy-cr && cy+cr > ry;
}
function Meteor() {
this.x = random(40,560);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.x = random(40,560);
this.speed = random(3, 7);
}
};
this.show = function() {
fill(128, 64, 64)
ellipse(this.x,this.y, 40, 40)
//image(meteorimg,this.x,this.y, 40, 40)
};
}
function meteormodel() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
/*
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
//this.x = random(40,560);
this.speed = random(3, 7);
}
};
*/
this.show = function() { ellipse(this.x,this.y, 20, 20) };
}
function level() {
if (score == 10){
levels = 25;
}
}
function setup() {
createCanvas(600, 400);
//timer = createP('');
for (let i = 0; i<5; i++) {
meteors[i] = new Meteor();
ecllipseMeteors[i] = new meteormodel();
}
interval = setInterval(scoreCount, 500);
}
function gameOver() {
textSize(20);
text("GAME OVER YOUR SCORE: " + score, 200, 200);
fill(255);
}
function preload() {
//spaceShipimg = loadImage('assets/spaceShip.png');
//meteorimg = loadImage('assets/meteor.png');
}
function scoreCount() {
score++;
}
function draw() {
background(11, 72, 170);
//console.log(meteor)
for (let i = 0; i<meteors.length; i++) {
hit = collideRectCircle(x1, 335, 20, 30, meteors[i].x, meteors[i].y, 40);
if(hit == true) {
health -= 1;
meteors[i].y = height+1;
if (health == 0) {
gameOver();
noLoop();
}
}
}
if (keyIsDown(LEFT_ARROW) && x > -46) {
x -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x < 508) {
x += 5;
}
if (keyIsDown(LEFT_ARROW) && x1 > 9) {
x1 -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x1 < 565) {
x1 += 5;
}
fill(255, 255, 0)
rect(x1, 335, 20, 30)
//image(spaceShipimg,x,260,120,120)
for (let meteor of meteors) {
meteor.fall();
meteor.show();
}
fill(255);
textSize(20);
text("Health: " + health, 10, 20);
textSize(20);
text("Score: " + score, 10, 40);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.js"></script>