Snake Pygame Tutorial #2
Skills:
Tool Use & Function Calling85%
Key Takeaways
The video demonstrates how to code a basic version of the snake game using Python and the Pygame module, covering topics such as event handling, key presses, and game development. The tutorial is suited for intermediate level programmers and uses Pygame's event handling and key module to control the snake's movement.
Full Transcript
so that's where things can get a bit complicated anyways to start what I'm gonna do is to say for event in PI game oops my doing of my typing here tie game at event dot gets like so and now we're just gonna say if event dot type equals equals PI game dot quit like this and this is the first one we always want to code in because we want to make sure that if they click that little red arrow it actually works and we can quit you're a new plugin quit like that and I'm just going to say if actually we don't even need another if because the way I'm going to do this is this other way you might have seen in PI game before to move things around he's like if vent dot type equals key down and then you just check which key was pressed and move to the left this is a similar thing but it works smoother I've used both of them a lot and this is one I prefer so I'm just going to use this so PI game duck key don't get underscored press so what this is gonna do is it's gonna get a list actually I think it's like a dictionary or something like that that has all the key values and then if they were pressed or not so that way if you press more than one key at once when you're looping through it'll adjust to that whereas the other one you can only click one at a time and you couldn't move like per se diagonally if you're moving like a character on the screen it doesn't matter too much for this but this is what I like to do it so now I'm gonna say four key in keys because remember this is gonna give us all of the keys and then like 1 or 0 value if they were clicked or not so we need to loop through all of them and check them say if keys and then PI game dot key or loop start ok underscore left I thought it was key then and then we're going to say if keys I game don't K underscore right like that and we can continue down here with up and down and then we'll get into exactly what is going to happen when we click these keys so last one here all right and there we okay so now what we're going to do is we're gonna change the direction according to what key we click so if we're going left that means that our X has to be negative because the way again coordinates work in PI game is in the top left hand corner of your screen is zero zero so if we want to be moving left we should be making our X negative to move more towards zero right if we want to move right now you make our X positive which would be one in this case because we're gonna move one cube at a time to go further to the right same thing with the Y it's kind of weird in PI game how the Y works because the more Y you add the further down you go so we'll do that inside so for X we're gonna set itself up during x equals negative one again except moving left and we're gonna set our journey y equal to zero and this is because again we don't to be moving in two directions at once and be going diagonal now this is what I was talking about it's we need to remember where we actually turned because if you just have one cube in the ground that's fine but when you have multiple cubes we need to be able to turn left right we need to remember where we turned so at the tail of our cube can turn at that point so that's why we have that self dot turns list up here or just this turns uh I want to say dictionary or set whatever so we're gonna add to that you know self thought turns and we're gonna give it a new key and I'll go through how this works in a second self thought pause here is equal to and then we're gonna give just a list of self duck turn x and self dot turned blind okay so you might be kind of confused on what's going on here so I've created a new dictionary up here that's what I'm gonna call it officially you could think of it as a set as well and pretty much we're gonna add a key which is the current position of the head of our snake and then it's gonna be set equal to what direction we turned so we're saying we have a new turn at this position and our character or where our snake moved left so it's gonna say we have self-join Y which is right here as negative one self dot certain X R and then during Y is equal to zero so we know what way we actually turned and I hope that makes sense that's yeah that's how we're gonna do that now I'm just gonna copy and paste these and then change these values so when we're going right this is gonna be one oops same thing here so put that in this is gonna be zero and turn why we're going up is gonna be negative one now when we're going down we need to change this to 1 and this to zero and I'm just gonna get rid of that and there we go okay so that is pretty much how we go about doing this and I can also just put elif's here as well because we don't want to be able to move in more than one direction so we're gonna prioritize the left arrow key if they're not clicking the left arrow key then we'll check they're doing the right otherwise up otherwise down because we don't want them to be able to click more than one key at once ok so next we need to actually move our cube and this is where it gets a little bit more tricky in terms of looking where our cube is and if it's at that turn then we're gonna turn it so I'm just gonna start coding that I'm kind of gonna go through exactly what I do so before I see in enumerate oops and I spoke that wrong didn't there like that and then we can say self-taught body cuz we're gonna look through the list of positions that we have in on the snake I'm gonna say P equals C top position and I'm just gonna copy this by the way that's what this little arrow does here it just makes a copy so that we're not changing the position of our snake when we do things it's just I just follow along with it's not super important okay so what I say if P is in self dot turns like this because remember what we did is we added the position of our head to turns so now we're going to say well if this position is in the turns then we're gonna move or we're gonna turn right so let's say here so we and say turn oops equals self dot trends P like that so now we've seen that it's in there so now it's in there we can find the index of it and we can grab the direction value so we know what way we're gonna be moving and I'm just gonna say see don't move like this and turn zero turn one like so okay now next I'm gonna say if I equals equals the length of self-thought body minus one oops then I'm so gonna do self I don't know why that's capital itself but turns.com okay so I know I just did a lot of coding here and this probably doesn't make a lot of sense you guys so I'm gonna kind of run through exactly what I just did it's it's pretty it's just a little bit difficult to explain so anyways what we start off by doing is we're gonna say we're gonna get the index and we're gonna get the cube object in our self up body because remember our self dot body is made up of cube objects right here that have these property right they're gonna have a direction Y Direction X a start color so on and we're gonna get we're gonna get to coding cube in just a second so there have cube objects now all of these cube objects have a position so I'm saying for each object here I'm going to grab their position and I'm gonna see if that position is in our turn list and we create the turn list and add to the turn list when we turn right when we click left sent it right so on and then we're gonna say okay so the actual turn so where we're actually going to be moving is equal to that are the turns list at that index right so we grab the turn direction X and Direction Y which we stored there and now we're going to just gonna say R cubed move which is another method that we're going to code and we're giving it that direction X in that direction y so it knows what weight it needs to move pretty well and then I say if I is equal to the length of self-thought body minus one so that means if we are on the last cube we're gonna remove that turn so once that last cube hits that turn we're gonna remove it because if we were just to leave that turn in the list and that would mean anytime you hit that position on the screen regardless of if the snake was turning there or not you'd automatically change directions if you don't remove it from the list hope that makes sense okay now also after this if here we're gonna say if the woody call it if it's not in the list so if our position is not in the list we still need to move the snake because it's constantly moving so what do we do to this when we write an else and I'm just gonna copy this in because it's gonna take a second to type because I have another file open and I'll go through what happened how this works okay no looks like a lot but pretty much what this is doing for us is we're checking whether or not we've reached the edge of the screen so saying if we're moving left and the position so the exposition of our cube is less than or equal to zero then we're gonna change that position so that it goes to the right side of the screen so the way that we can do that is by saying Cedros minus one because again in you start counting at zero and computers so if I rows is like 20 then the last cube would be 19 in a list right so they see that rows minus 1 we're gonna be at the same y-value so we'll leave that there okay and then we say otherwise we're going to check if we're going right for moving right and same thing if we're at the edge of the screen move back to the left side by putting 0 here and the next one if we're going down what we're gonna be doing is again checking are we less than rows minus 1 or greater than rows minus 1 no we're not or if we are then let's move us back up to the top of the screen by changing our Y value and then same thing here if you're moving upwards now if none of that's true so we're not at the edge of the screen we're not moving up left down to whatever I just said then we're just simply going to move our cube at the direction X and the direction Y of that cube already so if that cube is moving upwards and it's not turning and it's not going to the edge of the screen it doesn't need to be changed just keep moving it in whatever direction it's going and that's why I'm simply referencing the cubes already existing Direction X and Direction Y we're not changing anything we're simply just saying okay let's move you forward one like that I hope that makes sense that was a lot and that's probably one of the hardest things in this program is figuring out the movement the stake so if you guys understand that then that is the hard part kind of over with and we're gonna get into this cube objects you're gonna understand what all this means and how our rows and columns kind of work and stuff at that okay so I'm just gonna forget about reset and add Q for now cuz we'll worry about those later but let's get into the drama thing because what I see if there's actually working and put this on the screen so simply for I see and new rates good why can i oh it's well under way today like that same thing self that body like that we're going to say if I because equals zero C dot draw and you'll see why these in a second true else see draw circles like that okay so the reason I'm doing this old check here rather than just drawing every object is because when we draw the first snake object on sure if you remember my other program it had eyes so what I would simply want to do is make sure that when we draw that first object we add eyes on to it just so we know where the head of the snake is we don't get confused like what directions maybe even although you shouldn't we I want to add the little eye so maybe just for aesthetic as well so what this true does optional parameter simply says draw eyes if it's the first one in our list like that if it's the head and then in this draw method you can see I have eyes equals to false but when you make it equal to true we're gonna do a special thing in here that's gonna draw eyes for us okay so now that we've coated this this whole mess of stuff in the snake class let's just run the program quickly and see if anything's happening so currently we just have the grid we I don't have any errors anything that's because we haven't drawn the snake object onto the screen yet so what we need to do is we just need to do simply do an S draw it within our redraw window here so I'm just gonna type I'm gonna global s here and I'll global it down there as well si s top draw like that and we need to give a surface so let's give it a surface and I'm gonna global s so that we can reference it there uh and is there a reason we aren't being drawn to the screen let's just have a look quickly s draw start draw oh it's because yes we are indeed trying asked but we haven't drawn any of the cube objects yet and it is calling the draw method on all of our cube objects which we haven't yet created hopefully that makes sense but we'll go ahead and do that now so we have to code a little bit more before we can see everything so I'll just go ahead and get into the cube object and let's just say self don't start you will start except I'm actually just gonna change us to position because I believe this is what it's supposed to be and I'm gonna say self done during X because one self thought during Y is equal to zero and self thought color is equal to color and the reason we have this is because we're gonna be drawing later what I want to call like a snack for the for the cube to collect for the snake to collect so we wanna be able to change the color with them and spelled it wrong like three times during X in during why the reason I have during X you're set to one initially is because I I want to make sure that we start moving in a direction if I had this at zero you would have to click a key before the snake search me but I want right when we run the program for the snake to just start moving so I'm setting it already with a direction X and the reason these are optional again is so that when we create a new cube object we don't have to implicitly say Direction X is one direction y 0 because it's assumed that it's always gonna be like that unless otherwise stated ok let's have a look what else do we need to do here we need to now code this move function which is actually really easy almost gonna do is because say since we're changing the direction acts in the direction Y in our snake class we need to do that in here so that it stays with the object let's say during x equals three x and then the same thing self thought during why because then Y like that and then we'll go down here and all we're going to do is going to change our position so soft up position basically self dot pause zero which is our already existing position plus self thought 3x and they were setting self-thought pot is one which is our already existing y value plus eco is self-thought turn what like that and just in case I haven't mentioned it here right we're working with a grid system of 20 rows in this instant so when I say something like if our self deposition is like 1 3 or 10 10 I'm saying we're in the 10th row and the 10th color I'm not saying the x value is equal to 10 and the y value is equal to 10 because it's not in the case of drawing on the screen because our screen is actually a width of 500 pixels but just where we are in the grid which is most important to us right now is equal to like that values like 1 4 or 3 5 so on like that just just to make that clear for anyone who might have been confused [Music]
Original Description
Coding snake using python and pygame tutorial #2.
In this set of videos I will be showing you how to code a basic version of snake using python and the module pygame. This is suited for intermediate level programmers with some experience but anyone is welcome to follow along
How To Install Pygame: https://www.youtube.com/watch?v=Okh-aKuHr7Q
Full Code Here: https://techwithtim.net/tutorials/game-development-with-python/snake-pygame/tutorial-2/
Twitter: https://twitter.com/TechWithTimm
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- snake game python
- pyhton snake game
- Pygame
- Python Tutorials
- Pygame tutorials
- Coding snake with pygame
- Python snake tutorial
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
I built a free ATS resume checker that runs entirely in your browser - here's how
Dev.to · Dipesh Gyawali
3 Free AI Tools I Use to Save Hours Every Week
Dev.to · Hari Haran
My AI Bill Dropped 95% When I Switched to Chinese Models
Dev.to AI
AI Dubbing for Short Drama: The 2026 Playbook That Actually Scales
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI