(Fixed)Need Help With My Code/If Statements :(

edited October 2017 in Questions about Code

Hi, I'm Doing A School Project About The Sol System In My Computer Science Class And My If Statements Don't Seem To Work As I'm Kind Of Ahead Of The Class So Haven't Had Time To Fully Understand Processing. Could Any Of You Tell Me What I Did Wrong? MY Code Unfinished:

int Menus=0;
int Trails=0;

float SunDiameter = 1.914;
float MercuryDiameter = 0.4879;
float MercuryOrbitDistance = 7.8;
float MercuryOrbitSpeed = 0;

float VenusDiameter = 1.2104;
float VenusOrbitDistance = 14.4;
float VenusOrbitSpeed = 0;

float EarthDiameter = 1.2742;
float EarthOrbitDistance = 20;
float EarthOrbitSpeed = 0;
float EarthX = 0;
float EarthY = 0;


float MoonDiameter = 0.3474;
float MoonOrbitDistance = 5;
float MoonOrbitSpeed = 0;

float MarsDiameter = 0.64779;
float MarsOrbitDistance = 30.4;
float MarsOrbitSpeed = 0;

float JupiterDiameter = 13.9822;
float JupiterOrbitDistance = 102;
float JupiterOrbitSpeed = 0;

float SaturnDiameter = 11.6464;
float SaturnOrbitDistance = 190.8;
float SaturnOrbitSpeed = 0;

float UranusDiameter = 5.0724;
float UranusOrbitDistance = 384;
float UranusOrbitSpeed = 0;

float NeptuneDiameter = 4.9244;
float NeptuneOrbitDistance = 602;
float NeptuneOrbitSpeed = 0;

float PlutoDiameter = 0.2374;
float PlutoOrbitDistance = 788;
float PlutoOrbitSpeed = 0;


//PImage Earth;
//PImage Stars;
//PImage MainMenu;
void keyPressed() {
  if (key =='T') {
    Trails=1;
  }
}

void mousePressed () {
  if (Menus==0) {
    if (mouseX>683 && mouseX<833 && mouseY>470 && mouseY<520) {
      Menus=2;
    }
  }
  if (Menus==0) {
    if (mouseX>583 && mouseX<946 && mouseY>570 && mouseY<620) {
      Menus=1;
    }
  }
}



void setup() {
  background(0);
  size(1366, 768);
  frameRate(60);
  //Earth= loadImage("Earth.png");
  //Stars= loadImage("Stars.jpg");
  //MainMenu = loadImage("MainMenu.jpg");
}

void draw() {
  if (Menus==0) {
    //image(MainMenu, 0, -20);
    fill(255);
    textSize(25);
    text("Sol System", 683, 384);
    fill(#8A15EA);
    rect(683, 470, 150, 50);
    fill(255);
    textSize(30);
    text("Play Scene", 683, 500);
    fill(#8A15EA);
    rect(583, 570, 360, 50);
    fill(255);
    textSize(30);
    text("Instructions And Secrets", 583, 600);
  }
  if (Menus==1) {
    background(0);
    fill(255);
    textSize(25);
    fill(255);
    textSize(10);
    text("Fun Facts About My Project : ", 633, 180);
    fill(255);
    textSize(10);
    text("Each Pixel For Distance Is 7.4 Million Kilometers ", 583, 200);
    fill(255);
    textSize(10);
    text("Planets And Gas Giants Are Scaled Correctly In Comparison To Each Other. The Sun Is Not ", 553, 220);
    fill(255);
    textSize(10);
    text("The Planets And Gas Giants Are Not Scaled Correctly In Terms Of Size To Distance Due To Size Of The Solar System", 488, 240);
    fill(255);
    textSize(30);
    text("Instructions:", 488, 280);
    fill(255);
    textSize(10);
    text("All You Have To Do Is Sit Back And Watch The Orbits Of The Planets.", 488, 300);
    text("Press The Coresponding Keys For The Planets MVEMJSUNP To Learn About Them", 488, 320);
    text("Click The Sun To Make It Become A Red Giant And Consume The Inner Planets.", 488, 340);
    text("Click T To Show The Trails Of The Planets.", 488, 360);
  }
  if (Menus==2 && Trails==0) {
    background(0);
  }
  if (Menus==2)
  {
    translate(width/2, height/2); // Moves The Coordinates To The Centre. So 0,0 is at the centre of the screen.
    noStroke(); // No Stroke For The Sun/Planets
    fill(#FCD440); // Color Of The Sun Even Though Its Actually White In Real Life :(
    ellipse(0, 0, SunDiameter, SunDiameter);
    pushMatrix();
    fill(#6F5757);
    rotate(MercuryOrbitSpeed);
    translate(MercuryOrbitDistance, 0);
    ellipse(0, 0, MercuryDiameter, MercuryDiameter);
    popMatrix();
    pushMatrix();
    fill(#C4A122);
    rotate(VenusOrbitSpeed);
    translate(VenusOrbitDistance, 0);
    ellipse(0, 0, VenusDiameter, VenusDiameter);
    popMatrix();
    pushMatrix();
    rotate(MarsOrbitSpeed);
    translate(MarsOrbitDistance, 0);
    ellipse(0, 0, MarsDiameter, MarsDiameter);
    popMatrix();
    pushMatrix();
    rotate(JupiterOrbitSpeed);
    translate(JupiterOrbitDistance, 0);
    ellipse(0, 0, JupiterDiameter, JupiterDiameter);
    popMatrix();
    pushMatrix();
    rotate(SaturnOrbitSpeed);
    translate(SaturnOrbitDistance, 0);
    ellipse(0, 0, SaturnDiameter, SaturnDiameter);
    popMatrix();
    pushMatrix();
    fill(#4BBDDB);
    rotate(UranusOrbitSpeed);
    translate(UranusOrbitDistance, 0);
    ellipse(0, 0, UranusDiameter, UranusDiameter);
    popMatrix();    
    pushMatrix();
    fill(#426DC6);
    rotate(NeptuneOrbitSpeed);
    translate(NeptuneOrbitDistance, 0);
    ellipse(0, 0, NeptuneDiameter, NeptuneDiameter);
    popMatrix();
    pushMatrix();
    fill(#426DC6);
    rotate(PlutoOrbitSpeed);
    translate(PlutoOrbitDistance, 0);
    ellipse(0, 0, PlutoDiameter, PlutoDiameter);
    popMatrix();
    fill(#2EACD1);
    rotate(EarthOrbitSpeed);
    translate(EarthOrbitDistance, 0);
    ellipse(0, 0, EarthDiameter, EarthDiameter);
    fill(#AAA3A9);
    rotate(MoonOrbitSpeed);
    translate(MoonOrbitDistance, 0);
    ellipse(0, 0, MoonDiameter, MoonDiameter);



    VenusOrbitSpeed += 0.03244444444;
    EarthOrbitSpeed += 0.02;
    MoonOrbitSpeed += 7.3;
    MarsOrbitSpeed += 0.01062590975;
    MercuryOrbitSpeed += 0.08295454545;
    JupiterOrbitSpeed += 0.00166666666;
    SaturnOrbitSpeed += 0.00067895576;
    UranusOrbitSpeed += 0.00023809523;
    NeptuneOrbitSpeed += 0.00012135922;
    PlutoOrbitSpeed += 0.00008045375;
  }
}
Tagged:

Answers

  • Could You Elaborate On The Reason Why My If Statements Don't Work? Also, I don't really wanna use arrays as I'm not duplicating my planets instead giving them their real-life sizes and orbit periods.

  • you don't actually tell us what is wrong with your code. are we supposed to guess?

    what is it meant to do? what does it do instead?

  • " I don't really wanna use arrays as I'm not duplicating my planets instead giving them their real-life sizes and orbit periods"

    This is a misunderstanding.

    Imagine a spreadsheet:

    name   | size | diameter | orbitDist | orbitSpeed |
    =======|======|==========|===========|============|
    Earth  |
    Pluto  |   
    

    Each column is an array. So in the name[] array you have "Earth" and "Pluto". In the size[] array, you have a number for Earth and for Pluto. In the diameter array etc. etc.

    Now you have 5 parallel arrays instead of 5*10=50 variables.

  • edited October 2017

    IT does everything correctly, and its suppose to reflect the solar system with all the ellipses I drew in the program. But it doesn't execute key presses when its looping the draw function specifically my if statement= if (Menus==2) {. It Loops everything inside it and the orbits work but any if statement or key press that is pressed or executed after its(if (Menus==2); execution doesn't seem to work. The Loops Continues On But Nothing New Can Be Added.

  • edited October 2017

    jeremeydouglass posted it before i saw your post. Will try it.

  • edited October 2017 Answer ✓

    Here is what your interactions currently say:

    • If you press T
      • Trails = 1
    • If you click the mouse
      • and if Menus==0
        • check where the mouse is
          • then Menus=1 or 2;

    That logic means that after pressing T once, the keyboard will no longer change anything, and after pressing the mouse once, the mouse will no longer change anything (menus will never be 0 again).

  • edited October 2017

    Hey so I converted all my variables into an array like you said so it has helped organize my code a bit I guess, but I still have a problem with my if statements, more specifically when my planets start orbiting or if you press the play button in my game you'll see what I mean If you run my code. I have a keypress which should execute if the key E is pressed after "Menu==2"(My Variable Used For Different Scenes In The Game And Is Given The Value When You Press Play In My Game). The Keypress should move to a blank black-grey background but it doesn't. Any Ideas?

    • It's At the Bottom Of My Code

      if (key=='E')
      
      void keyPressed () {
      
        {
          Menus=3;
      
          background(8);
      
          setup();
        }
      

      My Code:

      } 
      
      int Menus=0;
      
      float[]Earth={1.2742, 20, 0};
      
      float[]Venus={1.2104, 14.4, 0};
      
      float[]Mercury={0.4879, 7.8, 0};
      
      float[]Moon={0.3474, 5, 0};
      
      float[]Sun={1.914, 0, 0};
      
      float[]Mars={0.64779, 30.4, 0};
      
      float[]Jupiter={13.9822, 102, 0};
      
      float[]Saturn={11.6464, 190.8, 0};
      
      float[]Uranus={5.0724, 384, 0};
      
      float[]Neptune={4.9244, 602, 0};
      
      float[]Pluto={0.2374, 788, 0};
      
       void mousePressed () 
      {
        if (Menus==0)
        { 
      
          if (mouseX>583 && mouseX<733 && mouseY>470 && mouseY<520) 
          {
      
            background(8);
            Menus=2;
          }
          if (mouseX>503 && mouseX<863 && mouseY>570 && mouseY<620) 
          {
            Menus=1;
          }
        }
        if (Menus==1) 
        {
          if (mouseX>583 && mouseX<653 && mouseY>470 && mouseY<520) 
          {          
        background(8);
      
        Menus=0;
          }
        }
      }
      
      
      
      void setup() {
      
      background(0);
      
      size(1366, 768);
      
      frameRate(60);
      
      }
      
      void draw() {
        if (Menus==0) { 
      
          fill(255);
      
          textSize(25);
      
          text("Sol System", 583, 384);
      
          fill(#8A15EA);
      
          rect(583, 470, 150, 50);
      
          fill(255);
      
          textSize(30);
      
          text("Play Scene", 583, 500);
      
          fill(#8A15EA);
      
          rect(503, 570, 360, 50);
      
          fill(255);
      
          textSize(30);
      
          text("Instructions And Secrets", 503, 600);
      
        }
      
        if (Menus==1) {
      
          background(0);
      
          image(Instructions, 0, 0);
      
          fill(155);
      
          textSize(25);
      
          fill(155);
      
          textSize(10);
      
          text("Fun Facts About My Project : ", 633, 180);
      
          fill(155);
      
          textSize(10);
      
          text("Each Pixel For Distance Is 7.4 Million Kilometers ", 583, 200);
      
              fill(155);
      
              textSize(10);
      
      
      The Sun Is Not ", 553, 220);
      
              fill(155);
      
          textSize(10);
      
      
      
          fill(155);
      
          textSize(30);
      
          text("Instructions:", 488, 280);
      
          fill(155);
      
          textSize(10);
      
      
      
          rect(583, 470, 70, 50);
      
          fill(255);
      
          textSize(30);
      
          text("Back", 583, 500);
        }
      
        if (Menus==3) {
          setup();
        }
      
        if (Menus==2) // If Statement For The Actual Scene With The Planets Orbiting
        {
      
          fill(0, 10);
      
          rect(0, 0, width, height);
      
          fill(255);
      
          if (Menus==2) { // If Statement For Fading Stars
      
            ellipse(random(width), random(height), 2, 2);
      
            ellipse(random(width), random(height), 2, 2);
      
            ellipse(random(width), random(height), 2, 2);
      
            ellipse(random(width), random(height), 2, 2);
      
            ellipse(random(width), random(height), 2, 2);
      
            ellipse(random(width), random(height), 2, 2);
      
            ellipse(random(width), random(height), 2, 2);
          }
      
          translate(width/2, height/2); 
      
          noStroke();    
      
          fill(#FCD440); 
      
          ellipse(0, 0, Sun[0], Sun[0]);
      
          pushMatrix();
      
          fill(#6F5757);
      
          rotate(Mercury[2]);
      
          translate(Mercury[1], 0);
      
          ellipse(0, 0, Mercury[0], Mercury[0]);
      
          popMatrix();
      
          pushMatrix();
      
          fill(#C4A122);
      
          rotate(Venus[2]);
      
          translate(Venus[1], 0);
      
          ellipse(0, 0, Venus[0], Venus[0]);
      
          popMatrix();
      
          pushMatrix();
      
          rotate(Mars[2]);
      
          translate(Mars[1], 0);
      
          ellipse(0, 0, Mars[0], Mars[0]);
      
          popMatrix();
      
          pushMatrix();
      
          rotate(Jupiter[2]);
      
          translate(Jupiter[1], 0);
      
          ellipse(0, 0, Jupiter[0], Jupiter[0]);
      
          popMatrix();
      
          pushMatrix();
      
          rotate(Saturn[2]);
      
      
      
          translate(Saturn[1], 0);
      
          ellipse(0, 0, Saturn[0], Saturn[0]);
      
          popMatrix();
      
          pushMatrix();
      
          fill(#4BBDDB);
      
          rotate(Uranus[2]);
      
          translate(Uranus[1], 0);
      
          ellipse(0, 0, Uranus[0], Uranus[0]);
      
          popMatrix();    
      
          pushMatrix();
      
          fill(#426DC6);
      
          rotate(Neptune[2]);
      
          translate(Neptune[1], 0);
      
          ellipse(0, 0, Neptune[0], Neptune[0]);
      
          popMatrix();
      
          pushMatrix();
      
          fill(#426DC6);
      
          rotate(Pluto[2]);
      
          translate(Pluto[1], 0);
      
          ellipse(0, 0, Pluto[0], Pluto[0]);
      
          popMatrix();
      
          fill(#2EACD1);
      
          rotate(Earth[2]);
      
          translate(Earth[1], 0);
      
          ellipse(0, 0, Earth[0], Earth[0]);
      
          fill(#AAA3A9);
      
          rotate(Moon[2]);
      
          translate(Moon[1], 0);
      
          ellipse(0, 0, Moon[0], Moon[0]);
      
      
      
          // All The Orbital Speeds For All Planets
      
          Venus[2] += 0.03244444444;
          Earth[2] += 0.02;
          Moon[2] += 7.3;
          Mars[2] += 0.01062590975;
          Mercury[2] += 0.08295454545;
          Jupiter[2] += 0.00166666666;
          Saturn[2] += 0.00067895576;
          Uranus[2] += 0.00023809523;
          Neptune[2] += 0.00012135922;
          Pluto[2] += 0.00008045375;
        }
      }
      
      
      
      void keyPressed () {
      
        if (Menus==2)
      
        {
      
          if (key=='E')
      
          {
      
            Menus=3;
      
            background(8);
      
            setup();
      
          }
      

      }

      }

    Ah My Code isn't Displayed Correctly :/

  • edited October 2017

    You corrected the format of your code, well done

    Remark

    It’s not recommended to call setup(), since it gets called automatically and makes some preparations for the sketch that should only be done once.

    Make a function reset() instead with all the code lines which you have in setup() now without size() and call reset instead of calling setup().

  • edited October 2017

    Also you have an array for each planet now

    it would also be possible to have one array for each property so that the data for one planet would be distributed over all arrays (and for each planet in the same slot number in each array): sizes, weights, ages....

    Best way would be to have a class Planet and an array of that type but that's for later

  • edited October 2017

    I'll try that, but you have any idea why this line of code wouldn't execute. The Menus variable is just how I keep track of what page they are on so Menus==0 is my main menu while Menus==1 is my instructions menu. Currently, I want the code to step into another blank black background so I can start something new on it, but once draw function starts my Menus==2 page it doesn't execute anything else? :

             void keyPressed () {
    
              if (Menus==2)
    
              {
    
                if (key=='E')
    
                {
    
                  Menus=3;
    
                  background(8);
    
                  setup();
    
                }
    
  • edited October 2017 Answer ✓

    If you want to go there, there’s a tutorial objects

    https://www.processing.org/tutorials/objects/

    Also in the technical faq in the forum it’s covered (one of the first posts) (from many arrays to class/objects or something)

    https://forum.processing.org/two/discussion/8093/technical-faq#latest

  • edited October 2017 Answer ✓

    In mousePressed and also in draw:

    It might be safer to use when you use if (menue==

    an else if.... so that only one of the ifs can be run at one time so

    if(menue==0) {
    
    .....
    
    else if(menue==1) { // !!!!!!!
    
    ....
    

    or use switch(menue) with break;

    translate

    Concerning translate: look at pushMatrix and popMatrix, they isolate translations from each other

    Which again would be easier if you had arrays of properties then you could for loop over the planets; now you have lots of redundant code lines

    In setup: size should be the first command in setup

  • I'll try it. Thanks For The Help

  • Answer ✓

    here is an example where there are countries (your planets) with their earthquakes shown as circles. The data is stored in parallel arrays

    Here you have one array for each property so that the data for one country/planet would be distributed over all arrays (and for each planet in the same slot number in each array, country[0] and magnis[0] belonging to the same country number 0).

    see

    https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming

    // parallel arrays 
    String[] headlines = {"Indonesia", "Australia", "Japan", "Mexico", "USA", "Germany"};
    float[] magnis = { 12, 44, 32, 24, 12, 8.3 };
    String[] date = { "12/10.", "9/3.", "3/2", "2/4", "1/2", "12/27" };
    color[] colorBall = new color [headlines.length]; // { 122, 255, 0, color(255, 2, 2), color(255, 2, 2), 122 }; 
    float[] diameter = new float [headlines.length];
    
    // the length of the arrays 
    int numBalls = headlines.length; // headlines is in the lead here: We want as many balls as we have countries
    
    // ------------------------------------------------------------
    
    void setup() {
      size(1275, 700);
      textSize(20); 
    
      // control check : length of the arrays 
      if (headlines.length == magnis.length && 
        headlines.length == date.length && 
        headlines.length == colorBall.length &&
        headlines.length == diameter.length) {
        // OK 
        println("the lengths match");
      } else 
      {
        // Error
        println("the lengths don't match  +++++++++++++");
      }//else 
    
      // init some fields 
      for (int i = 0; i < numBalls; i++) {
        //define fill col and diameter 
        colorBall[i] = color(random(255), random(255), random(255));
        diameter[i] = random (12, 63);
      }//for
    } // function 
    
    void draw() {
      background(255, 255, 255);
    
      noStroke();
    
      for (int i = 0; i < numBalls; i++) {
        //fill ellipse
        fill(colorBall[i]); // the ellipses own color
        ellipse(i*23+88, i*66+55, diameter[i], diameter[i]);
    
        //show text
        fill(0); // black 
        text(headlines[i]+ " /"+magnis[i], i*23+88+diameter[i], i*66+55);
      }//for
      //
    }//function 
    //
    
  • I see. I also solved the key-press problem :/ it was a capital E. Though I'll also try your previous suggestions.

  • Answer ✓

    Good to hear

Sign In or Register to comment.