Replace numbers with images for a puzzle game

edited March 2015 in How To...

I'm doing a homework assignment in which we need to make our own puzzle game with the coding the professor gave us. I want to replace the numbers with images but seems I have am having trouble putting replacing the images since it keeps on giving me certain errors

 gemArray[i][j].posX - 25 && mouseX < gemArray[i][j].posX + 25) {
        if (mouseY > gemArray[i][j].posY -25 && mouseY < gemArray[i][j].posY + 25) {
          gemArray[i][j].marked = true;
        }
      }
    }
  }
}

void MarkedTest()
{
  oneMarked = false;
  for ( int i = 0; i < gemArray.length; i++) {
    for (int j = 0; j < gemArray[i].length; j++) {
      if (gemArray[i][j].marked == true) {
        if (oneMarked == false) {
          oneMarked = true;
          markedOneColumn = i;
          markedOneRow = j;
        }
        else {
          SwitchGems(markedOneColumn, markedOneRow, i, j);
        }
      }
    }
  }
}

void SwitchGems(int oneColumn, int oneRow, int twoColumn, int twoRow) {
  holdNumber = gemArray[oneColumn][oneRow].myNumber;
  gemArray[oneColumn][oneRow].myNumber = gemArray[twoColumn][twoRow].myNumber;
  gemArray[twoColumn][twoRow].myNumber = holdNumber;

  gemArray[oneColumn][oneRow].marked = false;
  gemArray[twoColumn][twoRow].marked = false;
}

void CheckMatch() {
  for ( int i = 0; i < gemArray.length; i++) {
    for (int j = 0; j < gemArray[i].length; j++) {
      tileNumber = gemArray[i][j].myNumber;
      if (i < gemArray.length - 2) {
        if (gemArray[i+1][j].myNumber == tileNumber) {
          if (gemArray[i+2][j].myNumber == tileNumber) {
            gemArray[i][j].alive = false;
            gemArray[i+1][j].alive = false;
            gemArray[i+2][j].alive = false;
          }
        }
        //   Shaan hugs trees
        //                    a lot
      }
      if (j < gemArray[i].length - 2) {
        if (gemArray[i][j+1].myNumber == tileNumber) {
          if (gemArray[i][j+2].myNumber == tileNumber) {
            gemArray[i][j].alive = false;
            gemArray[i][j+1].alive = false;
            gemArray[i][j+2].alive = false;           
          }
        }
      }
    }
  }
}
class Gem{
  int column, row, posX, posY, myNumber;
  boolean marked;
  boolean alive;
  int aliveTimer;
  
  Gem(int myColumn, int myRow){
    column = myColumn;
    row = myRow;
    posX = 50 + column * 100;
    posY = 50 + row * 100;
    
    myNumber = (int)random(1, 10);
    alive = true;
  }
  
  void update(){
    SetNewNumber();
    if (marked == true) { strokeWeight(7); }
    else { strokeWeight(3); }
    
    ellipse(posX, posY, 50, 50);
    if (alive == true) { text(myNumber, posX - 10, posY + 14); }
  }
  
  void SetNewNumber(){
    if(alive == false){
     if(aliveTimer < 120){
      aliveTimer++;
     }
     else{
      myNumber = (int)random(1, 10);
      aliveTimer = 0;
      alive = true;
     }
    }
  }
}

Is there anyway I can get images like this one as a number?

Tagged:

Answers

  • "it keeps on giving me certain errors"
    What errors? Obviously, we cannot run the code you gave, so we can't reproduce these errors.
    Apparently, the shown code is cut in the middle of a function, and there is no setup() nor draw().
    Beside, in this code, I don't see any reference to an image.

  • If you can display the numbers 0 to N in your sketch, you can have an array of N images, and use the numbers as an index for that array instead. Here's an example that uses an array of colors instead of images.

    color[] colors = { color(255,0,0), color(0,255,0), color(0,0,255) };
    
    void setup(){
      size(220,220);
      background(0);
      for(int i=0;i<colors.length;i++){
        fill(colors[i]);
        text("" + i, 20+20*i, 20);
      }
      noLoop();
    }
    
Sign In or Register to comment.