We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How variables, subroutines etc. work
Page Index Toggle Pages: 1
How variables, subroutines etc. work? (Read 742 times)
How variables, subroutines etc. work?
Oct 11th, 2006, 8:12pm
 
Hello,

I’m new to this forum. Someone at the idevgames forum suggested I try Processing as a programming tool.  

Just a quick intro: I’m a teacher in Iowa and have been using Metal BASIC with my students and to develop lesson specific software.  However, with newer OSX machines the graphic elements no longer work properly, thus the search for a new programming tool.

I have studied programming (Pascal), but never worked witih object oriented programming.
I’m having some trouble understanding how to set up subroutines, how global and local variables work, how to pass variables to subroutines etc.

If someone would be willing to help me out with this I would really appreciate it.

Here’s some code I’ve been playing with for a statistics program where kids try to find the percentages of colors of M and Ms in a bag.

I’d like to know how to break it up into subroutines.

Thanks for you time and any help you can give me,
Dennis Ward
Tri-County School
Thornburg, IA


size (800,600);

//The graphics below are in my data folder
//I want the array MandMs to be used globally or be
//passed to subroutines that need it.

PImage Green,Yellow,Red,Blue,Brown,Check;
Red = loadImage("Red.jpg");
Green = loadImage("Green.jpg");
Blue = loadImage("Blue.jpg");
Brown = loadImage("Brown.jpg");
Yellow = loadImage("Yellow.jpg");
Check = loadImage("Check.jpg");
PImage[] MandMs = new PImage[7];
MandMs[1]=Red;
MandMs[2]=Green;
MandMs[3]=Blue;
MandMs[4]=Brown;
MandMs[5]=Yellow;
MandMs[6]=Check;

//I want this to be a subroutine that has access to the array MandMs

 for(int i=0; i<800; i=i+50) {
 for(int j=0; j<600; j=j+50){
   int m = round(random(4))+1;
 image(MandMs[m], i, j);
 }
}
Re: How variables, subroutines etc. work?
Reply #1 - Oct 11th, 2006, 10:08pm
 
Code:
// Globals go outside functions, but should only be 
// declared, not given values.
PImage[] MandMs;

void setup()
{
size (800,600);

//The graphics below are in my data folder
//I want the array MandMs to be used globally or be
//passed to subroutines that need it.
PImage Green,Yellow,Red,Blue,Brown,Check;
Red = loadImage("Red.jpg");
Green = loadImage("Green.jpg");
Blue = loadImage("Blue.jpg");
Brown = loadImage("Brown.jpg");
Yellow = loadImage("Yellow.jpg");
Check = loadImage("Check.jpg");
//Now define how big MandMs is.
MandMs = new PImage[7];
//And assign the values.
MandMs[1]=Red;
MandMs[2]=Green;
MandMs[3]=Blue;
MandMs[4]=Brown;
MandMs[5]=Yellow;
MandMs[6]=Check;
}


//I want this to be a subroutine that has access to the array MandMs

//since MandMs is global, dont' need to pass it. If you did
// need to pass it, you'd use:
// void myFunc(PImage[] foo)
// and then use image(foo[m],i,j);

void myFunc()
{
for(int i=0; i<800; i=i+50) {
for(int j=0; j<600; j=j+50){
int m = round(random(4))+1;
image(MandMs[m], i, j);
}
}
}
Re: How variables, subroutines etc. work?
Reply #2 - Oct 12th, 2006, 12:05am
 
JohnG,
Thanks, that helps.
d ward
Now I need a mouse routine?
Reply #3 - Oct 13th, 2006, 8:24am
 
Guys,

Just to show I've used your advice I put the code I've developed so far below.

Now I'm stuck on how to deal with the mouse.

Before in METAL I had a procedure named GetAClick that I called when I wanted the program to  wait for a mouse click and then return with the x and y coords.

I'm not seeing a way to do this with Processing 0118.

Can you help me on this one?

Thanks for your time,
d ward

PImage MandMs[];
int Box[];
int Bag[];
int Cx[];
int Cy[];
int Guess[];
int Actual[];
int Diffs[];
boolean NewSet;
int SetsPicked;

void setup()
{
size (700,500);
background(0);
MandMs = new PImage[7];
MandMs[1] = loadImage("Red.jpg");
MandMs[2] = loadImage("Green.jpg");
MandMs[3] = loadImage("Blue.jpg");
MandMs[4] = loadImage("Brown.jpg");
MandMs[5] = loadImage("Yellow.jpg");
MandMs[6] = loadImage("Check.jpg");
Box = new int[101];
Actual = new int[6];
Bag = new int[21];
Cx = new int[21];
Cy = new int[21];
MainProgram();
}

void MainProgram()
{
 SetButtons();
 MakeBox();
 MakeBag();
 PickCoords();
 ShowBag();
}

void draw(){
}

void ShowBag()
{
 for (int i=1; i<=20; i++)
 {
   image(MandMs[Bag[i]], Cx[i]*50, Cy[i]*50);
 }
}

void PickCoords()
{
 int SetsPicked;
 int Newx;
 int Newy;
 boolean NewSet;
 int CxTemp, CyTemp;
   for (int i=1; i<=20; i++)
   {
     Cx[i]=0;
     Cy[i]=0;
   }
   SetsPicked = 0;
   while (SetsPicked <= 19)
   {
     CxTemp = round(random(11))+1;
     CyTemp = round(random(5))+1;
     NewSet = true;
     for (int i=1; i<=SetsPicked; i++)
     {
       if (CxTemp == Cx[i] && CyTemp == Cy[i])
       {
         NewSet=false;
       }
     }
       if(NewSet==true)
       {
         SetsPicked = SetsPicked + 1;
         Cx[SetsPicked] = CxTemp;
         Cy[SetsPicked] = CyTemp;
       }
     
   }
   for (int i=1; i<=20; i++)
   {
     print (Cx[i]+" "+Cy[i]+"---");
   }
}

void MakeBag()
{
 for (int i=1; i<=20; i++)
 {
   Bag[i]=0;
 }
   for (int i=1; i<=20; i++)
 {
   int n = round(random(100));
   Bag[i]=Box[n];
 }
}

void MakeBox()
{
 for (int i=0; i<100; i++)
 {
   Box[i]=0;
 }
int a,b,c,d,e;
a=0;
b=0;
c=0;
d=0;
e=0;
while (a+b+c+d+e!=100)
{
 a = round(random(1,50));
 b = round(random(1,50));
 c = round(random(1,50));
 d = round(random(1,50));
 e = round(random(1,50));
}
Actual[1]=a;
Actual[2]=b;
Actual[3]=c;
Actual[4]=d;
Actual[5]=e;
for (int i=1; i<=a; i++)
{
 Box[i] = 1;
}
for (int i=a+1; i<=a+b; i++)
{
 Box[i] = 2;
}
for (int i=a+b+1; i<=a+b+c; i++)
{
 Box[i] = 3;
}
for (int i=a+b+c+1; i<=a+b+c+d; i++)
{
 Box[i] = 4;
}
for (int i=a+b+c+d+1; i<=a+b+c+d+e; i++)
{
 Box[i] = 5;
}

}

void SetButtons()
{
 image(MandMs[1],200,450);
 image(MandMs[2],250,450);
 image(MandMs[3],300,450);
 image(MandMs[4],350,450);
 image(MandMs[5],400,450);
 image(MandMs[6],450,450);
}


Re: How variables, subroutines etc. work?
Reply #4 - Oct 13th, 2006, 11:48am
 
hey d,

here's a simple mouse click for you:

Code:
//Create an actual Point that you can reference
//Point is from the Java API (which you can use in Processing)
Point mousePoint = new Point(0,0);

void setup(){}

void draw(){}

void mousePressed(){
//Pass the current mousePoint into a function
//that updates its location
setToMousePoint(mousePoint);
println("x: "+mousePoint.getX()+", y: "+mousePoint.getY());
}

//Sets any given point to mouse coordinates
void setToMousePoint(Point p){
p.setLocation(mouseX,mouseY);
}
Re: How variables, subroutines etc. work?
Reply #5 - Oct 13th, 2006, 11:50am
 
hey d,

here's a simple mouse click for you:

Code:
//Create an actual Point that you can reference
//Point is from the Java API (which you can use in Processing)
Point mousePoint = new Point(0,0);

void setup(){}

void draw(){}

void mousePressed(){
//Pass the current mousePoint into a function
//that updates its location
setToMousePoint(mousePoint);
println("x: "+mousePoint.getX()+", y: "+mousePoint.getY());
}

//Sets any given point to mouse coordinates
void setToMousePoint(Point p){
p.setLocation(mouseX,mouseY);
}
Page Index Toggle Pages: 1