0
How do I get a random loop
Working on a a ping pong game and I am wondering how to get a loop for when ball hits one side it doesn't just go back the direction it came from but in another. and is there a way that i can program so that the other side is computer operated?
1 Answer
+ 2
Matthew Klotz
From a Google search (just started js today so no expert...) :
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Example
// Returns a random number:
Math.random();
But you should be able to scale it to your needs:
// Returns a random integer from 0 to 99:
Math.floor(Math.random() *Â 100);
Not sure that helps but maybe worth checking out.
Or perhaps if you have a way to measure the angle at which it hits the wall you could use the inverse angle (ex. hits at a 60° angle, bounces away at a 120° degree angle as a flat surface is 180°).