54 Video Views·Apr 21, 2025
In this Python tutorial, I use PyGame, a great Python module designed for making simple video games, to build Snake! It can be used to build other retro arcade games like Space Invaders, Pong or Doodle Jump, all also on the channel, so check out more of our channel content!
We use classes, methods, functions and tons of great object oriented programming concepts to build this game start to finish in under an hour! If you have any questions about what you saw here or about what you'd like to see next on the channel leave a comment, and if you found this fun or useful be sure to leave a like on the video an subscribe to the channel!
Thanks for watching and good luck with your projects!
Get The Code Here:
https://github.com/plemaster01/LeMasterTechYT
TIMESTAMPS:
00:00 Intro
00:55 Import Modules, Structure The Project
7:52 Draw the Game Grid
14:01 Creating The Snake Class
31:44 Creating the Food Class
34:59 Calling the Food and Snake Class Functions
39:45 Game Done! Demo and Outro!
what's going on you guys welcome back to
the channel in today's video i'm gonna
show you how to make the classic game
snake in python using pygame
[Music]
all right i'm super excited to bring you
guys this classic game tutorial snake is
one of the first games it's super fun
it's super addictive basically as you
eat more food your body gets longer and
it's a really great coding exercise and
introduce introduction to a lot of
python and object oriented concepts so
if you do want the code and you don't
want to follow along line for line there
is a github repository the
link is in the description of the video
if you do grab the code and you're just
here to peace out then be sure to leave
a like on the video subscribe to the
channel it helps me out a ton and i
happy to bring all this content to you
constantly
all right so super excited to bring you
this classic game tutorial i'm gonna do
it all inside of python using pygame so
if you've done any projects with pygame
you know you need to start by importing
pygame if you haven't installed it
already you may need to run pip to
install it and then two other modules
we're going to use in this are the
system module so import sys and then
also the random module so import random
and then whenever you
are doing a pi game game it's a good
idea after you import pygame to just do
pygame dot in it right at the beginning
to initialize it
and uh i think let's just take a look at
how we want to set up this overall game
logic so there's really only two things
going on
in snake
there's the snake's body moving
throughout the grid and then there's the
food that you're trying to track down so
i think what we should do is we should
just make two object classes one will be
the snake itself your your character and
then let's go ahead and just come down
here and let's make the second one for
food and you'll see why i'm doing it
this way i think once we get into
filling these out but basically
initially i'm just gonna put pass in
both of these and before we run anything
we're going to come back but i want you
to think of all the objects that need to
go into the game right off the bat so
basically we're going to have the snake
we're going to have the food we're going
to need a space to actually draw the
grid so we'll call that define
draw grid just like that
and then beyond that so i'll do another
pass right here
beyond that
we really just need a spot to store
game variables so i'll call that a game
variables library and this is where
we'll put things like the screen width
and the screen height so for this game
because it's just a nice little game um
it fits nicely in a 480
by 480 square
but we'll put all of our game variables
here as we define them and then really
the last thing we have to do is just
define our main game loop so this main
game loop is something that i spend a
lot of time setting up
in in kind of every game and we'll try
to go quickly now because we do it every
single uh game so hopefully you've seen
a few other tutorials but i'll try to
explain as we go through it so you want
to create a clock because that's going
to control the speed at which your game
runs so you just do pygame.time.clock
and then you also want to define a
screen
by doing pygame dot display
dot
set mode
and then you give it a few arguments so
you give it as a tuple you your screen
width and height
um
and then zero three two there we go and
you define your screen and then if
you're going to have a game where you
want the potential for like a menu a few
levels anything like that it's not bad
idea to also define a surface
and this is something that if you've
made some simple games before you maybe
have skipped this step and just drawn
everything onto the screen
but technically this is the best way to
set your game up to have multiple
screens throughout it multiple surfaces
that you can draw things onto so to
create one like we want for snake which
is just going to take up the entire size
of the screen that we just defined
just use pygame dot surface with a
capital s and then screen dot get size
and then below that just add this
surface equals and then surface dot
convert
okay
and now let's move on so below that
we're going to want to call the draw
grid function
all inside the main loop here
so to do that you just
call
the uh
you just call the
function that you made for that uh sorry
it's a little late in the evening over
here
and then since we're going to draw the
grid onto the surface and not the screen
i'm going to pass surface in here and
then up in our draw grid function you're
going to want to
tell it right away that it's receiving
a surface to be drawn onto
so we'll we'll fill in the draw grid
surface
we'll fill in the draw grid function
next but let's just finish defining the
basics of our game loop here
so we'll draw
the surface and then the next thing we
want to do is create an instance of both
of our classes so we're going to have
the snake and we're going to have food
and i you can tell the instance of the
class separate from the class itself by
upper and lower case so the classes
defined with a first letter caps is kind
of the standard pythonic way of writing
it and then your instance with lowercase
is how you would tell that that's a
specific instance okay
so we define those and then we'll just
say the score is equal to zero when the
game boots up
and now we put this while true
and this is now the while loop that
we're setting up where as long as the
game has not been exited then we can
keep playing and we can put all of our
things that need to happen every loop of
the game underneath this layer
so we'll say
clock.tick and here's where if you have
like an action game like a platformer
you might put an fps here like 60 for
your frame rate but since this game is
snake i'm actually going to use this
clock to control how fast the snake
moves throughout it so we're going to do
clock.tick at 10.
and then in here we'll be putting a lot
of
snake and food
sub functions so like dot draw dot
update receive player input so we'll put
those things like here
once we've fleshed them out underneath
the class
and then we'll again draw the grid on
the surface and it's important actually
to put these in both places because the
first place that we did
that we did this exact line of code is
just doing it initially before the game
boots up
there we go so if you didn't have it
here every time you boot up the game
until
uh kind of one or two seconds in one or
two frames you might not even be able to
detect it but technically the game would
start up without a grid until it got to
this point
doing it here in the beginning is a
really good way to make sure your screen
loads in right away
okay
so then let's just go ahead and handle
what we need to do um
uh like update the screen and then close
it so the last thing that you need to do
is just pygame dot display
dot update
and then down here obviously you need to
actually call that main so the main
function that we made for the game
you need to call it
and let's go ahead now and actually fill
in the drawing the grid function so
go up to draw grid
and uh we're going to use this a few for
loops to make it procedurally generated
it is going to be so much faster than if
you were going to try to go in and draw
every rectangle where it needed to be
so what we'll do is we'll say for i in
range zero
and then what i think we should do is we
should make some new functions that are
going to tell us how large we want the
grid to be not functions variables
because we want this to be from zero to
however many grid um
grid
rows there are and then for the x we
want to be for from zero to however main
grid columns there are so we need a few
new variables here and to do that we're
going to need what i will call grid size
since we're going to make it a rectangle
let's say that it's going to be a 20 by
20 grid
and then we'll do grid
width and grid height
and i intentionally picked some sizes
that are going to divide evenly here so
grid width is going to be
width divided by
grid size
and then height obviously is going to be
height also divided by grid size and
there are ways you could change this up
so you could have like grid rows and
grid columns if you didn't want to be a
square
but for our purposes square is going to
be fine for everything so we'll say now
draw the grid for y in range 0 up to
and then make sure it's an integer just
on the off chance
because when you do division
even if it's a whole number into a whole
number python defaults
by turning that value into a float so
you did math and now instead of it just
being 480 divided by 20 which is like
24 it's going to be 24.0
and we want it to be an integer so that
we can iterate through the loop so
that's why use the int in that place
and then do integer of grid height
and then underneath that do for x in
range and then that'll be from 0 to in
this time an integer of grid width
okay and then a couple parentheses close
it out and then what we want to do is
actually make it a checkerboard pattern
so you don't have to do this step you
could just draw every rectangle the same
color but checkerboard is going to look
a lot better as you'll see once we get
this drawn so
what we'll say is if x plus y and now
i'm going to use uh something you maybe
don't see all the time if you're not
super familiar with python and
object-oriented programming
this per it looks like the percentage
sign it's actually the modulo operator i
think is what it's called but it's
basically division checking for
remainder
so what we're saying is if x plus y
divided by 2
is an even division division so there's
only two things that could be here right
it could be an even or it could be an
odd so it could be
0 2 4 6 8 or it could be 1 3 5 and 7.
and the evens are when this remainder is
going to be equal to 0
and we'll draw our rectangle
which we'll call rect
we're we're gonna draw that to be a
slightly different color when it's
when it's an even number than when it's
an odd number and that's how we'll get
our grid okay
so
uh we're gonna define the rectangle and
uh i'm gonna do this again procedurally
generated
so that it works for every rectangle in
the grid not really procedurally
generated but generically enough that
it'll work for every rectangle in the
grid so we'll take our x and y integer
which again are telling us what position
in the grid they are and now we're
defining an x and y start coordinate as
well as a width and a height
so we've got we want the x and y start
coordinate and the way pi game draws
objects is 0 0 would be at the top left
corner and as you go to the right and
down the screen you go farther out so
480 480 is the bottom right 0 0 is the
top left
so we'll do
x times grid size and then y times grid
size and that'll mean that
however whatever integer you are in your
rows and columns that's where your top
left corner is going to be
and then we need to give it
a width and a height and what's easy
about that is they are both the same
since we made everything square it's
just going to be grid size by grid size
um and then so there's two steps to
getting a rectangle actually on the
screen there's defining the rectangle
but then there's also pygame.drawrect
and this is where
this is where you could technically
combine steps you could do
pygame.draw.rect and then you could also
define the rectangle in there but it's
it's a little bit messier than just
doing it this way
and so we need to give it a color and
i'm going to do
colors in the same way that i do the
rest of these game variables where
rather than typing in
rgb values for everything i'm going to
define a color down here so if i want to
modify colors later in the game
i can just come right to one spot and i
think that's going to be way easier
so for grays if you're not sure and to
do our checkerboard we'll do two shades
of gray
um
for grays if you just basically make
your r g and b values all the same
you're going to get various shades of
gray so
that's what we'll do here
and then we're going to take almost that
exact same code that we used to draw
that one
and we will say else we don't even have
to check math here and then we'll say
rect is the same thing so it's the same
rectangle but this time we're gonna make
it rect two
okay so we have our grid drawn we have
the
overall like
boxes that the snake and the food are
going to move around inside of so let's
start actually defining the snake
and the food but to start let's think
about the snake and let's think about
all the different things that the snake
is able to do so
with
basically every class uh any class of
any kind of complic like complicated
functionality you're gonna want this def
init um function and then you're gonna
put a few things inside of this so
you'll start uh you'll put like how long
the snake is and then since snake the
body gets longer as you eat more food
you're going to want a list of all the
positions that the snake occupies so you
use this self.positions
equals and then make a list
and then initially so what we're doing
right here is we're initializing the
class so initially we're also going to
put its position just starting in the
middle of the screen
um and then obviously as you move
through the game and you eat more food
that changes and updates but we're going
to handle that in other functions
and then the self.direction is going to
be
either up down left or right and so we
will go ahead and make the handling for
how you actually make all those
movements in a second but to start let's
say when the game boots up you're going
to move in one direction let's just use
the random module that we imported in
the beginning to choose which one you
want to do and we haven't defined these
yet so let's go ahead and
let's go down into our game variables
and actually define what up down left
and right are going to mean in the
context of this game
so i'm going to go ahead and
make a variable for all four of these
and it's kind of neat if you think about
if you think about a grid and what we
just talked about with how pi game draws
objects onto the grid
up down left and right can each be
expressed in terms of a tuple or a set
of two coordinates so for up your x
isn't changing and your y is going up on
the screen which is actually negative
because 0 0 is at the very top 480 is at
the very bottom
so
if you're going up you have zero
negative one if you're going down you
have zero positive one
if you're going left then you have a
change in the x direction but not in the
y direction so minus one comma zero
and then
if you're going right it's the opposite
you're going one and then no change in
the y and the reason we're doing it this
way is we're thinking about the map as a
grid system and so this is telling how
our coordinates on the grid need to
change based on what direction we're
going so this is a really cool intuitive
way of setting up the movement and
that's why when the the snake boots up
right in the beginning we'll have it
randomly go in one direction or another
it won't really matter because you can't
collide with one instance so you're not
going to be at risk of hitting yourself
right when the game starts
okay and then let's go ahead and um use
self.color so later in the in the um
class definition when we draw the the
snake onto the screen we'll have already
given it a color let's come down into
our library by the other colors and
let's give it a nice green that's not
like
um too complicated let's just do r
g b let's just do all the way green
that's a little vibrant you may want to
change this up pick your own colors
that's totally up to you i just like
green it's very typical
so then let's use a little function to
get the head position
get head
positions because we're going to need to
check where the head is to be able to
check oops i always type self in twice
you're going to need to check where the
head is to check if the head is
colliding with the food um so this way
all we'll do is we'll check where on the
grid the head is and then we'll check
the position of the food and if those
two are the same then we'll know that
it's time to grow the body of the snake
by one regenerate a new piece of food
and give you plus one for your score so
we're going to need this function but
all we need to do
is return where in that positions list
the first item is so self.positions zero
okay
now let's talk about um
how to actually
take a turn so to do this we'll define
turn
and we'll say self and then we're going
to need to pass in another variable
which we'll call point
okay
and so what we want to check is if
self.length is greater than one so this
is going to be as soon as you've eaten
at least one point of food
and then we'll check if the first
coordinate of point because you got to
think self um we're checking our x and y
coordinate to be able to turn so we're
checking it against a point which is
going to also be an x and y coordinates
so what we'll do is we'll check point at
0
times
negative 1 which means that you're
turning right because if you're going
left
then what you're doing is you're moving
in the x direction and you're not moving
in the y direction and if you want to
turn to the right then you're flipping
that you're going uh to multiply both
the negative one and the zero
by
negative one um if you think back to how
we define let me say that another way if
you think back to how we define the four
turns
anytime a turn is taken
you're able to multiply both of these by
negative one because if you're up going
down and you multiply zero by negative
one then it's not going to change and
then you multiply
negative one by negative 1 and it will
change
so
it's going to be this and then comma and
then hopefully you see where it's going
point 1
will be times
negative 1.
and what we do is we check to see if
that is equal to self.direction
and if it is then we can just return
because we don't have to do anything
we're already
we're already going in that direction
but else
we'll set self.direction equal to the
point that we passed in so that's how
we'll check for turning and some of this
stuff that i'm going through is going to
make a lot more sense once we actually
boot up the game and you can see it in
action so for now just bear with me if
you have any questions that aren't
answered as we go through the tutorial
feel free to ask them in the comments
below and i will get back to you as soon
as i can okay
so next let's actually handle moving the
snake so
we'll say define move
i always type self in twice when i'm
doing tutorials
and we'll say current is going to be
equal to
self.gethead position so that's going to
tell us where the head is
and then we'll create a variable
uh x and y just coordinates
and we'll set equal to self.direction so
that's what's going to be up down left
or right depending on the player's
interaction with the arrow keys
and so
x and y will become variables that we
can use in place of the 0 1 negative 1
of our movement directions
and now what we'll say is our new
location is going to be equal to and
we're going to need a few sets of
parentheses
um and so the new location is going to
be equal to the first character of
current
plus and then this is going to be equal
to
x times the grid size
and then we're going to use again um so
close that off close that off
and then we're going to use again the
remainder function the modulo divided by
the overall width of the game
and then that's going to be the first
coordinate and then the second
coordinate open new parentheses is going
to be equal to current at the 1. so this
is the second item inside of the
coordinates
and then it's going to be equal to that
plus
and then
open the parentheses y times the grid
size
and then
again we're gonna use the
remainder operator
okay and uh
this hopefully now that it's typed out
you can stand back and look at what each
piece of this is actually doing but what
it's basically saying is
is okay whatever direction you're going
it's either moving one square up one
square down which is why we can use
these
direction coordinates the x and the y
because if it's positive then what this
is doing is adding one square to the
right one square to the to the down
direction if it's negative it's going to
add one square to the left or one
squared to the up and due to the four
directions it'll never be doing both
right one x or y is always going to be
equal to zero and the other one is going
to always be equal to either one or
negative one
so the only four things you can do in
snake is move one square to the right
one square to the left one square up and
one square down and then all we have to
do is move the rest of the body into the
next spot that the piece in front of it
took
so that's what we'll do next i just hope
that explanation was
semi-clear i know it's
coding is always a little bit easier to
write than it is to explain i find but
okay so what we'll do next is we'll
check if the length of the positions um
table of the list is greater than two
and our new position is already in
the
self.positions
library after the second place so this
is checking from the second
body part in the positions library all
the way through to the end and if our
new position is already in there then
we're going to need to reset so we'll
make an another function for that next
but basically that means that we've just
collided wherever we just commanded our
body to go
it hit a piece that already existed
so that means we need to reset and that
we basically lost the game or our snake
body has to reset
but we'll say else self
oops
self dot positions dot
insert and then
in place 0 we'll put the new coordinates
and then
if
the length again of self.positions
is greater than
self dot
length
then what we're going to do
is oh i put one two main parenthesis in
there
then what we'll do is we'll pop the last
position off so self.positions
and pop is just a list function that
that always removes the last function
from it
and the reason we're doing it this way
is because when we do hit ourselves
and we reset we're gonna reset our
length back to one and this will just
allow us to remove all the positions
that are
on the body too long now um back so we
get just the head
so uh that'll probably make a little
more sense now that we do this next one
we'll just define the reset function
um and we're going to so this is anytime
the snake hits itself which resets the
game we're gonna set the length back
equal to one and now i'm going to grab
the self.
positions and self.direction that we put
up here and just copy it down because
this is what we're doing we're
reinitializing the game so that you're
back to a length of one your head is
back to one technically you could reset
the score to zero here again if you
wanted
i think it's okay if the score is just
rolling and goes up up up until you
have completed the game um
until you're done playing so technically
you get a high score just as long as you
want to grind but your snake body will
limit but you could totally put score
in this reset as well
okay but now let's go on to a really
important one which is going to be
drawing and for this one we're also
going to want to pass in what surface
we're drawing the snake onto
and what we'll do is we'll say for pause
which i'm going to just use as an
abbreviation for positions and we'll say
for pause in self dot
positions
we'll say that this time kind of like
before when we drew the grid rect equals
pie game dot capital r rect and make
sure you get the cases right on these
because pygame has some built-in stuff
where you actually have to use case
sensitive
capital r's
uh so we're defining a rectangle now
which is going to say where
um and what size we want the uh the
snake body segments to look like and so
we're doing this for every position
in the positions table
and we're going to say the x coordinate
and the y coordinate are where we want
it to draw on which is why we defined
the grid in that way
and then the size again we already have
a variable for the x and the y
coordinate of the
sorry not the x y coordinate the length
and the width for the snake's body
and so then what we'll do is we'll say
pygame.draw.rect
and we'll put it on the surface
and you could totally just do green
right here but we actually um did
something i think is a little bit better
and we defined self.color a little
earlier on
and now we can just do rect and
i think what you'll see you don't need
this step if you don't want to but i'm
going to draw the exact same rectangle
on the screen except this time
we're going to make it an outline so
we'll make it black which will
i don't think we've defined that yet so
we'll have to make another one but what
i'm going to do is i'm going to add this
other condition which is a width so this
is how you make a hollow rectangle you
define a width for it and so we're just
gonna make it width of one which will
make it a real skinny line
you could do width of two or three you
can play along around with that one if
you want
but we'll say black is equal to zero
zero zero so that we can continue using
colors instead of directly defining the
rgb variable values
okay
so surface black rectangle and that is
everything we need for drawing all right
and now i really did save the most
important two
functions for the end of this class here
but the next one is handling keys so
this is basically our event handling and
what we're going to do is for event
in
pygame.event.get and this is uh pygame's
built-in handler for tracking everything
the player does every keystroke every
movement of the mouse everything the
system is doing is in pygame.event.get
okay and so the first thing we need to
do is what do we do if you want to quit
the game so if
the event.type equals pi game dot and
then all caps
quit
uh and then all you have to do
underneath that is pie game dot and then
this time not caps quit with parentheses
and then use that sys.exit as well to
actually close the program
okay and then that's the boring one that
you have to put in every game because
when you boot up by uh python if you
don't put that in um the little
red x in the corner isn't gonna do
anything and you'll have an infinite
while loop that never ends and then your
game will crash immediately so
the more exciting code for this game is
if event dot type
is equal to and then you want to check
for this game everything we're doing is
with pygame dot key down but there's
also key up mouse button down mouse
scroll wheel things like that so
just keep that in mind but all we have
to do here
is check if the event dot key is equal
to
and we'll start with up so k underscore
up
and then if that's it then we'll take a
turn
where we go
up
okay so the nice thing is the way we
defined the class is we don't have to do
any complicated code in this event
handling we can just call the correct
sub function with the correct point
all right so then we'll say else if it
is down
then we will take a turn
down and
you don't have to make these lifts
technically if they were just ifs it
would work
but that that can get weird if you press
multiple keys uh the nice thing about
elifs is if you're pressing multiple
keys it'll just look at whichever one
completes the the if statement first and
you don't have like overlapping code
but really it's it's a cleaner more
efficient way to write the code because
never in snake are you able to turn two
directions at once you have to do them
as separate keystrokes no matter how
close in time they are
okay so that's really the entire event
handling for the snake
and we'll see where we call all of these
sub functions
down below once we define the food class
and fortunately the real meat of this
project was in the snake class the food
class is a lot like what we just did but
way simpler so
just like before
because it is a class you want this in
itself
right at the top
and then
for self dot position let's just put the
first one
at 0 0
and then self dot color we'll set this
equal to red food uh red is kind of
classic for
um
credits
uh
red is kind of a classic python uh snake
color um i almost said python because
that's what we're doing so we'll say r
of two five five and then g and b of
zero feel free to play around with the
colors uh you don't have to follow this
and actually right in the beginning when
this thing's initialized we'll want to
randomize the position
so that position zero zero we obviously
don't want that to be the actual
starting position but we need to
initialize the variable self.position
which is why we did it there
so first things first let's make a
function called randomize position
and what we'll do is we'll set
self.position
equal to and we're going to use the
random module we imported
random and we'll do that random integer
and we'll have it go anywhere from zero
which is all the way at the left of the
screen
through to grid width
minus one because we don't want to be
off the screen
um and then we'll multiply it times grid
size so that will make it uh actually
its x starting coordinate and then we're
going to do the exact same thing so i'm
just going to copy it
random.rand
technically you don't even have to
change it to grid height because it's a
square but it's proper especially if
you're going to play around the sizing
later
make this grid height minus one
and then right at the beginning when you
call self dot
color and self.position also just call
this function once so that it runs right
in the beginning and places the first
piece of food somewhere random
okay so then uh let's create the draw
and this is going to be a lot like the
player draw because it's um
or the snake draw because it is also
just going to be a rectangle but this
time we only have to draw one because
the food is ever only occupying one grid
location
um so i'm just going to copy those three
lines from the the snake draw function
but we're going to modify it to be
accurate for
the snake or the food
so what we want is self dot position at
0 and self.position at one rather than
that intermediate pause variable the
index or variable and then we still want
to be grid size and grid size and we'll
still use self.color
and we'll still use an outline so you
can see right away how the way we set up
the
class for
the snake
works really well for the class for the
food okay
so this is actually everything we have
to do to define both classes now let's
just go down in the main loop and make
sure that we call the correct things
inside of the main game
okay so we already have the clock
ticking
let's go ahead and drop pygame dot in it
in this loop actually
you could put it right up here at the
top that's totally fine um but
let's put it inside of our main loop so
that once it's called we initialize
everything
and let's see once we run it if that
throws any flags i like it there a
little bit more
okay so after the surface
we draw the grid and then let's go ahead
and
create our classes just like before
score equals zero wall true clock equals
tick all right and so the first thing we
want to do
is snake dot handle keys so every time
through this we want to check if
the players interacted with the game and
do that first then we'll draw and then
we'll actually move the snake's body
based on whatever current direction it's
going
and here's where we should check if it
has eaten the food so that's if snake
dot get head position
is equal to
food position
and if that's true then we want the
snake dot
length
to be
one longer than it just was we want our
score to be one greater than it just was
and we want to run the randomized
position function for our food again so
it's going to give us a new randomly
located food position
and then we want to draw the snake again
because now it could be longer
if it actually ate the food
and then we want to draw the food again
wherever it got located to
and then uh
let's see we want to
screen dot blit so if you're going to do
what i talked about right at the
beginning and you're going to create a
surface separate from the screen you
need to make sure that you actually draw
your surface onto the screen otherwise
this game is never going to get
displayed anywhere so to do that you do
screen dot blit and then what you want
to blit or block transfer you're just
saying what data you want to push onto
the screen and in this case we want to
be everything that's on our surface and
the coordinate we want to be on is zero
zero the only reason you would not put
it at zero zero is if this was like a
pop-up or a partial screen or a sub
screen inside a larger screen
so we'll put it zero zero and then let's
put text on the screen that's going to
tell us what the current score is so to
do that we'll do text equals font dot
render
and then we're gonna do it this way
we'll say score
of and we'll say just zero initially but
by putting it in that curly brackets we
can come here afterwards and do dot
format and then tell it what variable to
put in that formatted curly brackets and
we'll say score
true and then black
and then what we have to do you see
we're getting the red underline in um
under font is we actually have to come
up and define a font so underneath our
up down left right
let's say font equals pie game dot
font with a lower case and then font
with the caps
and we'll say so i like to use free sans
bold in my games just because it's built
in
to any computer running windows
and make it a size 30 so that's just a
good font size
you can make it smaller if you find
that's kind of disruptive to the game
and actually since we're doing that
outside of the game loop we are going to
want to put pygame dot in it here as
well
because uh if you try to run it you
called a font here you need to have
initialized pi game by then
so
now you can see we're not getting the
red underline anymore which is good and
then just like we did with the surface
we also have to blit the text onto the
screen
and i'll just put it 510 so it's a
little bit to the right and a little bit
down
and this should be everything we have to
do as you can see like there's really
not that much to put in the main game
loop if you set up your classes so that
they take care of most of the
complicated functionality
and if you go back through this so we'll
run it in just a second but if you go
back through this and you look at each
subsection of code
you'll see at no point is there a really
complicated
operation it's a lot of really simple
operations that when combined give you
cool functionality
that is the essence that is the benefit
and the bread and butter of object
oriented programming so
knock on wood let's see how close we got
and what kind of troubleshooting we have
to do
okay so right away it moved to the left
without me pressing anything
and if you boot this up a few times
you should see it going in different
directions to start i hit the food my
body grows by one the score gets bigger
i can move in all four directions
if you find it's too slow or too fast
for your liking try changing the speed
at which that pie game dot time
ticks at
so for example if you were to go from 10
up to 20 you would find the game
extremely difficult it would move so
fast
but like 12 and 15 are just uh
slightly more challenging because what
that clock dot tick is doing is saying
the number of times to run through the
loop per second so basically 10 is
saying it'll only move 10 squares per
second if you
bring that up to 20 it'll move 20
squares per second
and since our grid is 24 by 24 you
that's going to be clear across the
entire board
okay
and so here you can see it's really cool
it can move off the screen to one side
uh and i hit myself there because i got
longer than the board we kept this score
going so that was one thing i talked
about different games of snake are kind
of they operate in different ways
some of them reset the score every time
you die some of them it's just as long
as you want to play in one sitting
that's your score but your snake body
resets every time
and honestly
no one's competitively playing snake
they're playing for fun so make it your
own this is really a great exercise to
learn the basics of python and pygame i
think it's a really great beginner
tutorial
if you did find this enjoyable be sure
to check out the channel we have a ton
of game tutorials ranging from other
basic ones like pong all the way up to
pretty complicated ones like entire apps
like idle clicker apps
we did a space invaders tutorial and a
lot of other things that range from
super basic to intermediate
don't really do too many advanced things
because the advanced people typically
know what they're doing
but i hope you enjoyed this video if you
do have any questions or ideas for what
you'd like to see in the future be sure
to let me know about in the comments
below as always i really appreciate the
support and if you'd be willing to leave
a like on the video subscribe to the
channel it helps me out a ton and as
always good luck with your code and
thanks for watching thanks bye