Sign in to follow this  
mollemouse

Random coordinate for snake game

Recommended Posts

I'm making a little snake game where you can move the snake in 360 degrees and I'm wondering how you would make a good algorithm to generate a random coordinate for the food. It should be within the game plane (my 640x480 window) and it should not be on the snake. My first thought was just to make a random number between 0+radius and 640-radius for the x coordinate and 0+radius to 480-radius for the y coordinate (radius = radius for the food) and then check if this circle is on the snake, if it is loop and try again. But the worst case scenario for that could take a very long time to complete right? And it will only get worse as the snake grows larger. Or should I just assume that it won't be a problem because most players will die long before they reach the max possible length?

Share this post


Link to post
Share on other sites
if your snake is occupying half the screen, then the probability of the food landing on the snake is about 1/2 (I'm guessing the food radius skews it a bit); if the snake is only occupying one twentieth the screen, then the probability
is only 1/20th, so it really shouldn't be much of an issue; occasionally running a few extra statements won't be noticeable in a snake game on all but the most archaic systems.

Creating an algorithm that does it another way would probably leave you with something more processor intensive on average, to avoid an occasional situation where it loops a good number of times.

Share this post


Link to post
Share on other sites
Given that your playing field is 640x480, that's ~300000 different places the food could be placed in. Assuming this, your player would have to eat food one hundred and fifty thousand times before you would, on average, have to randomize twice, although this of course doesn't take into account food radius, but that shouldn't change the conclusion. I don't know about you, but playing snake through 150000 meals seems rather heroic.

Ergo, it's not a problem.

Edit @ Ezbez since I cannot be bothered to add a reply:
Yes, I'm peripherally aware of that, however, in my semi-inebriated state I decided further approximations meaningless at best and downright misleading at worst, so I thought it best to neglect taking the width of food into account :]

[Edited by - SirLuthor on May 5, 2007 3:43:48 PM]

Share this post


Link to post
Share on other sites
If it hits the square, place it on the nearest empty square (a naive approach: cycle through each pixel and see how far from the point it is, and whether or not it is ontop of the player). Only fails if all the screen is covered, but at that point applaud your player.

Also, SirLuthor, I'm sure that food makes the snake more than 1 pixel longer, and there's also width to take into account. So it's certainly not 150,00. Though I'm sure it's still a very impossible number! But as you said, it's not really a problem.

Edit: Or you could have it that if it fails X number of times, *then* it does something else (eg: my approach, simply picking the first pixel that is unoccupied going left-to-right up-to-down from upper left corner.

Share this post


Link to post
Share on other sites
I'd do something like this;
void placeFood()
{
int x = rand(0, 640);
int y = rand(0, 480);

while(snakeIsOn(x, y) // Check whether a food "thing" on that coordinate would collide with the snake.
{
x = rand(0, 640);
y = rand(0, 480);
}

_addFood(x, y);
}

It might not be the best solution but I believe (haven't tested) it works.

Share this post


Link to post
Share on other sites
Well the snake is a 5 pixel wide curved line, and the food can have a random size of 10-20 radius in pixels (then I grow the snake based on the food's radius). So it can actually grow large pretty quick if you get many large foods in a row.
It's kinda like the game Pizza Worm if you have every played that.

Edit: Just made some tests now and yeah you guys were right, I made a function that tries a random coord and test it if it's on the snake and made a counter how many iterations it needed, and I played for a good 30 min and the average number of iterations was under 5.
I guess I just overthink things too much :)

Now I just gotta use my elite paint skills and make some nice graphics and my first game is done :)

[Edited by - mollemouse on May 5, 2007 4:20:05 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this