随机图像并获得胜利者(处理)

时间:2012-11-13 17:10:36

标签: processing

您好我正在建立一个老虎机游戏,并且遇到一些问题我处理起来很新。

我已经制作了这个代码并需要" wheel"是随机的,但它们都有相同的图像(垂直)

我意识到你无法看到图片,所以这里有一些链接,如果你需要它们。

http://tinypic.com/r/i1jq7t/6(diamond1)

http://tinypic.com/r/29fe5cg/6(cherry1)

http://tinypic.com/r/sen1jo/6(bell1)

int numFrames = 3;  // The number of frames in the animation
int frame = 0;
int spincount = 0;
int state = 0;
PImage[] images1 = new PImage[3];

PImage[] images2 = new PImage[3];

PImage[] images3 = new PImage[3];

void setup() {
  size(1080, 720);
  frameRate(12);

  // wheel 1 
  images1[0]  = loadImage("bell1.png");
  images1[1]  = loadImage("cherry1.png"); 
  images1[2]  = loadImage("diamond1.png");


  // wheel 3
  images3[0]  = loadImage("cherry1.png");
  images3[1]  = loadImage("bell1.png"); 
  images3[2]  = loadImage("diamond1.png");


  // wheel 2
  images2[0]  = loadImage("diamond1.png");
  images2[1]  = loadImage("bell1.png"); 
  images2[2]  = loadImage("cherry1.png");

} 

void draw() { 
  background(o);
  //test state to see if I should be spinning
  if(state == 1) {
    spin();   
  } 

}

//if a key is pressed then set the wheel to spin 
void keyReleased() {
 state = 1;
}


void spin() {
    //spin for 5 times the break out  
  if (frame == 3) { 
    frame = 0; 
    spincount ++;
      if (spincount == 10) { 
        state = 0;
        spincount = 0;
        //check if its a win and do stuff
        winner();
      }   
  }
    // wheel 1  
    image(images1[frame], 20, 0);
    image(images1[frame], 20, 170); //this is the image to test
    image(images1[frame], 20, 340);

    // wheel 2

    image(images3[frame], 200, 0);
    image(images3[frame], 200, 170); //this is the image to test
    image(images3[frame], 200, 340);

    // wheel 3

    image(images2[frame], 400, 0);
    image(images2[frame], 400, 170); //this is the image to test
    image(images2[frame], 400, 340);


    frame ++;

  }  



 void winner() {

    //are the names of the images the same   
    //if ((images3[frame].get(0,0)) == (images2[frame].get(0,0)) == (images1[frame].get(0,0))) {
       // display a question from you list of questions by generating a random number and selecting the text

       // wait for an answer
      for (int i = 0; i < 400; i = i+1) {
        if (keyPressed == true) {
          // whats the key is it correct
        }
        if (i > 400) {
        //display times up
        }
      }
    }
 // }

我在获得&#34;胜利者&#34;时遇到了问题。 (如果左侧角落中的水平图像的像素匹配到&#34;获胜者&#34;。

我真的很感激任何人都可以提供帮助。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

在绘图功能中,您可能想要绘制黑色背景,但是您当前使用字母“o”而不是数字0(零)。

您可能想要在绘图功能中将当前滚轮状态绘制到屏幕上,此时您只需要一个黑色窗口,除非您正在旋转。

您无法直接比较颜色。

以下是一些修改后的代码:

int numFrames = 3;  // The number of frames in the animation
int maxSpin = 10;
int frame = 0;
int spincount = 0;
boolean spinning = false;
boolean checkWinner = false;
PImage[] images1 = new PImage[3];
PImage[] images2 = new PImage[3];
PImage[] images3 = new PImage[3];

void setup() {
  size(1080, 720);
  frameRate(12);

  // wheel 1 
  images1[0]  = loadImage("bell1.png");
  images1[1]  = loadImage("cherry1.png"); 
  images1[2]  = loadImage("diamond1.png");


  // wheel 3
  images3[0]  = loadImage("cherry1.png");
  images3[1]  = loadImage("bell1.png"); 
  images3[2]  = loadImage("diamond1.png");


  // wheel 2
  images2[0]  = loadImage("diamond1.png");
  images2[1]  = loadImage("bell1.png"); 
  images2[2]  = loadImage("cherry1.png");
} 

void draw() { 
  background(0);
  //test state to see if I should be spinning

  // wheel 1  
  image(images1[frame], 20, 0);
  image(images1[frame], 20, 170); //this is the image to test
  image(images1[frame], 20, 340);

  // wheel 2

  image(images3[frame], 200, 0);
  image(images3[frame], 200, 170); //this is the image to test
  image(images3[frame], 200, 340);

  // wheel 3

  image(images2[frame], 400, 0);
  image(images2[frame], 400, 170); //this is the image to test
  image(images2[frame], 400, 340);

  if (spinning) {
    spin();
  }

  //this draws circles stroked in the color of the pixel at their center
  //this is just to show how the code works, you can remove this
  noFill(); // make the circles transparent
  loadPixels(); // required before using pixels[]
  color wheel1 = pixels[170*width+20];
  color wheel2 = pixels[170*width+200];
  color wheel3 = pixels[170*width+400];
  stroke(wheel1);
  ellipse(20, 170, 10, 10);
  stroke(wheel2);
  ellipse(200, 170, 10, 10);
  stroke(wheel3);
  ellipse(400, 170, 10, 10);
}

//if a key is pressed then set the wheel to spin 
void keyReleased() {
  if(!spinning) spinning = !spinning;
}


void spin() {
  //spin for maxSpin times then break out  

  if (frame == numFrames-1) { 
    spincount ++;
    if (spincount == maxSpin) { 
      spinning = !spinning;
      spincount = 0;
      //check if its a win and do stuff
      winner();
    }
    else frame = 0;
  }
  else frame++;
}  



void winner() {
  loadPixels();
  color wheel1 = pixels[171*width+21];
  color wheel2 = pixels[171*width+201];
  color wheel3 = pixels[171*width+401];
  if(hue(wheel1) == hue(wheel2) && hue(wheel2) == hue(wheel3)) println("winner");
  else println("not a winner");
}

这包括检查图像上角落颜色的理想方法,但我建议只跟踪当前正在中心显示的图像。

我希望这至少会让你朝着正确的方向前进。