Python Snake Game With Pygame - Create Your First Pygame Application -

Python Snake Game With Pygame – Create Your First Pygame Application

Patrick Loeber
Views: 28947
Like: 642
Implement the famous Snake game in this beginner tutorial and learn how to get started with pygame. Build your first pygame application and learn how to implement the game loop, how to display objects with pygame, and how to implement the game logic. Along the way I also show some nice Python features like Enum and namedtuple.

✅ Write cleaner code with Sourcery, instant refactoring suggestions in VS Code & PyCharm: *

🚀🚀 Get monthly Python and ML Tips:


If you enjoyed this video, please subscribe to the channel!


Anaconda tutorial:


You can find me here:



* This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏


  1. Here is some help if your first Python project like me.At 1:26 Make a new folder on your desktop. With command prompt type cd with the location name of the file. Example: "cd C:UsersadminDesktopSnake"To use conda there is a tutorial link in video, but I did it different. I searched for "anaconda python 3.7" to get the same version as this video. Installed anaconda and then used their program to launch command prompt. Command Prompt has a lunch button on the home of their program. I then used my Example from above to use the file location from this command prompt, not command prompt from start menu. At 2:47 add any type of file called snake_game in the folder you made. Just save as .py instead of original file type.

  2. Yay another video! I'm sure it's going to be amazing 🙂

  3. Looking forward to the next video! Great stuff.

  4. Oh my god dude. you cannot keep making quality content like this. You'll be the number one YouTuber at this rate and people are going to come after you! You deserve so many subs. I'm sharing your channel with my friends. Keep up the amazing work! I cant wait for the next one! Machine Learning sounds so exciting!

  5. I like it! Looking forward to the video with reinforcement learning 🙂

  6. Why are you using a while true loop and not a while game_over loop?

  7. Super video bro…🔥🔥….bro will you use nn.Sequential() while initializing neural net in next video it make more simple

  8. Bro can I write the same code as you write in visual studio code ( vs code ) plss reply bro
    Your explanation is Awesome !!

  9. First of all, THANK YOU SO MUCH for this tutorial.
    I just wanted to point out two things please:
    1. at 19:13 ,
    there are cases that "if something == True" yields a different result than "if something", e.g. for non-boolean something.
    2==True yields "false" whereas "2" evaluates to "true"; None==False is "false" but "not None" is "true".
    In our case, you can simply say:
    if game_over:
    # do something

    1. at 19:40 ,
    it make sense that you did that but you can also pass coordinates as a list:

    Again many thanks for everything. I am learning a lot!

  10. I have no errors but my game display window wont stay open it closes when I run program

  11. Where are u writing all this in I'm on windows and i don't know what it is

  12. when a class has two methods and one of those methods is an _init_ it's not a class. Nice tutorial though. Maybe use functions next time to setup the game loop. It's a lot more readable

  13. python

    python: can't open file '': [Errno 2] No such file or directory
    is what I get

  14. Add this if you dont want the snake to be able to go left when you move right and not go down when u go up etc

    def play_step(self):

    # 1. collect user input

    for event in pygame.event.get():

    if event.type == pygame.QUIT:



    if event.type == pygame.KEYDOWN:

    if event.key == pygame.K_LEFT and self.direction != Direction.RIGHT:

    self.direction = Direction.LEFT

    elif event.key == pygame.K_RIGHT and self.direction != Direction.LEFT:

    self.direction = Direction.RIGHT

    elif event.key == pygame.K_UP and self.direction != Direction.DOWN:

    self.direction = Direction.UP

    elif event.key == pygame.K_DOWN and self.direction != Direction.UP:

    self.direction = Direction.DOWN

  15. Hey dude, can you increase the speed of the snake whenever it collides with the food? I tried it and it doesn't work it seems like there must be a fixed speed value.

  16. Really liked the video, I would like to know which part of the code adds the additional block to the snake when it collects the food? and how?

  17. AttributeError: 'SnakeGame' object has no attribute 'play_setup'
    how do i fix this

  18. this is a terrible tutorial. did not understand why anything is where it is.

  19. if you have in course in udemy i will pay it —thanks your are the best agin

  20. The Formula for placing it perfectly doesnt work first of all make sure your Canvas is divisible by your BLOCKSIZE and then implement this formula

    x = random.randint(0, (self.w/BLOCKSIZE))*BLOCKSIZE

    y = random.randint(0, (self.h/BLOCKSIZE))*BLOCKSIZE

    Edit: Placing The Apple

  21. Really nice, thanks for the video, I'm learning Python because of all the tutorials of ML

    I have a suggestion, avoid the "implosion", if you are going RIGHT and then you press the LEFT key, it counts like a collision
    Another problem.. sometimes if you press 2 keys very quick, like if I'm going RIGHT, and I press TOP+LEFT, it "implodes" too, so I add a break at the end, so 1 key is computed per play_step(clock.tick)

    if event.type == pygame.KEYDOWN:

    if event.key == pygame.K_LEFT:

    if self.direction != Direction.RIGHT:

    self.direction = Direction.LEFT

    elif event.key == pygame.K_RIGHT:

    if self.direction != Direction.LEFT:

    self.direction = Direction.RIGHT

    elif event.key == pygame.K_UP:

    if self.direction != Direction.DOWN:

    self.direction = Direction.UP

    elif event.key == pygame.K_DOWN:

    if self.direction != Direction.UP:

    self.direction = Direction.DOWN

  22. 2nd. param of a namedtuple is a sequence, could be a list or a string sequence separated by space or commas.

  23. this is pure shit, I'm literally watching you code and not learning anything 👎

  24. Why do you use an underscore before the place_food function?

  25. I dont get where he even is in the beginning, what is that window? 🙁

  26. I recommend skipping the virtual environment part,
    As someone who uses Pygame myself, id say anyone who wants to start using it would have to have at least enough knowledge in basic python to be able to use Virtual Environments if necessary
    Also after the virtual environment part of the video you were uhm, teaching on how to install a package? I believe you have tried too have to make this video aimed towards people who barely know even how to use for loops with Python, which is definitely not the audience i would recommend for Pygame
    Considering its a 2d game framework in which your relying more on Python objects and elements over Pygame elements themselves, knowing control flow and the process of building an application is crucial

    3:17 So one thing i really dislike about many other YouTube tutorials on how to code is how they import everything beforehand, and with no reason if you don't know why you would want to be using the random library at all

    3:44 Im happy that your following conventions and standards(Such as PascalCase for classes and usage of if __name__…) but this brings me back to "setting before using". You hadn't made the SnakeGame class yet, and also just to keep it simple i would probably go with just a simple while loop with everything inside, or an file in the src/ folder to be imported to, but thats not really that much of an issue

    Last thing is… you are running it after writing like 100+ lines of code.. In real development if you were sitting down and deciding to make a game with Python(Pygame) then you would probably first just run a normal, empty, window

    I don't mean to do baseless hate of course, Just a few things I would suggest changing

  27. where is the logic that moves the entire body behind the head ? I can't find it pls help

  28. I fucking love you , thank you so much.
    You saved my school project .

  29. Hi. Great video! Although, I don't understand where did you code the increasing of snake's dimensions when it eats the food

  30. thanks for vido but you are writing code like you just memorise it and no explanation like
    what is the usage of clock?
    how did you move entire body behind the head?
    what is arial.ttf ?

  31. how does the snakes body follow the head?

    how does the body turn at the same spot as the head?

    Which part of the code is that?

  32. when i change the window size the game logic stops working… the snake is not collecting the food or anything and the rects don't even overlap, no idea why :

  33. I think this is not completely beginner friendly as some of them didn't know many of the things used in the video.
    🚨SUGGESTION : Patrick u could also take little more time to explain the necessary details of the Pygame Module 👍👍👍
    Btw Loved the idea🔥🔥🌟

  34. when the snake hits the boundary, shouldn't it be when self.head.x > self.w, not necessarily self.w-BLOCK_SIZE? I have a little bit of trouble trying to understand why we need to subtract the BLOCK_SIZE at 37:17.

  35. at the in _name_ == '__main__':
    game = SnakeGame( ), it says snakegame is not defined,any way to fix?

  36. I can't figure out which part of the code is growing the snake whenever it eats an apple. Anyone?

  37. 24 string, Error. pygame.display.set_mode(size=(self.w, self.h))

  38. Thank you, Patrick. I got so inspired by this video. I learned it and made my own enhancements. I posted a video about it on my channel. Thank you for inspiring me! Much love ♥

Leave a Reply

Your email address will not be published.