The game in this code is sort of ping pong with a ball and a player versus AI computer.
As I understand correctly the randomSeed is used so that numbers could be randomly generated later in the code.
The main concerns I have is with bVel as I assume these stand for ball velocity x and y.
What I am wondering is what all of this calculation means. Is this some sort of formula for calculating velocity?
Also what do the : and ? do in this part (random(0, 2) == 0 ? 1 : -1);
I was looking for answers online but couldnt find anything about the operators : and ?
And another thing is why is the Serial.begin needed? I was reading about it online but I cannot understand why it is necessary to have it here.
No, it is used to have different sequences of pseudo random numbers on each run.
It can be useful to repeat exact the same steps (like in debugging a game) or a random sequence,
so it is not automatically seeded.
It is kind of an inline if then else, but value based. <condition> ? <valueForTrue> : <valueForFalse>
The whole expression is just a value, that depends.
It is only needed if you want to send or receive data over the serial port,
which is usually the attached PC.
Personally it is a lot easier to read it this way.
But I guess you just have to get used to the whole ? : thing.
It's the first time I'm seeing it so it's very confusing for me right now
An inline function constrain(what, min, max) would make the intent clearer,
and it would not really matter how verbose its implementation is.
The generated code for both versions will be very close, if not identical.
The usefulness of the ternary operator is (only?) that it can select between different expressions, where a conditional statement like 'if-else' can only select between different statements.