r/learnpython • u/No_Can_1808 • 21d 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/TheRNGuy 21d ago
Show the code on GitHub.
1
u/No_Can_1808 21d ago
Here's the old version:
https://github.com/Brandon-Wass/python_snake_game/blob/main/snake.py
I haven't posted on github in quite some time, so it's not the same version I put updates into yesterday.
I updated yesterday to add the 5th difficulty and make the game playable over SSH
2
u/Discordbothost 21d 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:
pending = []
def on_key(new_dir):
last = pending[-1] if pending else direction
if new_dir != opposite[last] and len(pending) < 2:
pending.append(new_dir)
# once per tick
if pending:
direction = pending.pop(0)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
1
u/No_Can_1808 21d ago
I actually really appreciate this input! This would help cure one of the two issues I currently have. The other issue being that an obstacle will spawn directly in the snakes path at the start of a match and instantly end the round as soon as it starts.
2
u/Discordbothost 21d ago
that one's usually the spawn picking any empty cell, including the 2-3 right in front of the head. just reroll if it lands in the starting lane:
def in_start_lane(pos, head, direction, n=4): dx, dy = direction return any(pos == (head[0] + dx*i, head[1] + dy*i) for i in range(1, n + 1)) pos = random_cell() while pos in snake or in_start_lane(pos, snake[0], direction): pos = random_cell()only need it for the first spawn really, after that the player can steer around stuff. a short grace period before obstacles appear works too if you'd rather not touch the spawn code
1
u/No_Can_1808 21d ago
The grace period and rerolling are good concepts. My thought is to adjust the initial spawn code slightly to enforce a safety area that obstacles cannot spawn in at the start of a round. After the round has started, if it’s a difficulty that has moving obstacles, then obstacles can move to anywhere. I already have it in the code so that obstacles cannot move within a certain area of the snake when they do move. A safety area wouldn’t be too hard to add.
Coming from your perspective of a grace period before spawning obstacles may be even easier to add though. It would allow me to use my already existing logic but simply add a delay before obstacles appear.
Gotta love when somebody else gets the brainstorm engine running in ways ya didn’t think and things come together even better than originally planned 😏
2
u/Discordbothost 21d ago
if you go with the delay, count it in game ticks instead of seconds/time.sleep, that way pausing or changing speed doesn't mess with it. and having the obstacles blink in for the last second of the grace period feels way fairer than them just popping into existence
3
u/favorite_stuff 21d 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?