We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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;
}
}
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?
This is a misunderstanding.
Imagine a spreadsheet:
Each column is an array. So in the
name[]
array you have "Earth" and "Pluto". In thesize[]
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.
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.
jeremeydouglass posted it before i saw your post. Will try it.
Here is what your interactions currently say:
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).
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
My Code:
}
}
Ah My Code isn't Displayed Correctly :/
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest
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().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
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? :
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
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
or use
switch(menue)
withbreak;
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
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
I see. I also solved the key-press problem :/ it was a capital E. Though I'll also try your previous suggestions.
Good to hear