r/learnpython • u/No_Can_1808 • 17d ago
Tell us about your snake game
Chances are if you’re a retro game lover, you’ve tried your hand at building a snake game.
Mine has multiple difficulties, a leaderboard for each difficulty, and obstacles on the harder difficulties.
Easy - speed stays at 10, no obstacles
Medium - speed stays at 10, has obstacles
Hard - speed starts at 10 and increases by 0.25 every food you eat, has obstacles that randomly move every 5 food you eat
Expert - speed starts at 10 and increases by 1.0 every food you eat, has obstacles that randomly move every 5 food you eat
Veteran - speed starts at 10 and increases by 2.0 every food you eat, has obstacles that randomly move every 5 food you eat, has a 30 second food timer that takes 1 off your score and snake length if you don’t get a food in time but resets if you do get a food(yes, your score can go negative and your snake will “wither” to nothing and die if you don’t eat)
My son and I have a lot of fun playing it. The board size automatically adjusts to the screen resolution, but food, obstacle, and snake sizing are hardcoded using 5pixel by 5pixel blocks. Playing on a true 4k(3840x2160) display gives you 4 times the game board size compared to playing on a 1920x1080 display.
3
u/favorite_stuff 17d ago
The toughest part is timing the inputs. If you press the button to turn but you're only 50% of the way through a cell on the grid, do you
- wait until you complete the cell, and make the input feel delayed?
- immediately snap to a turn, and make the movement feel jumpy?
- have the snake moving fast enough, or the cells small enough, that it becomes less of an issue?
3
u/TheRNGuy 17d ago
Show the code on GitHub.
2
u/Discordbothost 17d ago
the veteran food timer is evil, i like it. on the input timing thing people mentioned: the classic snake bug is pressing up then left really fast inside one tick, the second key overwrites the first and the snake turns straight back into itself. what fixed it in mine was a tiny queue of turns instead of a single direction variable:
checking against the last queued direction instead of the current one is the important bit. capping it at 2 keeps it from feeling laggy when someone mashes keys, especially at the higher speeds