I have the following main code loop that controls a random snake. A significant portion of the time, the snake is curled up in a little block as might be expected of a random walk. It's even worse in the corners and it looks bad. I'd it like to be more decisive and make progress across the screen until it hits a boundary.
How can I get the snake to go in a straighter line? I think that it's something to do with weighting the random number generator, but I'm not sure of the algorithm as it would have to weight in different directions at different times.
I'm not looking for a self avoiding random walk though as they block very quickly when in 2D.
void loop() {
do {
switch (random(0, 4)) { // Pick 1 of 4 directions
case 0:
delta_x = 0;
delta_y = 1;
break;
case 1:
delta_x = 1;
delta_y = 0;
break;
case 2:
delta_x = 0;
delta_y = -1;
break;
case 3:
delta_x = -1;
delta_y = 0;
break;
}
} while (willBeOutOfBounds() == true);
head.x += delta_x;
head.y += delta_y;
advanceWorm();
displayWorm();
}
Aucun commentaire:
Enregistrer un commentaire