我有一堆随机放置在0 + circle radiusheight/width - circle radius之间的圆圈,但出于某种原因,偶尔会有一个圆圈离开画布,即使代码另有说明(出于开发目的,那个圆圈是深灰色的).

const circlesToMake = 50;
let circleLayer, circles, r, g, b;
let lives = 3;
let round = -1;
let dead = false;

function setup() {
  createCanvas(600, 450);
  noStroke();
  textFont('JetBrains Mono');
  setupCircles();
  fill(0);
  noLoop();
  circleLayer = createGraphics(width, height);
}

function draw() {
  image(circleLayer, 0, 0);
  fill(0);
  drawCircles(circleLayer);
  dispInfo();
}

function setupCircles() {
  circles = []
  r = random(20, 235)
  g = random(20, 235)
  b = random(20, 235)
  round++;

  while (circles.length < circlesToMake) {
    const circle = {
      x: random(width),
      y: random(height),
      r: 20,
    };

    let overlapping = false;
    for (let j = 0; j < circles.length; j++) {
      const other = circles[j];
      const d = dist(circle.x, circle.y, other.x, other.y);

      if (d < circle.r + other.r) {
        overlapping = true;
      } if (circle.x < 0 + circle.r || circle.x > width - circle.r) {
        overlapping = true;
      } if (circle.y < 0 + circle.r || circle.y > height - circle.r) {
        overlapping = true;
      }
    }

    if (!overlapping) {
      circles.push(circle);
    }
  }
}

function drawCircles(circleLayer) {
  clear();
  stroke(0);
  fill(255);
  rect(0, 0, width-1, height-1);
  noStroke();
  circles.forEach(c => {
    fill(r, g, b, 200); ellipse(c.x, c.y, c.r * 2, c.r * 2);
  });
  
  // fill(r, g, b, 220); // normal
  fill(32, 32, 32, 255); // dev mode
  
  const target = circles[0];
  ellipse(target.x, target.y, target.r * 2, target.r * 2);
}

function dispInfo() {
  fill(0, 0, 0, 100);
  textAlign(LEFT, TOP);
  text(`Lives: ${lives}`, 10, 10);
  text(`Round: ${round + 1}`, 10, 25);
}

function youDied() {
  clear();
  dead = true;
  textAlign(CENTER, CENTER);
  textSize(20);
  fill (0, 0, 0);
  text(`You survived ${round} rounds.`, width/2, height/2);
  fill(r, g, b, 255);
  ellipse(circles[0].x, circles[0].y, circles[0].r * 2, circles[0].r * 2);
}

function mousePressed() {
  if (!dead) {
    if (dist(mouseX, mouseY, circles[0].x, circles[0].y) < circles[0].r) {
      setupCircles();
      drawCircles();
      redraw();
    } else {
      const wrongClick = circles.some(c => dist(mouseX, mouseY, c.x, c.y) < c.r);
      if (wrongClick) {
        lives--;
        redraw();
      } if (lives <= 0) {
        youDied();
      }
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js"></script>

我试图添加一个if语句,以便当随机的xy值大于0 + radius或小于height - radius/width - radius时,它会重新轮询这些值,但它有时不适用于target个圆(circles[0]).

推荐答案

x: random(width)会将圆的中心置于0和宽度之间.但这还不包括圆圈的半径.

将半径考虑在内,使圆的中心在radiuswidth - radius之间:

const radius = 20;
const circle = {
  x: random(radius, width - radius),
  y: random(radius, height - radius),
  r: radius,
};

Javascript相关问答推荐

如何在NightWatch.js测试中允许浏览器权限?

当在select中 Select 选项时,我必须禁用不匹配的 Select

被CSS优先级所迷惑

使用JavaScript重新排序行

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

有没有可能使滑动img动画以更快的速度连续?

将本机导航路由react 到导航栏中未列出的屏幕?

如何从调整大小/zoom 的SVG路径定义新的d属性?""

我的角模板订阅后不刷新'

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

React.Development.js和未捕获的ReferenceError:未定义useState

如何使用JS创建一个明暗功能按钮?

Jest toHaveBeenNthCalledWith返回当前设置的变量值,而不是调用时的值

按下单键和多值

在Java脚本中录制视频后看不到曲目

如何阻止外部脚本进入顶层导航

当S点击按钮时,我如何才能改变它的样式?

暂停后只有一次旋转JS

:host::ng-Deep不将样式应用于material 复选框

带有JS模块模式的Rails的Importmap错误:";Net::ERR_ABORTED 404(未找到)&Quot;