Learning pygame by creating Snake [python tutorial]

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ค. 2024
  • A tutorial to teach you the fundamentals of pygame by creating the classic snake game. It is designed for absolute beginners, so you don't need to know any pygame; but you would need knowledge of basic python. But besides that, the project should be useful to understand a slightly more complex project, how to use python classes and how to create a basic game in general.
    Hope you enjoy :)
    The project file is available here: github.com/clear-code-projects/Snake
    Timestamps:
    0:00:00 - Intro
    0:00:40 - How games work
    0:02:52 - A basic intro to pygame
    0:12:26 - Drawing in pygame
    0:29:25 - The logic of snake
    0:30:29 - Setting up the board
    0:43:42 - Creating the snake
    0:49:00 - Moving the snake
    0:57:19 - Make sneck munch!
    1:06:45 - Game over states
    1:16:17 - Creating the fruit graphic
    1:18:27 - Creating the snake graphics
    1:38:45 - Grass pattern
    1:44:25 - Adding the score
    1:56:50 - Adding the sound
    2:00:32 - Final tweaks

ความคิดเห็น • 605

  • @ClearCode
    @ClearCode  3 ปีที่แล้ว +101

    Slight correction at 0:03:12 the text at the bottom is supposed to say python 3.9 not pygame 3.9

    • @hariharanthegreat5576
      @hariharanthegreat5576 3 ปีที่แล้ว +2

      Can you give link to assets you used here

    • @ClearCode
      @ClearCode  3 ปีที่แล้ว +6

      @@hariharanthegreat5576 check the video description, there is a link to all the files :)

    • @aarushpal3736
      @aarushpal3736 3 ปีที่แล้ว +3

      you are so sensitive to your mistake love it xD
      :->

    • @jakelanghel8917
      @jakelanghel8917 3 ปีที่แล้ว +2

      do you hav a link to all the graphics you used??

    • @jakelanghel8917
      @jakelanghel8917 3 ปีที่แล้ว +3

      nvm I didn't notice the github link lol thank you.. I've been trying to teach myself how to program and this is by far one of the best tutorials I have found for free!!! super detailed and you explain everything!! alot of other tutorial I felt like I was just copying what they were doing but not rly understanding why im doing it!! thanks again

  • @marnierogers3931
    @marnierogers3931 2 ปีที่แล้ว +73

    If anyone is new here and gets stuck around the 40min mark returning an attribute error. Try adding a double underscore before and after __init__. I just learnt a looooong lesson that I'll now never forget! haha

    • @kafactz7710
      @kafactz7710 ปีที่แล้ว +1

      Thanks bro

    • @BonkySonky
      @BonkySonky 2 หลายเดือนก่อน +1

      this does nothing

  • @mixlaproduction
    @mixlaproduction 3 ปีที่แล้ว +137

    I just found out your stuff. I was blown away by the amount of detail and effort you put into your videos! Your channel seems golden! Can’t wait to watch all your python game-developing related videos. Keep up with those awesome simple projects! 😎

  • @RandomJeevanYT
    @RandomJeevanYT 2 ปีที่แล้ว +13

    1:06:39 "Well takes no time at all to create it"
    More like 2 days for me cuz im doing this sort of stuff for first time 😥
    But i understood everything clearly cuz u explain so well so thanks for that🤗

  • @samtheserious19
    @samtheserious19 2 ปีที่แล้ว +6

    Man, your channel is amazing, your tutorials and explanations are clear and we'll explained. You have teacher material and the fact that all this is for free makes me appreciate and respect your work even more. Thanks mate

  • @apopodo
    @apopodo 3 ปีที่แล้ว +54

    yor channel is INSANELY underrated. I've seen many pygame tutorials yet I only understood 100% of what I was told is in your video, you explain everything so in depth and stop at every line of code to explain. You give examples and even show little animations to show us what you're trying to say. 10/10 keep it up man

  • @roiho5405
    @roiho5405 3 ปีที่แล้ว +6

    I love your Pygame videos. I'm having so much fun with this module, I hope to see more from you :)

  • @flippingflipbooks565
    @flippingflipbooks565 3 ปีที่แล้ว +50

    I am currently learning Python, but I don't know ANYTHING about pygame! I really wanted to learn it: My main motive for learning programming was to make games! You are the best person for me to help me learn. You add in litte bits of humor, so your vids aren't just plain coding. I really think that you are the best!

    • @uskumru3106
      @uskumru3106 2 ปีที่แล้ว +2

      Python is realy bad choice if you want making game

    • @user-hx1lp6fc9r
      @user-hx1lp6fc9r ปีที่แล้ว +2

      Hey friend! How did it go for you?

    • @No3inator
      @No3inator ปีที่แล้ว

      yeah after learning python you can easily start using Godot using gd script which is 90% similar to python because python just has many bad things in game development which you cant avoid

    • @Bluemoon_wav
      @Bluemoon_wav ปีที่แล้ว

      @@No3inator could you tell me in a broad way what those "bad things" are?

    • @jeremyhubaker1199
      @jeremyhubaker1199 11 หลายเดือนก่อน

      IF YOU REALLY WANT TO MAKE GAMES THEN YOU NEED TO LEARN C++ ,C#, JAVA YOU WILL BE LIMITED WITH PYTHON AS IT IS BETTER SUITED FOR DATA MANIPULATION AND BACK ND STUFF. ALTHOUGH PYTHON IS A GOOD START BECAUSE IT IS SIMPLE

  • @e.e.schonborn9344
    @e.e.schonborn9344 ปีที่แล้ว

    This was by far the most helpful python tutorial I have ever watched! Every single detail was explained super precisely and yet so easily at the same time! Thank you so so much!

  • @TheDrumtracks
    @TheDrumtracks 3 ปีที่แล้ว +7

    This is the best tutorial I found so far to this topic. (and the prettiest one. :D) Every line is explained in more detail than all others tutorial I watched. You Sir, did a great Job! I felt like I was able to understand everything!

  • @OettingerPirat
    @OettingerPirat 2 ปีที่แล้ว +1

    I can't imagine how I've could have invested the last 2-ish hours better than following your video and code along. Thank you so much for your fantastic work! :)

  • @RohanKumar-zd3pc
    @RohanKumar-zd3pc 3 ปีที่แล้ว +15

    Amazing, It is explained in a very detailed way
    Of all the logic from the setup to finishing

  • @MouseCodes
    @MouseCodes 3 ปีที่แล้ว +21

    I was happy getting the notification of this video. Keep it up, bro.

  • @iamailasama
    @iamailasama 3 ปีที่แล้ว +6

    thanks man! you are one of the best teachers out there, and you brought back my interest in python and programming!

  • @christophercoronaios4732
    @christophercoronaios4732 3 ปีที่แล้ว +1

    Wow, you are extremely helpful. I love how you explain each and every single line of code and just how you have put this tutorial together in general. This has been a great experience for me. Keep it up man!

  • @thelogbob281
    @thelogbob281 11 หลายเดือนก่อน +1

    Incredible incredible video! Very descriptive and thorough with every little detail! You are an amazing youtuber, and I love the effort!

  • @chikaosita662
    @chikaosita662 2 ปีที่แล้ว

    This channel is truly amazing ive recently got into pygame and I will definetly be using this channel for all my needs. Thank you so much !!!!!

  • @rxphi5382
    @rxphi5382 2 ปีที่แล้ว +6

    Hi, this is a great video, I learned a lot!
    The draw_grass() function at 1:38:45 seemed a little bit overkill to me tbh, so here is how I did it if anyone is interested in it:
    def draw_grass(self):
    for i in range(cell_number):
    for j in range(cell_number):
    if (i+j)%2==0:
    grass_rect = pygame.Rect(j*cell_size, i*cell_size, cell_size, cell_size)
    pygame.draw.rect(screen, grass_color, grass_rect)

    • @arkamukhopadhyay2177
      @arkamukhopadhyay2177 2 ปีที่แล้ว

      BG_COLORS = [(125, 215, 70), (100, 200, 50)]
      for x in range(cell_number):
      for y in range(cell_number):
      rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
      pygame.draw.rect(screen, BG_COLORS[(x + y) % 2], rect)
      this does the job as well :)

    • @arkamukhopadhyay2177
      @arkamukhopadhyay2177 2 ปีที่แล้ว

      @@vihasharma804 works for me though

  • @thecomputer1424
    @thecomputer1424 8 วันที่ผ่านมา +1

    This is a great tutorial even without having a solid understanding of python classes, I am still able to comprehend this a bit.

  • @sebzanardo
    @sebzanardo 3 ปีที่แล้ว

    AWESOME VIDEO! Super clear explanations of how pygame works and breaking down the program into manageable sections. subscribed and can't wait to see more pygame and coding videos in the future : D

  • @imherovirat
    @imherovirat 3 ปีที่แล้ว +4

    Perfectly Explained, You are genius. Keep making videos on pygame, I'm proud to find your channel.

  • @dogesh828
    @dogesh828 3 ปีที่แล้ว +32

    It's midnight i am gonna watch it tomorrow.
    But i know it's will be awesome as always.😆😆😆
    Please keep making more quality content.
    Lots of love❤❤❤

  • @bobbyreily3018
    @bobbyreily3018 3 ปีที่แล้ว +2

    You definitely deserve more subscribers! Keep the videos coming and they will flock in.

  • @Xnothen
    @Xnothen 3 ปีที่แล้ว +6

    Just started but I'm already loving the detail description of everything we are doing which is far better than some other tutorial which goes blazingly fast yet neglects to explain what is it that we are doing.

  • @tayyabsajid7552
    @tayyabsajid7552 3 ปีที่แล้ว +4

    How do you not already have a huge fan base, you're epic!

  • @cup624
    @cup624 3 ปีที่แล้ว +2

    i believe this tutorial is the most underrated one of other pyhton tutorials. thanks for your efforts.

  • @yussefhallak981
    @yussefhallak981 3 ปีที่แล้ว +3

    I got so happy when I saw the notification. This tutorial is gonna help a lot. Thank you!

  • @sarankumar9370
    @sarankumar9370 2 ปีที่แล้ว +1

    Ohh my God!! This is the best tutorial I had ever seen till today... The way you are explaining every tiny bit details is just amazing... Keep doing more video's..

  • @Omer-or8pg
    @Omer-or8pg ปีที่แล้ว +4

    Your videos are so informative and well-put. I love your channel SO much. I am so grateful for the effort you put into making your videos, and that you explain concepts beyond the code in an easily comprehensible way. Your pacing is great too. I eeply appreciate your content.

  • @momanirful
    @momanirful 6 หลายเดือนก่อน

    Fantastic tutorial, explains concepts clearly and then applies knowledge to write code. Love your delivery style, perfect for young children to learn coding.

  • @klausbrier6307
    @klausbrier6307 2 ปีที่แล้ว

    I found a lot of snake-game-tutorials, but I think, this is really the best one .

  • @JayeshSarvaiya
    @JayeshSarvaiya 3 ปีที่แล้ว +4

    I am just blown away by such an effort taken for smooth explanation, planning and gaming skills, you really came up with beautiful channel, All the Best for upcoming videos. Great Stuff !!! Thanks a tonnnnn... for everything. 😃

  • @clairehua7545
    @clairehua7545 3 ปีที่แล้ว

    Wow! This vid is awesome! I didn't even get stuck once! This is probably one of my better python projects. I currently code: block code, text code, java, javascript, C, C+, C++, html, and python. Would love to see more awesome tutorials on those coding languages. Keep up the AMAZING work!

  • @burak9802_
    @burak9802_ 3 ปีที่แล้ว +2

    Christian you're awesome keep on loading videos. We all have been waiting for new game videos!

  • @Brandonator24
    @Brandonator24 3 ปีที่แล้ว

    One of the best tutorials around. Good work mate!!

  • @Thoalfeqargamer
    @Thoalfeqargamer 3 ปีที่แล้ว

    What a legendary channel. Keep up the amazing work man!

  • @Codingforkids
    @Codingforkids 3 ปีที่แล้ว

    Thank you for the detailed and precise tutorial! Keep up the great work!

  • @soumenmondal7638
    @soumenmondal7638 3 ปีที่แล้ว +2

    THANK YOU SOO MUCH FOR CLEARING EACH AND EVERY PORTION SOO WELL ❤

  • @aimeedillard9665
    @aimeedillard9665 2 ปีที่แล้ว

    So glad I found this channel, I immediately subscribed! Can’t wait to try some of this out

  • @HarryTK
    @HarryTK ปีที่แล้ว

    broken it down makes it seem so user-friendly and easy to use. I can’t wait to start making soft! Thanks again!

  • @parthgupta4850
    @parthgupta4850 3 ปีที่แล้ว +2

    100th comment says that this is awesome tutorial you teach explain everything as other skip many things and at last we are confused but you are not like them you teach and explain and write everything, best tutorial as well as game of your channel.

  • @shivamkantprasad
    @shivamkantprasad 3 ปีที่แล้ว +2

    Keep making these tutorials they are fun and easy to understand

  • @nullifier_
    @nullifier_ 2 ปีที่แล้ว

    Thank you so much! You helped me create my first game ever.
    I even added things like out-of-bound wrapping check for when the snake goes outside of the grid and tail cut for when the head intercepts the body

  • @sabirkhan6898
    @sabirkhan6898 3 ปีที่แล้ว

    Well explained! I loved the amount of detail given throughout the video

  • @YanisCharef-dy5rk
    @YanisCharef-dy5rk 11 หลายเดือนก่อน +1

    That was a simple and a clear tutorial, thank you!

  • @HoRRoRlets
    @HoRRoRlets ปีที่แล้ว

    Great job on this Tutorial. Helped me get my head around vectors quite a bit more.

  • @sahidabegum7365
    @sahidabegum7365 6 หลายเดือนก่อน

    This is the best tutorial about pygame. I enjoyed it and also understand the concepts and logics very easily.

  • @Daloshka
    @Daloshka 3 ปีที่แล้ว +2

    I've just started watching your video about Flappy Bird. I want to say thanks for your videos. You make a huge influence on me and other people

  • @muradarif7373
    @muradarif7373 2 ปีที่แล้ว +2

    I have finished the tutorial, Huge thank you for such a video 🤝👌

  • @liukang81
    @liukang81 ปีที่แล้ว

    Your explanation is awesome man! You’re the best teacher! 👍

  • @majdnassar3257
    @majdnassar3257 3 ปีที่แล้ว

    Thanks alot, first time i use pygame lib and i understand your tutorial, because you explained every thing very well and clear
    :)

  • @jananni123456789
    @jananni123456789 3 ปีที่แล้ว +3

    Such clean, logical, well organised code!

  • @Hotterman10
    @Hotterman10 3 หลายเดือนก่อน

    Wow, best pygame tutorial ive seen so far. Bravo.

  • @mr.curious1714
    @mr.curious1714 2 ปีที่แล้ว +2

    This is the best video on #PygameBasics and #SnakeGamePython . You have explained everything in the perfect pace. I would recommend this tutorial to everyone who wants to learn pygame and also snake game using pygame. Best tutorial!!!! You Earned a sub!!!!

  • @pitermg
    @pitermg 9 หลายเดือนก่อน

    Very nice, complete and thorough tutorial!

  • @parthgupta4850
    @parthgupta4850 3 ปีที่แล้ว +1

    Congrats!
    1 unlike only out of 5000 views great job this proves that you're videos are quality driven.

  • @Schachlover
    @Schachlover 3 ปีที่แล้ว

    Awesome as always. Plz. keep up the good work!!

  • @catastrophiccarrots5626
    @catastrophiccarrots5626 3 ปีที่แล้ว +3

    I love this tutorial it is so clear and informative! Plus every error i get is always my fault as i did a typo or forgot the () thanks so much i will definitely watch more of your videos and you got another sub!

  • @shubharamanath3049
    @shubharamanath3049 3 ปีที่แล้ว

    thank you for this tutorial, I was struggling to get my snake program to work then I searched the internet to find the solutions but I did not find any , then I saw your vid , I learnt the pygame basics now I have coded many projects in which I had very few errors. thank you again for this very good tutorial. 😀😀😀😀

  • @jakenik1241
    @jakenik1241 3 ปีที่แล้ว +2

    Very good work and very good explanation. The only thing I found is that if you press two buttons at the same time like key.up and key.left and the snake goes to the right direction then you cause the game to end, i believe likely because the snake has not had time to go up and gives the command to go right. Again pretty amazing work keep it up!

  • @rubenbartelet756
    @rubenbartelet756 3 ปีที่แล้ว

    You need more subscribers man! This is quality content!

  • @yisus.avocado
    @yisus.avocado 3 ปีที่แล้ว

    This so awesome, really interesting video, keep up doing well!

  • @bycorrohmarketingdesigns7852
    @bycorrohmarketingdesigns7852 2 ปีที่แล้ว

    you are AMAZING MAN! Very clear and good class man!! CONGRATS!!!

  • @atharvapatil7474
    @atharvapatil7474 3 ปีที่แล้ว

    Best tutorial of snake game ever found on TH-cam

  • @astrowow9377
    @astrowow9377 3 ปีที่แล้ว +2

    You are the best mate, my greetings from Mexico ✌️🇲🇽💕

  • @hoatran-dg3ir
    @hoatran-dg3ir 2 ปีที่แล้ว

    Wow this tutorial is really good! Thanks man!

  • @jeffreywen8901
    @jeffreywen8901 3 ปีที่แล้ว

    awesome tutorial, I learned a lot. great work!

  • @anupambayen5554
    @anupambayen5554 2 ปีที่แล้ว

    Fantastic Tutorial for beginners. I have learned lot from this tutorial.

  • @banji8171
    @banji8171 3 ปีที่แล้ว

    Wow, we need more tutorial like this, keep it up🥺❤️❤️

  • @armeidiandanny0522
    @armeidiandanny0522 ปีที่แล้ว

    Very Helpful and really easy to understand man.. keep fight and consistenly upload to create implementation PyGame learning with another game 👍

  • @AbhishekMishraiitkgp
    @AbhishekMishraiitkgp 11 หลายเดือนก่อน

    Extremely well explained tutorial. Thanks!!!

  • @kalashshah1049
    @kalashshah1049 3 ปีที่แล้ว

    Hey clear code
    you deserved more subscribers.
    really nice videos.

  • @RandomJeevanYT
    @RandomJeevanYT 2 ปีที่แล้ว

    I wanted to creat snake game in python i saw so many tutorials could do the code and it worked but i wasn't satisfied cuz i understood only partly.
    But ur vids are amazing the differences u show between surfaces and display surface, explaining each line code. Just prefect 🤗🤗

  • @zgreatest69
    @zgreatest69 3 ปีที่แล้ว +1

    You explained it so well I'm so glad

  • @Sc4rletAss4ssin
    @Sc4rletAss4ssin 3 ปีที่แล้ว

    Your tutorial is fantastic thank you so much for making this

  • @NoisyMachines
    @NoisyMachines 3 ปีที่แล้ว

    That it's a master CLASS! Thank you! ::)

  • @danielDev-hg3jo
    @danielDev-hg3jo 6 หลายเดือนก่อน +1

    Great video, excellent explanation.

  • @zCrabOG
    @zCrabOG ปีที่แล้ว

    Thank you for explaining the "why's". The puzzle pieces just fall straight into place

  • @builder481
    @builder481 3 ปีที่แล้ว +3

    Friends : what did you had for breakfast?
    Me : I had some eggs in a rectangle plate

  • @isakurbanov8627
    @isakurbanov8627 3 ปีที่แล้ว

    Great content keep up the hard work!

  • @mariozaldivar7802
    @mariozaldivar7802 3 ปีที่แล้ว

    Great tutorial! really helped me out!

  • @muhammadahmadkhan3304
    @muhammadahmadkhan3304 11 หลายเดือนก่อน +1

    Jazakallah ... Very beautiful explanation ... Thank you so much❤❤❤❤

  • @ElloBruh69
    @ElloBruh69 2 ปีที่แล้ว

    Thank you. I have learnt a lot from this video!

  • @JohnSmith952
    @JohnSmith952 3 ปีที่แล้ว

    Every your video is pure gold

  • @RahilKasimi
    @RahilKasimi 3 ปีที่แล้ว

    Just Awesome brother, I learned a lot.

  • @mrmizan1977
    @mrmizan1977 3 ปีที่แล้ว +2

    You’re really a great programmer!

  • @potato420
    @potato420 3 ปีที่แล้ว

    i love your videos so much you explain perfectly

  • @olavlilj1145
    @olavlilj1145 3 ปีที่แล้ว

    This was great!!! Thank you!

  • @solijanov_05
    @solijanov_05 ปีที่แล้ว +1

    good man👍 thank you for video

  • @Vincent-fo9ie
    @Vincent-fo9ie 3 ปีที่แล้ว

    thankyou very much this really helps. very detailed and explanation is very good. :D

  • @samkh746
    @samkh746 3 ปีที่แล้ว

    Really informative, thanks

  • @Code_in_simple_English
    @Code_in_simple_English 2 ปีที่แล้ว

    These lessons are just awesome. Thank you for posting them. What would be useful in the comments, if you were interested in adding them, are the key concepts taught: like Vector2, enumerate, slicing.. and so on.

  • @angelinalieske8516
    @angelinalieske8516 3 ปีที่แล้ว

    okey, this video is saving my life !
    thank u!

  • @aymaneaymane1950
    @aymaneaymane1950 3 ปีที่แล้ว

    Thank you a lot
    I was really happy when I saw it

  • @sermeliodas8618
    @sermeliodas8618 3 ปีที่แล้ว

    Amazing work thank you so much

  • @PixelGaming_2020
    @PixelGaming_2020 ปีที่แล้ว

    Really good tutorial. You explained everything in amazing detail.
    With a few other tutorials, they move too quickly, which causes me to have errors in my code that I don't know how to correct.
    This one is nicely paced, and I end up with a fully working and functional snake game.

    • @imSHEN
      @imSHEN ปีที่แล้ว

      how do you fixed 'MAIN' object has no attribute 'snake'

  • @007JackTR
    @007JackTR ปีที่แล้ว

    Awesome stuff!!! Thanks

  • @reemnasser9105
    @reemnasser9105 2 ปีที่แล้ว

    Thanks for this great video. it made my weekend :))

  • @yaroslavkovaliv6884
    @yaroslavkovaliv6884 3 ปีที่แล้ว

    great video, love it, pls make some more pygame videos!

  • @salouhimohammed1030
    @salouhimohammed1030 2 ปีที่แล้ว

    i think is more efficient to use a while loop to avoid the fruit spawns over the snake, this part i coded it like below
    while self.fruit.pos in self.snake.body[1:]:
    self.fruit.randomize()
    thanks for the great tutorial, a channel like the yours i never seen before