How do I get a random loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

13th Dec 2022, 4:30 AM
Matthew Klotz
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°).
13th Dec 2022, 5:03 AM
Scott D
Scott D - avatar