Placeholder Image

Subtitles section Play video

  • Today, I am going to show you how to make a Simon game, using mainly JavaScript but also HTML and CSS.

  • Simon is a game.

  • It's It's based on a real game, a physical game in real life, and we're going to recreate it with JavaScript.

  • And there is a A four different lights or colors, and they will light up in a certain order, so the player has to remember the order.

  • Each round, an additional light is added, and the player has to remember all the the order so far in the game until you get to 20 rounds and then you win the game.

  • So let's see what the final product is going to look like.

  • So here's how this Simon game works.

  • There are the power button, the strict but in the start button.

  • So this is supposed to simulate a physical Simon toy game, and the physical Simon Toy game has a power switch.

  • So that's what this this check box turns the game on.

  • You can see when I click the power button you can see right in this Count Square.

  • This red line appears to indicate that the game is on, and then the strict mode.

  • It means that if you get one wrong answer, you lose the whole game.

  • So a strict mon is on you get one wrong answer and the whole game is lost with strict mode off.

  • If you get a wrong answer, you can start from where you left off.

  • So let me show you an example how it works.

  • This is a memory matching game.

  • So you have to remember the order of the colors.

  • Let's do this If I press start so I have to remember the order of these.

  • Oh, I got it wrong.

  • So since I got it wrong, it starts from where it left off.

  • So let's do this again.

  • You will notice that for every round the count goes up by one.

  • And if I get something wrong, the count flashes.

  • No.

  • So it starts from where I left off because I'm not in strict mode.

  • But if I turn on strict mode and I get it wrong, you can see this count is gonna go down toe one x I got it wrong and I have to start the game all over again.

  • So let's go into the code because this is what we're going to be building.

  • Okay, I have sublime text open on the left, and then I just have my browser with the Simon game open on the right.

  • And in this tutorial, I'm mainly gonna focus on the Java script.

  • So I already have the HTML and the CSS written.

  • You can see the HTML file here, the CSS file here the Java script file is empty.

  • So I'm going to review the HD amount of CSS and show you how those work.

  • And then I'm actually going to write all the Java script and explained that as I go first, let's see how the project is structured.

  • So my root folder just Simon game and then I have my index html file.

  • That's the file you see here.

  • And then I have a job script folder and a CSS folder.

  • Now, inside these folders there just one file.

  • So really, we could have probably put all this into the root folder, but it's good to get into Ah, just the mindset of the best practices.

  • And generally you'll have all of your job script in one folder and all of your CSS and another folder so right now, we're not gonna need to see this file structure again, so I'll just close that, Okay?

  • And let me just re adjust this a little bit and I start going through this HTML here so you can see from here.

  • Here is the heading.

  • Now, this is stuff that's not going to really appear on the page.

  • But it's stuff important for the page.

  • The first thing I wanna point out is this font here.

  • I'm bringing in a Google font.

  • Now Google makes all these fonts that you can use on websites.

  • They're they're free to use.

  • And you can.

  • They have a lot of unique fonts that you can use.

  • So that's the word.

  • Simon here uses the ghoul font.

  • It's called Original Surfer.

  • So if you just look up Google fonts, it gives you instructions on how to include them in your in your Web page.

  • And also there's ghoul font effects so you can see right here.

  • It says effect equals M boss, and that's why you can kind of see a border around around this.

  • It's embossed and ghoul fonts have all sorts of effects you can use, so I would highly recommend looking up cool fonts and the effects if you're not already familiar with them And then here is where we link to our CSS file.

  • That's the one right here and then something else look at is how we are basically importing these audio files to use.

  • So there's four different audio files.

  • Um, we have the audio tag and there's the opening and closing tag in HTML.

  • Most tags have opening and closing tags, and we have the set of different ideas, which we will reference later in our JavaScript.

  • And the source is just this file right here.

  • If you're falling along, you can use these same files or you can use your own files.

  • So we have the four different audio files for the sounds they're gonna play when each of the buttons are pressed before you go over the rest of the HTML.

  • Eso this HTML here is what actually makes up the actual page that you can see here.

  • But let's go over to the CSS.

  • Um, I'm not gonna go through everything on here.

  • Um, I will link to both the HTML and the CSS in the description of the video.

  • So you're not gonna have to type all of this out.

  • If you're following along, you can just download the HTML file and the CSS file.

  • In fact, all these files will be on the get hub depository that's linked to in the description.

  • So you can see, um, for the body you can You can really use anything for the background, but I just use this repeating pattern here.

  • That's why we have this texture.

  • We could have also used a solid color.

  • And then this is just a standard CSS.

  • You can see that I am using the Google font for some of these things.

  • And so for CSS, just a quick refresher.

  • If you have a dot that's going to apply to a class and then if you have a hash mark like this, it's going to apply to an I.

  • D.

  • So, um, now, actually explain that a little more once we get back to our HTML here.

  • So in the body of HTML, we have this outer circle and this outer circle you can see this is the opening def tag, and this is the closing in tag.

  • So everything is within the outer circle.

  • That's this dark black circle around here.

  • And then here we have the top left top, right, bottom, left, bottom right.

  • And those air, Just these colors here.

  • And the ideas are important for two reasons.

  • The point of ideas is that their ah, there's only one of them.

  • So whereas classes, you can have multiple things with the same class ideas.

  • You could only have one element with the same I d.

  • So we use the I.

  • D.

  • S and R CSS.

  • That's one of the ways we use them.

  • Francis, with this top left here, if I go on here, if I scroll down, I will be able to see top left.

  • And this is the CSS to create this green section here.

  • You can even see background is dark green here.

  • The other purpose of the I.

  • D.

  • S is that we can reference them in JavaScript.

  • And a lot of these we are gonna reference in Java stripped because we're going to add click handlers, and we're going to use javascript to show what's going to happen when you click the different elements on the page.

  • So we also have this inner circle that's this grey circle here, and it has a bunch of stuff inside the inner circle you can see nestled within the circle.

  • So we have the title and you can see that there's a special class font infect in Boss.

  • And that's how we get the effect on this from the ghoul font.

  • And then we have these switches.

  • The switches are these check marks here?

  • Now we could change these check parts to be toggle switches or buttons or something that would look more similar to the physical Simon game toy.

  • But for now, I just have him as check boxes like we have the power, but then the strict button and then we also the start button.

  • So if you want to improve upon this project, one idea would be to style these a little, a little more toe look like actual buttons or toggles or switches.

  • You could do that using CSS, or maybe even used some Java script for that.

  • So we have these three buttons here, and then we have the labels power and strict.

  • Now turn.

  • That's this box here that's gonna tell what turn were on when we're playing the game.

  • And that's pretty much everything here.

  • The last thing I want to show you in the HTML is the script tag that links to our Java script file and you'll see it's at the very end.

  • Now the the CSS is up here at the top.

  • We linked to the CSS file, but we linked to the Java script file at the end.

  • Some people do link to Josh through files at the beginning, just like the CSS file.

  • But it's really best practice to link to the job script at the end.

  • And that is because we want to make sure all of the HTML is loaded before we load the Java script, because the Java script is going to reference some of the items in the HTML.

  • And so if the job script loaded too quickly before the HTML was loaded, it could create some problems.

  • Okay, it's time to get to the good stuff, the JavaScript.

  • So hopefully you're following along and actually typing this all out yourself, that's how you're really gonna learn.

  • I mean, you can just watch and just try to learn just from observing.

  • But if you actually type this out yourself and follow along, it's gonna help you understand the concepts a lot more and you can try out different things.

  • So, like I said, you can just download the CSS and the HTML from from the links in the description.

  • But the Java script will be nice of you typed it out with me.

  • So first, we are going to just type out some of the variables we're gonna use.

  • So I'm gonna declare our first variable with a let let's order equal.

  • And then I'm going to put an empty array here, these two brackets.

  • That's how we're going to set this variable named order to this array now jobs trip.

  • You're usually gonna end all your lines with a semi colon.

  • Let is a way to initialize a variable.

  • There's there's three different ways to initialize a variable We have let we have var And then there's Const and ah of our I'm just gonna do a brief overview of what the different things are.

  • Um, var will apply to the entire the scope of your entire program.

  • Let will be a little more of a limited scope and on Lee apply to where it was defined, and const stands for Constant and Constance cannot be changed.

  • So once you set a variable with Const.

  • You can't set it to something else later.

  • So most people say you should only use let and constant and you should never use of are so we're just going to use a lead here.

  • So order, This is a variable that's going to keep track of the order of the lights and how they're gonna flash.

  • We're also gonna have let player order.

  • Now, this is going to be the order that the player is pressing the lights in and let flash.

  • This is going to be, ah, basically an integer now with Java script, you don't set the types of the variables, but generally you do keep him at one type for the most part, and this is just gonna be a number the number of flashes that have appeared in the game We're also gonna have let turn to keep track of what turn were on.

  • Um, let good and this is going to be a Boolean true or false, whether the player is doing well or not.

  • If the player has, um hit all the right colors or if the players not hit all the right colors and let comp turn also is gonna be a Boolean True or false, it's gonna keep track of whether it's the computers turn or the players turn.

  • We're also gonna have let interval.

  • I d.

  • I'm gonna explain that when a little later, um then let strict is going to keep track of if the strict button has been Ah, the check mark has been checked.

  • It's going to start it false because it's gonna start unchecked.

  • Um, let noise equals True.

  • If we're playing noise, this will make more sense later in the program.

  • Let on equals false.

  • And that's if the program has been turned on the power button here and it's gonna start turned off and then let when this is just going to say, if the player has won the game or not.

  • Um, so now we're going to use some constants.

  • Um, we have to have a way to get some of our HTML elements to reference them in JavaScript.

  • So that's what we're gonna do here.

  • We're gonna try to we're going to reference some of our HTML elements in JavaScript.

  • I'm gonna show you how to do that.

  • We're gonna have the turn counter.

  • Now the turn counter Is this right here?

  • And we want a reference that in Java stripped.

  • So I'm gonna use this document, that query selector, and then I'm gonna pass in lips.

  • I'm going to pass in a CSS selector, so I'm gonna just type in turn here.

  • So why do you document that query selector?

  • I can pass in any CSS selector.

  • And this hashtag turn means an element with the i d of turn.

  • So if I go into my HTML, you'll see it right here.

  • We have an i d of turn.

  • So now we've actually taken that html element and put it into this variable turn counter, and now we can use our javascript to manipulate that.

  • We're gonna do that a few more times.

  • Um Const Top left equals document, that query selector.

  • And then this time it will just be top left.

  • And then we'll just kind of speed this up a little bit.

  • Okay?

  • You can see now I have a variable for all the elements were gonna reference the top left top, right, bottom left, bottom right.

  • The strict button, the on button and the start button.

  • These are all the elements that you can interact with Besides the turn counter, you can't interact with the turn counter, But all these other elements we can interact with and we're going to use JavaScript for the interaction.

  • So we just used the CSS electors for each of those elements, like top left, you can see here.

  • Ah, idea of top left.

  • And then we got all these elements into variables, and since they're always gonna stay the same, we really use Const.

  • So now that we have the variables set up, we can start ah, writing the code that's going to allow us to interact with the game and play the game.

  • So how I'm gonna organize this is I'm going to write the program in the order that you would play the game.

  • So first of all, you're going to interact with these buttons yearly, like the power button, the strict button and the start button.

  • So I'm just going to start, um, we'll just start with the strict button for now, so I'm going to reference the strict button now.

  • This is the variable we just made up here, and I'm going to add event listener.

  • And so, if we add an event listener, it's going Thio be listening for some event on that element and the event is going to be changed.

  • And so I'm going to pass in a function now This is going to be, ah, an aero function here.

  • There's a couple ways to make functions in JavaScript, and this is a common way for event listeners where this is what we're gonna be passing into the function and you put the aero equal and then the, um, more than sign and we have his curly braces.

  • And this is what's going to happen when that, um, that check bark boxes changed.

  • So I'm gonna put if, ah, strict.

  • Well, before I actually do this, I want to actually show you it working.

  • So let's just do a console dot log, and I'm gonna say, um, checked.

  • So a council that log if we open up the job ship console, we can see something happen with this.

  • So I'm gonna save this.

  • I'm going to, um, refresh here, not open up the jobs from council on the side here.

  • Now this if I click the strict but and we should see something appear here.

  • Okay?

  • you can see it now has checked because this changed.

  • Now, this would actually work.

  • You can see every time I click here, it increases the number because it's saying that has put that word that many times this could really be also event listener click.

  • This could be click or change and it would do the same thing we were just trying to keep track of when we clicked it or changed it because we're going to in the code logic.

  • The code is going to check whether the check box is checked or not.

  • So let me show you that Now, First of all, I'll close the job strip Consul, and we'll just move this back over.

  • So, um, we can see the code a little better.

  • Come.

  • So what I'm gonna do here is say, if strict button that checked equals equals true s so that that's a way that we can see If this is a check now dot checked is on.

  • Lee gonna be something we can ah use if we're dealing with a check box.

  • But since strict button is a check box, we can see if it's checked as true or if it's not checked will be false that we could do something.

  • So if it's check, if it's true, we're gonna set strict to equal true else, we're going to set strict to equal false.

  • So this is just a standard, if else statement.

  • So if it's checked strict to be true else strict should be false.

  • So we've now just finished that whole button and we can go on to the next button, which is going to be the on button.

  • So again, I'm gonna do an ad event listener.

  • And again, it's going to be a click event.

  • And so I'll pass in the event you'll see.

  • Notice that above we pass in the event to this function, and we never use that event here.

  • That's OK.

  • It's still that's still when you're creating event listener, you always pass in the event, even when you're not gonna use the event in the function.

  • So this time again, we're going to check if the on button that checked equals equals true and so if the arm button is checked will do a few now we're going to do two things.

  • Um, we are going to set on two equal True Um just like we said Strict, equal, True.

  • And these variables come from up here on and strict.

  • But we'll do one more thing if you remember in the example I show you at the beginning.

  • When you turn the power on, we want a line to appear right here to show that it's on.

  • So to do that, I'm gonna do a turn counter dot inner HTML Now enter HTML.

  • Well, let me just finish trying this line.

  • So this is going to equal and just this dash inner HTML is something that you can, um, use on any HTML element to set the HTML to something different.

  • So now the HTML with in this turn counter is going to be this line.

  • So if we look in here right now, here's the turn counter and within the turn counter is nothing between these two brackets here.

  • There's nothing there.

  • But when we do this, turn countered at internal HTML, it's going to put a dash right in the turn counter.

  • So we'll do then.

  • Else here, we're going to do on equals false, and then turn counter.

  • The inner HTML equals and we're gonna set it back to nothing.

  • So let's just see if this works.

  • Now, say that refresh.

  • And if I turn the power on, you'll now see this line appear when I check in.

  • Uncheck it.

  • So this is starting to have at least some functionality.

  • Um, we're gonna do a few more things in the else here.

  • Only makes her it's it's higher.

  • So it's easier to see.

  • Um, so we're gonna do a few more things when you turn this off, so we're going to clear the color.

  • So this is a function.

  • This is how you call a function and Java script.

  • I still need to define this function, but let's say you're playing the game and one of the colors lit up when you turn the power off all the colors.

  • Should, um, all the lights should go off for these.

  • The colors, they're a lighter color should go back to the darker color.

  • And then the other thing we're gonna do is to clear an interval.

  • Now, I'm gonna be talking more about what this is more later when I define the interval.

  • But, um, I'll just put it here.

  • Now we're gonna come back to that.

  • Um, you can see we passed in this interval, I d So just keep this in mind When I come back to that later, I'm I'm going to explain that.

  • So we have the on button done.

  • The final thing is the start button.

  • So just like before, we're gonna do an ad event listener, and it's going to be a click event listener, and we're going to do a function and anonymous function in here.

  • It's an aero function.

  • And here we're going to just do one thing.

  • If this is the start button, and it should only do something if the game is on So if on or there's been a win, we're going to play, it's only go through this if on.

  • Um, this is just a shorthand way of saying if on equals true or when equals true.

  • Then call the play function, which we're gonna get to in a minute here it will play the game.

  • So will you pick click the start button and we're gonna play the game.

  • So that's what we're gonna do now is define the play function.

  • So this is how s O.

  • This is one way to define a function.

  • This is called aero function that was introduced in E s six.

  • There's another way to define a function.

  • A more common way is like this.

  • I'm gonna use the function key word function, play, and I'm going to, um so this is the name of the function.

  • This is anything we're gonna pass in to the function, which is gonna be nothing in this case.

  • And then the whole function is gonna take place within these curly braces here.

  • So, um, first of all, I'm gonna go up and actually add some semi goings to these here and at the When we first tried to play the game, the first thing we want to do is reset variables.

  • Now, if it's the first time you've played the game, the variables will already be reset.

  • But if you're playing again, we just wanna make sure the variables have been reset.

  • So let's say we're gonna set when t equal false because you're starting the game.

  • You have one.

  • Yet order is going to equal empty, empty array.

  • We're gonna fill that in a second here, Um, the player order again is going to equal empty array.

  • The flash is going to equal zero that there's been zero flashes.

  • Um, Interval I d equal zero.

  • Um, screw a little bit.

  • Here.

  • Term equals one turn counter dot inner html equals one.

  • So that's this county's gonna goto one as soon as you press start because we're on the first round of the game.

  • Um, well, so we're gonna have umm good equals True.

  • That means the player hasn't hit anything incorrect yet.

  • And now we're going to use a four loop to fill up this order.

  • So we need to randomly fill up this order, um, of this array here.

  • So we have four different quadrants.

  • So we're gonna fill up the this array with a random series of numbers to indicate the order that these air going to light up in the game.

  • So this is how we're gonna do the four loop for far.

  • So when you were four loop, you pass in three different things.

  • You initialize a variable, I equals zero.

  • We're going to loop until I is.

  • If we're going to loop while I is less than 20 and then at the end of the loop, we're going to increment I so it's going to go through this loop 20 times because in this game you have to get 20.

  • You have to do 20 rounds before you win the game.

  • So this is how we're going to fill the array order dot push math, top floor, math dot random times four and then plus one.

  • So this whole line basically gets a random numbers between one and four and fill this fills us array.

  • So this the array, we're going to push something onto the array and what we're gonna push on to the ray.

  • We have this a random number.

  • This gets a random decimal.

  • But when we multiply it by four and then add one here, Admit gets number between one and five, but it could still be a decimal.

  • So, math, that floor is going to round it down so we can actually see what happens here by just doing another council that log some of your council that log.

  • And I'm just going to log order.

  • So if we go into our page here and I'm going to open up the job counsel you with the power button and the start button, you will see that we have this array.

  • This is a rare and the consul, and it's just 20 numbers when random numbers between one and four.

  • Um so that's how we have made this array that's going to show the order that those lights are gonna flash in the game.

  • Let's go back over here and we'll set the comp turn to equal.

  • True, it starts with the computer flashing lights, and then the player has to match those lights, and then we start the first turn.

  • So this, um I'm gonna just typing that.

  • I'll explain what it means.

  • The interval I d equal set, Interval game, turn 800.

  • So let me break this down.

  • Um, set interval Means is going to run a function after every certain amount of milliseconds.

  • So sent Interval.

  • It's gonna run the game, turn function every 800 milliseconds that's gonna make it.

  • So the computer flashes a light on here every 800 milliseconds, and it's gonna keep repeating until this interval is cleared.

  • Now, the intervals gonna clear After all, the lights have flashed.

  • But I haven't I haven't done that yet, but what we need this interval I d year.

  • Um, that's what we set up here, and we have it down here.

  • The reason why we have an interval idea so we can clear the interval.

  • So I told you, we're gonna come back up to hear if you turn the power off Clear interval.

  • That means it's going to stop Simon from flashing the colors because it's going to stop.

  • Um, running the game turn function every 800 milliseconds and the game turned function is what flashes the colors.

  • So this starts the game.

  • So now that we have referenced the game turn function, let's define the game turn function.

  • Okay, the game turned function.

  • So I define the function the same way function, game, turn.

  • And then we're going to say what's gonna happen when this function is called so on is going to equal false.

  • Now, this is just a tricky thing where whenever on equals false, the player cannot click any of these buttons.

  • So while the computer is flashing colors, we don't want the player to be able to click any of the buttons.

  • So that's why honest set a false here s o.

  • Now let's see if flash equals can.

  • Then we're gonna do something.

  • So that means if the number of times the lights have flashed equals the turn that were on the turn shows right here.

  • Then that means the computers turn us over.

  • So if the computers turn is over, we're going to, um, clear the interval in a bit.

  • Will will show what happens if the computers turn is not over.

  • But if it's done flashing all the lights like that, we're gonna clear the interval.

  • That is what we talked about earlier.

  • With the interval I d comp turn is going to equal false and then we're going to clear the color if any colors air are lit up and on is going to equal True.

  • Now the honor is equal to true.

  • That means the player can now start pressing buttons on there.

  • So down here, if if it is the computers turn, then we'll do these things.

  • If the turns not over.

  • If it's the first thing we're gonna do, clear the color cause we're gonna clear the color from the last time the computer lit up something.

  • Oh, and that reminds me.

  • I forgot to put the parentheses on the end.

  • Whenever you wanna call function here to put parentheses on the end of the name there so could a color.

  • And now I'm going to set a time out.

  • Set time out is very similar to set interval eso set intervals.

  • Gonna keep repeating this over and over.

  • Set time out is going to do something once after a certain number of milliseconds.

  • So again, I'm going to use one of these arrow functions from E s six and it's going to run this function after a certain number of milliseconds, so I'll put the milliseconds at the end, so I haven't defined what the function is, but you can see here that, um every for it's gonna wait 200 milliseconds and then do what's what's in here.

  • So what's in here is it's going to flash a number or a color.

  • So we have to know what colors is Gonna flash is gonna flash the green, red, yellow or blue.

  • So we're gonna have an if statement here.

  • So if order flash equals one.

  • Okay, let me explain this line here.

  • So order is an array, and so flash is the number of times we've flashed a color and flash starts at zero.

  • And so zero is the first item in the array.

  • So if the first item in the array is one and remember, this array is an array of random numbers 1234 So if the first time is one we're going to run the one function which we have to define later that's gonna flash the the green right here.

  • And I'm just gonna copy this line, and we're going to do it three more times.

  • Another thing is normally with an if statement here, you're going to put in these parentheses what the condition is, and then you're gonna have a curly brackets, and you're gonna run the things in the curly brackets if the condition is true.

  • But you can also skip the curly brackets and just put the thing to run if the condition is true, right after the parentheses.

  • So inside the parentheses is the condition, and then right afterwards is the function you're gonna run.

  • If this condition is true, So this is gonna be 234 and these are gonna be functions too.

  • Three, four.

  • So it's gonna run the function of Ping on what the number is, and then the last thing that has to happen is the flash has to be incremental.

  • Did eso it started zero, but it's gonna go up one every time the computer flashes.

  • This happens after 200 milliseconds.

  • Because of this set interval up here, a light is goingto flash every 800 milliseconds.

  • But since we have this 200 milliseconds here, that means it's going to stop flashing for 200 milliseconds, and that's gonna flash again to the end of the 800 milliseconds.

  • Well, I can demonstrate it by just making these functions here.

  • The 1234 functions.

  • And we can actually run this and see what happens.

  • So I'm gonna make the function one, and we don't.

  • We're not gonna pass in things any of these functions.

  • That's why this always empty here and now we're going to say, if noise and so if it's if it should make a noise, then it will make a noise.

  • When we had these sounds here and html and now we're going to use them in her JavaScript.

  • So far, audio equals document dot Get element.

  • Bye.

  • I d clip one.

  • So this get element by i d is pretty much is very similar to the query selector, but quick query selector You can use any CSS selector and get element by a d.

  • You just put an idea in here.

  • But since we already know it's an idea, we don't have to put hash mark now.

  • I could have put query selector and then put hash mark clip one.

  • But I just want to show you a different way.

  • You could do this.

  • Now, This idea clip one is right here.

  • If we go back to our Asian Male clip one here.

  • So we're now getting this sound effect.

  • And on the next line, we're going to play it by doing audio dot play.

  • Now, we could have put this at the top.

  • Um, but I just decided to do it this way.

  • We're it's it's right in here instead of appear with the rest.

  • Um, and actually, I'm gonna make one more change right quick instead of our This will be a Let already told you that that we don't really use of our anymore.

  • It's gonna play the sound.

  • I'm gonna set the noise to equal.

  • True, just in case it was set to false.

  • Now you'll understand more of this noise thing later when I When I show you when the noise set to false and then top top left that style that background color equals light, Corinne.

  • Okay, let me break this down for you.

  • So top left is this thing right here that we got with the query selector style means we're going to change the CSS style.

  • You can change CSS using Java script, and we're going to you change the background color element.

  • Right now, background color is green, but we're gonna set it to light green.

  • I go into my style and I find top left.

  • Actually, the background is dark green.

  • And then I set the background color to be light green.

  • So we have this thing here for what happens when you click with one.

  • Now it's gonna be a pretty similar for 23 and four.

  • So I'm just gonna copy this, and that's gonna be 234 So I'm gonna change this to to And this is clip, too.

  • And the color is gonna be tomato now, just so you know, I could be using hex codes or some other way to indicate a color, but ht Mel has a set of key words that you can use to specify certain colors.

  • So tomato is a key word to signify a specific color in job in a c M l that you will see soon when I do the example.

  • So this is gonna be three.

  • And set that three.

  • Um, four said that four, This color is going to be yellow, and then this color is going to be a light sky blue.

  • Okay, I think we have enough code here to do an example.

  • So let me save that refresh here.

  • And if I press start, okay, it's not working.

  • Let's check the console.

  • Clear colors not defined.

  • Oh, yeah.

  • So I I just spelled this wrong.

  • So let's see.

  • Clear a letter and then I'll refresh.

  • Now let's try see if I get any clear, okay?

  • It's still I spell the wrong, But the another problem is that I actually never find that function.

  • So let's do that right now.

  • I'm gonna go down here Clear color.

  • Oh, to put the word function so function clear color.

  • I'm gonna use a line very similar to this.

  • Where says top left a style that background politicals light green.

  • So top left that style that background color equals dark green.

  • Because that's the initial color that we specified in the CSS.

  • And then saying with this on, um, tough we got Oh, now I'm seeing another problem.

  • Now that I'm actually copping a pacing, we have top left here.

  • This is supposed to be okay, We got top left here.

  • This is supposed to be a top, right?

  • This is supposed to be bottom left, and this is supposed to be bottom, right?

  • So now I can continue to copy and paste, and I'll be getting the right code here.

  • So let's see.

  • This instead of tomato is going to be dark red and them the bottom left instead of yellow is gonna be a golden rod.

  • See these air, all HTML color names and then the bottom right instead of light, sky blue is going to be dark blue.

  • Okay, let's see if we're going to get any theaters.

  • Now.

  • I'll power this on press start and it seems to be working.

  • So let me close that for now, so he could see it just went, and but it never really went back.

  • Mmm.

  • So let's see if we've done something wrong.

  • I was expecting the color to turn off at some point.

  • Oh, but the problem was they put dark color here instead of dark green.

  • So it never turned back to Dark green.

  • So let's try it again.

  • I'll save this refresh and start this.

  • Okay, so now it turned green.

  • But now I can't click it, though, so I'm trying to click here to show that I know that this just lit up.

  • But that's not working because we haven't programmed that yet.

  • So that's the next thing we're gonna program.

  • So to make it so a player can click these colors, we're going to have to have some more event listeners.

  • So let me move this code up a little bit, and I'm going to add some or event listener.

  • So top left that even that ad event listener and this is gonna be another click event listener because we're just look listening for the user to click the top left and just like before event.

  • So now we're going to check if it's on.

  • We only want a user villa.

  • Click there if the program is on, Um, so if it is on, will do a few things the player order that push one Remember player order is an array of the sections that the player has clicked or pressed.

  • So if the player puts us here, we're gonna push one onto the player or array.

  • Then we're going to check if the player was correct.

  • This is gonna be a function to check to see if the player was right Now whether or not the player was right, we're going to call the one function now.

  • This is the same function that the computer called.

  • That just lights up that color.

  • I am now.

  • If the player has not won, if the players not one yet, then we're gonna set the time out and we're going to clear the color.

  • So after 300 milliseconds, we're going to make sure the color is clear that the color that the user clicked is going to clear.

  • After 300 milliseconds.

  • Let's actually test this out.

  • But I'm going to comment out this checking if if they've won because we haven't made that function yet.

  • So let me refresh here.

  • Oh, actually, only works if I click up here.

  • Yep, that worked, except the current clear because I forgot to put parentheses here, so let me just try again to make sure everything works.

  • Okay?

  • We got that one toe work, and now, uh, well, let me uncommon this.

  • I'm just gonna do some copying and pasting because we're gonna do this for every corner.

  • So we got top left.

  • No, I'm gonna do a top, right.

  • And for top, right?

  • We're going to push on the number two.

  • I want to call the to function, but everything else will be the same.

  • So now we have bottom left bottom push on a three.

  • This is me three.

  • Everything else is the same.

  • Now we have bottom, right, Push on a four four and everything is the same.

  • And now we just need our check function, these air all calling a cheque function.

  • And this will be one of our more complicated functions.

  • So let's get to it.

  • Function check.

  • And so we're gonna check if if it's correct.

  • So if player order in these brackets, I'll put player order that length minus one.

  • That just means the last thing that the player clicked.

  • If the player order does not equal the rial order at player order, dot linked last one.

  • The last thing that the player clicked then good equals false.

  • So if they're not the same good equals false rumor, good is true with the players getting everything correct and good as false.

  • If the player has something incorrect, we'll just put that on to the next lines.

  • So it's easier to read.

  • And so now if Player ordered out length equals equals 20 that means they've gotten every singles one right and good.

  • That means they haven't missed anything.

  • If good equals true now it could have just put and good I didn't have to put good equals True.

  • In fact, I will just do that.

  • If Ron the 20th round and good, we haven't done anything wrong, then we'll call the win game function.

  • If good equals false, that means the player got something wrong.

  • We're gonna do something else.

  • So first, gonna flash the color.

  • This is a function that we saw it too, right?

  • Then the turn counter dot inner html is going to equal No, that just means right in here.

  • It's gonna say no.

  • And then the next thing that's gonna happen is that we're gonna set a time out, and then the turn counter is going to be set back to the turn number.

  • So it was the turn counter was set to know.

  • But after a certain amount of milliseconds is gonna go back to the turn number and then also, we're going to clear the color.

  • So we're flashing the color here, and then we're gonna clear the color here after a certain number of milliseconds, and I'll just put those milliseconds, which is gonna be 800 milliseconds.

  • We're also gonna do something special if we're in strict mode here.

  • So if strict it for in strict mode and I called a play function and when her play function is just like repeating the whole game, starting the game over with else, if we're not in strict mode, then we can repeat the round.

  • So let me set some things up here to repeat the round.

  • So if you get something wrong than the roundest repeated, if you're not in strict mode, so calm turn is going to equal true and the flash is going to equal zero, and the player order is going to be an empty array.

  • Good equals true because we now we're back to having nothing wrong because the round it just started and an ellipse interval I d equals set interval.

  • So we're setting We're starting this just like we did before in the play function is with a set interval by calling game turn with 800 every 800 milliseconds And then the final thing that's gonna be in this function is noise equals false.

  • So so remember, we're still in the good equals false if statement.

  • So if the player got something wrong, we're not gonna play noise.

  • Noise is going to equal false.

  • They you only play the noise.

  • If the player got the right, it clicked on the right thing.

  • So now we can see up here.

  • Um, why we checked to see if noise is true.

  • We have a few conditions so far, the condition if the player wins the game, the condition if the player is wrong.

  • But now there's the condition.

  • If the player got it correct but has not won the game yet, So that's the condition I'm gonna do now.

  • So if Kern equals player, order that length and good and we haven't won yet, then term plus Plus we've now gone to the next turn.

  • Um, and we're gonna clear the player order for the next term to begin.

  • Um, comp turn is going to equal true flashes going to equal zero.

  • Um, turn counter.

  • The inner a CML equals turned.

  • Now there's there's some ah, code that's repeated from here to here, So there could be some room for re factoring here.

  • Um, an Interval buddy.

  • Equal set interval game turned again.

  • This would be a prime place for Samara factoring since we have some repeated code here and that's it for this check function.

  • But within the cheque function we referenced to functions haven't been created yet.

  • The wing game function and the flash color function.

  • So let's do those now First, a flash color function.

  • It's going to be very similar to the Czech color function so similar that I'm gonna start by copying and pasting.

  • So the flash color to flash color we're going to just set these to the other colors so light green and then said dark red is going to be tomato.

  • Um, it's a golden rod will have yellow and this is me light Skype a little, and then the other function is the wind game function.

  • I'll put that at the end, so function when game.

  • And so when the game.

  • If someone has won the game first, we'll do the flash color function that I just defined here.

  • Whoops.

  • And then we're gonna do turn counter dot inner html is going to be set to when and on is going to equal false.

  • So now a user can't click anything because on his equal, false and then when is equal to true?

  • Because the game has been won and that actually should be the full game.

  • So let's do some tests to see if anything goes wrong.

  • So just saved.

  • I'm going Thio refresh here.

  • Let's see.

  • We'll hit the power button start.

  • Okay, Killer.

  • See what happens when you press the wrong thing says no and that goes back.

  • Who?

  • It was supposed to play this again, but it didn't.

  • So let's see what went wrong.

  • Okay, I have a spelling error here.

  • So this said sticked should say strict.

  • I'm sure some of the viewers probably saw that I spelled it wrong.

  • Okay, so that was supposed to be, if strict.

  • So let's say that, um refresh Ah, let's see what happens here.

  • Okay, good.

  • It replayed from where I lost.

  • Now, if I should turn under strict mode If I get something wrong, it should replay from the beginning to start a new game if I get something wrong.

  • Yep.

  • It went down toe one.

  • It just started a new game.

  • Now let's test what happens if we win.

  • Now, I'm not gonna sit here and go through 20 rounds to see what happens when I win.

  • I'm going to just make some change in the code here.

  • So instead of length equal 20 I'm gonna change this toe lengthy, equal three.

  • So we can check what happens after three rounds for winning the game here, comes up and see all them light up, and it says win right there.

  • And so we should be able to start again by just pressing the start button.

  • Yeah, and then if we don't turn, we can turn it off anytime.

  • See?

  • A quick the power, but in the light went out and then this went out.

  • So we have just created an entire Simon game.

  • Everything works.

  • I talked about some areas of reef, factoring that you may be a little work on on your own.

  • You can make the design a little better.

  • Um, some of this code that was repeated.

  • We can try toe, not have repeated code.

  • And I saw some other places, like right here.

  • If cop turned, this could really even be in l statement.

  • And you may be left.

  • Figure out a way to not even have the comp turn variable.

  • But that's something you can try to figure out on your own.

  • Thanks for watching again.

  • My name is Bo Kearns.

  • Don't forget to subscribe.

  • And remember, use your code for good.

Today, I am going to show you how to make a Simon game, using mainly JavaScript but also HTML and CSS.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it