Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class tile
- {
- int Xmin;
- int Xmax;
- int Ymin;
- int Ymax;
- int type;
- boolean flipped;
- boolean paired;
- tile(int xmin, int xmax, int ymin, int ymax)
- {
- Xmin = xmin;
- Xmax = xmax;
- Ymin = ymin;
- Ymax = ymax;
- }
- void setType(int t)
- {
- type = t;
- }
- void changeFlip()
- {
- flipped = !flipped;
- }
- void flipReset()
- {
- flipped = false;
- }
- void pair()
- {
- paired = true;
- }
- }
- int numTiles = 4*3; //width * height to get how many tiles
- int flipCounter = 0;
- tile[] tiles = new tile[numTiles];
- PImage[] types;
- void setup()
- {
- size(500, 500);
- initTiles(4, 3);
- }
- void draw()
- {
- background(255); //redraw the background
- PImage[] types = { //types, in images
- loadImage("circle.png"), loadImage("square.png"), loadImage("spiral.png"), loadImage("heart.png"), loadImage("x.png"), loadImage("checker.png")
- };
- drawTiles(4, 3, types); //draw all the tiles every time
- }
- void initTiles(int w, int h)
- {
- int squareW = width/w, squareH = height/h; //calculate the square width and set the square height
- int squareX = 0, squareY = 0; //X starts at 0, Y stays at 0
- int i = 0;
- while (i < numTiles)
- {
- tiles[i] = new tile(squareX, (squareX) + (squareW), squareY, (squareY) + (squareH)); //give the object its location
- squareX += squareW; //move the X coordinate over by one box width
- if (squareX+squareW > width)
- {
- squareY += squareH;
- squareX = 0;
- }
- i++;
- }
- //predetermined pattern
- tiles[0].setType(1);
- tiles[1].setType(3);
- tiles[2].setType(0);
- tiles[3].setType(2);
- tiles[4].setType(4);
- tiles[5].setType(5);
- tiles[6].setType(0);
- tiles[7].setType(1);
- tiles[8].setType(2);
- tiles[9].setType(3);
- tiles[10].setType(4);
- tiles[11].setType(5);
- }
- void drawTiles(int w, int h, PImage[] type)
- {
- int squareW = width/w, squareH = height/h; //calculate the square width and set the square height
- int squareX = 0, squareY = 0; //X starts at 0, Y stays at 0
- int i = 0; //counting variables
- while (i < w*h)
- {
- if (!tiles[i].flipped) //if they are not flipped, make red
- {
- fill(255, 0, 0);
- rect(tiles[i].Xmin, tiles[i].Ymin, squareW, squareH);
- } else //if flipped, draw their type on the card
- {
- fill(255);
- rect(tiles[i].Xmin, tiles[i].Ymin, squareW, squareH);
- image(type[tiles[i].type], tiles[i].Xmin+1, tiles[i].Ymin+1 );
- }
- i++;
- }
- }
- void mouseClicked()
- {
- int j = 0;
- while (j < numTiles)
- {
- if ( ((mouseX > tiles[j].Xmin && mouseX < tiles[j].Xmax) && (mouseY > tiles[j].Ymin && mouseY < tiles[j].Ymax)) && !tiles[j].flipped ) //check where they clicked and flip that card if it isn't already flipped
- {
- tiles[j].changeFlip();
- flipCounter++;
- if (flipCounter > 2) //flip them back over if you exceed 2 cards at a time
- {
- for (int i = 0; i < numTiles; i++)
- {
- if (!tiles[i].paired) //flip all the unpaired ones back over
- {
- tiles[i].flipReset();
- }
- }
- flipCounter = 1; //continue and flip the one they wanted to flip and set the counter apropriately
- tiles[j].changeFlip(); //flip the next one without wait
- }
- }
- j++;
- }
- //this SHOULD check all tiles against each other and see if they're flipped and not paired and the same type
- /*for (int i = 0; i < numTiles; i++)
- {
- for (int k = 0; k < numTiles; k++)
- {
- if (tiles[i].type == tiles[k].type && !tiles[i].paired && !tiles[k].paired && tiles[i].flipped && tiles[k].flipped)
- {
- tiles[i].pair();
- tiles[k].pair();
- }
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement