gpt4 book ai didi

javascript - 我有一颗 meteor ,一颗随机掉落的 meteor ,我想制造更多 meteor

转载 作者:行者123 更新时间:2023-11-30 13:59:51 26 4
gpt4 key购买 nike

我有一颗 meteor ,当只有一颗 meteor 时碰撞会起作用,但是当我添加更多 meteor 时碰撞会停止,这可以防止健康状况被减去。

我试图在互联网上搜索,但没有得到任何好的答案。我还问了一些表格,他们说我必须创建一个地方来存储所有的 meteor ,然后检查是否有任何碰撞。

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);
}

我期待着 meteor 与宇宙飞船之间的碰撞——就像只有一颗 meteor 。

最佳答案

不是检查与 meteor 的碰撞,而是检查与数组 meteors 中的 meteor 的碰撞,在一个循环中:

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


此外,我建议在新 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>

关于javascript - 我有一颗 meteor ,一颗随机掉落的 meteor ,我想制造更多 meteor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56492869/

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