How can one add the food in a snake game (pygame) more elegantly?

Viewed 54

I saw quite a few ways to do it, but I don't understand why they calculate /10*10 in the end. This is how the food is handled, it doesn't work without the last part(/10.0)*10.0) but I don't understand what it is there for. Can someone please explain this?

foodx = round(random.randrange(0, window_width - snake_block)/10.0)*10.0
foody = round(random.randrange(0, window_width - snake_block)/10.0)*10.0
1 Answers

y = round(x/10.0)*10.0 ensures that y is divisible by 10 without remainder.

The more "elegant" way is:

foodx = random.randrange(0, window_width - snake_block, 10)
foody = random.randrange(0, window_width - snake_block, 10)

The lase argument to random.randrange specifies the step for the random number generator.

Related