Placeholder Image

字幕列表 影片播放

  • Hello, everyone.

  • In today's video, we're gonna be taking a look at making a speed typing game, which is a ton of fun to make and also incredibly educational, since it teaches you concepts such as dominant relation A P I calls timers and so much more.

  • So let's get started now.

  • Welcome back to whether to have simplified.

  • My name is Kyle, and my job is to simplify the web for you.

  • So that sounds interesting.

  • Make sure you subscribe to my channel for more videos just like this one.

  • Now, as you can see on the right here I have the finished product.

  • We have a timer counting up.

  • We have an area we can enter the text in that were typing.

  • It will tell us whether we're right, whether we're wrong and if we type the entire thing correctly, it's gonna move on to the next question for us.

  • So to get started with this, the very first thing we need to do is create an index dot html file where we can put all the html And if we just type exclamation point and hit enter, it's going to generate all the boy over a plate for us.

  • And here we can just give it a title of speed typing.

  • And then, since we're gonna need CSS and we're gonna need javascript, we're gonna need a link tag for our CSS.

  • And we're just gonna creative style sheet called style styles Nazi SS and then also a script tag here.

  • And we're gonna call this script dot Js We need to make sure we defer This is that it loads after the body of our HTML.

  • I have a video on this Differ, keyword, I'll leave in the cards and the description below.

  • So check that out if you're confused on that topic.

  • But that's all that out of the way.

  • Now we can actually move on to the HTML and inside this HTML, we need to put our timer.

  • But you can see here the html for our container, our input as well.

  • Those are quote text right here.

  • So the very first thing we want to do is that timer We can just create a div with the class of timer and we're also going to give it an idea of timer so we can access that easily in our javascript and for now, we'll just put in the text of zero so that we actually can see what's going on.

  • Next, we're gonna need a container Div And inside of this container, Div is everything in this yellow section you can see here.

  • So first we're gonna have a div and this is going to be for a quote display, and we're also gonna want to give that an I D, which is again going to be quote display.

  • And that's just so we can access that in the job a script and for now, let's just give it the text of quote so we can actually see what's going on.

  • Next, we need a text area, which is going to be this content inside of here.

  • And we want this here.

  • We don't really need to worry about a name.

  • We wanna have this say, quote input as their i.

  • D.

  • We don't need columns or rose, and then we can put a class which is going to be quote input and we'll just put a hyphen amerisource CSS like.

  • And for now, we'll put the content of quote inside of here just so we have something to play with and now with all that out of the way, that's all the HTML that we're gonna need.

  • Actually, one last thing is, let's make this auto focus.

  • That just means as soon as we load our page, are cursors automatically gonna focus inside that box, which will make starting the typing incredibly easy.

  • Now we can open this with live server by right clicking and clicking open with live server, which is an extension for visual studio code I have downloaded.

  • So if you don't see that, make sure you download that extension.

  • And as you can see, we have our timer or quote as well as our input, where we can type whatever we want inside of it.

  • Obviously it looks nothing like this, but we can get started on making it look like that by adding in our styles.

  • Does CSS, where we're gonna put all the styles for our entire application?

  • Very first thing I like to do is to make my box sizing to be border box so we could come in here change our box, sizing the border box, this mix working with wits.

  • Patten's margins borders so much easier.

  • And the next thing I want to do is work on styling our body because, as you can see, over here, we have our stuff in this center.

  • So we want to make sure our container is centered instead of our body.

  • So we can change this to be a display of flex and justify the content in the center.

  • And we can align the items in the center and then if we put a min height of 100 view height that's going to center are content as you can see, and then to make it so that our scroll bar does not exist.

  • We could just remove the margin by setting the margin to zero.

  • And that's going to get rid of the scroll bar for us now to make the background look the same as the background over here, we could just put on the background color.

  • I just have this copied over This is a purple li background color.

  • And if we say that you now see, we have that purple looking background color also are font is going to be slightly different over here.

  • So what I want to do is I want to select our body as well as the quote input.

  • So we have quote input and I want to change the font family for this.

  • And we're just gonna use something like this and Gil Sands here.

  • And if we say you can see now we have a slightly more interesting farm toe Look at its nothing amazing, but at least it's something a little bit different now.

  • Next, I'm gonna work on our container, and this container is this yellow color that you see here.

  • We want to make sure that we sent to this inside of our screen make that yellow color and do all that fun stuff.

  • So I'm gonna copy over that color so we can go background color.

  • This is going to be a yellowy color.

  • And if we say that, you could see that yellow color is showing up.

  • But obviously everything is really scrunched up, so we're gonna add a padding of one R e m that pushes everything away from each other.

  • Looks a lot better Already will also add a slight border radius Would issues 10.5 r e m.

  • Just to give it rounded corners.

  • I think it looks a little bit better, in my opinion.

  • Also, we're gonna set a default with of 700 pixels, and obviously this is kind of large.

  • So we're also going to set a max with and this Max.

  • But there's just going to be 90% of the screen size that we have never expands too far past the edge of our screen.

  • And the last thing we want to do is actually work on making our timer so it doesn't show up right next to it.

  • Here.

  • We want a timer to show in the top section so we can just select our timer.

  • And what we want to do is position it absolutely so we can say position absolute and we'll just say top of three r e m, for example.

  • And as you can see, that's pushing our timer all the way up to the very top of our page.

  • Let's actually make that two R E m.

  • So it's a little bit closer to the top, and we're gonna make the font size.

  • Huge will say three R E m.

  • And we'll also change the color to be exactly the same as our background color for a container.

  • And now if I say you can see we have this really large timer at the top and to make it stand out a little bit more.

  • Let's just make that fart way too bold.

  • Okay, that looks really good right there.

  • Obviously, it's not counting up.

  • We need the JavaScript to do that.

  • But for now, that looks really great.

  • Now, the next thing that we should probably work on is this quote display right here.

  • All we want to do is just space it out a little bit.

  • So we'll say quote display.

  • And all we want to do is add a margin on the bottom.

  • We'll do one R E m.

  • Let's just make sure I spell quote properly.

  • There we go.

  • That just gives us a little bit of space between our input and our actual text here that we should be typing.

  • It looks a little bit more pleasing, and now we can move on to styling the input, which is going to be the most complex styling for this entire application.

  • The first thing I want to do, they want to remove the background color here.

  • So we're just gonna set that too transparent and I want to set the border to be two pixels solid and issue is going to be a slightly darker version of the background color that we have here.

  • I just copied this over.

  • So if we save, you can see that it's just a slightly darker color of this yellow.

  • That way it doesn't stand out too much.

  • It kind of just blends in with the background, which is really what I want.

  • Also, as you noticed when we selected, we get this really annoying blue outline.

  • So I'm just gonna disable the outline by setting it to none.

  • But if you do this, it's impossible to tell that this is clicked on.

  • There's no focus state.

  • So what we need to do also is make sure we select our quote input, and we want to select for the focus state.

  • And here we're gonna put a border color, which is just going to be a black That way.

  • When we click on it, you can see that turns black.

  • So that way we know we're focused inside of that element.

  • It really just makes it easier for people to know where the focus is.

  • And if you ever disable outline, you really need to make sure that you highlight this focus state to make sure that it actually is working.

  • How you want it to next.

  • We want to work on sizing this.

  • So one thing led to is obviously make it take up 100% of.

  • But with that way, it's kind of fill the entire container.

  • We're also going to give it a height.

  • Will just say eight R E m That way.

  • It's really large That way.

  • If we have any large text, it'll easily fit inside of this box.

  • Also, we're gonna set the margin to auto.

  • That's just going to make sure that this always stay centered.

  • If we set resized to none, you'll notice.

  • Right now I can resize this box.

  • But if we set resized to none, it's going to prevent me for re sizing that.

  • So let's refresh and you can see that drag box is no longer here, so I can no longer resize our input, which is great.

  • Next, let's make its letter.

  • Text isn't quite so pushed up against the edge.

  • We can do that.

  • But a little bit of Patty would you say 0.5 are Ian and one r e M as you can see that already looks a lot better.

  • It makes our text a little bit more inside the center.

  • Also, you noticed that our text up here is not in line with our techs down here, so we should probably fix that and the easiest way to do that it's just set some negative margins.

  • So we can say margin left.

  • And we want to calculate this.

  • It's going to be our left patting, which is negative one r e m.

  • So we just want the opposite of this.

  • And we also want to subtract out our border so minus two pixels.

  • And now if we save that and we do the exact same thing for a margin, right.

  • But of course, instead of negative, we want these to be positive.

  • So we want one r e m plus two pixels and here one r e m plus do pixels.

  • You can now see that this text has exactly lined up with the text inside of our input, and it just makes it easier for more typing to know exactly where everything is gonna be.

  • You also noticed the font size here is slightly smaller inner input.

  • So we can just change our font size to be one R E m.

  • And that is exactly the same font sizes out here.

  • And now everything lines up 1 to 1, which makes it easier for the person typing to see exactly where they are.

  • Inside of this quote, the last thing that we have left to do is should just set a border radius so we can say border radius 0.5 r E m.

  • That gives us these nice rounded corners, and I think that just looks a lot better.

  • And that's the entirety of our CSS that we're going to need to worry about.

  • And we can finally move on to the most fun part, in my opinion, which is going to be the JavaScript.

  • So let's create that script at J s, which we already linked up here instead of her index.

  • And inside of here is where we can take care of all of our JavaScript fund.

  • The very first thing we need to do is figure out how we're gonna get our quotes into our text box here, and there's a free a p I that we can use called a p i dot quotable that I owe you go to slash random and every time we access as you are, well, we're gonna get a random quote inside of this content attributes.

  • So, for example, here's a quote for Do it again.

  • We're gonna get a different quote so we can just use that a p I So it's copy that, and we'll credit constant variable to store that you're oh, so it's a random quote, a p i euro.

  • It's going to be equal to that quote a p I and you'll build to get this from the get help repo linked in the description if you want.

  • Now that we have that done we need to do is actually get our a p i quote, so we can just create a function which we could just call get random quote and inside of this function of what we want to do is we want to use the fetch a p I with If if you're unfamiliar with this, fetch a P I.

  • I have an entire video covering fetch a p I.

  • So just make sure you check that out Lincoln, the cards and a description down below.

  • But essentially we just need to put in the URL for our a p i, which is this euro.

  • And then we want to call the dot then property because this returns a promise, and inside here, we're going to get a response object.

  • And to convert that to Jason, we just want to say response dot Jason, this also returns a promise.

  • So we need a separate then inside of here.

  • And this is our data.

  • So there's a data attributes is essentially what we see right here.

  • And we want to get this content key so we can just say data dot content and we just want to make sure we return this and we're gonna do this in an asynchronous function in order to actually handle this.

  • So we can just say, for example, function get next quote just like this, and we're gonna make sure this is an a sink function.

  • And then inside of here, we can call, get random quote, and if we just put the awake, he would in front of here.

  • We can actually get our quote, for example, like this.

  • And then we can just log out that coat quote so you can say concert out log of quote.

  • And now if we call that function, which is get next quote and we can come over to our game.

  • We inspected this element and we go to the council.

  • You can see that the quote is being printed out.

  • And if we were to refresh this, you can see we get a different quote being printed up.

  • And every single time we go to the page and refresh, we're getting a brand new coat quote being generated every single time.

  • So that's how our get random quote, functions, working.

  • And if you're unfamiliar with this promise, syntax, where the awaits syntax.

  • I have videos both on promises and a single weight in the cards and description that you can check out.

  • Now, the next thing we need to do is actually change this function.

  • We're gonna call this a rendered new quote because we're actually going to render our quote inside this function, and we're gonna make sure you call this right at the beginning like this and not what we want to do is get our elements.

  • We have our quote display where we're gonna show our quote and unquote in foot where we're going to type are quote.

  • So it's actually going to our job script and we want to get a constant variable, which we're just gonna call quote display element.

  • We're gonna set that to document that get element by i d.

  • And then it just quote display like that.

  • And this is going to be our quote to spay element.

  • And we can actually just come in here, say that our inner text is going to be equal to that quote.

  • And now every time we refresh, you can see that are quote is being put inside of here, and it's always going to be a different quote, which is great.

  • Now also, what we need to do is make sure we clear out this input.

  • So we want to get our quote input element.

  • And this over here is just quote input like that and we want to do is just cleared out.

  • So we can say quote input element dot value is going to be equal to know.

  • And as you can see, that clears out the value inside of this input every single time that we get a new quote, which is exactly what we want now one thing is that we can't actually just put the inter text of our quote display element to the quote because, as you remember, over here, we changed the color of each individual letter based on whether or not the letter was correct were incorrect.

  • So in order to do that, we actually need an individual element for each character of our quote.

  • So to do that, we actually need to go through a loop so we can say quote dot split and we can say what we want to split on.

  • If we pass in an empty string, it's going to convert our string to an array.

  • Were each individual character is one element in that array, so we can then look over that array by saying for each character, Then we can do something for each one of these different characters, and we want to create a new element.

  • We're gonna call this our character span, and it's going to be document dot create element on.

  • We're gonna create a span, and then we can take that span.

  • We can set the inner text to be equal to that character.

  • So essentially what we're doing here is we're getting each individual character in our string, creating a span for it and then setting the text of that span to that individual character.

  • And then what we can do is we can take our quote display element and we can just upend Ah, child of that character span.

  • And if we come in here and just make sure that we clear out our inner HTML by setting it to a blank string every time, now what we can do and also this needs to be in her text of character like that direction you just set to the character text.

  • I talked about it, but never actually did it.

  • Now, if we say that, of course everything is not working, and this because inter text is obviously not a function, I need to set the inner text like this instead of calling a function.

  • And now, as you can see, everything is working over here.

  • But if I actually inspect this element, you'll notice that we have a bunch different spans.

  • As you could see, each span is one individual letter which is going to make it so we can apply individual colors to each one of our letters has needed, which is perfect.

  • We can actually test that by just coming in here and saying that we want our character span dot class list dot ad and we're just gonna add a class of correct.

  • This is going to be what our element looks like.

  • If we have the correct letter, that green color instead of our style sheet, we could just come in here and say that we want to set the color to green.

  • As you can see, everything is green if it has a class of correct.

  • If we want to test for incorrect, that's going to be the incorrect class and inside of here.

  • Obviously, we can just get incorrect.

  • And what we want to do is we want to set the color to be read, as you can see here.

  • And we're also gonna set the text decoration to underline that way it really sticks out if they type something in incorrectly.

  • With this giant red underlined word, it's really easy to see.

  • Obviously, we don't want these classes to be set right away, so we're gonna remove that and it's going to start off with just the black text.

  • But as we type characters down here.

  • We're gonna want to do things by checking to see if these characters air.

  • Correct.

  • So to do that, we need to set up an event listener so we can say quote input element dot ad event listener We wanna add on the event listener input and essentially, this input element or event listener gets called every single time something inside of here changes, and we can actually see that by just saying, consul dot Log changed.

  • And if we inspect our page, move this way off to the side and I start typing in here.

  • You can see every single time I type something will remove something.

  • It's walking out this change text, so let's remove that and obviously implement what we need to do.

  • But before that, we should think about exactly what this function is going to do.

  • Essentially, we want to loop over all of the different characters investor ray here, all of our different spans, and we want to compare each individual character to the individual character in this input based on their positions.

  • The first character here compared to the first character here, if they're the same, we have the correct class.

  • And if they're different, we add the incorrect class.

  • And if, for example, here we only have five characters.

  • So this character here, this zero or this Oh, we don't want to apply anything to it because we haven't actually typed that far yet.

  • We just want to leave that completely plain and black.

  • So to do that, all we need to do is first to get all of our different spans.

  • So to do that, we know that they're inside of our quote display element.

  • And we can do a query selector, all four spans.

  • This is going to get every span that inside of her quote display element, essentially every single one of these characters.

  • And we can just set that here too, a variable, which we're just gonna call array of quote that we know that this is our actual quote and then we want to do a very similar thing.

  • We're gonna get another array.

  • This one is going to be for our value, and it's going to come from our input element.

  • And we want to get the value of that.

  • And we just want to split that on an empty string again.

  • same thing.

  • We're gonna convert our string to an array of each individual character.

  • And now all we need to do is look through our away array quote so we can say array.

  • Quote that for each and we wanted to this for each character inside of this.

  • And we want to make sure we have access to the index, and this is actually going to be a SPAN will call it character span.

  • So we know that this is our span.

  • And the second parameter here is just the index.

  • So the first character is gonna be zero second is 1/3 is going to be too, and so on.

  • So with that index, we can actually get the character from our value by saying already of value.

  • And we want to get the index.

  • Essentially, the first character here is going to be the first character inside of this quote.

  • Second one's gonna be the 2nd 1 of the quote and so on.

  • It allows us to match up the characters.

  • Then we could just do a check.

  • We can say if character is equal to character span dot inner text.

  • So if they're exactly the same, we want to do is take our character span.

  • We're gonna get the class list we want to add in the correct class.

  • But we also need to make sure that we remove the incorrect class is that it never shows up is incorrect and correct at the same time.

  • Obviously, that's not possible.

  • Otherwise, what we want to dio is the exact opposite.

  • For example, if they're not equal to each other, we want to remove the correct Kanis and add the incorrect class.

  • Now, if I start typing, you can see when I take the D.

  • This shows up green.

  • But all of this other stuff is showing up a red, and this is because we're not checking to see if their character is no.

  • So we need to have another check here, which is character equals no, and what this means is we haven't typed that character yet, And if we haven't typed the character yet, we want to remove the incorrect class and remove the correct class because we haven't gotten there yet, and then we can change this toe else.

  • If so, essentially, what we're saying is if we have not typed this character than just make a black remove all of our classes if we have typed it and it's the same as our other character thing.

  • We want to add the correct class.

  • And if we haven't have we have typed it and it's not the same.

  • We want to add the incorrect class.

  • Now, if I say that and I start typing, you can see that is working.

  • But if I type something incorrect like a D here, you can see it's going to underline that everything else like type incorrectly is gonna be underlined in red.

  • And all of the correct stuff is going to be highlighted in green.

  • But if I get to the point where I typed everything out correctly, I want to move on to the next quote because obviously it doesn't make sense to keep typing after I got it all.

  • Correct.

  • So what we need to do is actually check here, we can create a variable.

  • We're gonna call this correct, and we're going to set it to true.

  • So by default, we're assuming that everything is correct.

  • So every time something is incorrect, we want to change this very bolt of false.

  • So here we're gonna set the variable to false because we don't have a correct element.

  • We haven't typed that far yet.

  • And down here we're gonna set corrective falls because obviously we typed the wrong character and then all the way at the bottom.

  • If we do have everything correct.

  • So if this variable is true, we just wanted to surrender the next quote.

  • So say, render new quote.

  • No, If I start typing, nothing is going to happen even if I finish this all the way out.

  • But if I type this all the way, someone copy this.

  • And if I type this last period, you can see it clears everything out, grabs us a new quote.

  • And now we can start typing this.

  • And if I type all of that, it gives me a new quote and so on.

  • All we have left to do is to hook up our timer.

  • So in order to hook up timer up, it's actually going to be really simple.

  • We could just come down here and create a function.

  • Would reporters call start timer?

  • And the first thing we need to do is obviously get our timer element.

  • So if you remember, we created a new element with the I D of timer.

  • So we can just select that like this.

  • And as you remember in here, we have that idea of timer on our timer.

  • And now, instead of start timer, well, we need to do is we first want to set the timer to zero so we can set a timer element dot inner text is going to be equal to zero.

  • We just wanted to start at zero, and as you can see, that's working.

  • And even if we remove the default of zero instead of here, it's actually remove all of these default values.

  • We no longer need them.

  • You can see.

  • Of course, it's not showing up is zero.

  • And that's because we're not actually calling start timer.

  • So let's just make sure we call that start timer function in our render new clothes.

  • Every time we render a new coat quote, we want to restart our timer.

  • Now, if we say that you could see our timer is being set to zero, which is exactly what we want now inside of this timer.

  • What we need to do is just every single second we want to update our timer and we can use set interval for this because this takes a function and then it also takes a second parameter, which is how often you want to run that function in milliseconds.

  • So everyone, 1000 milliseconds or one second, we're gonna run this function.

  • But this has a slight problem because set interval is not exact.

  • It's not gonna run exactly every one second.

  • It might run every 1.1 2nd or 1.1 seconds.

  • It's not 100% accurate.

  • So in order to get around the fact that it's not accurate, we need to use a start date and we compare our start date toe our current date to get the actual time.

  • That's elapsed because we can't depend on this by just adding one each time.

  • So let's come in here and we're gonna get a variable called to start time.

  • And this is just going to be the date when we start our timer.

  • So inside of here we can set our start time to be equal to a new date, and this just gives us the current time, the current date, and now, instead of our set interval, we want to do is just wanna call a function called get timer Time just like that and we'll create that function down here.

  • Get time or time inside of here.

  • All we want to do is you want to take a new date.

  • This is going to be the current time and we want to subtract this from our start time and this is going to be in milliseconds, so we want to actually convert this two seconds.

  • So to do that, we just divide by 1000 and now this is going to give us the time in seconds.

  • But it might be a decimal.

  • It might be 1.111112 seconds.

  • So in order to make us that's always an imager, we could just say math dot floor.

  • This is always going to round down.

  • So if it's 1.111 is just going to be one fits 1.9, it'll be one you'll just always around down, which is exactly what we want.

  • And we just want to return that obviously.

  • And now all we need to do is set the text of our timer so we can say timer that dinner text is equal to that time we just got, and now you should see that this starts counting up and every single second it's going to take up one more.

  • And the reason that we know that it's accurate is because it's based on this start time.

  • And this current date, which is always accurate to the millisecond while set interval is not accurate, we can't depend on it.

  • So now let's assume I'm typing along.

  • I get all of this typed out, and all I have left to do is say it and period.

  • You can see it gives us a new quote.

  • It clears out our input and it restarts our timer.

  • So everything is working perfectly, and that's all there is to creating this speed typing game and JavaScript.

  • If you enjoy this video, make sure to check out my other project based videos linked over here and subscribe to the channel from more videos just like this one.

Hello, everyone.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

用JavaScript建立一個速度打字遊戲 - 教程 (Build A Speed Typing Game With JavaScript - Tutorial)

  • 10 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字