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.
Page Index Toggle Pages: 1
Rain Game (Read 1496 times)
Rain Game
Jun 9th, 2010, 1:59am
 
Hi guys. A few people here helped me generate a rain effect and now I'm almost finished making it into a kind of game but there's one thing I can't figure out.



Rain r1;
DraggableObject draggable;

int numDrops = 10;
Rain[] drops = new Rain[numDrops]; // Declare and create the array

int numLadies = 0;
Poshlady[] lady = new Poshlady[3000];

void setup() {
 size(600,600);
 background(255);
 smooth();
 noStroke();
 //Loop through array to create each object
 for (int i = 0; i < drops.length; i++) {

   drops[i] = new Rain(); // Create each object
   r1 = new Rain();
   draggable = new DraggableObject(width/2, height/2);
 }
 lady[0] = new Poshlady();
}

void draw(){
 fill(255,80);
 rect(0,0,600,600);
 //Loop through array to use objects.
 for (int i = 0; i < drops.length; i++) {
   drops[i].fall();
   float distance = dist(drops[i].r, drops[i].y, draggable.x, draggable.y);
   if(distance < drops[i].radius + draggable.radius) {
     drops[i].r = random(600);
     drops[i].y = random(-200);
   }
 }
 draggable.draw();

 if (numLadies == lady.length - 1) exit();

 if (random(1000)>997) {
   lady[++numLadies] = new Poshlady();
 }
 for (int i = 0; i < numLadies; i++) {
   lady[i].walk();
   float distance2 = dist(lady[i].x, lady[i].y, drops[i].r, drops[i].y);
   if(distance2 < lady[i].radius + drops[i].radius) {
     exit();

   }
 }
}

void mousePressed() {
 draggable.mousePressed();
}

void mouseDragged() {
 draggable.mouseDragged();
}

void mouseReleased() {
 draggable.mouseReleased();
}




class Rain {
 float r = random(600);
 float y = random(-height);
 float radius = 5;


 void fall() {
   y = y + 7;
   fill(0,10,200,180);
   ellipse(r, y, 2*radius, 2*radius);

   if(y>height){
     r = random(600);
     y = random(-200);
   }
 }
}


class DraggableObject {
 float x, y;
 float radius;
 boolean drag;
 float dragX, dragY;

 DraggableObject(float _x, float _y) {
   x = _x;
   y = _y;
   radius = 30;
   drag = false;
   dragX = 0;
   dragY = 0;
 }

 boolean inside(float ix, float iy) {
   return (dist(x, y, ix, iy) < radius);
 }

 void draw() {
   ellipseMode(CENTER);
   fill(200,0,0);
   ellipse(x, y, 2*radius, 2*radius);
 }

 void mousePressed() {
   drag = inside(mouseX, mouseY);
   if (drag) {
     dragX = mouseX - x;
     dragY = mouseY - y;
   }
 }

 void mouseDragged() {
   if (drag) {
     x = mouseX - dragX;
     y = mouseY - dragY;
   }
 }

 void mouseReleased() {
   drag = false;
 }
}


class Poshlady {
 float x = random(width,width+width/3);
 float y = height-height/5;
 float radius = 40;
 float r = random(3);


 void walk() {
   x = x - r;
   fill(0,10,200,180);
   ellipseMode(CENTER);
   ellipse(x, y, 2*radius, 2*radius);
 }
}


Eventually the red circle will be an umbrella and the large blue ones will be 'posh ladies' you have to protect from the rain (random I know). The problem is for some reason most of the time you can get away with the rain touching the blue circles (it is supposed to exit if that happens for the moment..). Sometimes it woks and other times the rain goes straight through. Sad Any ideas?

Re: Rain Game
Reply #1 - Jun 9th, 2010, 2:22am
 
You check lady[i] vs. drops[i], ie. only vs. one (semi-random) drop per lady...
You need two loops!
Re: Rain Game
Reply #2 - Jun 9th, 2010, 2:31am
 
Hi, thanks for taking the time to help me out again.. I'm still a little confused though. I can't get my head around how to use the second loop..How would I use two? sorry for all the nooby questions lol I've only been learning a few weeks..
Re: Rain Game
Reply #3 - Jun 9th, 2010, 4:49am
 
No problem, and your game starts to be fun... Smiley
The double loop is:
Code:
 for (int i = 0; i < numLadies; i++) {
lady[i].walk();
for (int j = 0; j < drops.length; j++) {
float distance = dist(lady[i].x, lady[i].y, drops[j].r, drops[j].y);
if (distance < lady[i].radius + drops[j].radius) {
println("Hit!");
exit();
}
}
}
Page Index Toggle Pages: 1