1

嗨,我正在构建一个老虎机游戏,但遇到了一些问题,我对处理很陌生。

我制作了这段代码,需要“轮子”是随机的,但它们都具有相同的图像(垂直)

我知道您将无法看到这些图像,所以如果您想要它们,这里有一些链接。

http://tinypic.com/r/i1jq7t/6(钻石1)

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
        }
      }
    }
 // }

我在获得“获胜者”方面也遇到了问题(如果左侧角匹配中的水平图像像素进入“获胜者”。

我非常感谢任何人可以提供的任何帮助。

4

1 回答 1

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

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

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

于 2012-11-13T22:43:46.617 回答