Coding A Snake Game in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·5y ago

Key Takeaways

The video demonstrates how to code a simple snake game in Python using Pygame, covering game development, game logic, and event handling. It utilizes tools such as Tabnine for auto-completion suggestions, Pygame for game development, and pip for installation.

Full Transcript

[Music] what is going on guys welcome back in today's video we're going to build a simple snake game in python so let's get right into it now before we get into the actual coding of the snake game i want to mention that this video is sponsored by tab nine and before you skip that part because it's just a sponsorship let me tell you that i would never recommend something or advertise something unless i think it's useful and interesting so i'm going to use tab 9 in today's video because tab 9 is an auto completion model that you can use in modern development environments like pycharm intellij or vs code for example and what it does is it suggests you some auto completion that goes beyond the basic auto completion that you already have in pycharm or vs code because it does not work with a static model but it uses a gpt2 language model so it uses deep learning in combination with gpt2 in order to suggest the best auto completion possible and it's also learning over time so it this means that when you're coding using tab 9 in the background it not only gives you intelligent auto completion suggestions but it also trains itself to give better suggestions over time which means that for example if a suggestion might not be perfect right now and you delete it it will learn based on that deletion and it will not recommend the same thing or it's less likely to recommend i recommend a similar thing in the future and vice versa if you use a good auto completion or if you take a suggestion that it gave to you it's going to give you more of those suggestions in the future um so what i want to tell you here is that tab 9 is completely free if you want to use the free plan it's forever free you don't have to pay anything you can just install it in your uh development environment so you go to pycharm for example and to settings and plugins and you install tab 9 or you go to vs code extensions and then tab 9 and you can use this free basic code completion model here so you have a basic completion model and you have community support if you want to pay for it you get a better model you get a cloud model you get unlimited code completions uh you get up to five suggestions at the same time and just overall the auto completion experience is better so if you want to pay for it or if you even have a team with with with which you want to use um tab nine you can also pay for the team with even more support and more customized and uh personalized models and with analytics and so on so this is a very interesting product and i'm going to use it throughout this video today and you can take a look at it and let me know in the comment section what you think about it if you're interested you can just install it on your favorite development environment all right so let's get into the coding the first thing we want to do is want to install pygame because pie game is what we're going to use in order to build the snake game and for this we're going to import pie game of course if you don't have pie game installed you need to open up the cmd command line or your terminal depending on the operating system and we're going to say pip install and pi game just like that this is a very simple install and once you have pycam installed you import it and then we also import two other modules which are time and random we're going to need time for some timing and we're going to need random in order to spawn uh the food on random places or in random places so we're going to say import time and import random like that now what we're going to do now is we're going to initialize pygame we're going to set some colors we're going to set some dimensions we're going to set up the whole display and then we're going to start coding the game logic so first of all we're going to initialize pie game and for this we're going to use pie game pie game dot init as you can see we already have an auto completion here pie game dot init and then we're going to define some colors so let's just add some comments here define colors up here we're initializing pie game and for the colors we're going to say white equals and i'm going to use two five five two five five two five five here um then i'm going to say black equals and the interesting thing that you can see here is when i start typing black it already suggests that i can use 0 0 0 this is tab 9 auto completion then i can say red equals and i can use 2 5 5 0 0 here and then i can use orange and orange is one 255 five zero so those are just the colors white is going to be the color of the snake black is going to be the color of the background red is going to be the color of the game over message and orange is going to be the color of the food that we're chasing as a snake or with the snake then we're going to set width and height height of this uh of this display so we're going to say 600 times 400 and then we're going to set up the display by saying game display game display equals pie game dot display dot set mode and we're going to set a tuple of width and height so that we have the dimensions here and then we're going to set a title so we're going to say pie game dot display dot set caption and we're going to call this i don't know neural nine snake game like that all right so after that we don't have too much else to set up we need some fonts and we need a clock of course because the clock is going to time the whole thing or keep the whole thing running so we're going to say clock equals pi game time clock as you can see perfect auto completion here um and then we're going to set also the snake size and the snake speed so let's say snake size is going to be 10 pixels and the snake speed is going to be 15 pixels and then we're going to set two fonts or actually we can use the same font it doesn't really matter uh maybe one should be larger than the other one so we're going to have a message font so giving us information um like game over for example message font and this is going to be pygame dot font.sysfont and it's going to be ubuntu in this case you can choose whatever font you like and i'm going to use size 18 or actually we can make this larger let's use i don't know 30. uh maybe that's too large but let's see and then we're also going to have a font for the score because you always want to know what your score is if you eat one food you have one score another food piece of food then you have two score and so on so you want to have that font as well score font is going to be pi game font cis font and also ubuntu in this case with the font size i don't know 25 for example so this is the basic setup of our snaking all right so what we're going to do next is we're going to define two functions that we're then going to be calling later on in the main function in the run game function and those two functions are just responsible for updating or drawing the score and updating or drawing the snake the full snake with its position so the first function is going to be the print score function or you could also say draw score function here we're going to pass a score to it and then we're going to say uh the text value here is going to be score font which is the font we used to to write this message or to draw this message in this case score not the message and then dot render so score font dot render and we're going to render the score but we're not going to just render the numerical value but we're going to say score colon and then the string of the score so we're going to have one string here um and we're going to anti-alias it and we're going to pass a color here which is going to be orange for the score so i didn't mention this in the beginning i think i said the food is going to be orange but this message the score message is also going to be orange of course you can feel free to change that to green for example if you want to um and once we have that text object we're just going to uh draw it onto the display so we're going to say game display dot blit text and uh we're going to pass zero zero here as a position so this is the first function the second function is the draw snake function the draw snake function is going to take the snake size and the snake pixels so a snake consists of many pixels especially if it eats a lot of food pieces it's going to have a large size and what we're going to do is we're going to have a list of all the positions of these pixels and the size of the individual pixel so in this case we're just going to say draw snake and we're going to say for every pixel in snake pixels we're going to say pie game dot draw draw dot rectangle or just rect and then on to the display we're going to draw in white color and here we're going to now pass a list which is going to be the pixel 0 and pixel one which is just the position of this individual block and we're going to pass the snake size here so snake size and another snake size so the basic idea is that we draw a rectangle uh let me just shrink this a little bit we draw a rectangle which is just a pixel itself um on the position 0 1 or on the position of index 0 so we have this pixel and this has the x coordinate and the y-coordinate at this position we plot a pixel or we draw a pixel of that size of the snake size so those are the two basic functions all right so now that we have all that we can go ahead and start coding the run game function which is the main function that we're going to call later on to start the game and for this we're going to say def run game and here we're going to have to define a couple of things first of all we're going to say game over is going to be false because that is a variable that we're going to use in a while loop later on which says while not game over do the same thing over and over again uh and we're also going to have game close defined as false because game closing game over are not the same thing i can be game over but still uh the game is not terminated i can start a second round or i can just look at the score i can do whatever i want game close means that we're actually closing off the game um so those are two different variables here then what we're going to do is we're going to define a starting position the starting position is going to be x at width divided by 2 and y is going to be height divided by two and since we only have one pixel that's enough um and we can also define a uh default speed and since we haven't started the game yet since we haven't done anything yet we're just going to keep this snake in the center of the screen without it moving anywhere unless we start pressing some keys so we're going to say x speed is just going to be zero and y speed is also going to be zero here um once we have that we also need to define the snake as a list right now we only have one pixel so that's not really important i mean we have a pixel or one block you could say it's not a pixel um but we have only one so a list is not really necessary but over time the snake is going to grow in size so we need to add some more blocks to it and for this we're going to say snake pixels is going to be an empty list and the snake length is currently one in the beginning since uh we don't have anything but this one pixel in the middle of the screen now once we have that we need to also spawn a random target or the target is not random but the position of the target is random so we need to have some food somewhere in the map that is not out of bounds and for this we're going to say target x or food x you can call it whatever you want is going to be around random dot rand rage not rent rage but rent range um it's going to start at zero and it's going to go to width minus the snake size so that we have enough space to fit in the snake itself and then we're going to divide the result by 10 as a floating point number so that we get a floating point result and then we're going to multiply the whole thing by 10 floating point and we're going to do the same thing for target y and here we're going to use the height and not the width so we're going to say height there you go and that is how we spawn the random targets all right so now we're going to start writing the game loop the main game loop which is going to be while not game over so while we haven't lost yet we're going to say for event in pygamevent.get which is a great audit completion again we're going to say if that event the type of this event is pie game dot quit so if that's the event um then we're going to say game over equals true otherwise if the event type is not quit but if the event type is event type is going to be pygame dot key down if that is the case and we actually want to have this come on like that um if the event is key down we're going to ask for which key we actually press so we're going to say if the event key so if event key equals pygame dot k and then we want to know if it's left for example we're going to do different things than if we press up down right and so on so if we if we press left what we want to do is we want the snake to go to the left so we want to change the speed so that the snake goes to the left as much as it is uh as much as the size it has so we want wanted to move one block per second or per clock tick to the left um so what we're going to do is we're going to say x speed is going to be negative uh size so it's going to be negative snake size why negative because if you have a python um if you have a python window maybe it's it's better if i show it with a mouse because here it's inverted the top left is zero zero and the more you move to the right the more you're increasing uh the position the more you move down the more you're increasing the y position so if you go to the left you're actually decreasing the x coordinate or the x position and the y speed is going to stay zero or to become zero because we cannot move diagonally in snake so we're just going to set this to zero and we're going to repeat this process for our if event key is going to be pie game right then we're going to say x speed is going to be the snake size so the positive snake size and the y speed is going to be zero again then if event key is pi game dot k up then we're going to say um xp is going to be 0 y speed is going to be negative snake size because again we're moving up so we're decreasing the y coordinate um and then if the event key event dot key is going to be pi game k down then we're going to say x b to zero and the y speed is going to be negative uh not negative it's going to be positive snake size because then we're moving down and increasing the y coordinate like that um now we need to take care of the indentation here uh there you go so this is the basic thing that we're going to do and of course if we hit the boundaries so if we go out of the screen uh we're going to have to close the game as well so we're going to just say after that so after the uh the for loop actually so here we're going to say if x is larger or equal to to the width so if we reach the the right limit or if x is less than zero which means we reach the left limit or if y is larger or equal to the height or if y is less than zero then we're going to say game close equals true which means that we lost but we didn't quit but we just lost uh so we're going to move to a screen that we haven't defined yet we're going to add it up here but that's the basic idea here um so then what we're going to do of course is no matter what we did maybe we did nothing maybe we increase the speed or we change the direction we cannot really increase the speed but whatever happened we need to advance so what we need to do is we need to say the position of the head is going to be increased by the speed so if the speed is zero if the x speed is zero because we're going up or down nothing is going to change otherwise it's going to change depending on the speed and here we're going to say y plus equals y speed so this is just the movement and uh this is how we move the snake so we wait for the events if we quit we quit and if we get a key press we're going to change the speed and with each iteration we're going to change the position based on the speed so now we have all the information about this game we know where the snake is we know where the target is we know what colors we want to use we will know the speed of the snake we know what key we pressed and so on but we haven't drawn anything onto the pie game window yet so this is what we need to do next and the first thing we want to do is we want to set the background so we're going to say game display dot fill with black color this is just the background and onto that background we're now going to plot the target we're going to draw the target piece this is just going to be a snake sized uh square that is going to be the target and for this we're going to say pie game dot draw dot rectangle onto the game display in orange color and the position is going to be target x that we already have target y that we already have uh and it's going to be snake size and snake size so this is just a basic target and what we need to do next is we need to update the snake which means that we need to add the head of the snake which is constantly moving to the list of the snake pixels while also removing the tail so that the snake moves and it's not just growing all the time so we need to add the head and we need to remove the last piece of the tail constantly and of course we don't want to remove the last piece of the tail if the snake length increased so if we actually got a target so because then the snake actually gets longer and what we need to do in order to accomplish that is we need to say snake pixels dot append and we're going to append the current position x y to the snake pixels and afterwards we're going to check if the length of the snake pixels is larger than the snake length then we're going to delete the tail so we're going to say snake pixels zero so the idea is that we start with an empty list which has length zero we have the snake length of one which means that when we add this initial pixel it's not going to delete anything it's just going to move around that pixel and if we and it's going to the it's not going to do anything but if we have more pixels it's going to uh move the head and move all the other pixels as well and it's going to always delete the last pixel so that the snake is not growing automatically but of course if we then find a target if we eat a target we're going to increase the snake length which is going to allow for one more block to pop out in the end so this is the basic idea here um and then we're going to save 4 pixel in snake pixels up until the last block up until the one before end um we're going to see if this pixel so we're checking all the pixels except for the hat if this pixel or if one of those pixels is in the same position as the current let me just remove this if pixel is in the current position of the head this means that the snake ran into itself so if you have like five blocks and the snake is going right and without going up immediately turning to the left it's running into itself uh or if it's just going in circles and bumping it into itself this means that the snake crashed into itself and this is also game over so what we need to do here is we need to say game close equals true then of course we're going to plot the actual snake and the score so we're just going to say draw snake which is the function that we have to find above we're going to pass the snake size and we're going to pass the snake pixels list and then we're also going to print the score which is always going to be the snake length minus one why is it going to be the slake snake length minus one it's going to be the snake length minus one because we start at a default length of one which means that we haven't done anything and we already have a length of one but we still want to have a score of zero because we haven't done anything and when we got uh to one target when we eat one piece of food of course we want to have a score of one even though the length is two and so on so this is why we have snake length minus one score and then we're going to say pie game dot display dot update so that the changes actually happen or all the drawing that we did actually happens on the display uh and once we have that we need to check if we are running into a target so if the snake if the head of the snake is at a position where a piece of food is then we're going to say okay we can eat that piece of food we can respawn a new piece of food and we can also increase the snake length by one so we're going to say if the current position is the same position as the target position so if x equals target x and y equals target y if that happens we are in the right position and we're going to spawn a new target which means that we're going to just copy these two lines here and there you go come on um and after that of course we're going to increase the snake length by one because then it's not going to delete the tail immediately like that and of course don't forget to do a clock tick in the end because otherwise we don't have any progress so clock tick and snake speed is going to be what we passed here and once we're done with all that so if we get out of the loop uh we're going to just say pie game dot quit and just quit in general and we're going to run the game so last but not least we're going to make use of this game close variable here that we have because we haven't used it up until now and what this is going to be used for is for a game over screen so that the game doesn't just disappear once we crash into a wall or into the boundaries or when the crowd when the snake crashes into itself we don't want the game to just end we want to have a game over screen and maybe the option to restart the game instead of just closing it and for this we're going to go inside of the game loop here and we're going to say while game close which is interesting because this serves also as an if because if the game is closed we're going to enter this otherwise we're not going to enter it but when we enter it we're going to stay inside of the while loop as long until um or as long as game closes true so we need to change it in order to get out of the out of it um and for this we're going to say first of all we're going to fill the screen again with black we're going to say game over and we're going to say um we're going to show the score and then we're going to have the possibility to restart the game or to leave the game so we're going to first say game display fill game display dot fill we're going to fill it with black color and then we're going to have a game over message and the game over message is going to be message font render there you go perfect auto completion game over like that we're going to use anti-aliasing and the red color and we're going to say game display dot blit game over message there you go and the game over message is going to be positioned in width divided by two height divided by two uh we can also do and divide it by three shouldn't be too ugly um and then we're going to update the score again so we're going to say print score snake length minus one and pie game dot display dot update so that we actually see the changes and once we have that we're going to wait for um for user input so if the user inputs uh or if the user presses one where the player presses one this is going to quit the game if the player presses two it's going to restart the game so we're going to save for event in pie game pie game event get auto completion again here if the event type is going to be pie game key down if this is the case we're going to check which key was pressed so if event key equals pi uh pi game dot k 1 then we're going to quit and in order to quit we need to exit this loop and we need to exit this loop so we need to set this year to false and this here to true so we need to say game over equals true and game close is going to be false so that we exit all that um and once we have that we can also say if event key equals pi game dot k2 so if this is the case we're going to restart the game in order to restart the game all we need to do is we need to just recursively call the run game function and that's it now alternatively we can also say if event type is going to be pi game quit then we can also just do the same thing that we did up here and there you go this works perfectly fine as well so we're finally done and we can go ahead and give this game a try we can just start it and you will see that we have a score zero we have a white snake in the center of the screen we have the target down here which is very unfortunate because it's very hard to get there i hope i won't fail there you go and there you go score is one and we can just go ahead and play snake for a while here i'm not very good at that game come on there you go score two score three score four and so on and after a while you can just decide to crash into the wall for example then you have game over if i now press one it's going to quit if i press two it's going to start the game again and i'm going to show you that i can also crash into myself so i i just have to collect collect a little bit of food like that come on and one more maybe and one more maybe so now i should be able to or maybe one more come on i should be somehow able to crash into myself now the problem is right now i crashed into myself but it was because i was going right and then i turned left without going up or down and then i crashed to myself if you don't want this to be possible you can just say okay if there is a block if you're currently going right you cannot just go left if you're going up you cannot go down you have to go like uh first right and then down if you're going up for example um but otherwise you're just going to crash in yourself if you're long enough of course you can also crash into your tail you don't have to crash into your second block uh but that's how you how it's going and now we can also press one and the game is quit or the game did quit and this is how you build a simple snake game in python so that's it for today's video hope you enjoyed and hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free also don't forget to check out tab 9 installed in visual studio code or in pycharm it's an interesting product it's very useful and the auto completions are quite interesting as you saw in this video so make sure you check it out and other than that thank you very much for watching see you in the next video and bye [Music] you

Original Description

Today we learn how to code a simple snake game in Python. Get Tabnine for FREE here: https://bit.ly/3cO76nS ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to code a simple snake game in Python using Pygame, covering game development, game logic, and event handling. It provides a comprehensive guide on how to create a snake game from scratch, including setting up the game display, handling user input, and implementing game logic.

Key Takeaways
  1. Install Pygame with pip install pygame
  2. Import necessary modules
  3. Initialize Pygame
  4. Define colors and display dimensions
  5. Set up display and fonts
  6. Define game over and game close variables
  7. Define starting position and default speed
  8. Define snake as list of pixels
  9. Use while loop to run game until game over
  10. Handle user input using event types
💡 The video demonstrates how to use Pygame to create a simple snake game, highlighting the importance of event handling and game logic in game development.

Related Reads

📰
AI CLI Tools Are Eating Each Other's Lunch
AI CLI tools are becoming increasingly similar, making it difficult to choose between them, and their failure patterns are identical, highlighting the need for more transparency and accountability in AI development
Dev.to · Tracepilot
📰
Loop Engineering: The Skill That Just Made Your AI Workflow Obsolete
Learn how loop engineering can automate AI workflows, making traditional prompting methods obsolete
Medium · Machine Learning
📰
We built a real-time Pokémon TCG AR overlay for live streams and open-sourced everything
Learn how to build a real-time Pokémon TCG AR overlay using a webcam, gaming PC, and open-source AI
Medium · Deep Learning
📰
I tested the new Claude Desktop on Linux - here's how it compares to rival apps
Learn how Claude Desktop on Linux compares to rival apps and its limitations with local AI
ZDNet
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →