Code a Snake Game with Python and Pygame 🐍 - Tutorial -

Code a Snake Game with Python and Pygame 🐍 – Tutorial
Views: 292901
Like: 5451
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 created by Dhaval Patel from codebasics. Check out his channel:

💻 Code:

🎥 Watch the Hindi version of this course:

⭐️ 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
Katia Moran

Learn to code for free and get a developer job:

Read hundreds of articles on programming:


  1. im sorry but i dont understand the part where you put an block and move it around ive been trying to put the block image for a long time now but i still cant figure out how to does anyone mind helping me?

  2. idk why i can't run it in exe file
    when i try to run it, it instant disappear

  3. Click baitI want to see how the thumbnail thing is done but anyway your code is very good

  4. Can someone let me know what colour theme this is? Thanks!

  5. Is there a way to code for the score/snake size to decrease if they eat rotten food?

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

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

  7. i need the jpg of the blocks and apples cause those i find are too small

  8. 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.

  9. How can I use a picture of my kids as the snakes head?

  10. Thanks for the tutorial. What snake is that? A python?

  11. I've tried to make a few different snake games in python now and none of them work…either I can't move or it just doesn't run altogether. is there something I'm doing wrong? I've even tried to copy paste someone else's code from a website to see if I was doing it wrong and that didn't work either

  12. From the given final code – When the snake eats the first apple, Game Overs………… y so ? I'm unable to fix this error

  13. i cannot understand the collision logic can someone help me

  14. when i try to install the game and turn it into a .exe file it doesn't load. Does anyone know why?

  15. is this tutorial for kids? Because I'm just 10

  16. when I draw the apple and run, I can see my apple on the screen but it glitches, anyone know the problem to this?

  17. when i begin to add the surface displays, it gives me errors on every line with the word surface in it. Help plz?

  18. In one tutorial i found on google, the part that generates the snake's 'food' in random locations is as follows:

    if Snake_Head_Location_X == Food_Square_Location_X and Snake_Head_Location_Y== Food_Square_Location_Y:

    Food_Square_Location_X = round(random.randrange(0, WIDTH – Size_Of_Snake) / 10.0) * 10.0

    Food_Square_Location_Y= round(random.randrange(0, HEIGHT – Size_Of_Snake) / 10.0) * 10.0

    Can someone explain to me why it was divided and multiplied by 10?

  19. Every time I try to write this out it never allows me to use the import pygame statement it always goes gray and says it is an unused statement

  20. Can anyone explain why we use

    >> self.snake = Snake(self.surface) at 26:23 .


  21. Okay tutorial but why are you not testing along the way? Multiple lines of code rearrangement are done without testing their functionality, you kinda just verbally explain it and move on!

  22. how do i get the square that he first uses in replacement with the snake 12:55

  23. actually, at 1:10:25 in the vid, it can collide with the second block. how? Ex. if you're going right, and then you start going left, you will collide with the second block, wont you?

  24. Does the surface.fill just cover up the previous blocks or does the blitz remove it?
    Is it just stacking on top of it self each time(possible memory issues?)

  25. How do you get this directory on your desktop. was running everything well until I couldnt get the block.jpg in and realized I never downloaded the directory in the beginning because I dont know how?

  26. i need a tutorial on how to make a simple python game using anaconda

  27. can you say the version of python what u is using

  28. 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.

  29. guys whats the difference between having having draw function called in the init() method and in the while loop? can i have it without the other?

  30. I have a question, suppose my snake is moving in the "right" direction and I don't want it to suddenly change it's direction to left by pressing "left key" and same as in the of moving up , down , right (I dont want my snake to change its direction to "opposite" direction of the current one by pressing the opposite direction key) how can I do this? Please help

  31. for some reason my block just will not move

  32. There is no link to download image yaar koi help karo kaha se download Karu 😢

  33. 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…

Leave a Reply

Your email address will not be published.