苍蝇拍正在处理中 - 几乎可以工作了

问题描述 投票:0回答:1

任务是在Processing中创建一个拍苍蝇游戏,其中一只苍蝇出现在屏幕上的随机位置,一旦苍蝇被拍到,它就会被被拍到的苍蝇的图像所取代,另一只苍蝇会出现在屏幕上的随机位置。 目前,当按下鼠标来扑打苍蝇时,窗口中也会出现多只苍蝇(而不是只有一只)。 窗口中出现的额外苍蝇也会为随机生成的每只苍蝇增加 1 分。

我怀疑这与我的阵列有关,但我正在努力寻找解决方案。

如果能澄清我的错误,我将不胜感激。

为了更清晰一些屏幕截图:

Initial screen when starting the game

Swatting one fly. If swatter is within the collision detection of
a fly & the fly is being swatted, many fly's then appear on the screen

For as long as a fly has been swatted and the mouse button is being pressed, more fly's keep appearing on the screen, along with the score incrementing with each fly added (but not swatted)

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);
  }
}
java processing
1个回答
0
投票

您已经非常接近了,但是您的部分代码需要仔细查看,因为存在一些错误:

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

其他可以改进的地方:

  • 格式化:可以使用Ctrl+T / CMD+T 来整理代码。这使得阅读变得更容易,并且这是一个好习惯。从长远来看,您将花费更多时间阅读/理解代码而不是编写代码。如果您进行格式化/注释等,即使在两周或更长时间内重新访问您自己的代码也会更容易。让未来的自己生活更轻松
  • 您可以通过封装可重用的代码块来避免复制/粘贴代码。在这种情况下,添加一只苍蝇(一旦在设置中,在碰撞时),而且检查碰撞的条件也可以封装在一个函数中,您可以轻松地在任何其他草图中重复使用。

例如

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

一步一步来。有时,对于代码,放慢速度/重新检查假设会让你比匆忙更快地完成任务。玩得开心!

© www.soinside.com 2019 - 2024. All rights reserved.