My text gets written over when based on a variable.

edited January 2016 in Questions about Code

Here's my code:

int box11=100;
int box12=100;
int box13=100;
int box21=100;
int box22=100;
int box23=100;
int box31=100;
int box32=100;
int box33=100;
boolean p1Turn=true;
int Pscore=0;
int Dscore=0;
PImage Car;
PImage Den;

void setup() {
  size(720, 1000);
  Car = loadImage("Panthers.png");
  Den = loadImage("Broncos.png");
}

void draw() {
  line(240, 0, 240, 720);
  line(480, 0, 480, 720);
  line(0, 240, 720, 240);
  line(0, 480, 720, 480);
  textSize(50);
  fill(200, 80, 0);
  text("Broncos:", 370, 800);
  text(Dscore, 590, 800);
  fill(0, 0, 250);
  text("Panthers:", 10, 800);
  text(Pscore, 250, 800);
  fill(0);
}

void mousePressed() {
  if (mouseX>1 && mouseX<239 && mouseY>1 && mouseY<239 && box11==100) {
    if (p1Turn==true) {
      image(Car, 5, 5, 240, 240);
      p1Turn = false;
      box11=0;
    }
    else if (p1Turn==false) {
      image(Den, 0, 5, 240, 240);
      p1Turn = true;
      box11=1;
    }
  }
  if (mouseX>241 && mouseX<479 && mouseY>1 && mouseY<239 && box12==100) {
    if (p1Turn==true) {
      image(Car, 245, 5, 240, 240);
      p1Turn = false;
      box12=0;
    }
    else if (p1Turn==false) {
      image(Den, 240, 5, 240, 240);
      p1Turn = true;
      box12=1;
    }
  }
  if (mouseX>481 && mouseX<719 && mouseY>1 && mouseY<239 && box13==100) {
    if (p1Turn==true) {
      image(Car, 485, 5, 240, 240);
      p1Turn = false;
      box13=0;
    }
    else if (p1Turn==false) {
      image(Den, 480, 5, 240, 240);
      p1Turn = true;
      box13=1;
    }
  }
  if (mouseX>1 && mouseX<239 && mouseY>241 && mouseY<479 && box21==100) {
    if (p1Turn==true) {
      image(Car, 5, 245, 240, 240);
      p1Turn = false;
      box21=0;
    }
    else if (p1Turn==false) {
      image(Den, 0, 245, 240, 240);
      p1Turn = true;
      box21=1;
    }
  }
  if (mouseX>241 && mouseX<479 && mouseY>241 && mouseY<479 && box22==100) {
    if (p1Turn==true) {
      image(Car, 245, 245, 240, 240);
      p1Turn = false;
      box22=0;
    }
    else if (p1Turn==false) {
      image(Den, 240, 245, 240, 240);
      p1Turn = true;
      box22=1;
    }
  }
  if (mouseX>481 && mouseX<719 && mouseY>241 && mouseY<479 && box23==100) {
    if (p1Turn==true) {
      image(Car, 485, 245, 240, 240);
      p1Turn = false;
      box23=0;
    }
    else if (p1Turn==false) {
      image(Den, 480, 245, 240, 240);
      p1Turn = true;
      box23=1;
    }
  }
  if (mouseX>1 && mouseX<239 && mouseY>481 && mouseY<719 && box31==100) {
    if (p1Turn==true) {
      image(Car, 5, 485, 240, 240);
      p1Turn = false;
      box31=0;
    }
    else if (p1Turn==false) {
      image(Den, 0, 485, 240, 240);
      p1Turn = true;
      box31=1;
    }
  }
  if (mouseX>241 && mouseX<479 && mouseY>481 && mouseY<719 && box32==100) {
    if (p1Turn==true) {
      image(Car, 245, 485, 240, 240);
      p1Turn = false;
      box32=0;
    }
    else if (p1Turn==false) {
      image(Den, 240, 485, 240, 240);
      p1Turn = true;
      box32=1;
    }
  }
  if (mouseX>481 && mouseX<719 && mouseY>481 && mouseY<719 && box33==100)
  {
    if (p1Turn==true) {
      image(Car, 485, 485, 240, 240);
      p1Turn = false;
      box33=0;
    } 
    else if (p1Turn==false) {
      image(Den, 480, 485, 240, 240);
      p1Turn = true;
      box33=1;
    }
  }
  if ((box11==0 && box12==0 && box13==0) || (box11==0 && box21==0 && box31==0) || (box11==0 && box22==0 && box33==0) || (box21==0 && box22==0 && box23==0) || (box31==0 && box32==0 && box33==0) || (box31==0 && box22==0 && box13==0) || (box12==0 && box22==0 && box32==0) || (box31==0 && box32==0 && box33==0)) {
    box11=0;
    box12=0;
    box13=0;
    box21=0;
    box22=0;
    box23=0;
    box31=0;
    box32=0;
    box33=0;
    fill(0, 0, 250);
    textSize(59);
    text("TOUCHDOWN PANTHERS", 10, 240);
    Pscore+=7;
  }
  if ((box11==1 && box12==1 && box13==1) || (box11==1 && box21==1 && box31==1) || (box11==1 && box22==1 && box33==1) || (box21==1 && box22==1 && box23==1) || (box31==1 && box32==1 && box33==1) || (box31==1 && box22==1 && box13==1) || (box12==1 && box22==1 && box32==1) || (box31==1 && box32==1 && box33==1)) {
    box11=0;
    box12=0;
    box13=0;
    box21=0;
    box22=0;
    box23=0;
    box31=0;
    box32=0;
    box33=0;
    fill(200, 80, 0);
    textSize(59);
    text("TOUCHDOWN BRONCOS", 35, 240);
    Dscore+=7;
  }
}

Answers

  • You'll get a better response if you format your code. Here's how:

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • don't load images in draw()

    select code, hit ctrl-o to format it.

  • Ok I edited the code, koogs, but that doesn't fix the problem at hand...

  • Try "Dscore += 7" instead of "Dscore =+ 7"

  • Thanks for the suggestion, barefists, unfortunately it still doesn't fix the code.

  • Answer ✓

    Your problem is not related to variables. Since you don't use a background inside of draw(), you will have to draw a rectangle on top of the text in the bottom of your window. Something like this

    rect(0, 750, width, 250);

    before using text().

    A better approach would be to use a background all the images depending on the state of your box.. variables. (Inside of draw)

  • Answer ✓
    if (p1Turn==true) {
        ...
    } else if (p1Turn==false) {
        ...
    }
    

    if something isn't true then it is obviously false. so that simplifies to

    if  (p1Turn == true) {
        ...
    } else {
        ...
    }
    

    but if it's a boolean you don't need to check if it ==s true

    if  (p1Turn) {
        ...
    } else {
        ...
    }
    
  • edited January 2016

    int box13=100;

    int box21=100;

    int box22=100;

    int box23=100;

    int box31=100;

    int box32=100;

    int box33=100;

    boolean p1Turn=true;

    int Pscore=0;

    int Dscore=0;

    PImage Car;

    PImage Den;

    void setup(){

    size(720, 1000);

    Car = loadImage("Panthers.png");

    Den = loadImage("Broncos.png");

    }

    void draw(){

    line(240, 0, 240, 720);

    line(480, 0, 480, 720);

    line(0, 240, 720, 240);

    line(0, 480, 720, 480);

    rect(0, 750, width, 250);

    textSize(50);

    fill(200,80,0);

    text("Broncos:", 400, 800);

    text(Dscore,620,800);

    fill(0,0,250);

    text("Panthers:", 40, 800);

    text(Pscore,280,800);

    fill(0);

    }

    void mousePressed(){

    if (mouseX>1 && mouseX<239 && mouseY>1 && mouseY<239 && box11==100){
    
    if(p1Turn){
    
        image(Car,5,5,240,240);
    
        p1Turn = false;
    
        box11=0;
    
    }
    
    else {
    
      image(Den,0,5,240,240);
    
      p1Turn = true;
    
      box11=1;
    
    }
    
      }
    
    if (mouseX>241 && mouseX<479 && mouseY>1 && mouseY<239 &&
    

    box12==100){

    if(p1Turn){
    
        image(Car,245,5,240,240);
    
        p1Turn = false;
    
        box12=0;
    
    }
    
    else {
    
      image(Den,240,5,240,240);
    
      p1Turn = true;
    
      box12=1;
    
    }
    
      }
    
    if (mouseX>481 && mouseX<719 && mouseY>1 && mouseY<239 &&
    

    box13==100){

    if(p1Turn){
    
        image(Car,485,5,240,240);
    
        p1Turn = false;
    
        box13=0;
    
    }
    
    else {
    
      image(Den,480,5,240,240);
    
      p1Turn = true;
    
      box13=1;
    
    }
    
      }
    
    if (mouseX>1 && mouseX<239 && mouseY>241 && mouseY<479 &&
    

    box21==100){

    if(p1Turn){
    
        image(Car,5,245,240,240);
    
        p1Turn = false;
    
        box21=0;
    
    }
    
    else {
    
      image(Den,0,245,240,240);
    
      p1Turn = true;
    
      box21=1;
    
    }
    
      }
    
    if (mouseX>241 && mouseX<479 && mouseY>241 && mouseY<479 &&
    

    box22==100){

    if(p1Turn){
    
        image(Car,245,245,240,240);
    
        p1Turn = false;
    
        box22=0;
    
    }
    
    else {
    
      image(Den,240,245,240,240);
    
      p1Turn = true;
    
      box22=1;
    
    }
    
      }
    
    if (mouseX>481 && mouseX<719 && mouseY>241 && mouseY<479 && 
    

    box23==100){

    if(p1Turn){
    
        image(Car,485,245,240,240);
    
        p1Turn = false;
    
        box23=0;
    
    }
    
    else {
    
      image(Den,480,245,240,240);
    
      p1Turn = true;
    
      box23=1;
    
    }
    
      }
    
    if (mouseX>1 && mouseX<239 && mouseY>481 && mouseY<719 && 
    

    box31==100){

    if(p1Turn){
    
        image(Car,5,485,240,240);
    
        p1Turn = false;
    
        box31=0;
    
    }
    
    else {
    
      image(Den,0,485,240,240);
    
      p1Turn = true;
    
      box31=1;
    
    }
    
      }
    
      if (mouseX>241 && mouseX<479 && mouseY>481 && mouseY<719 && 
    

    box32==100){

    if(p1Turn){
    
        image(Car,245,485,240,240);
    
        p1Turn = false;
    
        box32=0;
    
    }
    
    else {
    
      image(Den,240,485,240,240);
    
      p1Turn = true;
    
      box32=1;
    
    }
    
      }
    
      if (mouseX>481 && mouseX<719 && mouseY>481 && mouseY<719 && 
    

    box33==100)

      {
    
    if(p1Turn){
    
        image(Car,485,485,240,240);
    
        p1Turn = false;
    
        box33=0;
    
    } 
    
    else {
    
      image(Den,480,485,240,240);
    
      p1Turn = true;
    
      box33=1;
    
    }
    
      }
    

    if ((box11==0 && box12==0 && box13==0) || (box11==0 && box21==0 &&

    box31==0) || (box11==0 && box22==0 && box33==0) || (box21==0 && box22==0

    && box23==0) || (box31==0 && box32==0 && box33==0) || (box31==0 &&

    box22==0 && box13==0) || (box12==0 && box22==0 && box32==0) ||

    (box13==0 && box23==0 && box33==0)){

    box11=0;
    
    box12=0;
    
    box13=0;
    
    box21=0;
    
    box22=0;
    
    box23=0;
    
    box31=0;
    
    box32=0;
    
    box33=0;
    
    fill(0,0,250);
    
    textSize(59);
    
    text("TOUCHDOWN PANTHERS", 8, 240);
    
    Pscore+=7;
    

    }

    if ((box11==1 && box12==1 && box13==1) || (box11==1 && box21==1 &&

    box31==1) || (box11==1 && box22==1 && box33==1) || (box21==1 && box22==1

    && box23==1) || (box31==1 && box32==1 && box33==1) || (box31==1 &&

    box22==1 && box13==1) || (box12==1 && box22==1 && box32==1) ||

    (box13==1 && box23==1 && box33==1)){

    box11=0;
    
    box12=0;
    
    box13=0;
    
    box21=0;
    
    box22=0;
    
    box23=0;
    
    box31=0;
    
    box32=0;
    
    box33=0;
    
    fill(200,80,0);
    
    textSize(59);
    
    text("TOUCHDOWN BRONCOS", 15, 240);
    
    Dscore+=7;
    

    }

    }

    That problem has been fixed, but my new problem is that once someone wins, they keep getting 7 points every time a square is clicked. I also need help with the "next drive" button because it's not as easy as I thought it was. Thank you!

  • please select code and press ctrl-o to format it for this forum

Sign In or Register to comment.