Processing 随机()只发生一次

Processing 随机()只发生一次,processing,Processing,我有一个for()循环,我将随机选择一幅图片的切片,以随机顺序显示一幅图像的16个切片 我从一个数组中选取这些切片,我有一个变量,用于选取数组中要选择的切片 问题是我认为随机函数会在每一帧被触发,但它只被触发一次 代码如下: void setup() { size(720,720); slices = new PImage[16]; slices[0] = loadImage("1.png"); slices[1] = l

我有一个for()循环,我将随机选择一幅图片的切片,以随机顺序显示一幅图像的16个切片

我从一个数组中选取这些切片,我有一个变量,用于选取数组中要选择的切片

问题是我认为随机函数会在每一帧被触发,但它只被触发一次

代码如下:

void setup() {

    size(720,720);

    slices = new PImage[16];

        slices[0] = loadImage("1.png");
        slices[1] = loadImage("2.png");
        slices[2] = loadImage("3.png");
        slices[3] = loadImage("4.png");
        slices[4] = loadImage("5.png");
        slices[5] = loadImage("6.png");
        slices[6] = loadImage("7.png");
        slices[7] = loadImage("8.png");
        slices[8] = loadImage("9.png");
        slices[9] = loadImage("10.png");
        slices[10] = loadImage("11.png");
        slices[11] = loadImage("12.png");
        slices[12] = loadImage("13.png");
        slices[13] = loadImage("14.png");
        slices[14] = loadImage("15.png");
        slices[15] = loadImage("16.png");
        
        frameRate(1);


    
}

void draw() {

        

        for (int a = 0; a < 16; a++){

        int rand = int(random(slices.length));
        
        image(slices[rand],x,y,size,size);
         x += size;

         if (a % 4 == 3){
            y += size;
            x = 0;
            }
            
        }
void setup(){
规模(720);
切片=新的图像[16];
切片[0]=loadImage(“1.png”);
切片[1]=loadImage(“2.png”);
切片[2]=loadImage(“3.png”);
切片[3]=loadImage(“4.png”);
切片[4]=loadImage(“5.png”);
切片[5]=加载图像(“6.png”);
切片[6]=loadImage(“7.png”);
切片[7]=loadImage(“8.png”);
切片[8]=loadImage(“9.png”);
切片[9]=loadImage(“10.png”);
切片[10]=加载图像(“11.png”);
切片[11]=加载图像(“12.png”);
切片[12]=加载图像(“13.png”);
切片[13]=加载图像(“14.png”);
切片[14]=加载图像(“15.png”);
切片[15]=加载图像(“16.png”);
帧率(1);
}
作废提款(){
对于(int a=0;a<16;a++){
int rand=int(随机(切片长度));
图像(切片[rand],x,y,大小,大小);
x+=尺寸;
如果(a%4==3){
y+=尺寸;
x=0;
}
}
它只显示一次随机切片,然后我得到一个固定的图像。我想要的是在每一帧出现随机切片


感谢您的帮助!

可能是因为您忘记清除屏幕(例如,调用
background()
)(这意味着一旦您绘制了图像,它将保持渲染状态)

您还可以在设置中使用for循环来避免:

除了学习经验之外,玩它的另一个原因是,它不太可能重复使用相同的随机索引

关于拼接/洗牌,以下是的修改版本:

/**
*加载和显示
* 
*图像可以按其实际大小加载并显示到屏幕上
*或任何其他尺寸。
*/
PImage img;//声明PImage类型的变量“a”
//混洗图像
皮迈奇·伊姆斯赫夫勒;
//要洗牌的索引列表
IntList shuffleIndices=新建IntList();
//配置图像切片行/列
int行=4;
int cols=4;
//总截面
int numSections=行*列;
//图像截面尺寸
int截面宽度;
内部截面高度;
无效设置(){
尺寸(640360);
帧率(1);
//图像文件必须位于当前草图的数据文件夹中
//成功加载
img=加载图像(“https://processing.org/examples/moonwalk.jpg“”;//将图像加载到程序中
//计算截面尺寸
截面宽度=img.width/cols;
截面高度=img.height/行;
//分配一个单独的图像以将无序像素复制到
imgShuffled=createImage(img.width、img.height、RGB);
//填充图像节索引
对于(int i=0;i

可能是因为您忘记清除屏幕(例如,调用
background()
)(这意味着一旦您绘制了图像,它将保持渲染状态)

您还可以在设置中使用for循环来避免:

除了学习经验之外,玩它的另一个原因是,它不太可能重复使用相同的随机索引

关于拼接/洗牌,以下是的修改版本:

/**
*加载和显示
* 
*图像可以按其实际大小加载并显示到屏幕上
*或任何其他尺寸。
*/
PImage img;//声明PImage类型的变量“a”
//混洗图像
皮迈奇·伊姆斯赫夫勒;
//要洗牌的索引列表
IntList shuffleIndices=新建IntList();
//配置图像切片行/列
int行=4;
int cols=4;
//总截面
int numSections=行*列;
//图像截面尺寸
int截面宽度;
内部截面高度;
无效设置(){
尺寸(640360);
帧率(1);
//图像文件必须位于当前草图的数据文件夹中
//成功加载
img=加载图像(“https://processing.org/examples/moonwalk.jpg“”;//将图像加载到程序中
//计算截面尺寸
截面宽度=img.width/cols;
截面高度=img.height/行;
//分配一个单独的图像以将无序像素复制到
imgShuffled=createImage(img.width、img.height、RGB);
//填充图像节索引
对于(int i=0;iint numSlices = 16;
PImage[] slices = new PImage[numSlices];
float x, y;
float size = 180;

void setup() {

  size(720, 720);

  
  for(int i = 0 ; i < numSlices; i++){
    slices[i] = loadImage((i+1) + ".png");
  }
  
  frameRate(1);
}

void draw() {
  background(255);
  
  for (int a = 0; a < numSlices; a++) {

    int rand = int(random(numSlices));

    image(slices[rand], x, y, size, size);
    x += size;

    if (a % 4 == 3) {
      y += size;
      x = 0;
    }
  }
  y = 0;
}
int numSlices = 16;
PImage[] slices = new PImage[numSlices];
float x, y;
float size = 180;

IntList indices = new IntList();

void setup() {

  size(720, 720);


  for(int i = 0 ; i < numSlices; i++){
    slices[i] = loadImage((i+1) + ".png");
    indices.append(i);
  }

  frameRate(1);
}

void draw() {
  background(255);
  // shuffle list
  indices.shuffle();
  // reset y
  y = 0;
  
  for (int a = 0; a < numSlices; a++) {

    int rand = indices.get(a);

    image(slices[rand], x, y, size, size);
    x += size;

    if (a % 4 == 3) {
      y += size;
      x = 0;
    }
  }
  
}
/**
 * Load and Display 
 * 
 * Images can be loaded and displayed to the screen at their actual size
 * or any other size. 
 */

PImage img;  // Declare variable "a" of type PImage
// shuffled image
PImage imgShuffled;
// list of indices to shuffle
IntList shuffleIndices = new IntList();
// configure image slicing rows/columns
int rows = 4;
int cols = 4;
// total sections
int numSections = rows * cols;
// image section dimensions
int sectionWidth;
int sectionHeight;

void setup() {
  size(640, 360);
  frameRate(1);
  // The image file must be in the data folder of the current sketch 
  // to load successfully
  img = loadImage("https://processing.org/examples/moonwalk.jpg");  // Load the image into the program
  // calculate section dimensions
  sectionWidth  = img.width / cols;
  sectionHeight = img.height / rows;
  // allocate a separate image to copy shuffled pixels into
  imgShuffled = createImage(img.width, img.height, RGB);
  // populate image section indices
  for(int i = 0 ; i < numSections; i++){
    shuffleIndices.append(i);
  }
}

void shuffleImage(){
  // shuffle the list
  shuffleIndices.shuffle();
  // Ta-da!
  println(shuffleIndices);
  // loop through each section
  for(int i = 0 ; i < numSections; i++){
    // index to row, col conversion
    int srcCol = i % cols;
    int srcRow = i / cols;
    // convert to pixel coordinates to copy from
    int srcX = srcCol * sectionWidth;
    int srcY = srcRow * sectionHeight;
    // get random / shuffled index
    int index = shuffleIndices.get(i);
    // same row, col, to pixel conversion to copy to
    int dstCol = index % cols;
    int dstRow = index / cols;
    int dstX = dstCol * sectionWidth;
    int dstY = dstRow * sectionHeight;
    // copy from original image to shuffled pixel coordinates
    imgShuffled.copy(img,srcX,srcY,sectionWidth,sectionHeight,dstX,dstY,sectionWidth,sectionHeight);
  }
}

void draw() {
  shuffleImage();
  // Displays the image at its actual size at point (0,0)
  image(imgShuffled, 0, 0);
}
for (int i = slices.length; i > 1; i--) {
  //choose a random index for the i-th element to be swapped with
  int j = (int)random(i);

  //swap them
  PImage temp = slices[j];
  slices[j] = slices[i-1];
  slices[i-1] = temp;
}
y = 0;