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 & HelpPrograms › Adding on to Shiffman's "Raindrop Catcher"
Page Index Toggle Pages: 1
Adding on to Shiffman's "Raindrop Catcher" (Read 1352 times)
Adding on to Shiffman's "Raindrop Catcher"
Mar 26th, 2010, 6:46pm
 
The link to the game and his code may be found here:

http://www.learningprocessing.com/examples/chapter-10/example-10-10/

What I'm trying to do sounds simple. I'm trying to have a rectangle appear on screen with its length decreasing when a raindrop is missed. Under Shiffman's "Drops" tab, he has a boolean statement "boolean reachedBottom() {". If I want to check if this is true, what do I do? I tried:
Code:

if (reachBottom()) {
(rectlength=rectlength-1);
}


to no avail. I don't understand how this boolean is set up. Why is it set up as a function?

I figured there was a different way to accomplish this same task, so I tried:
Code:

if (y > height + r*4) {
(rectlength=rectlength-1);
}


and placed it under the "Drops" tab. Yet again, to no avail.

The next thing I'm attempting to do is have the raindrops become faster after 80 raindrops have fallen. Will this involve an edit to the array itself? I'm very lost on that one.

There are a few other things I'm trying to do with the program yet I think I can manage it on my own once I understand how the two tasks I mentioned can be accomplished.

Thank you very much for any help you can give me. It's ok if you guys don't want to write out the actual code, but some helpful advice on what I'm doing wrong and how to accomplish what I want would be nice.  Smiley



Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #1 - Mar 26th, 2010, 8:04pm
 
Drop is a class and reachedBotttom() is a function of that class. You can only call it on an object of the class ...i.e a drop. Try adding this to the for loop in the main tab....

for (int i = 0; i < totalDrops; i++ ) {
   drops[i].move();
   drops[i].display();
   if (catcher.intersect(drops[i])) {
     drops[i].caught();
   if(drops[i].reachedBottom())
   (rectlength=rectlength-1);
   }
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #2 - Mar 26th, 2010, 9:42pm
 
Thanks for the response Giles. Yet when I run it with that coding, I get a syntax error, "insert 'AssignmentOperator expression' to complete Expression". I've come across this error before and I remember it was easy to fix, yet I can't find how to fix it here. I've defined float rectlength=80; at the top.  Huh
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #3 - Mar 27th, 2010, 3:01pm
 
I've been messing with that bit of code you gave me and it still isn't working. I'm certain there's nothing misspelled, there's the right number of {'s and }'s and all. Does anyone see the problem?
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #4 - Mar 27th, 2010, 3:22pm
 
I'll download the project and have a look.....
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #5 - Mar 27th, 2010, 3:35pm
 
The problem was that the if statement was inside the other if statement (my mistake).... so I moved it outside. I also added my own rectangle code, and it works OK.... except....

Now you have the problem that the "reached bottom" is being detected every frame for the raindrops below the bottom, so that the rectangle's size gets decreased very rapidly. I'll let you decide how to solve this yourself (if you get stuck let me know).

Code:
float rectlength=80;

Catcher catcher;    // One catcher object
Timer timer;  // One timer object
Drop[] drops; // An array of drop objects
int totalDrops = 0; // totalDrops

void setup() {
 size(400,400);
 smooth();
 noStroke();
 catcher = new Catcher(32); // Create the catcher with a radius of 32
 drops = new Drop[1000];    // Create 1000 spots in the array
 timer = new Timer(300);   // Create a timer that goes off every 2 seconds
 timer.start(); // Starting the timer
}

void draw() {
 background(255);
 fill(255,0,0);
 rect(20, height-20, rectlength, 10);
 // Set catcher location
 catcher.setLocation(mouseX,mouseY);
 // Display the catcher
 catcher.display();
 
 // Check the timer
 if (timer.isFinished()) {
   // Deal with raindrops
   // Initialize one drop
   drops[totalDrops] = new Drop();
   // Increment totalDrops
   totalDrops ++ ;
   // If we hit the end of the array
   if (totalDrops >= drops.length) {
totalDrops = 0; // Start over
   }
   timer.start();
   
 }
 
 // Move and display all drops
 for (int i = 0; i < totalDrops; i++ ) {
   drops[i].move();
   drops[i].display();
   if (catcher.intersect(drops[i])) {
drops[i].caught();

   }
   if(drops[i].reachedBottom())
   rectlength=rectlength-1;
 }
}
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #6 - Mar 27th, 2010, 5:01pm
 
I thought about having a sort of "timer" start once the first drop is missed, and setting rectlength=rectlenght-.1 instead of 1, yet I'm still not so sure I like it like that (especially when the rectangle is completely depleted, it starts building from the left). I might try using the constrain() function.

I want when 10 drops are missed, to have a screen saying "You lose" or something to that effect. I intend to add an image and font and stuff, but I can handle that. So I know the pseudocode goes something like:

If 10 drops are missed, then
"You lose" shows with a picture in the background.

I think the idea behind this is the same as that behind the code you gave me. Maybe something to the effect of, "if reachedBottom() is gone through more than 10 times, then do something"?

Thanks again  Smiley
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #7 - Mar 27th, 2010, 8:18pm
 
One way to do it (I tried this, and it works), is to have a new boolean field for each drop called bottomed, initially set to false... Every time through the for loop you check to see if bottom has been reached AND "bottomed" is still false. If so,  you decrement your counter, rectangle etc by 1.... and then set bottomed to true. So the next time you come through the loop....bottomreached() is true, but bottomed is also now true - so the counter doesn't get decremented... so you only ever get one decrement for every drop that reaches the bottom....

"bottomed" is like a flag that gets put up when a drop reaches the bottom, and it can only be put up once.....
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #8 - Mar 27th, 2010, 8:40pm
 
I'm sorry, but I don't understand. I'm sure you're making perfect sense to someone who is more experienced than I am, but it's still a bit muddy for me.

Maybe it'll be simpler for me to get a grasp of if I see this in use in regards to showing the current score on screen. Let's say I already have the font saved in my data folder and loaded in the program. Then I put:

text("Score: " + numbers of drops caught);

I don't understand how to get that working correctly. Under the "Drop" tab, it has void caught() and in this function it has what the drop should do once it is caught. But I can't find anywhere in the code where it defines what caught is. Obviously the computer itself doesn't know what caught means, but I don't see where Shiffman defined it. I'm really feeling helpless right now.

Another task I'm trying to accomplish is after, say, 100 raindrops fall, a new "level" begins in which the rain drops being falling faster. Yet this task seems to rely on the same concept as the other tasks I'm trying to accomplish.

Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #9 - Mar 28th, 2010, 3:29pm
 
Well...here's the code I added...

Code:
for (int i = 0; i < totalDrops; i++ ) {
   drops[i].move();
   drops[i].display();
   if (catcher.intersect(drops[i])) {
drops[i].caught();

   }
   if(drops[i].reachedBottom() && !drops[i].signalled)
   {
rectlength=rectlength-1;
drops[i].signalled=true;
   }
 }


And in Drop class:

Code:
class Drop {
 float x,y;   // Variables for location of raindrop
 float speed; // Speed of raindrop
 color c;
 float r;     // Radius of raindrop
 boolean signalled=false;
//etc..rest the same


Note you could just add a counter in the for loop, which counts the number of drops which has been missed.... at the moment it is just making the rectangle smaller each time - but the code ensures it only happens once per drop when it reaches the bottom.
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #10 - Mar 29th, 2010, 5:32pm
 
Thank you. I like how you make your code easy to follow! Smiley I've almost achieved what I set out to do. Now all I want to do is have the drops move faster after 80 drops have fallen.

I know I may change the size of my array from 1000 drops to 80 drops. I also know I will be changing something with my Drop tab. In the move function under the Drop tab, it says y+=speed;. I want something to the effect of "if array has been gone through, then speed=speed*2" (or maybe 3/2, something like that).

Is there a special piece of code that checks to see if an array has been "emptied" or not?
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #11 - Mar 29th, 2010, 5:57pm
 
I think the best way to do this is to make use of the timer class you already have. You can create a new timer - and when this expires you increase the speed. And then re-initialise the timer again, as is done with the exisiting timer (I think this timer as a separate class is unnecessarily complicated ...but, I'm trying to work within the structure you already have).
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #12 - Mar 29th, 2010, 6:09pm
 
I see what you mean. Unfortunately my speed variable within the Drop tab doesn't apply in the Timer tab, so I tried making a global variable "dropspeed." Yet this doesn't work. All drops move at the same speed and then stop when one gets about an inch down the screen.

I know I'll want to edit this part of the code:
Code:
if (passedTime > totalTime) {
return true;
} else {
return false;
}


found within the "Timer" tab. But how, I do not know. Am I on the right track with my global variable idea?
Re: Adding on to Shiffman's "Raindrop Catcher"
Reply #13 - Mar 29th, 2010, 8:32pm
 
I kind of get the feeling....(and don't take offense at this), that you don't fully understand how classes work - the difference between a class and an object....

You don't need to change the timer class....it will do what you want already as it is set up. What you need is a new timer object which is an particular instance of the class. The timer class is like a blueprint for the object. It doesn't actually do anything until you create an instance of the class - a timer object. Once you have the blueprint you can make as many timers as you like.

timer = new Timer(300);

Here you created a timer with a time of 300 milliseconds.
If you want another timer, just create one, giving it a new name...so if you want something to happen every 80 drops, and you know that you are getting one drop every 300 milliseconds...then you need a timer of 24000 milliseconds....

timer2= new timer(24000);

Then you can say if(timer2.isFinished()) {increase the speed of all the drops}

Similarly....every drop is an object of the Drop class....every drop has it's own individual properties:

 float x,y;   // Variables for location of raindrop
 float speed; // Speed of raindrop
 color c;
 float r;     // Radius of raindrop
 boolean signalled;

So every drop object "remembers" its own x position, y position, color etc. If you want to change them you have to tell every single drop to change its speed, for example...normally done with a for loop if the objects are in an array.

for (int i = 0; i < totalDrops; i++ )
{
   drops[i].speed+=1; //increase speed of drop by 1
}

I recommend that you read through the bit in the book about classes and objects and make sure you really understand these concepts and how they work.....

Hope this helps and that you don't find it patronising (just trying to see where you are at in your understanding and making a recommendation)....

Good luck....
Page Index Toggle Pages: 1