在处理中创建随机像素线

发布于 2025-01-10 01:17:26 字数 444 浏览 2 评论 0原文

我正在尝试制作一款游戏,但我陷入了随机关卡设计。基本上,我试图创建一条从一个边缘/角到另一个边缘/角的线,同时具有一定的随机性。

有关示例,请参见下图 1 [链接已损坏] 和 2。我正在 processing 中执行此操作,并且我尝试的每一次尝试都没有产生正确的结果。我可以让它们随机填充,但不能排成一行或从一边到另一边。顺便说一句,我正在尝试在 16 x 16 网格上执行此操作。任何想法或帮助将不胜感激,谢谢!

图片2:

“在此处输入图像描述"

I'm trying to make a game and I'm stuck on random level design. Basically, I'm trying to create a line from one edge/corner to another edge/corner while having some randomness to it.

See below image 1 [link broken] and 2 for examples. I'm doing this in processing and every attempt I've tried hasn't yielded proper results. I can get them to populate randomly but not in a line or from edge to edge. I'm trying to do this on a 16 x 16 grid by the way. Any ideas or help would be greatly appreciated thanks!

Image 2:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-01-17 01:17:26

根据您的描述,挑战在于从上到下有一条连接线,并且具有一点随机性驱动左/右方向。

有多种选择。

我想到的一个基本想法是:

  • 左边就是中间的右边
  • 选择一个起始 x 位置:从 0 到 15 的每一行(对于 16 px 级别),
    • 从 3 个数字中随机选择一个:
      • 如果是第一次向左走(x 减量)
      • 如果是第二次向右走(x 增量)
      • 如果是第三个:忽略:这意味着该迭代将直接向下移动

这是一个使用 PImage 说明这一点的基本草图 来可视化数据:

void setup(){
  size(160, 160);
  noSmooth();
  
  int levelSize = 16;
  
  PImage level = createImage(levelSize, levelSize, RGB);
  level.loadPixels();
  java.util.Arrays.fill(level.pixels, color(255));
  
  int x = levelSize / 2;
  for(int y = 0 ; y < levelSize; y++){
    int randomDirection = (int)random(3);
    if(randomDirection == 1) x--;
    if(randomDirection == 2) x++;
    // if randomDirection is 0 ignore as we don't change x -> just go down
    // constrain to valid pixel
    x = constrain(x, 0, levelSize - 1);
    // render dot
    level.pixels[x + y * levelSize] = color(0);
  }
  
  level.updatePixels();
  
  // render result;
  image(level, 0, 0, width, height);
  fill(127);
  text("click to reset", 10, 15);
}

// hacky reset
void draw(){}
void mousePressed(){
  setup();
}

上面的逻辑非常简单,但可以随意用其他选项替换 random(3) (也许 扔骰子来确定方向或探索其他伪随机数生成器 (PRNG),例如randomGaussian()noise()(以及相关函数)等)

这是上述内容的 p5.js 版本:

let levelSize = 16;
let numBlocks = levelSize * levelSize;
let level = new Array(numBlocks);

function setup() {
  createCanvas(320, 320);
  
  level.fill(0);
  let x = floor(levelSize / 2);
  for(let y = 0 ; y < levelSize; y++){
    let randomDirection = floor(random(3));
    if(randomDirection === 1) x--;
    if(randomDirection === 2) x++;
    // if randomDirection is 0 ignore as we don't change x -> just go down
    // constrain to valid pixel
    x = constrain(x, 0, levelSize - 1);
    // render dot
    level[x + y * levelSize] = 1;
  }
  
  // optional: print to console
  // prettyPrintLevel(level, levelSize, numBlocks); 
}

function draw() {
  background(255);
  // visualise
  for(let i = 0 ; i < numBlocks; i++){
    let x = i % levelSize;
    let y = floor(i / levelSize);
    fill(level[i] == 1 ? color(0) : color(255));
    rect(x * 20, y * 20, 20, 20);
  }
}

function prettyPrintLevel(level, levelSize, numBlocks){
  for(let i = 0; i < numBlocks; i+= levelSize){
    print(level.slice(i, i + levelSize));
  }
}

function mousePressed(){
  setup(); 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>

在这两个示例中,数据都是结构化的一维数组,但是,如果它更容易,它也可以很容易地成为二维数组。在这个开发阶段,最简单、最具可读性的选项就是最佳选择。

Based on your description, the challenge is in having a connected line from top to bottom with a bit of randomness driving left/right direction.

There are multiple options.

Here's a basic idea that comes to mind:

  • pick a starting x position: left's say right down the middle
  • for each row from 0 to 15 (for 16 px level)
    • pick a random between 3 numbers:
      • if it's the 1st go left (x decrements)
      • if it's the 2nd go right (x increments)
      • if it's the 3rd: ignore: it means the line will go straight down for this iteration

Here's a basic sketch that illustrates this using PImage to visualise the data:

void setup(){
  size(160, 160);
  noSmooth();
  
  int levelSize = 16;
  
  PImage level = createImage(levelSize, levelSize, RGB);
  level.loadPixels();
  java.util.Arrays.fill(level.pixels, color(255));
  
  int x = levelSize / 2;
  for(int y = 0 ; y < levelSize; y++){
    int randomDirection = (int)random(3);
    if(randomDirection == 1) x--;
    if(randomDirection == 2) x++;
    // if randomDirection is 0 ignore as we don't change x -> just go down
    // constrain to valid pixel
    x = constrain(x, 0, levelSize - 1);
    // render dot
    level.pixels[x + y * levelSize] = color(0);
  }
  
  level.updatePixels();
  
  // render result;
  image(level, 0, 0, width, height);
  fill(127);
  text("click to reset", 10, 15);
}

// hacky reset
void draw(){}
void mousePressed(){
  setup();
}

The logic is be pretty plain above, but free to replace random(3) with other options (perhaps throwing dice to determine direction or exploring other psuedo-random number generators (PRNGs) such as randomGaussian(), noise() (and related functions), etc.)

Here's a p5.js version of the above:

let levelSize = 16;
let numBlocks = levelSize * levelSize;
let level = new Array(numBlocks);

function setup() {
  createCanvas(320, 320);
  
  level.fill(0);
  let x = floor(levelSize / 2);
  for(let y = 0 ; y < levelSize; y++){
    let randomDirection = floor(random(3));
    if(randomDirection === 1) x--;
    if(randomDirection === 2) x++;
    // if randomDirection is 0 ignore as we don't change x -> just go down
    // constrain to valid pixel
    x = constrain(x, 0, levelSize - 1);
    // render dot
    level[x + y * levelSize] = 1;
  }
  
  // optional: print to console
  // prettyPrintLevel(level, levelSize, numBlocks); 
}

function draw() {
  background(255);
  // visualise
  for(let i = 0 ; i < numBlocks; i++){
    let x = i % levelSize;
    let y = floor(i / levelSize);
    fill(level[i] == 1 ? color(0) : color(255));
    rect(x * 20, y * 20, 20, 20);
  }
}

function prettyPrintLevel(level, levelSize, numBlocks){
  for(let i = 0; i < numBlocks; i+= levelSize){
    print(level.slice(i, i + levelSize));
  }
}

function mousePressed(){
  setup(); 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>

The data is a structured a 1D array in both examples, however, if it makes it easier it could easily be a 2D array. At this stage of development, whatever is the simplest, most readable option is the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文