p5.js - How do I make a random spawn outside a specific area -
i'm trying make basic little game there platform in middle of screen , there circles spawn randomly around area when touch food items in game snake. problem circle spawn inside platform in middle making impossible touch. i'm using random function x , y value in play area each time spawns it's given random location. there way make sure doesn't show in specific platform in middle? i'm doing in basic javascript in p5js
function coin () { this.x = random (16, width-16); this.y = random (16, height-91); this.show = function() { ellipse (this.x, this.y, 32, 32); } }
think how in head. try write out set of steps, in english (not in code) follow when come random number 2 ranges.
how i'd this:
- flip coin decide side number should come from.
- now side chosen, use plain old
random()
function generate number range.
for example, let's want choose number 1-30, don't want 11-19 options. here's how i'd that:
var number; if(random() < .5){ number = random(1, 10); } else{ number = random(20, 30); }
the if(random() < .5)
line flipping coin, select each range 50% of time. might want adjust percentage if ranges not equal size.
you come random number until it's outside range want. this:
var number = random(1, 30); while(number >= 11 && number <=19){ number = random(1, 30); }
Comments
Post a Comment