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 › First Processing project, need help.
Page Index Toggle Pages: 1
First Processing project, need help. (Read 659 times)
First Processing project, need help.
Sep 24th, 2009, 11:09am
 
For a school assignment, I am trying to simulate snow by having a number of ellipses move down the screen. I tried it with just one at first and it worked but what I´m having a hard time figuring out is how to continuously have it fall over and over in random places.
I wanna keep it as simple as possible as I only have a few days to complete it. I tried using an if-sentence to determine the Y-axis and so forth but I can´t seem to get it working.

CODE:


int size = 3;

float xpos, ypos;



float xspeed = 1.0;  // Speed of the shape
float yspeed = 1.0;  // Speed of the shape

int xdirection = 0;  // Left or Right
int ydirection = 1;  // Top to Bottom


void setup()
{
 size(640, 200);
 noStroke();
 frameRate(30);
 smooth();
 // Set the starting position of the shape
 xpos = random(0,640);
 ypos = 0;  
}

void draw()
{
 background(0);

 // Update the position of the shape
 xpos = xpos + ( xspeed * xdirection );
 ypos = ypos + ( yspeed * ydirection );
 snow();

}

void snow() {
 // Draw the shape
 ellipse(xpos+size/2, ypos+size/2, size, size);
 println(ypos);

}




Any help is appreciated Smiley
Re: First Processing project, need help.
Reply #1 - Sep 24th, 2009, 12:33pm
 
Not bad for a start, although I would subtract Code:
size/2 

instead. Smiley
I appreciate you heavily parametrized your sketch, lot of new programmers tend to put literal numbers all over the place! :-D
Note: you can use Code:
random(0, width); 

too.

Not sure what you attempted with the Code:
if 

. I don't want to give a complete code... I suggest to check the ypos against height and when it is above, restart at the top.
You might want an array of positions, to take in account all the flakes.
Launch them at random intervals.
The xpos is a bit too fixed, you can perhaps add a little random number (eg. between -2 and 2) to make it more erratic.

Just throwing some ideas... Wink
Page Index Toggle Pages: 1