memory game: score button and reset button

edited October 2013 in Questions about Code

i have the game working. i just need to make the buttons work. reset, to restart the game, and score, to display the score. right now i can display the score but if i press it again, the new score displays over the original. please help!

//variables

int numOfTiles = 4;
int numOfImages = 4;
PImage images[] = new PImage[numOfImages];
int assignment[] = new int[numOfTiles*numOfTiles];
int correct[] = new int[numOfTiles * numOfTiles];
int score = 0;


void setup()
{
  size(numOfTiles*100, numOfTiles*100+200);
  background(255);
  for(int i =0; i< numOfImages; i++)
  {
    images[i] = loadImage("pic" + i + ".jpg");
  }
  init(numOfTiles);
  assign(numOfTiles, numOfImages);
} 

void draw() 
{
  if( s1!= -1 && s2!= -1)
  {if(assignment[s1]!= assignment[s2] && s1!=s2)
   {
    myDelay(2000); 
    score = score - 10;
    rect((s1%numOfTiles)*100,(s1/numOfTiles)*100,100,100);
    rect((s2%numOfTiles)*100,(s2/numOfTiles)*100,100,100);
    }
    else
  {
    score = score + 25;
    correct[s1] = 1;
    correct[s2] = 1;
    println("Current Score is " + score);
  }
  s1 =-1;
  s2 =-1;
  }

}

void myDelay(int delay)
{
 int time = millis();
while(millis() < time + delay); 
}


int s1 = -1, s2 = -1;
void mousePressed()
{
  if(mouseY < numOfTiles*100)
  {
 int x = mouseX/100;
 int y = mouseY/100;

if (s1 == -1) 
{ 
  s1 = y*numOfTiles + x;
  if(correct[s1] == 1)
  {
    s1 = -1;
  }
  else
  {
    image(images[assignment[s1]], x*100, y*100, 100, 100);
  }
}
else if (s2 == -1)
{
 s2 = y*numOfTiles + x;
 if(correct[s2] == 1)
 {
  s2 = -1;
 }
 else
  {
   image(images[assignment[s2]], x*100, y*100, 100, 100);
   }
  }
 }

 if(mouseX>200 && mouseY>500)
 {
   text("Score ="+ score, 80, 480);
 }
}

void assign(int n1, int n2)
{
 for(int i=0 ; i<n1*n1 ; i++)
  {
   assignment[i] = -1;
  } 
  int count = 0;
  while(count < n1*n1)
  {
    int v1 = int(random(n1*n1));
    int v2 = int(random(n1*n1));
    if(assignment[v1] == -1 && assignment[v2] == -1 && v1!=v2)
    {
    assignment[v1] = int(random(n2));
    assignment[v2] = assignment[v1];
    count+=2;

    }
  }
}


void init(int n)
{
  fill(255);
  rect(0, n*100, n*100, n*100+100);
  textSize(35);
  fill(0);
  text("Memory Game", 80, n*100+50);
  fill(70,14, 55);
  PImage reset = loadImage("resetButton.jpg");
  image(reset,0, n*100+100, 200, 100);
  PImage score = loadImage("scoreButton.jpg");
  image(score, 200, n*100+100, 200, 100);
  for(int i=0; i<n ; i++)
  {
    for(int j=0 ; j<n ; j++)
    {
      rect(i*100, j*100, 100, 100);
    }
  }
}

void displayScore(int n)
{
  if(mouseX > 200 && mouseY > 450)
 { 
  if (mousePressed == true)
  {
    text("Score is",80, n*100+70);
  }
 }
}

Answers

  • i have deleted the void displayScore at the very end

  • Please paste your code properly, see here how. The way it is, is vary hard to read the code :)

  • edited October 2013

    A shortcut to post code: B-)

    • Highlight it.
    • CTRL + K.
    • Deleted the duplicate thread.
    • Moved this one to the proper category (Using Processing is more for questions on the PDE!)
    • Reformatted code.
  • Quick glance at the code:

    • Use background() at the start of draw(), might solve one of your problem.
    • Don't use a delay like this. Particularly like this, hogging the CPU! Processing has its own delay(), and in general, we avoid to use it, better let draw() do its stuff, and find out when to change things when millis() reaches a given (relative) value.
Sign In or Register to comment.