作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一颗 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/
想象我有字符串 abcD 我想提取abc出来了。我想到使用 ^(.+)D$ 然而,在匹配的group1中,不仅是abc ,但是abcD包含 - 如何制作.+不那么贪婪,所以D不包含在组中?我知道我可以
在使用 fabrication gem 时,我有一个循环依赖。在这里我会告诉你我做了什么。假设我有 2 个模型: class User < AR::Base has_many :messages
我是一名优秀的程序员,十分优秀!