You have a few bad habits that i see. Ill start with those.
first of all vars is a bad name to name a module. You are overwriting the built-in vars. Whether you use it or not, its not a good habit to get into as it can cause you a lot of headaches later.
You should only ever have one statement in your entire game of
You are not using pygame's built-in Rect. Which is a simple way to handle collision (such as collision with snake and food). Python's 3rd party libraries have methods and built-in ways to handle things. This is to create simplicity in code, so others can easily read it. But it also helps as the 3rd party libraries perfect the code to make it fast and correct. Instead of checking the distance between the two, i would just check for rect collision.
For changing the speed based on how long the snake is you could limit the snake movement with a delay
However then you come upon another problem. A function should only ever do one thing. So snake.move() should only move, not move and eat, not move and change its size, just move.
Also you might want to set a large delay in the beginning because sometimes the snake runs off the screen at the start of the game giving no chance of success to the user.
first of all vars is a bad name to name a module. You are overwriting the built-in vars. Whether you use it or not, its not a good habit to get into as it can cause you a lot of headaches later.
You should only ever have one statement in your entire game of
pygame.display.update()
. By having more than one i know your structure is fragmented. Your trying to make two game states (playing and end screen with score) incorrectly. The proper (or better) way is to use classes and inheritance. More info here. You are not using pygame's built-in Rect. Which is a simple way to handle collision (such as collision with snake and food). Python's 3rd party libraries have methods and built-in ways to handle things. This is to create simplicity in code, so others can easily read it. But it also helps as the 3rd party libraries perfect the code to make it fast and correct. Instead of checking the distance between the two, i would just check for rect collision.
(Feb-17-2019, 05:29 PM)andrerocha1998 Wrote: I achieved this by limiting the FPS number based on the current score but it seems like a cheap way to do it.Yes you are right. It should be set at 60 for almost any game and should not change. You can move an object by making it jump X number of pixels or use delta time. More described here.
For changing the speed based on how long the snake is you could limit the snake movement with a delay
timer = 0 delay = 50 ... if pygame.time.get_ticks()-timer > delay: timer = pygame.time.get_ticks() snake.move(eat)The shorter delay the faster the snake moves. You can decrease delay every time the snake gets longer.
However then you come upon another problem. A function should only ever do one thing. So snake.move() should only move, not move and eat, not move and change its size, just move.
Also you might want to set a large delay in the beginning because sometimes the snake runs off the screen at the start of the game giving no chance of success to the user.
Recommended Tutorials: