Trouble with the bouncing ball example from the NoC book

on my first embark of the first example in The Nature of Code, Processing is telling me

Missing right parenthesis “)”

and I compared my code to Daniel’s sample code… and I couldn’t figure out why. Can someone help me, please???

Thank you!!!

picture can be very helpful,
but for test might need code,
or here better just link to where you get it from
https://github.com/nature-of-code/noc-examples-processing/blob/master/chp01_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pde ,

and that version is working here/
same like the right side of your picture.

now for the left side / the problem /
again original right:

  if ((x > width) || (x < 0)) {
    xspeed = xspeed * -1;
  }
  if ((y > height) || (y < 0)) {
    yspeed = yspeed * -1;
  }

you have left:

  if ((x > width || (x < 0)) {
    xspeed = xspeed * -1;
  }
  if ((y > height || (y < 0)) {
    yspeed = yspeed * -1;
  }

so you deleted 2 times a ‘)’
and the red error info ( in the editor and in the console header line )
says it all.

also please see that the editor has that great feature that it can indicate code blocks
{ } or ( )
by posting the cursor ( ? by mouse click ?) behind one see (| and see the corresponding as [)]


now you can choose between many coding styles and formatting,
but one point should be to test if your free form code format
will survive using the auto format [ctrl][t]

i like following style / for me more easy readable without UNNEEDED (){}

  if ( x > width  || x < 0 ) xspeed *= -1;
  if ( y > height || y < 0 ) yspeed *= -1;

so my version of this whole code would look like

// The Nature of Code / Daniel Shiffman / http://natureofcode.com
// Example 1-1: Bouncing Ball, no vectors

float x = 100, y = 10, xspeed = 2.5, yspeed = 2;

void setup() {
  size(800, 200);
  stroke(0);
  strokeWeight(2);
  fill(127);
}

void draw() {
  background(255);
  if ( ( x += xspeed ) > width  || x < 0 ) xspeed *=  -1;
  if ( ( y += yspeed ) > height || y < 0 ) yspeed *=  -1;
  ellipse(x, y, 48, 48);
}

does that look more snappy?

and short is good HA,
BUT what we should NOT delete is the info where we take it from
even if would not only legally be required, the book is CC BY NC 3.0
still for respect keep the AUTHORs name in your code.

( your very first action as a beginner was to delete the first 4 lines of the tutorials code)

2 Likes

I agree with your formatting being a very clean code for smaller files as well and more oversight. But, if someone is a beginner I think it is important to write out every variable separately in the global floats and create the if/for/while statements on separate lines.

This is just for the learning curve. It’s the same as when they teach children English and they make them write out the complete words as in: it’s = it is, he’s = he is, won’t = will not etc etc etc.

Learning the basic way will help staying out of trouble at a later stage.

That said, it’s a great book that I have just gotten myself and will start working with soon. It may well be the best book I have got for Processing so far. There are many but it’s hard to find good ones for things like generative art etc.

Just to emphasize, these is what’s missing in your code but it’s been explained really well by @kll already:
57

ps I bookmarked your post myself because I was actually looking for a way to condense my code! so thanks :slight_smile:

2 Likes

hi kll, thank you very much for the reply. I was typing out the code by following the code from the kindle book but found the book missed out plus and minus. Then I went finding the code from github.

I will keep the credit in the code in the future, thank you for clarifying that.

now I see the missing parenthesis, gosh I was blind. :joy:
And no idea about the formatting style.

2 Likes

yes I finally saw it. it was so obvious now i read it myself.

Thank you guys!!

possibly i am more the visual type, ( and also like excel lists )
but what i found for me is / if i not can simplify / delete ‘((’ /

 if ( (x > width) || (x < 0) ) {
// looks better as
 if ((x > width) || (x < 0)) {

what you say?

same as :

if (x > width || x < 0) {

1 Like

thanks,
we had that here [question] help! trouble with the bouncing ball example from the NoC book already.

just wanted to say the ( ( spacing can help me AND
is not killed by [ctrl][t]

1 Like

By the way @cyml is on a mac so it would be [cmd]+[t] :wink:

2 Likes