samedi 22 décembre 2018

How to select a value from array randomly for a 2d game (not min, max)

i am making a snake game , if the snake ate the apple, it will spawn randomly in corners

i tried codes like getRandomInt(0, 25), but is spawned in the range of 0 to 25 (0,1,2,3,4,5,6,7,8,9,10, ...25)

code i used:

var grid = 16;

if (cell.x === apple.x && cell.y === apple.y) { snake.maxCells++;

apple.x = getRandomInt(0, 25) * grid;

apple.y = getRandomInt(0, 25) * grid;

i want the apple to spawn randomly in one of the 4 coners of the map (2d)

at x = 25 or 0

  or

at y= 25 or 0

not x,y at 0 or 1 or 2 or 3 or 4 or.. 25 (min, max)

Hope you understood what i am trying to say (sorry for my english)




Aucun commentaire:

Enregistrer un commentaire