任务是在Processing中创建一个拍苍蝇游戏,其中一只苍蝇出现在屏幕上的随机位置,一旦苍蝇被拍到,它就会被被拍到的苍蝇的图像所取代,另一只苍蝇会出现在屏幕上的随机位置。 目前,当按下鼠标来扑打苍蝇时,窗口中也会出现多只苍蝇(而不是只有一只)。 窗口中出现的额外苍蝇也会为随机生成的每只苍蝇增加 1 分。
我怀疑这与我的阵列有关,但我正在努力寻找解决方案。
如果能澄清我的错误,我将不胜感激。
为了更清晰一些屏幕截图:
PImage fly, flybye, swatter, swatted;
float[] fX, fY; // Declaring fX & fY locaiton arrays
int[] swat;
int score = 0;
void setup() {
size(800, 400);
// Images
fly = loadImage("fly.png");
fly.resize(50,0);
flybye = loadImage("flybye.png");
flybye.resize(50,0);
swatter = loadImage("swatter.png");
swatted = loadImage("swatted.png");
fX = new float[0];
fY = new float[0];
swat = new int[0];
fX = append(fX, random(width));
fY = append(fY, random(height));
swat = append(swat, 0);
}
void draw() {
background(255);
populate(); // Displays flys to the screen
fill(0);
textSize(30);
text("Score: " + score, 20, 50); // Displays the score in the top left corner
noCursor(); // Removes the cursor from the screen
mousePressed();
}
void populate() {
for (int i = 0; i < fX.length; i++) {
if (swat[i] == 1) { // if swatted
image(flybye, fX[i], fY[i]);
} else {
image(fly, fX[i], fY[i]);
}
}
}
void collisionDetect() {
for (int i = 0; i < swat.length; i++) {
if (mouseX > fX[i] && mouseX < fX[i] + fly.width &&
mouseY > fY[i] && mouseY < fY[i] + fly.height) {
swat[i] = 1;
fX = append(fX, random(width));
fY = append(fY, random(height));
swat = append(swat, 0);
score++;
}
}
}
void mousePressed() {
if (mousePressed) {
collisionDetect();
image(swatted, mouseX-35, mouseY-25);
} else {
image(swatter, mouseX-35, mouseY-25);
}
}
您已经非常接近了,但是您的部分代码需要仔细查看,因为存在一些错误:
mousePressed()
您可以在任何处理草图中覆盖的函数:draw()
调用它是不必要的(这就是为什么一个原因你看到重复的苍蝇)if (mousePressed) {
在 mousePressed()
中是多余的,因为该函数每次点击只能调用一次。 (尝试单独草图中的参考示例以掌握窍门)collisionDetect()
if (mouseX > fX[i] && mouseX < fX[i] + fly.width &&
mouseY > fY[i] && mouseY < fY[i] + fly.height)...
仅检查盒子内是否有苍蝇(任何苍蝇,甚至已经被拍到的苍蝇)。 您可能想通过仅在增加分数和添加新苍蝇之前过滤未拍打的苍蝇来进一步限制这一点。
例如
void collisionDetect() {
for (int i = 0; i < swat.length; i++) {
if (mouseX > fX[i] && mouseX < fX[i] + fly.width &&
mouseY > fY[i] && mouseY < fY[i] + fly.height &&
// also check this fly has not been swatted (yet)
swat[i] == 0) {
swat[i] = 1;
fX = append(fX, random(width));
fY = append(fY, random(height));
swat = append(swat, 0);
score++;
}
}
}
这是添加了上述内容的代码:
PImage fly, flybye, swatter, swatted;
float[] fX, fY; // Declaring fX & fY location arrays
int[] swat;
int score = 0;
void setup() {
size(800, 400);
// Images
fly = loadImage("fly.png");
fly.resize(50,0);
flybye = loadImage("flybye.png");
flybye.resize(50,0);
swatter = loadImage("swatter.png");
swatted = loadImage("swatted.png");
fX = new float[0];
fY = new float[0];
swat = new int[0];
fX = append(fX, random(width));
fY = append(fY, random(height));
swat = append(swat, 0);
}
void draw() {
background(255);
populate(); // Displays flys to the screen
fill(0);
textSize(30);
text("Score: " + score, 20, 50); // Displays the score in the top left corner
noCursor(); // Removes the cursor from the screen
}
void populate() {
for (int i = 0; i < fX.length; i++) {
if (swat[i] == 1) { // if swatted
image(flybye, fX[i], fY[i]);
} else {
image(fly, fX[i], fY[i]);
}
}
}
void collisionDetect() {
for (int i = 0; i < swat.length; i++) {
if (mouseX > fX[i] && mouseX < fX[i] + fly.width &&
mouseY > fY[i] && mouseY < fY[i] + fly.height &&
// also check this fly has not been swatted (yet)
swat[i] == 0) {
swat[i] = 1;
fX = append(fX, random(width));
fY = append(fY, random(height));
swat = append(swat, 0);
score++;
}
}
}
void mousePressed() {
collisionDetect();
}
其他可以改进的地方:
例如
PImage fly, flybye, swatter, swatted;
float[] fX, fY; // Declaring fX & fY location arrays
int[] swat;
int score = 0;
void setup() {
size(800, 400);
noCursor(); // Removes the cursor from the screen
// Images
fly = loadImage("fly.png");
fly.resize(50, 0);
flybye = loadImage("flybye.png");
flybye.resize(50, 0);
swatter = loadImage("swatter.png");
swatted = loadImage("swatted.png");
fX = new float[0];
fY = new float[0];
swat = new int[0];
addFly();
}
void draw() {
background(255);
populate(); // Displays flys to the screen
fill(0);
textSize(30);
text("Score: " + score, 20, 50); // Displays the score in the top left corner
}
void populate() {
for (int i = 0; i < swat.length; i++) {
if (swat[i] == 1) { // if swatted
image(flybye, fX[i], fY[i]);
} else {
image(fly, fX[i], fY[i]);
}
}
}
void collisionDetect() {
for (int i = 0; i < swat.length; i++) {
if (isPointInRect(mouseX, mouseY, fX[i], fY[i], fly.width, fly.height) &&
// also check this fly has not been swatted (yet)
swat[i] == 0) {
// mark this index as swatted
swat[i] = 1;
// increment score
score++;
// add new fly
addFly();
}
}
}
void addFly() {
fX = append(fX, random(width));
fY = append(fY, random(height));
swat = append(swat, 0);
}
boolean isPointInRect(int mx, int my, float rx, float ry, float rw, float rh) {
return (mx >= rx && mx <= rx + rw) &&
(my >= ry && my <= ry + rh);
}
void mousePressed() {
collisionDetect();
}
一步一步来。有时,对于代码,放慢速度/重新检查假设会让你比匆忙更快地完成任务。玩得开心!