I'm working on a nibbles-type game (available here) and I've hit a problem with the function that places apples randomly on the screen without placing them off-screen or underneath the worm.
The basic structure of the game is a worm (arrays of worm.x and worm.y moving at worm.speed, with circle of radius worm.speed at each point) and an apple (circle at apple.x, apple.y with radius apple.size). Here's the function that I came up with:
I figure that that should work, but the apple still appears underneath the worm, any ideas?Code:bool appleok = false; while (!appleok) { apple.x = rand()%(SCREEN_W - (apple.size * 4)) + (apple.size * 2); apple.y = rand()%(SCREEN_H - (apple.size * 4)) + (apple.size * 2); for (i = 1; i <= worm.length; ++i) { dist = sqrt(pow(worm.x[i]-apple.x,2)+pow(worm.y[i]-apple.y,2)); if (dist > worm.speed*2 + apple.size*2) appleok = true; } }