Snake game has been the best entertaining mobile game since 1990’s. Mostly in snake game, we need one snake and a food to eat. The snake goes to eat the food while the size of the snake increases slowly. The game will end when the snake bites himself after failing to eat the food. The game is simple and can’t be cheated. The origins of Hangman are obscure meaning not discovered, but it seems to have arisen in Victorian times, ” says Tony Augarde, author of The Oxford Guide to Word Games. The game is mentioned in Alice Bertha Gomme’s “Traditional Games” in 1894 under the name “Birds, Beasts and Fishes.
Active1 year, 9 months ago
$begingroup$
I'm working for a month or so on a Snake game in Python. It's working well (I got some good help on it already), but since I'm a beginner in pygame, I would like some feedback from better programmers than me. Anything that improves performance, readability and/organization is welcomed.
Keep in mind that the sounds are in a separate folder so this code will not work on your computer unless you have the files. I just want a code review.
Jamal♦
32k12 gold badges123 silver badges231 bronze badges
MateusMateus
$endgroup$
1 Answer
$begingroup$
Thank you for sharing. Mac mini keyboard and mouse. Much of this looks nice, and the code is understandable. Your identifiers are well chosen.
Please run flake8. It will ask you to add a few blank lines, that sort of thing. Also, PEP-008 asks that one-line docstrings end with a '.' period.
Keeping variables out of global scope is good. But 'main() is the new global' is an odd style. Consider moving at least some def's to top level, or creating a SnakeGame class.
You have a lot of magic numbers. Some are mostly OK, like
pre_init(44100, -16, 1, 512)
. But things like grid resolution of 10 pixels, and grid size, really need to be constants, like GRID_WIDTH = 72 or GRID_SIZE = (72, 46). Refactor this. Refactor it now.
![Game Game](https://i.ytimg.com/vi/KZ0-_dHpzoE/maxresdefault.jpg)
There is maybe an opportunity to introduce a Display class that knows the 'secret' of grid resolution being 10px. So much of the game would manipulate food + snake in game space coordinates less than GRID_SIZE, and would ask Display to plot points in the much larger screen space. With that in hand, imagine adding one blue pill (food) and one red pill to the board. Eating blue causes Display to flip mirror image around X axis, and red, around Y axis. The rest of your game wouldn't know or care about the visual effect.
This is perfectly nice:
bugs = pygame.init()
. But consider re-phrasing it as pass, fail = pygame.init()
, as bugs[1]
is slightly obscure (I had to consult the docs).
DRY: don't repeat yourself. Consider writing a helper function for game_sound(), to factor out the common expressions.
Referring to sys.argv[0] in game_sound is an anti-pattern. Parse it out early and store it in a named variable.
![Game Game](https://cdn.simplesite.com/i/1d/40/282600882140037149/i282600889655239492._szw1280h1280_.jpg)
Your comments are weirdly specific, e.g. on you_lose(). Please don't say in comments what the code already says clearly. At some point you'll make it 42px Arial font, and then either you'll remember to update the comment, or you'll have a comment that lies. Put generalities into English language comments, and put specifics into the code.
The same literal text is repeated several times. You should add a manifest constant like FONT = 'times new roman'.
Things like pause_position.midtop and score_position.midtop could use an expression like GRID_WIDTH * 0.5.
Aug 07, 2019 This website has got excellence in the quality of the anime present. If you ever wanted to download HD anime then 9anime is the best place for that. The only problem I had is that one needs to play an episode for downloading the particular episode. There is no download anime in bulk option. Anime episodes download free. Tubemate is an Android video downloader app for Android mobile devices which allows you to download One Piece episodes for free. This one piece anime downloader episodes app is able to download videos from YouTube, Dailymotion, KissAnime and a few other sites directly to the Android mobile and tablets. AnimeOut provides free Anime Download. We have encoded over 800 Anime series and 30,000+ episodes, which are available for Direct Download for free. Direct Download anime episodes,games,movies,OVA's and more directly with no restrictions or ads. Simple and straightforward from Anime Kaizoku. Missing an episode from your favourite anime series? Didn't get to see it on TV? This is the place to come. TH offers many unknown series and episodes up for download. The aim of this site is not only to offer anime up for download for anyone, but to popularize great anime series that otherwise do not have enough steam to take off.
It appears food_spawn is misnamed, and should be food_spawned. Spawning more food when it is False is surprising. Perhaps a name like out_of_food would be clearer.
You defined food_position to be a list,
[x, y]
. Please make it a tuple, (x, y)
. Lists are variable length, unlike tuples. (At some point you may support a list of multiple food locations, and the spawn check would test if its length is zero.)
modeling direction
I recommend you parse key_{up,right,down,left} into dx and dy values drawn from {-1, 0, 1}. And model current direction in the same way. This would reduce 'going backwards?' to two tests. And if you store direction as an index 0.3 into this:
Snake Game Code Python 3
then 'going backwards?' is just a test of
(direction + 2) % 4
. (That's plus len(delta)/2, mod len(delta).)
Advancing the snake is merely adding (dx, dy) to snake_position.
detecting collision
This seems more expensive than necessary:
Consider maintaining a
set
alongside snake_body. (This is an opportunity to define a Snake class.) When pixel is added to head of snake_body, add it to the set as well (constant time, independent of snake length.) When pixel is removed from tail, remove it from set. Now instead of linear scan you can do a single set membership query.
Of course, there is already a data structure that can be queried in constant (less than linear) time. The screen. Ask the display if we're moving onto a snake-colored pixel when detecting collision.
J_HJ_H
$endgroup$