[Music]
so let's just go ahead and get a look at
what this final product is going to look
like um so on like an x by x grid so
whatever you want it to be you have a
little snake moving around there's a
snack is what i want to call it that
shows up and as you collect it it
increases the length of the tail and
then obviously if you run into yourself
and i just did that by clicking the back
key while i was going forward it gives
you a little error message here says you
ran into your tail um your length was
nine dot dot dot whatever um and then
you can continue and
play as long as you want um anyways what
i'm going to be doing in here is i'm
going to be coding everything object
orientated i know that there's a much
faster and easier way to do this but it
doesn't really teach you that much
this way you're going to learn a bit
about objects about classes
how we can kind of work with them
and you'll see like how the flow of my
program goes and you should learn quite
a bit especially if you're somewhat a
beginner programmer
what we're going to be using to make
this is module called pygame if you
don't know about pi game and you want to
learn about it before you do this i
would recommend that all right so let's
start coding so pretty much what i've
done is i've just set up the flow of the
program all the classes and the function
just to remind myself what i need to
write
and while i'm talking here it's probably
a good idea if you guys copy this down
so what we're gonna have is we're gonna
have two main objects we're gonna have a
cube object and a snake object and our
snake object is going to contain cube
objects if that makes sense so each one
of those little red
i don't know squares that was moving
around is a cube object and the whole
thing is our snake object we're going to
have a few functions here draw grid
redraw window random snack message box
and main
so let's get started with
our main function here and this is
what's going to be our main loop so
essentially what i need for the main
loop to start is we need to make a
surface so in high game to do this we do
win equals pi game
dot
display dot set underscore mode and then
i'm going to set it um
let's see here what do i want the height
to be let's say width
and height like this
oops bad spelling and i'm just going to
make a variable here called width equals
height equals and we'll just set this to
500 by what i have 500 like so
now we need another variable i'm going
to set this as rows uh i can delete this
down here it's going to do something but
i'll do that later um and this is how
many rows you're going to have or rows
or columns whatever you can set this to
whatever you want just make sure it
divides 500 evenly otherwise you're
going to have like weird looking rows if
you know what i mean so i'm just going
to set mine to 20 that's what i'm using
in the other one but if you want to make
it harder set it to something like 10
and there won't be as much room for the
snake to move around and the games will
go faster pretty well
okay next what we need to do is we need
to set up a snake object so i know that
uh we haven't even created like anything
in the snake class yet but i'm just
gonna do s equals snake
and i'm just gonna give it a position so
in this case
we want to or a color sorry
which is going to be red because red
green blue 255 for red and then i'm
going to give it a position we're going
to start in the middle so i'm going to
start at 10 10. um now let's move into
our main loop so i'm just going to say
wow flag create my variable up here flag
equals true
and then we're just going to start by
doing a pi game tick so pi game dot time
dot delay
50 like so and that's just going to
delay us uh i want to say like 0.5 or 50
milliseconds
every time so that our program doesn't
run too fast i'm also just going to
create this clock.tick i'm going to put
that at 10 and what i'm going to do is
i'm going to create a clock object up
here um and this is a built-in thing in
pie game and what this is going to do is
explain in just one second once i finish
typing it pie game dot
clock i believe it is oops dot time dot
clock
like so
so what this clock dot tick is actually
going to do is it's going to make sure
that our game doesn't run at more than
10 frames per second um so that would
mean essentially that our snake would be
able to move like 10 blocks in one
second and again we don't want it to be
that fast so that's why we're also
delaying um by like a few milliseconds
here if you put this too low um then
again it's going to move too low you can
play around with the speed this is kind
of what i found was the best speed um
and it may vary depending on what
machine you're on it shouldn't it might
so you might want to just change these
numbers um again the lower this goes is
the faster it's going to be and the
lower this one goes is the slower it's
going to be so they're kind of inversely
proportional like that
all right
um next
what are we going to do here i'm just
going to call
redraw
window
and i butcher that so bad oops
redraw window like so i'm just going to
give it a surface which is in this case
going to be win
that we've created up here
and for now that's all i'm going to put
in my main loop here and we'll move more
into that once i start coding some other
stuff okay so now that we have our
redraw window being called here
i'm going to go and i'm going to start
coding our redraw window
so what this is essentially going to do
is we first need to update the display
so pygame.display
dot
update like so
and we also need to um draw the grid
so we're simply going to draw a grid
we're going to pass it that same surface
that we were given um and move on from
that we're also going to fill the screen
so
when dot
fill
in this case i'm just going to use black
so it's zero zero zero um and there we
are that's all we need to do right now
but essentially we're also gonna need to
draw the snake uh we're gonna need to
draw a few other things on the screen uh
that we'll get to later okay so draw
grid let's move into this one now uh
what we need to do here is a set like
draw a grid which is pretty easy to do
um so what we do is we we're given rows
and surface so it's probably a good idea
if i actually passed in the rows and
surface so i'm just going to global them
here so that
i can reference them i think this is
going to work we'll do global
rows
and width like that
and here we also need to make these
global just because i don't want to pass
them in every time i draw a grid we're
just going to say width uh oops
androids and this height variable is
actually not necessary because
we are just going to draw a square um
like a square surface every time so we
can just make this the same we don't
need two variables for width and height
to it are going to contain the same
number anyway so global width and rows
i go here draw a grid
given with rows and surface so now we're
going to put in
well first we're going to put our rows
or oops
width
row
and surface like that
okay so that should be working now
in here what we first need to do
when we're drawing a grid
is we want to figure out um
how big each square in the grid is going
to be because what we're going to do is
we're going to just draw lines going
down and across but we need to figure
out where to draw those lines so we have
to figure out kind of like the gap
between each of the lines so the way we
do that i'm just going to create a
variable here size between is equal to
our width integer divided by our rows
this is just so that we don't get like
large decimal numbers because that we
cannot pass into our uh drawline method
in uh in python okay so now i'm just
going to create x variable
and a y variable
and i'm going to set these to zero and
say 4l
in
range and this is just standing for line
pretty well and rows like that now what
i'm going to do is i'm going to
increment my x by the size
between and i'm going to increment my y
by the same so
y equals y
plus oops
size between i can get rid of one of the
spaces here
oh what okay there we are
and now we're simply just going to draw
two lines so to draw a line in pie game
all we have to do is
pygame
dot draw dot line
and the arguments that takes is a
surface
we need a color in this case it's gonna
be white
so we do 255 255 255 for white
we're going to draw at x 0 comma
x w and i'll go through this in just one
second
what this does and i'm just going to
copy this and draw one more and then
explain
how this is going to work pretty well
okay so now we need
0
y
and
w
y like so okay
so what this is going to do is this is
going to draw two lines for us every
loop of this for loop
and these arguments here is the start
position of the line and the end
position of the line so the first line
that we want to draw is going horizontal
which means we don't want to change the
y value at all um so what we need to do
is we need to find the x
and then we're going to put y at 0
because we're going to be at the top
i think that's right at least oh sorry
this this line is dropped being drawn
down not being drawn to the right i was
getting confused there okay so we're
going to change the x but we're going to
keep the y at zero and then we're going
to stay at that same x and we're going
to keep the y at the width of the screen
so that we're going uh far enough down
and then same thing here this one is
going horizontal excuse me i messed that
up before so our x is always going to
stay at zero and our y is going to be
what's changing as we draw horizontal
lines across the screen i hope that
makes sense to everyone how that works
anyways i'm going to move on from that
and that's all we need for the draw grid
uh function so let's just go ahead i
probably made a mistake here but we'll
run the program to see if everything's
working
again our name win is not defined
win.fill ah so i've called this win one
really needs to be
surface like that
so let's try now and there we go we get
a nice little grid um on our screen like
so 20 by 20 grid
there we are so now let's move into
another uh function actually let's start
coding on the classes here so like i was
talking about before we're going to have
a snake object which is going to contain
a bunch of cube objects so essentially
what we're going to do is we're going to
have a list of cubes and that's going to
be known as the snake body so let's go
ahead and write that in now
what i want to do for that is just
create a list so we're just going to say
body
is equal to and then a blank list i'm
also just while we're already up here
and creating a class variable i'm going
to create turns
is equal to and it's just going to be
this set of squiggly brackets here that
we'll uh we'll get into in a bit
okay so now that we're already in the
snake function
what we need to do is we just need to
define our
parameters here that we're passed in so
we're just going to say self.color
equals color
say self.head
equals
cube which i'll get into in a second
given the position say self thought body
dot append
our head
self.head like this so what we're doing
here now is we're saying okay so the
head of our snake which is going to be
important because we need to know where
that is at all times so we can move
accordingly is equal to a cube at the
given position and the given position is
what we pass in here as like the
starting position of our snake
or we could create a new snake like we'd
have multiple snakes moving around and
anyways that's the position
now we're saying we're going to append
to the body
this head so now this is
in our list here and then we can go
through that list and we can draw things
we can move it we can check things we
want all of our cubes to be ordered
within this list so that's why i'm
putting that in first now i'm just going
to give
this one which is called during x
is equal to zero and
turn y which is equal to one
so what this is going to do is we have a
direction for x in a direction for y for
moving our snake so obviously these are
going to be
a value like negative 1
1
or 0 and that's going to be the same for
y and x if y or x is equal to 1 negative
1 then the other one's going to be equal
to 0 because you can only be moving in
one direction at the same time and this
is just going to keep track of
what direction we're moving in and we'll
use this in the next function that we're
going to code which is our move function
so moving is pretty straightforward
at least in terms of just if you have
one object moving around the screen
getting it to go left straight right but
when you have the snake object it has to
turn at certain points so when i click
left um
the rest of the snake is still moving
forward once it reaches the point where
the head turned left then it has to turn
left so that's where things can get a
bit complicated
anyways to start what i'm going to do is
i'm going to say for event
in pi game oops am i doing with my
typing here
pygame dot events dot gets like so
and then 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 we're going to do pi
game dot 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 you
said if uh event 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 pygame dot pygame.key.get
underscore press so what this is going
to do is it's going to 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 the way i like to do it
so now i'm going to say four key
in keys
because remember this is going to give
us all of the keys and then like one or
zero value if they were clicked or not
um so we need to loop through all of
them in check we're going to say if keys
and then
pygame dot key
or oops dot k underscore left
i thought it was key there and then
we're going to say if keys
high game dot okay 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 uh these
keys so last one here
all right and there we go okay so now
what we're going to do
is we're going to 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 0
0
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 just going to 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 um
so we'll do that inside so for x we're
going to say itself.dern x
equals negative one again because moving
left and we're going to set our turn y
equal to zero
and this is because again we don't wanna
be moving in two directions at once and
be going uh diagonally
now this is what i was talking about
is we need to remember where we actually
turned because if you just have one cube
moving around that's fine but when we
have multiple cubes we need to be able
to turn left
right we need to remember where we
turned so the tail of our cube can turn
at that point so that's why we have that
self.turns list up here or just this
turns um i want to say dictionary or set
whatever
so we're going to add to that self.turns
and
we're going to give it a new key
and i'll go through how this works in a
second
self.pause here
is equal to and then we're going to give
just a list of self. during x
and self dot during y
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
going to call it officially you could
think of it as a set as well
and pretty much we're going to add a key
which is the current position of the
head of our snake
and then it's going to be set equal to
what direction we turned so we're saying
we have a new turn at this position and
our character or our snake moved left so
it's going to say we have self dot
during y which is right here as negative
one self dot er surf dot during x array
and then during y is equal to zero so we
know what
way we actually turned and i hope that
makes sense that's uh yeah that's how
we're going to do that now i'm just
going to copy and paste these and then
change these values so when we're going
right this is going to be one
oops
same thing here so we'll put that in
this is going to be zero
and during y we're going up is going to
be negative one
now when we're going down
we need to change this to one
and this to zero and i'm just going to
get rid of that
and there we go okay so
that is uh pretty much how we go about
doing this um and i can also just put
lifts here as well because we don't want
to be able to move in more than one
direction so we're going to 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 okay 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 going to
turn it
so i'm just going to start coding and
i'm kind of going to go through exactly
what i do sort for i see and
enumerate
oops
and i spoke that wrong didn't they
like that uh and then we're going to say
self.body because we're going to look
through the list of uh positions that we
have in
on the snake we're going to say p equals
c dot position
and i'm just going to copy this um 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 um
when we do things each
just uh just follow along with it it's
not super important okay so we're gonna
say if p is in self dot turns
like this because remember what we did
is we added the position
of our head two turns so now we're gonna
say well if this position
is in the turns
then we're going to move
or we're going to turn right so let's
say here so we're going to say
turn
oops
equals
self.turns p
like that
so now we've seen that it's in there uh
so now it's in there we can find the
index of it uh and we can grab the
direction value so we know what way
we're gonna be moving and now we're just
gonna say c dot 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.body
minus one
oops then i'm gonna do self i don't know
why that's capital self.turns
dot pop p okay so i know i just did a
lot of coding here and this probably
doesn't make a lot of sense to you guys
so i'm going to kind of run through
exactly what i just did um it's it's
pretty it's a little bit difficult to
explain so anyways what we start off by
doing is we're going to say we're going
to get the index and we're going to get
the cube
object in our self.buddy because
remember our self.body is made up of
cube objects right here um that have
these properties right they're going to
have a directory in y direction x um a
start color so on and we're going to get
we're going to get to coding cube in
just a second so they're 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 going to see if that position is
in our turn list and we create the turn
list and add to the turn list um
when we turn right when we click left
center right so on um
and then we're going to say okay so the
actual turn so where we're actually
going to be moving is equal to
um that are the turns list at that index
right so we grab
um the turn direction x and direction y
which we stored there and now we're just
going to say our cube dot 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 way it
needs to move
pretty well
and then i say if i is equal to the
length of self.body minus one so that
means if we are on the last cube
we're going to remove that turn so once
that last cube hits that turn we're
going to remove it because if we were
just to leave that turn in the list that
would mean any time 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 um
i hope that makes sense
okay now also after this if here
we're going to say if the um
what do you 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 well we write an else
and i'm just going to copy this in
because it's going to take a second to
type because i have another file open
and i'll go through what happened how
this works okay
now this 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 we're saying if we're moving left
and the position so the x position of
our cube is less than or equal to zero
um then we're going to 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 c dot rows minus one because
again in you start counting at zero in
computers um so if our rows is like 20
then the last cube would be 19 in a list
right
so c dot rows minus one and we're gonna
be at the same y value so we'll leave
that there
okay and then we say otherwise uh we're
gonna check if we're going right from
moving right
and same thing if we're at the edge of
the screen move back to the left side by
putting zero here and then next one if
we're going down uh what we're going to
be doing is again checking
are we less than rows minus one or
greater than rows minus one 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 uh 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 cube's 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 of the stake so if you guys
understand that um then that is the hard
part kind of over with and we're going
to get into this cube object so you're
going to understand what all this means
and how our rows and columns kind of
work and stuff like that
okay so i'm just going to forget about
reset and add cue for now because we'll
worry about those later um but let's get
into the uh draw method because we want
to see if it's actually working and put
this on the screen so simply for i
see
[Music]
numerate
why can i not spell enumerate today like
that uh same thing self.body like that
we're going to say if i equals equal 0
c dot draw and you'll see why it is in a
second
true
else
c dot draw
surface
like that okay so the reason i'm
doing this little check here rather than
just drawing every object
is because when we uh draw the first
snake object i'm 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 onto it just so we know where
the head of the snake is uh we don't get
confused like what direction it's moving
in although you shouldn't we i want to
add the little eyes maybe just for
aesthetic as well so what this true does
optional parameter simply says draw eyes
if it's the uh first one in our list
like that if it's the head uh 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 going to do a
special thing in here that's going to
draw eyes for us
okay so now that we've coded 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 don't
have any errors or anything
but 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 simply do s dot draw
it within our redraw window here so i'm
just going to type i'm going to global s
here and i'll global it down there as
well i'm going to say s dot draw
like that
and we need to give it surface so
let's give it a surface and i'm just
going to 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 dot draw
let's.draw oh it's because yes we are
indeed drawing s 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 let's just go ahead and
get into the cube object here and let's
just say self.start
equals start
except i'm actually just going to change
this to position because i believe this
is what it's supposed to be
and then we're going to say
self.turn x
equals one
self.
during y
is equal to zero and self.color
is equal to color and the reason we have
this is because we're going to be
drawing later what i want to call like a
snack for the uh for the cube to collect
for the snake to collect um so we want
to be able to change the color with that
and i spelled it wrong like three times
during x and during y the reason i have
duran x here 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 starts
moving 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 is
zero because it's assumed that it's
always going to be like that unless
otherwise stated
okay
let's have a look what else do we need
to do here uh we need to now code this
move function which is actually really
easy um
all i'm just gonna do is we're gonna say
since we're changing the direction x 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
during x
and then the same thing self thought
during y
equals during y
like that
and then we'll go down here and all
we're going to do is we're going to
change our position so self.position
is equal to self.pause 0 which is our
already existing position plus
self thought during x
and they're saying self.
pause one
which is our already existing y value
plus equals self dot
during y 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 uh if our
self-deposition is like 1 3 or 10 10 i'm
saying we're in the 10th row in the 10th
column 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 um but
just where we are in the grid which is
most important to us right now um is
equal to like that values like one four
or three five um so on like that just
just to make that clear for anyone who
might have been confused okay so now for
drawing
um it's a little bit harder just because
of what i mentioned as well
is the fact that we need to figure out
the distance between each x value and
each y value so when we're drawing
something in pi game it draws in the top
left hand corner of the object so if we
draw a cube
we're drawing that in the top left um so
we need to figure out what that x and y
value is going to be for each cube when
we're drawing it to the screen
so
i'm just going to call this gap here
that is going to be our distance and the
same thing self.w
by self.rows
and we're going to change w and rows
down in our program later but we can
actually just type it in now
like this so 500 and 200 just so we have
that set okay so self.w answer divided
by self.rows again that's the same thing
we did when we were drawing the grid to
figure out the distance between our x
and y values okay we're going to say i
is equal to
self.pause zero that's going to save us
a bit of typing in our function here or
in our method i'm just going to say
j.self.pause1
so we're just saying i which stands for
row j stands for column
um like a classic like convention for it
so that we don't have to keep typing
pause zero pause one when we move okay
so the next one next thing we need to do
is now draw a rectangle we're gonna say
pygame.draw.correct
like this
and then we need a surface
we need a color so self.color and then
we need a rect and here is a little bit
of math i'm just going to type it and
i'll go through exactly what what it
does
j multiplied by
this one
this minus two
this s2
okay so the reason i have these little
plus ones and these minus twos here is
just so you can still see the grid when
we draw the rectangle um
because if we were to draw with exactly
the dimensions of this distance um
then what would end up happening is we'd
cover the white lines of the grid and it
just looks kind of weird if you can't
see the white lines um so that's why i
have plus one
added here and minus two just so we're
drawn inside of the uh the circle or
inside of the square a little bit and
you'll see that in a second so what i'm
simply doing is i'm multiplying my i
value
which is going to be my current like row
column by the distance that we need to
get to the next one and then same thing
for j
and then these little add ones minus
twos is just to make sure that we stay
inside so a quick quick example if you
don't understand is say we're in a
position zero zero so our i is zero and
our j is zero
we're going to say i multiplied by
distance let's say distance is like 40.
okay so zero multiplied by forty zero
zero multiplied by forty 0. so our
position is 0 0 and that's correct
because if we're drawing in the top left
hand corner here and we're in position 0
0 then that's where we should draw again
if you go something like 0 10
and you or and you move down i guess so
10 you're moving wide down you're going
to say 0 minus distance 0 and you're
going to say 10 multiplied by distance
let's say it's 40 then we move to that
correct position down the screen
so this is rectangle x y with height
okay makes sense now the next thing we
need to do is we need to draw the eyes
now you can skip this part if you want
i'm just going to copy it in because
it's a little bit of math to make it
them like perfectly aligned on the
square um
but yeah so we say if eyes and we're
saying our center is equal to distance
modulus 2 because that's going to be the
center obviously or much so i'm saying
divided by 2 is going to be the middle
of our cube
and we're saying the radius that's how
big the i radius is going to be
and then we go through a little bit of
math here so circle middle is going to
be high multiplied by distance plus
center minus radius
and then we're finding out the x and
this is just like a random x that i'm
setting by just putting it 8 pixels up
like every time because i don't want to
calculate the x
i don't feel like doing that and then
i'm drawing two circles based on circle
middle one circle middle two and then
the radius and a color of black
or a color of white actually sir or no
that's black i'm confused all right
anyways
with all that being said and done and
you guys probably pretty confused right
now let's just run and see if anything's
actually happening and you can see we
have a cube in the middle of the screen
yay so that's all we've done with all
this coding um so far it's drawn a
cube so actually not quite because we
still
all we have to do now simply to start
having that cube move and seeing our key
presses work is just do a little bit in
the main loop so let's move down to the
main loop now
and have a look here what we need to do
okay so what i'm going to do is i'm
simply just going to call
s dot move which is our snake object
every time this main loop runs
and what this will allow us to do um is
i want to call this before i read draw
window is it's going to go up to that
method in snake it's going to check
every time that we run the loop if a key
has been pressed if it has we're going
to move accordingly and remember that in
this move loop here it moves all of the
objects for us so it moves all those
cube objects
and then we're just drawing the grid
we're drawing everything in this redraw
function so it'll just display that and
update that to the screen so let's see
here
and we get an error tuple object is not
callable self dot pause zero plus
although they're next
um all right one second guys oh it's
because i've used
okay so apparently i am an idiot and i
forgot to add an equal sign here so the
issue was simply that this looked
something like this then i changed it to
square brackets when all i needed to do
was put an equal sign like this and now
we should be up and we're working so to
reiterate the line here
that the issue is out you guys probably
saw this when i coded it to be honest um
is
in move in cube it's near the top of the
program all you have to do is change
this add an equal sign between this like
the bracket and the pause so now that we
run it we can see that we have a moving
object and that we can move up down left
right etc so on and so forth like that
okay so play around with that for a
second and now we'll get into adding
cubes
and we're actually almost done we just
need to add the add cube function
message box a little bit in the main
loop and then we'll be finished
okay so what we're going to do now is
that's all great we're moving around but
we need to add stuff to our cube right
like we need to progress the game so to
do this we are going to generate what i
want to call a random snack that we're
going to place on the screen
that our snake can't eat so
the way that we do this i'm just going
to say
positions
equals item dot body and this item oops
shouldn't be items should be item
it's just going to be a snake object i
don't know why i'm calling it item but
that's what i'm calling it
um and we're simply just going to set
like a new list equal to
that list
so on okay so now we're going to say
while true
capital t
we're gonna do x equals random dot rand
range if you haven't imported random yet
make sure you do that and y equals
random dot range
range rows
and rows again should be
global so let's make sure that
this works with global rows
like that and then after this we're
going to say
if don't know how i just typed that but
the length of
and i'm doing something fancy here and
i'll explain what this does in a second
okay
so i know i just typed uh this stuff
that probably makes absolutely no sense
you guys if you haven't been using
python for a little bit but pretty much
what this is going to do
is we're going to get a list
of a filtered uh list
and we're going to see if any of the
positions are the same as
like the current position of the snake
pretty well so what we're doing is we're
saying that we want to make sure that
we're not going to put a snack on top of
the snake because if we have a really
long snake the chance that happens
actually somewhat high and i didn't do
this when i first coded the game so what
happened is when you have your snake
moving around the screen all of a sudden
the snack would pop up but it's like on
the tail of the snake
um which you don't want so what this is
doing and just type it out you don't
really have to understand it
is we're saying this this means a
function
uh z
and we're just checking if the z dot
position uh is equal to uh x y
so if it's equal to x y which is the
position we just generated here by
getting two random numbers so x and y um
then we're going to have to do this
again so we say
continue
and else
we break
like that and then i'm just simply going
to return
a tuple or a tubal whatever you want to
call it x y
okay so this is confusing but um you can
do this in like a much longer way with a
for loop where you pretty much loop
through every position in this list you
check it against x and y
you see if it's the same if it's the
same then do this loop again otherwise
you break and you return x y that's all
we're doing in there uh to generate a
random snap
any message box you're gonna leave that
and in here now what we're gonna okay so
now that i've created this uh this
random snack function what i need to do
is i need to use it so i'm just going to
create a new object i'm going to call
this snack and it's going to be equal to
another cube right because we want to
have the same functionality we want to
be able to draw it
possibly move it around right so we're
just going to create a new cube object
we're going to give it a position of a
random snack which is going to be
something like this
random snack and then random snack i
believe oh it takes a row in an item so
what we're going to do is we're going to
give it
or rows and we'll just give it our item
which is gonna be s
um and then after that what else do we
need for cube
i don't think we actually need anything
else i think that's all we need oh i'm
gonna change the color that's what i
wanna do i'm gonna change the color
equal to green so red green blue 255 for
green
so that it shows up and is visible on
screen and now what i'm going to do down
here in this while loop is i'm going to
say
snack i'm going to check if our the head
of our snake has hit the the snack and
if it has we're gonna add
another part to the body of the snake
otherwise we're not gonna do that right
and then we're gonna generate a new
snack and so on so we're just gonna say
if
s dot body
zero
dot pause and this is going to be the
head right because we have it ordered i
don't need those brackets
is equal to
snap dot pause and remember these are
both cube objects so this works fine
because they're going to be tuples um
then what we're going to do
is we're simply going to say s dot add
cube which is a method that we have
there that we haven't actually written
yet and we're going to say snack is now
equal to well a new cube so i'm just
going to copy this
and paste it here um so all we're doing
is now generating a new cube for snack
i'm sure i could put something in that
like changed it but this is just the way
that works okay so now that we've done
that we should be able to move around
the screen and get the snack but we need
to code in the add snack method here
which i believe i have here or add cue
so the ad cube is pretty straightforward
we just have to figure out where we're
adding that snack or wherever we're
adding that cube i don't know why i keep
calling it snack to the uh list so what
we're going to do is we're going to
figure out where the tail is and we want
to add it after that tail
so tail is equal to
self dot body
negative one the last element in that
list i'm going to say dx dy which again
is going to be our direction x direction
why i'm just shorting it shorting it
here it's going to be tail dot dern x
and
tail dot
turn y like this
now again you can do this in two lines
i'm just doing it like this because it's
faster and now i'm going to copy some
again and we'll just go through exactly
what this does
okay so pretty much
this looks complicated but all it's
going to be doing is checking what
direction that we're currently moving in
like the head of the cube or the tail
actually of the cube is moving in so
that we can then make sure that when we
add that um
cube we know where to add it so for
adding it like to the right of the cube
to the left of the cube above it below
it and we can give it the correct
direction to be moving in
so pretty much
um what we do here
is we just say we're going to append a
new cube to our body
and its position if we're moving to the
right which in here we are because dx
equals 1
is one less than the x position of that
tail
so whatever the last cube is one less
than that and that's so that we don't
add a cube per se to the right when
we're moving to the right and then
we're just gonna have overlapping cubes
and you're not gonna see them right
same thing down here if we're moving
down
that means or not down sorry for moving
left that means we need to add the cube
to the right side um so one plus that
tail of the exposition of the tail of
the cube
or of the snake sorry so that we can um
have it in the proper position right and
the same thing here with x and y uh or
with y so that when we're moving up or
we're moving down then we add it above
otherwise we move we put it below
so i just stumbled through that um
because there was a lot of different
words they're very similar anyways
what we need to do now is simply set the
direction uh for that cube so now that
we added it in if we just left it like
that it wouldn't be moving anywhere so
we just need to change that to the
current direction of that tail so
whatever the tail is moving that's where
this new cube is going to be moving in
that direction so we're just going to
say self.body
negative1.dern
x equals dx and then same thing down
here except we're going to change these
to y
during y
equals d y and that again is just the uh
where our tail is moving at that current
moment
okay so now that we've got all that done
it's time to see what syntax air we get
next name rows is parameter and global
one second
so i've actually never even ran into
that error oh global rows as
okay so let's just make this r
and let's change this to be
one second ah we can just get rid of
this
and this should work there we go so now
we move around like this the only issue
is our snack is not being generated on
the screen and i don't know why that is
exactly happening oh it's because we're
not drawing the snack that's why so
now i'm just going to global snack up
here i believe that's what i called it
and i'm simply just going to say snack
dot
draw
like that and i gotta give it a surface
so let's give it a surface let's run
and error name snack is not defined
it's because i didn't global it down
here so let's
go over that
okay so now we have the snack it's on
the screen
and you can see that when we collect it
we have that cube added to the end of
our snake
and that pretty much is almost the game
done so all we need to do now
is write that reset method and then we
need to just simply check like when we
lose like right now the snake can go
through each other right through itself
and you can see we have a huge error
when the snake can go through itself so
we need to make sure that when we hit
ourselves um we like end the game and we
need to write that message method as
well so to do this um
again i'm just going to copy in this for
loop uh and then we'll kind of go go
from there so
let's move in here
okay so for x and range the length of s
dot body uh we're going to say s dot
body x which is going to be we're just
looping through every cube in our snake
body we're checking if the position
is in a list of um
all the positions after that works um
that's how we're checking the collision
if you want to do it your own way um go
ahead but this is the way that i like to
do it okay so now what i'm going to do
um is i just want to print to the
console like their score uh just so that
i know what it is let's say a score
oops
and then we'll just say plus is the
length of
s dot body but this is going to have to
be converted to a string unless i put a
comma like that so let's just do it like
that
and then i am going to simply display a
message box will say message box
like this message underscore box uh we
have a subject and we have content and
we'll we'll do that in just a second
because i haven't yet um actually coded
that
and then we're going to reset our snake
by just doing s dot reset and this is
going to take a position which is going
to be our starting position again 10 10.
um and then we're going to break out of
this for loop because if we collided
once we don't really care if we collided
again
and go back and we continue the game
with a snake that has length one now
okay
so our reset um this is pretty
straightforward all i'm doing in this
reset
is i'm just gonna get rid of
our turns i'm gonna get rid of our body
um and i'm just going to change like the
direction x and the direction y so maybe
it's faster if i just copy it and you
guys can
just copy this out
so pretty much all i'm doing is i'm
setting a new head um which is going to
be equal to
again like whatever position we give in
because we can move it at a different
position if we wanted to i'm clearing
self.body which is in the class variable
here
and then i'm adding head i'm setting
turns equal to blank direction x is zero
direction y is set to one again so we
start moving and you can see it's pretty
much exactly what i have typed out here
except i'm just resetting the turns list
okay so now that we've done that
all we need to code is message box so
this one's pretty easy um you can see up
here just in case you haven't looked yet
um i import tkinter stk and then from
decanter i import message box
now this is how you create a message box
in a pie game and this is how you create
one that actually shows up on top of the
screen um and that doesn't like float
kind of below it or it doesn't show up
right away if you want to say that um so
just watch this is extremely useful to
use in other programs too if you if you
copy out this uh
function and then you just drag it into
other ones so
attributes
topmost
comma true what this simply does is
we're creating a root oops which is not
root.tk it's root equal to tk.tk which
is going to be a new tkinter window
we're making sure that this window is
going to be on top of anything so if we
have like a bunch of different windows
open it just comes up on top which is
what we want i'm just going to simply
make this window now
invisible and i'll explain how this
works in a second and then from this
message box um
what do you call it method class
whatever it's going to say message box
dot show info
and then we're going to do subject
and content
and this just takes it takes a few more
parameters as well if you want but
pretty much just just shows info
given whatever subject we type in and
whatever content we have and now i'm
going to say try
root dot
destroy
like that
accept oops
uh pass and don't ask me what this does
because honestly i don't know but it
just works so pretty much it shows this
message box
and i'm actually pretty sure the way
that this works is it constantly keeps
trying to destroy the message box until
eventually you click the x button and
then it actually can i think it's like
something like that so anyways that's
how you create a message box and with
that we all need to do is add a subject
and content so i'm going to say you lost
and
our message will be play again
let's run the program for the last time
and make sure everything is working
so there we go i just need to get my
snake to a length that is uh large
enough to the point where i can actually
hit myself
oh okay and there we go so pretty much
we ran into an error
what is the air i spelt attributes wrong
that is lovely air to run into let's
run back up here
attributes attributes attributes where
did i even make this function i must
have scrolled path it all right there we
go
and i believe that's correct
let's try this now
so i'm actually just going to go and see
if i can there we go okay so we get a
message says you lost play again and the
reason i died there by the way is
because while i was moving right i moved
left so technically um the head of my
snake actually ran into another part of
it so it says you lost play again um and
you can make that say exactly what you
want and then we can see that our guy
just continues to move
so on
so anyways
if you guys want to see more content
like this go check out my channel it is
named tech with tim and i do have a
bunch of videos teaching stuff like pie
game and doing a bunch of python
tutorials you guys will definitely learn
a lot following through some of my
videos and i would be very grateful if
you guys went over there and liked some
of my videos and subscribed