In this video we recreate the viral word guessing game Wordle in Python with PyGame! https://github.com/plemaster01/LeMasterTechYT - look for the Wordle ZIP folder Before you grab my code leave a like! :)
Start from scratch and learn to build the complete game in under an hour! We begin by importing and setting up the PyGame module, and then we build the board, handle user inputs when keys are pressed, compare to the secret word and handle game over events!
We also show how to import a word list and randomly select one on the start of a game!
Please like the video and subscribe to the channel if you liked it or found it useful!!
If you have any questions or comments be sure to let me know in the comments below and I'll get back to you as soon as I can! Let me know what you want to see in a future video!
Thanks for watching and good luck with your code!
hello welcome back to the channel and
today's video is going to be how to
build the popular word guessing game
wordle in python using pygame
[Music]
okay so wordle you've probably heard of
it by now if you haven't been playing it
and getting addicted to it it's
essentially a word guessing game where
you have six chances to guess a five
letter word if every time you make a
guess if you got a letter correct and in
the correct spot then you get a green
marker so that's a clue for the next
time if you got the correct letter but
it's in the wrong spot you get a yellow
marker on that letter and you only have
six chances to try to guess the word
right super popular but currently the
game only lets you play once so let's
say you wanted to play more than once a
day maybe you should build your own in
python using pygame
so first thing we do anytime we
build a game using pygame and i build a
lot of my little just widgets for fun in
pygame it's a super user friendly module
really easy to get started with first
thing you do is just import pygame
if you don't have it already installed
you'll just probably want to run pip to
install it real quick
there's a million videos on how to do
that if you're unfamiliar but so import
pie game and then the next thing to do
is just pygame dot in it and now we've
called pygame and we can use everything
inside of it so next thing let's just go
ahead and get our
screen setup okay so that we can have
somewhere to display everything so i'm
just going to create a constant variable
called width
and i'm going to create a constant
variable called height
this is a pretty normal way to do it you
can definitely just punch them in when
you create the screen because the next
thing to do is make this screen variable
and set equal to pygame dot display
dot
set mode
and then you give it a size and that's
why i created width and height
but if you wanted to you could just
punch a 500 and a 700 in there but as
you'll see when we're creating the game
board and a lot of the stuff we're going
to put on the screen
there's actually reason to
want to be able to reference width and
height and not just need to have it
memorized as 500 and 700
so let's go ahead and start there and
that's the screen setup and then let's
go ahead and create a frame rate as well
so if you've seen any of the previous
pie game videos on this channel you'll
see i always create a timer and a frame
rate right in the beginning and that is
because uh you want to control the speed
at which your game is played so a really
typical you know good
frame rate is 60 frames per second and
that's just regulating it so your app
runs the same speed on all
um on all machines
one more thing we'll come up here and
we'll just set the caption so that's
going to be pygame
dot display dot set caption and we'll
just call it
wordle
knockoff
maybe don't
call it that if you plan on releasing
this or using it somewhere but that's
what i'm going to call it okay now just
to get a screen pop up so that we can
get started we'll go ahead and just
create running equals true so this is
going to be our main game loop and we're
going to say wall running
and
again if you've watched a few of the
videos on here you should be familiar
with the fact that
this is what i'm calling the main game
loop you basically create a variable and
you say while that variable is true do
everything inside of this but that's
also how you control like once per scan
it does this 60 times per second you add
the timer dot tick at your frame rate
so that's gonna run this 60 times per
second and then
immediately so that you don't have an
infinite loop you should always put the
conditions in here for exiting and
that's going to look like for event
in pygame.event
dot get
if event dot type is equal to and then
pygame dot quit so i'm going to assume
you know a little bit of python in a
little bit of pygame for this tutorial
if you don't
you can copy it down you can follow
along and you can ask any of your
questions in the comments below i'll try
to get back to you as soon as i can
but if you say the event.type is equal
to pygame.quit
then we want to set running equal to
false and that's how we get out of our
infinite loop so now this is a working
game
okay so to get the screen to actually
show up let's go ahead and up in our
screen setup i'm going to add a few
colors because color in pie game is rgb
so if we want a few that we can call
just by typing in the names of them
let's just define them in variables up
above so to start i'll just call i'll
create white and black as rgbs
and um right around time timer.tick
we're just going to also add screen.fill
screen dot fill and we're going to give
it a color i think actual wordle fills
with like a dark gray but we'll just do
black to get started here and you can
play around with rgb values and settings
as much as you want make it your own
and then to get the screen showing up
we'll do pygame.display.flip
and then right underneath it
pygame dot quit okay so this should be
all we need right here to get a 500 by
700 window popping up there we go and
that looks pretty good but it looks
nothing like wordle obviously because we
haven't drawn a board yet
so let's go ahead and create a board um
and right in the beginning just to
initialize it and so that we can talk
about it i'm going to create a list of
lists aka a matrix
and i'm going to put one row
of info in each spot for this matrix so
it is i believe six turns one two three
four five
and then six
and i'm going to
make six just rows oops
those should be commas
so i'm just going to type this and then
talk about a little bit because it's
kind of tricky to uh it's kind of tricky
to explain everything i'm doing as i'm
typing it but i'll give it a go
so to start i essentially want every
piece of the board to have a blank space
in it so this is how i'm going to tell
the game how many total spaces to be
expecting to see
um and so we're gonna just follow like
the traditional wordle which is five
letters but you could also use the same
format to make it more than five letters
but i'm just gonna put each row
and i'm gonna give it five blank spaces
and what we've actually done is we've
created basically a matrix that's the
correct size
for our
for our game okay
so now what i want to do is i actually
want to create a function i don't want
to do this in the main game loop it
would be a lot of code to stick in there
and it's much more pythonic to create a
function
call it drawboard or whatever you want
draw game
and we're going to create a function
that we're going to
draw the board in okay so define draw
board
and let's go ahead and call a couple
things so i'm going to create a variable
called turn and then i'm going to call
the board we just made
and the reason i'm going to call turn is
we want to show somewhere on the screen
what turn you're on um and indicate that
because in wordle you know it's
typically just whatever the lowest blank
column is but i think we can make it a
little better by maybe indicating the
turn that you're on
so call those things first you could
pass them in and pass them back out but
we're not really modifying them here
we're just using them here
and let's use some for loops to create a
board so we'll say for column in range
zero to five and the reason it's zero to
five and not zero to four so we're
starting with zero and then going zero
one two three four
is when you use this in range it's not
end inclusive but it is start inclusive
so it's gonna go from 0 to 5 but not
count 5.
so it's kind of confusing but if you're
familiar with python that should be old
news to you
and then we'll do 4 column in range and
then just like before we're going to go
one more than we actually want we're
going to go 0 to 6 and what we want to
do is we want to draw a rectangle for
each of those so we're going to draw out
all of the empty spaces
for our game so we're going to put it on
the screen
let's make these white rectangles
because we have a black background and
this will make them stick out
and then let's we have to start playing
around with sizing so if i say you know
column times a hundred and row times a
hundred right think about spacing them
out we made our screen 500 by 700 we
want it to be five spaces wide and we
want to use most of the screen so if we
think about splitting the screen into
five for the pieces
maybe we want to split it and make them
100 a piece and then six times the
pieces is gonna be you know 600 and
they'll give us a little space at the
bottom for instructions or score
whatever we want
so let's just start here and we'll
probably modify it because i don't think
this will look perfect
and then just so we don't have a bunch
of rectangles smushed together we're
going to make them 75 wide by 75 tall so
i'll explain this a little bit more when
we create it but these are the four
things you give every rectangle
in pi game to fully define it this is an
x and y start coordinate and then width
and then height
so uh we'll just go ahead and boot this
up but a couple other things i want to
do is i want to give it a width so i
want these to be hollow rectangles with
a
border of three
if you want them to be solid rectangles
you just put a zero here and that'll
give you a solid rectangle but then i'm
also going to make them rounded just
because i think i think it looks nice
that's just me
but
i'm gonna do that
and so this should if we go ahead and
run it let's see if i goofed up nope
there we go so it gives us basically the
wordle board right it's not necessarily
perfectly spaced because it's smushed up
in the top left corner and it doesn't
really
it doesn't look nicely spaced so what
we'll do there i always call this a
fudge factor
but we'll just add some limits so if we
think you know we took 25 off of the
total width of the screen so what if we
split that or off of the width of the
grid we gave it what if we split that
and move them both by 12 how does that
look
and i think that looks pretty good it
looks a lot more even
right in the center of the screen so
that's the kind of thing you can play
around with um i like the look of that
you can play around with it if you want
one more thing i'm going to do is i'm
just going to do
pygame.draw.rect and here's where i was
talking about wanting to
wanting to make a
indicator of what turn you're on i'm
going to add another color to my color
library and i'm going to make green now
and we'll use this a couple times
r g
b okay
and i want to add a rectangle on the
screen i want to make it green and i
would like to put this on whatever turn
you're on so we'll always start it just
a little inside of the screen
and then we'll take the turn times 100
which is how we spaced out the boxes
thinking back to the previous step
and then we'll add a little fudge factor
because we're not going to want this to
be the whole size of the screen either
and then we'll do width
minus 10. so remember i said you could
just type 490 in here if you wanted this
game to be you know all very rigid um
and you were okay with hard coding
everything you could do that but if you
made those variables you might as well
use those variables so i'm doing width
minus 10 and then i'm going to make it
almost the entire height of the grid so
that's going to be 90 tall
and then i want this to be hollow again
and i want it to be rounded just because
i like the look of it you don't have to
make it rounded but there you go so
my idea here is as you go through the
game it's going to highlight whatever
turn you're on
you could say you don't need to know
that because it's obviously going to be
whatever row isn't currently filled in i
like the look of this it's my tutorial
let's move on
okay so we have the board getting drawn
and what i'd like to do is i would like
to turn to handling
player entries now so
when we have
you know
let's do it this way let's write the
text of each of the board keys onto the
screen and i'll put some dummy values in
and then what we'll do is we'll go ahead
and
we'll go ahead and
let's just get these typed in i'm going
to put some info in just so we can show
examples of the data getting written
onto the buttons but then i'm going to
modify this and we're going to go back
and we're actually going to handle the
user
stuff so
since we're in the draw board the the
other piece of drawing the board is
drawing whatever letters have been
guessed right and that's why we passed
the board in so what we're going to need
to do is we're going to have to create a
font in pi game and if you've done a
little bit of pie game this is old news
to you but if you haven't
just
follow along basically
writing text on the screen happens in a
few pieces first you need to define your
font by doing
pygame.font.font where the second f is
caps
and then you can pull a ttf font file
into your
program or you can point your computer
to it
if you have a specific look for your
game that's reliant on a font then go
ahead and do that
i use freesands bold because it's built
in
and i can use it without having to add a
ttf file to every one of my projects
so i'm going to just call this title
font because it's kind of huge i mean
let's just call it huge font to be more
descriptive so 56 is pretty big but
that's because we want one character to
completely fill the screen what we're
doing here is we're giving the font name
and then the font size and so we're
making it 56
but now we've defined a font and then to
actually create text so we'll call this
piece text and you'll see why i'm
putting it inside of both for loops here
okay but so i want the title font
dot render so this is step one in
getting
stuff drawn onto a screen okay
and what we're going to do is we're
going to say the text that i'm
interested in is the board at row and
then column so when you have a matrix or
you have a list of lists you need to
index something inside of something that
you're already indexing you just do it
this way with two square brackets one
after another and the first one is going
to take you to which entry so each entry
is a row and that's why the first
variable is row and then the second one
is going to take you to an item inside
of each row and that's why we do column
so what we're saying here
piece text
equals
title font
dot render there we go
i called it huge font
i usually call my biggest font title so
force of habit there but we just called
it huge font
okay so what we said is okay the text
that we just put in there and we want to
be true and this is getting drawn onto a
black background so let's make it white
okay
and now that alone is not going to draw
it onto the screen what you have to do
is you have to do screen dot blit
and that's block transfer i believe i
don't know it's just it's screen dot
blit that's what it is and you say what
text it is that you want to draw into
the screen and then you give it x and y
coordinates and so similarly to before
thinking about where we placed these
it's column times 100 but then we
scooched all of those by 12 and then
this is text so it's going inside so we
want to scooch it by even a little bit
more
so let's do column
times 100 plus 30 and then let's do row
times 100 plus 25
and let's just take a look at that so i
put some dummy variables dummy values
inside of our board just so you can see
it
that looks pretty good
so this is nice every square should have
one character in it so we're
representing things pretty
true to form here even once we start
text
typing values in
so this is a pretty good start and i'm
going to go ahead and just take my
numbers out
and
make them spaces again and the reason i
made them spaces is just because they're
gonna be one character whoops they're
gonna be one character once we type in
the in the values and so having a space
initially is
for me it's one character and it just
it's easier to model that way because we
haven't yet started adding values but
those could absolutely just be blanks
okay so let's go ahead and actually take
a look at what happens when a player
presses the key because that's how you
play the game is you guess at the letter
so to start i'm just going to create a
variable that i will call
secret word and before the end of the
game i'm going to show you how to
look up the words so to actually check
and see um
you know
like look into a list i've got this
second python file with just a few five
letter um words and i'll show you how
like you bring in a huge list of words
and then randomly select from that list
we'll get to that at the end but while
we're just setting the game up
i'm just gonna have it hard coded and
since it's about drive and it's about
power and we stay hungry and we devour
we'll just do secret word equals power
okay
and uh now let's go down into our event
handling so everything under for event
in
pygame.event.get is what i consider
event handling and this is where we're
going to set up um checking what key the
player is pressing okay
so what i'm gonna say is if event.type
and we're gonna use one that's pretty
specific to needing text okay so this is
actually not the most common
event type in pygame if you've built
like jumpers or shooters or anything
like that you maybe haven't used text
input before but it's super useful for
just grabbing the actual text from
a key that's pressed on the keyboard so
it's if event dot type just like before
equals pygame dot text input and then we
want to check one more thing we want to
check and not game over
and i just like to think ahead about
this
because we're going to put some restart
conditions in there and at the beginning
of the game game over is obviously false
but eventually we're going to want to
add some like game over conditions and
restart and if the player's pressing
keys at that point we don't want to do
anything so just going to add that right
here but we definitely would run into it
later so
just something to keep in mind
and then i'm going to create a variable
called entry and that's going to be what
entry the player just made
and i'm going to set equal to and this
one's a little specific it's event and
then get attribute because inside of a
text input there are a few different
attributes
and it's essentially got a dictionary of
values and what the text that was
actually pressed is called is called
text and to show that i'll actually just
go ahead and
um print the event real quick
and i think this is going to work i just
want to show you what happens i'm going
to press the a key
okay there we go so if you see down here
there's a few attributes there's what
window it happened in and then there's
also the text but there's also this pie
game specific like event 771 text input
so when we use this get attribute text
it looks into the attributes dictionary
which is right here and it's a bunch of
key value pairs
and it's um checking for a key that's
equal to text okay so that's what we did
here and we just set the letter a
equal to this variable entry
and now what we want to do is set board
at
and then if you think about
if you think about where we want to
write this to it's going to be whatever
turn you're on so that's telling you
what row you're on and then we need to
make a new variable that'll tell you
what letter you're on so we'll call it
letters and to start
i'm going to set letters
equal to 0 at the beginning because no
letters have been entered so when we do
that though
we're going to set that equal to entry
we're defining a new space and since we
put a
variable in there already but we're
overwriting it with entry it is just
going to get overwritten so you don't
have to worry about being space and then
an a at the end it's just gonna be the
letter a when you press a
but then we have to move letters forward
so letters plus equal one and you should
be familiar this is just shorthand for
writing letters equals letters plus one
so whatever value you already had in the
variable letters we're just adding one
to it
and now i'm just going to boot it up and
see if i can type a few
yeah so i can do you know just letters
that's nice but
yeah so if i go past 5
it's going to give me this value out of
range because we're not checking to see
if that's the end of a turn if you
filled
up your letterbox so let's go ahead and
create some code in here just checking
to see if
letters is equal to
and let me think when you've added
zero
and then you put one letter in one two
three four five okay if letters is equal
to five right then what we want to do is
we want to
kind of end the turn so i'm going to
create a new variable that i'll call
turn active and so
this is gonna just be another condition
so we'll go up here and we'll say and
turn active
and what we're gonna do now is we're
gonna just check and only let you enter
um it it's only going to let you enter a
new letter if turn active is true so if
letters is equal to 5 then turn active
is going to be
false and then if letters is less than
5
turn active will be true and i think
this will make sense once we
add some backspacing in why i wrote it
that way but let's go ahead and make
that variable turn active
and to start it's going to be equal to
true okay
so um before we go back in and i show
you like what the code we just did
does let's add one more event which is
going to be if dot event dot type
equals
and it's going to be pi game dot key
down actually we should have put this
above text input actually because text
input backspace the space bar they're
all this kind of event so if event dot
type
equals
pi game
and then dot
key down
i knew it i just didn't want to get it
wrong okay
so if event dot type is key down and
then we check if it's a text input if
event dot type is equal to
pi game dot and then we'll say k
underscore backspace and it's
backspace and this is not supposed to be
event.type this is supposed to be
event.key
okay and if it's
the backspace key and they've entered
something so we don't want to let them
go backwards if there hasn't been any
letters entered yet so we'll say and
letters is greater than zero but if they
press the back key
all we need to do is take the board
piece at um you know
turn and then letters because that's
where that's the last piece that got
entered right and all we're gonna do is
we're gonna set back equal to a space
like it was when the game started and
we're gonna take that letters variable
we made and we subtract out one okay so
now i'm going to show
if i type in you know oh is it gonna
work
it is not what did i do
oh it's because
this is not supposed to be under key
down just backspace
i am having a goofy day all right let's
see so if i type in you know my name we
haven't done anything yet to let it go
forward so i can't enter this turn but
i'm typing more keys and it's not
erroring out it's not going forward
anymore
and if i start doing
board dot letters oh i did not put a
minus one in there
gosh good gosh okay
all right so let's check that out
peter
backspace
list index out of range oh this is
supposed to be letters minus one
okay because what so once you enter a
letter we increment letters forward by
one so when we clear out the last one
that was entered we need to do letters
minus one
let me go ahead and check this out i'm
going to type in pete oh i made a
mistake i want to go back okay
all right there we go so
very cool um
but i can't enter more letters so what's
going on here
and turn letters minus one letters oh
letters minus minus one is moving it
forward
okay pete i can go back i can go back
twice and i can enter more letters okay
that was a fancy bit of troubleshooting
there not fancy at all um just
irritating you got to be careful with
case and you got to be careful with
negative so i had letters minus equal
minus 1 which is actually adding one to
it so that's why i wasn't able to enter
more letters but we've got it working
now you enter letters
and once you've filled it out to five it
won't let you enter any more letters but
before you have gone on to the next turn
you can enter backspace to or you can
hit the backspace key to change your
guess
so that's cool let's go ahead and do the
next piece which is going to be moving
the game forward so actually taking a
guess getting feedback and then moving
on from there and you can use enter if
you want i'm actually going to use space
for entering a guess
just because i i prefer it
but so if they enter space and the game
is not over then what we want to do is
basically just increment turn um
so we want to do turn plus equals one
and then we wanna set letters back equal
to zero okay so that's all space is
gonna do
um and i'll go ahead and show this so if
i do my name and i hit space i'm on to
the next one and i can type in i can do
that i can hit space again so the turn
mechanism seems to be working but
obviously what we aren't doing yet is
checking if it's
if we got any of the letters right or
anything against our secret word
and i'm actually not going to do that in
event handling i'm going to create
another function that's basically called
check words
and so just like drawing the board i'm
going to isolate the piece of code that
checks our answers and i'm just going to
put it into its own function
that's very much so the way python was
intended like isolate your your key
functions into their own
into their own spots so what we'll do is
just like before in in our
drawing of the board we're going to call
a few things we're going to call turn
and we're going to call
the board
and we are also going to call the secret
word for this one because a lot of what
we're doing is comparing to the secret
word
and now what we're going to do we're
going to use some more for loops pretty
similar to what we just did for um
the full board drawing
so for the full board essentially and
i'm you know you could put this same
code up above if you wanted um it could
be considered part of drawing the board
but i like to see where the different
pieces are coming from
so essentially this double for loop is
just going through every piece in our
matrix every time the the routine scans
and what we're checking is to see if the
secret word at position column and
that's all we have to check
is
off of the column because it's going to
go through each row individually but the
secret word is not a word inside of a
list or something it's just a word
um
so if the secret word at column is equal
to and then the board
piece at row and column so we're
checking a specific character
but we only want to do it for turns for
pieces after we've finished the turn so
we'll say and turn is greater than row
so this is only going to check for
pieces that the row is behind how far
you've gone and it'll look good once we
run it that should make a little bit
more sense but we're gonna go ahead and
draw a rectangle and we're gonna draw it
on the screen and we're gonna make it
green
and let's go ahead and put it
basically in the exact same position as
the first rectangles so we want it to
look like it's not even a new rectangle
we just want to look like we're changing
the color of the previous rectangle
so this is how i'm choosing to do it and
you for sure could do it a few different
ways you could actually try to modify
the original
statement to where this is going to
check for correctness and color your
rectangle based off of that i'm just
going to draw a white rectangle in every
spot and then draw a colored rectangle
if they got the
it correct or partly correct but i'm
going to make this one solid so where i
was talking about before with the zero
and i'll still make it rounded and then
the second piece to playing wordle is if
you get a letter correct
so if the piece at board row
and then column
is just in the word but not in the
correct position so i'm going to use an
l if because we don't want it to check
if it's um we don't want to color it
yellow if it was already green but what
i'm going to say is just if board at row
column is in
secret word
and turn is greater than row just like
before and i'm going to go ahead and
copy this rectangle
and we're going to have to make one more
new color
and we're gonna make it yellow to really
replicate
what's going on with wordle
and we haven't made yellow yet so we'll
go back up here
underneath green
and let's make yellow and now if you're
not familiar with the color wheel
yellow is what you get when you have red
and green
and no blue feel free to play play
around with your colors as much as you
want but that's
it so that is going to give us yellow
and green letters when we go on and
since we know the word is power i should
be able to test this
i'll do
po and i'll do roo
and you can see
that we're getting the solid rectangles
which is nice but they're over top of
the other rectangles so here's a really
important part of pie game these are
happening in the order you see them here
so it's going screen.fill drawing the
board and then checking the words all we
have to do to get that not overwriting
is check the words before we draw the
board
okay so let's go ahead and check
and we you can see we got three greens
let's do
oh i have a few special characters in
there okay and so we get yellow and we
get green and that's nice but man white
is hard to read on top of those colors
so let's add another color and let's
just make the text gray because gray is
something that we'll be able to
read on top of
black on top of white on top of green on
top of yellow and all we have to do to
modify it is go down where we define the
piece text and change the color from
white to gray
and let's see so you can see it looks
fine on black
and then it looks fine on green and i
guess it looks fine on yellow too so i
like the gray you definitely could pick
any color you want you could even make
it to where the text draws a different
color once the pieces behind it are
colored in if you want but gray looks
good for now so what you probably
noticed is nothing happens when the game
is over and that's really the next
important piece is we need to check
and see if um if the game is over so
we're going to go ahead
and come down underneath our event
handling kind of where we check the
turns against the letters and we are
going to add some code to go through the
secret word and check it against the
against the guesses so we'll say for row
in range and 0 to 6 because we're going
to go through every row
and we'll just say guess is equal to
because what we don't have is we don't
have a really nice
put together concatenation of
all of the board pieces so we need to
concatenate all of them you could do a
little for loop here if you really want
to
i'm just going to do it this way because
it's only five letters it's really not
going to take long
and what i'm doing is i'm taking the
five characters that have been entered
and i'm just setting that equal to this
variable guess okay and then what i'm
doing is i'm checking to see
if guess is equal to
secret word
and then i'll also say and row is less
than turn because we don't want to
actively show you
um
we don't want to actively show you that
you got it if you haven't pressed space
yet like you have to commit to the guess
so that's why i'm adding the and row is
less than equal to turn
but that's how we're gonna set game over
equal to um equal to uh true is
basically just have you gotten the
correct word in one of your past turns
okay so that should be pretty simple
that should make sense to you this piece
of code is pretty basic but um
let's go ahead and check that let's just
get the word right real quick we'll do
sweet word
okay we got right and it's not doing
anything i'm not able to type any more
letters i'm not able to hit backspace or
space which is good because you
shouldn't be able to do anything
but
we want to create some code that'll like
tell you hey you won and then also tell
you how to like restart the game if you
want um so to do that
you could put this in a function but
it's really basic uh stuff so i'm
actually just to put it down here
and say if game over
actually let's do two different things
if game over and turn is greater than
six
so that means that game is over or turn
is equal to six
that means the game is over because you
didn't get it in time right because we
start at zero and then you get zero one
two three four five as chances so if
turn is equal to six then what i think
we should do is we should make a
variable called game over text
actually let's be harsh let's call it
loser text
and just say
[Music]
what we call a huge font dot render this
might be too big we might have to make
another font but let's just start here
and say
loser
sweet let him know
and then we'll say true and why don't we
make that uh i don't care just white
so we'll make white text and let's go
ahead and throw it on the bottom of the
screen so screen dot blit
uh
what i call loser text and let's put it
in a good spot which is going to be
somewhere down at the bottom 400 by
uh i don't know 610 get it out of the
way
and
the other way you can lose is actually
or not is not lose it's game over but if
game over and turn is
less than six
then what we want is
winner text
winner text and we will just say
winner and we'll put it in the same spot
because these two are never gonna be the
same and actually i'm thinking about it
we never indicated that game over was
true when turn is equal to six so i
think what we need to do
is actually just change this to if turn
equals six and then game over equals
true
okay so i think this is going to be
sweet let's go ahead and check this out
in both conditions so let's just say you
get the word
oh
winner text yeah i forgot to change it
in both places
winner text okay let's go ahead and just
get the word right first power
nice winner it shows up there it looks
fine um now let's see if we can lose the
game shouldn't be too hard because we
know what to avoid puller
sloop
pow wow i almost typed the right word
that would have been really stupid i'm
trying to lose slot
and
rent i don't know okay and it tells you
loser okay sweet um but now what i think
we should do is build in some code to
where we're to repurpose the space bar
so right now if the
key is space and it's not game over then
we move on to the next turn i'm going to
say if you press space and it is game
over then what i want us to do is use
this space as basically a reset so i
want us to go back to the beginning and
say turn is equal to zero
letters is equal to zero
um and then game over is false and then
one more thing i want to do is i want to
reinitialize the board so up here when
we defined it and we had it all emptied
out i want to empty it out again when we
hit res restart so i want to put it
here and that should be all we need to
do to get it to restart so sloop
space space actually i can just space it
out with empty
oh but it's gonna be kind of mad if i
don't air anything okay let's do power i
got it and now game over so i can't type
anything else i can't hit backspace but
if i hit space
it reboots the game
except it moves us to the second line
let's take a look here and see what's
going on and game over i hit space and
turn is equal to oh it's because so
again order matters um this one should
be above
because i was hitting space and it was
setting turn equal to zero but then it
saw game over was no longer true and i
had hit space and then it added one to
turn
so if you get some crazy stuff that
makes absolutely no sense when you go
through your code line by line chances
are you've got something goofy going on
in your order
okay and it looks like somewhere
we're getting something yeah adding to
letters as well
um
so let me see here because this one is a
little bit trickier for order we're
going to move this whole event type
up above event key down okay
so
that way it's not going to write in
order it's not going to write a letter
for us it's not going to move a turn
forward and it is going to restart the
game okay
so let's do that
power
suite it's about drive it's about power
and it's still adding one to letters man
that is
there's minus one you press space we put
zero into letters
all right
well let's go ahead and add something
that i think's um
super important to this which is not
just having a hard-coded word so that's
kind of a boring way to play this game
is to have the whole word have someone
come over and play your game that you
hard coded the word a more fun way is
going to be if you create a list of all
of the words you want to be able to
guess from so
you know
you can have this in a text file you can
have it in a python file it's really
easy to access if you
have a python file
so
i just made a list
made a variable list called words and
i'm going to import
that file so that file's called words
and then i'm going to have the secret
word be equal to and here's where it
gets a little funky if you're not smart
and
with your variable naming so from word
dot words which is the list that i made
i want to use random dot random integer
and so i'm going to need to import
the random module as well
but so i'm going to import the random
module
and then from the random module i want
to use the built-in random integer tool
and i want to select a word
anywhere in that range so from zero
all the way up to the length of
the words dot words
list
and then i'm gonna add minus one because
uh that could go out of range by one it
could go off to the right
by one for your list
but what this is doing is it's just
gonna at the start of the game randomly
grab a word
from the list whatever your list of
words is you could use a dictionary api
and look up all available words that
would be pretty wild
um i'm just going to do a human curated
list just like this and then one more
thing if you're going to do it that way
then you have to make sure wherever you
do your restart logic you also put in
um you also put in the
uh
the secret word look up otherwise you're
going to get the same word every time
every time you boot up the game
okay so this i believe should give us a
word that's not
um
not just power every time so we'll take
a look
unsupport operand type for list and
lists let's take a look and see what i
did
okay that's in our lookup so the length
of
word dot words
that's right because this is going to
give us an integer that's going to give
us an integer
and then we do a parenthesis
okay that should do it i'm gonna have to
change that in both places so what i had
is i had
i had put the minus one inside of the
length statement and it needs to be
after the length statement is closed so
that's right there now let's go ahead
and play the game and i'm gonna pull up
the words list just so i don't get stuck
uh let's try
let's try my name okay we get an er at
the end is it power that would be wild
it is not okay that's a good sign
actually
then it's probably loser okay we got it
let's hit space see if we get a
different letter or a different word so
we'll do angry
we've got an r somewhere in there let's
do super oh it was super that's actually
pretty wild i got it okay so uh you can
see this game is still not fully fleshed
out because i i don't have um a whole
lot of words i just didn't want to spend
a ton of time making a word list because
from what you've seen in this video you
can take it you can make it your own i
hope you like the tutorial
a few things just to talk about it that
i would consider adding for your game to
make it more fun to share with people
you could add high score tracking
you could add a tracker to see your
average so how many turns it typically
takes you to solve it you could add all
kinds of stuff you could make it um to
where you could select how many letters
to play so you could play a four letter
version a six letter version and make
the size of your screen scaled to
however many letters you want to play
there are so many different ways you
could take this you could take the basic
wordle game that people know and love
and you could expand it out to basically
be your own game in a million different
ways
if there is enough interest if i see
enough comments asking for like a
follow-up or
that fleshed-out version to really take
it the extra mile i could do a video but
i really recommend you guys as
programmers spend some time add the
features have a little fun with it and
let me know what you decide to go with
because i love seeing feedback saying
hey i did it i added these things you
know how did it what do you think of
that so
i hope you liked the tutorial if you
have any questions about anything you
saw here today feel free to leave me a
comment i will get back to you as soon
as possible if you have found this
content or anything else on the channel
useful i really appreciate a like on the
video subscribe to the channel it helps
me out a ton as i'm getting going here
and i try to crank out a lot of great
content just like this so as always
thanks for watching and good luck with
your code thanks bye