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 to make my code do what I want
Page Index Toggle Pages: 1
How to make my code do what I want (Read 511 times)
How to make my code do what I want
Nov 26th, 2009, 3:42am
 
First of all: I'm a complete Processing-Newby, I mean a REAL newby learning everything about programming for the first time and just via Internet so there a many things I don't understand or didn't get yet.

Therefore I'm not making any games or realy usefull programs but just small stuff to help me learn and understand the processing language (so please don't wonder how anyone can have as "small" problems, I realy need some help)
and I'm German so chances are that I say something gramatically wrong or misspell words.

But now to something completly different: My problem.

I want to write a sketch in which an amount of balls (7+) bounce through the picture and change direction as wel as color(randomly) everytime they hit the "border".
The single balls shall move with different speeds and "bounce" at different spots.


int radius = 30;
float xpos,ypos;

float xspeed=random(1,10);
float yspeed=random(1,10);

int xdirection = 1;
int ydirection = 1;
float r=random (255);
float b=random (255);
float g=random (255);

void setup ()
{
 size (800,800);
 noStroke ();
 frameRate(30);
 smooth();
 xpos = 0;
 ypos = 800-radius;
}

void draw ()
{
 background (0);
 fill (r,g,b);

 xpos=xpos+(xspeed*xdirection);
 ypos=ypos-(yspeed*ydirection);

 if (ypos<0+radius||ypos>800-radius) {
   ydirection*=-1;
   r=random (255);
   b=random (255);
   g=random (255);
 }
 
 if(xpos>800-radius||xpos<0) {
   xdirection*=-1;
   r=random (255);
   b=random (255);
   g=random (255);
 }
 
ellipse(xpos, ypos,radius,radius);

}



this is my sketch so far,
if I increase the number of balls, they change direction as soon as the first "hits" the "side-border"
thats perfectly logical to me but what do I have to do to make every ball jump individually?

I tried a ball class but since all about Processing is realy new to me, I couldn't get a class that actually worked, I don't know what part of the comands to write in the class and which to write in other parts of the code and where there (void draw, or outside of all voids, or should I build a void move/void display)?

I hope someone can help me and thanks for that Smiley
Re: How to make my code do what I want
Reply #1 - Nov 26th, 2009, 4:39am
 
Hi, well all you need to do is assign individual variables to each ball, or each ball need its individual variables, how to do that? well there are many ways, you already know how to move them so all you need is to check out how to create and work with a Class, it is not mandatory but it will be best if you create a Ball Class.

If you are starting to programing in general, I will recomend to get one of the processings books specially the blue or the orange, they will illustrate how to work with classes.

Hope that helps
rS
Re: How to make my code do what I want
Reply #2 - Nov 26th, 2009, 4:54am
 
Ok, here it is what I said about the classes using your code, I hope this help you get started, sorry I didnt comment the code

Here is the main sketch code
Code:
int numBalls = 10;
Ball[] balls = new Ball[numBalls];

void setup() {
size (800,800);
noStroke ();
frameRate(30);
smooth();

for (int i = 0; i < numBalls; i++) {
  balls[i] = new Ball();
}
}

void draw() {
 background(0);

 for (int i = 0; i < numBalls; i++) {
    balls[i].update();
    balls[i].display();
 }
}


and here is the Ball Class this needs to be added to a new tab with the name "Ball" Code:
class Ball {

 int radius = 30;
 float xpos, ypos;

 float xspeed = random(1, 10);
 float yspeed = random(1, 10);

 int xdirection = 1;
 int ydirection = 1;

 float r = random(255);
 float b = random(255);
 float g = random(255);


 // class constructor
 Ball() {
   xpos = 0;
   ypos = 800 - radius;
 }
 
 
 void update() {
   xpos = xpos + (xspeed * xdirection);
   ypos = ypos - (yspeed * ydirection);

   if (ypos < 0 + radius || ypos > 800 - radius) {
     ydirection*=-1;
     r = random(255);
     b = random(255);
     g = random(255);
   }

   if (xpos > 800 - radius || xpos < 0) {
     xdirection *= -1;
     r = random (255);
     b = random (255);
     g = random (255);
   }
 }
 
 void display() {
   fill(r, g, b);
   ellipse(xpos, ypos,radius,radius);
 }

}
Re: How to make my code do what I want
Reply #3 - Nov 26th, 2009, 5:17am
 
thank you very much

yeah, I definitely gonna buy a book about processing, can't totally decide on which one would be better but I think I'll take the orange Wink
Page Index Toggle Pages: 1