Code a Snake Game with Python and Pygame 🐍 - Tutorial

preview_player
Показать описание
In this Python / Pygame tutorial you will build a snake and apple game. This Python project for beginners is perfect for you to get exposure on how you can build an end-to-end Python project after learning the basics of Python.

⭐️ Course Contents ⭐️
⌨️ (0:00:00) Intro and create surface
⌨️ (0:21:03) Converting into OOP
⌨️ (0:31:48) Moving block with timer
⌨️ (0:37:10) Draw snake and apple
⌨️ (0:49:34) Snake eats apple, find score
⌨️ (1:07:33) Game over logic
⌨️ (1:20:07) Add background music,image
⌨️ (1:31:45) Convert python to exe

🎉 Thanks to our Champion supporters:
Otis Morgan
DeezMaster
Katia Moran

--

Рекомендации по теме
Комментарии
Автор

Thanks for posting and happy learning :)

codebasics
Автор

with the collision, since everything is on a grid, you can just make it:

if y1 == y2:
ifx1 == x2:
(collision stuff)

slugginatubb
Автор

If you are experiencing the issue where the apple is flickering/ disappearing and reappearing, I believe I discovered the issue. It is most likely because there are two different pygame.display.flip() happening. The way I discovered I could fix this problem was by removing the pygame.display.flip() from the snake class. Removing it from the apple draw will make the apple disappear, but removing it from the Snake draw ends up working fine. Hope this helps anyone who is having this very same issue as I was, and have a nice python experience.

nightformula
Автор

You’re an excellent teacher. Thank you my man 🦻🏾

Teewaree
Автор

I'm a beginner in python but making this although was difficult and took some time (I hadn't understood classes) I improved A LOT, thank youu!!

bartomiejpotaman
Автор

For me the best way to learn something is not discuss about abstract concepts but doing like this ‘LIVE CODING’ because it shows us how to do it and contributes to acquiring all the concepts by examples.
Tks again freecodecamp 🇧🇷🇧🇷🇧🇷

diegosantosmoto
Автор

If you're on Linux and having trouble with the sounds triggering the "game over", try converting them to ogg.

Plus, in the play() method the error is not raised correctly, try replacing it by : raise ValueError("Game over")
Then in the try except block (in the run() method) : except ValueError: instead of except Exception as e:

This way the game will "game over" only when intended, not every time there's a error occuring in the play() method

Thanks for this tutorial, i've learned a LOT :)

TheXxionxX
Автор

What i find funny is, that he iterates so much through the main concepts and the most important ones. But falls so deep into the explaination hole, when talking about the details, as if someone could watch that and rebuild it himself after that. Its so much more important to understand the main concepts here...

jeannoelhonisch
Автор

Your coordinate system for the y1 and y2 must be reversed - The y axis increments while the snake is going down this is the part why I confused haha by the way I understood it. Thanks!

zoilo
Автор

To complete this excelent tutorial, I want to leave my contribution for make limits in our display, if snake touch this limits then game will be over

In class Game, make a new def named "limit" ---- def limit(self, x1, x2, y1, y2):
then define a condition : if x1 >= x2 or x1 < 0 or y1 >= y2 or y1 <0:
return True

after, into "def play (self):
call the def limit under snake colliding with apple condition

if self.limit(self.snake.x[0], x_w, self.snake.y[0], y_h):
raise "Game Over"

Guchi_lu
Автор

After searching for an hour, I found an RGB background color for the game, 102, 167, 67. This color matches the background of the apple.

stanulbrych
Автор

man you are awesome i have searched many videos in youtube they will ask to install this that etc.. etc... and in the starting itself i will be strucking, but today i did it and understood every part awesome dude
keep doing many videos as tutorial for projects to put in resume ... i am ur subscriber from now

barathm
Автор

great job !

way better than that "chris' courses" being advertised and spammed on all my several search results leading up to finding this page and this community's YT account.

i love this.

noir
Автор

In the "Add background music" section your code is changed and you don't show what you did exactly. If someone with the first version of your code just adds the lines you add in that section, the game just does not work.

grisoe
Автор

Finished! Thanks to everyone who made this tutorial possible!!

Clank
Автор

I did his Data Science Course it was great.

AjayKumar-idmb
Автор

very confused, I had an issue where eating the apple was giving me a game over... so I went and took his exact code and it is still giving me game over after consuming the apple. very odd and incredibly confusing

TheHubOfHubs
Автор

Has anyone else come across the issue of the apple causing game over after adding the sound effects?

nolanpeterson
Автор

FYI, when you start the 3rd section (Moving block with timer), he changes the Snake __init__ to (self, surface). I was trying to debug why mine wasn't running haha

zestful
Автор

Thank you for preparing a great tutorial on OOP approach to game creation in Python! I totally enjoyed it and learned a lot from it.

StalinFromTambov