diff --git a/4-typing-game/typing-game/README.md b/4-typing-game/typing-game/README.md index 12e96c5d..3985d622 100644 --- a/4-typing-game/typing-game/README.md +++ b/4-typing-game/typing-game/README.md @@ -1,47 +1,94 @@ # Creating a game using events +Event-driven programming is the foundation of interactive web applications, and there's no better way to understand it than by building something fun and engaging. In this lesson, you'll create a typing speed game that responds to user interactions in real-time, teaching you how to handle events, manage application state, and create dynamic user experiences. + +By building this typing game, you'll discover how web browsers communicate with your JavaScript code through events, and how to write code that responds intelligently to user actions. You'll also learn essential programming patterns that form the backbone of modern web development, from simple button clicks to complex user interfaces. + +By the end of this lesson, you'll have created a fully functional typing game and gained the skills to build any interactive web application. Let's dive into the exciting world of event-driven programming and bring your web pages to life! + ## Pre-Lecture Quiz [Pre-lecture quiz](https://ff-quizzes.netlify.app/web/quiz/21) ## Event driven programming +User interaction is what makes web applications come alive, transforming static pages into dynamic, responsive experiences. Understanding how to capture and respond to user actions is fundamental to creating engaging web applications that feel intuitive and professional. + +The challenge we face as developers is that we can't predict when users will click buttons, type in text fields, or interact with our interface. This uncertainty requires a different programming approach than the sequential, step-by-step code you might be familiar with. + When creating a browser based application, we provide a graphical user interface (GUI) for the user to use when interacting with what we've built. The most common way to interact with the browser is through clicking and typing in various elements. The challenge we face as a developer is we don't know when they're going to perform these operations! [Event-driven programming](https://en.wikipedia.org/wiki/Event-driven_programming) is the name for the type of programming we need to do to create our GUI. If we break this phrase down a little bit, we see the core word here is **event**. [Event](https://www.merriam-webster.com/dictionary/event), according to Merriam-Webster, is defined as "something which happens". This describes our situation perfectly. We know something is going to happen for which we want to execute some code in response, but we don't know when it will take place. The way we mark a section of code we want to execute is by creating a function. When we think about [procedural programming](https://en.wikipedia.org/wiki/Procedural_programming), functions are called in a specific order. This same thing is going to be true with event driven programming. The difference is **how** the functions will be called. -To handle events (button clicking, typing, etc.), we register **event listeners**. An event listener is a function which listens for an event to occur and executes in response. Event listeners can update the UI, make calls to the server, or whatever else needs to be done in response to the user's action. We add an event listener by using [addEventListener](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener), and providing a function to execute. +To handle events (button clicking, typing, etc.), we register **event listeners**. An event listener is a function which listens for an event to occur and executes in response. Event listeners can update the UI, make calls to the server, or whatever else needs to be done in response to the user's action. We add an event listener by using `addEventListener()`, and providing a function to execute. + +**Here's how event listeners work:** +- **Listens** for specific user actions like clicks, keystrokes, or mouse movements +- **Executes** your custom code when the specified event occurs +- **Responds** immediately to user interactions, creating a seamless experience +- **Handles** multiple events on the same element using different listeners -> **NOTE:** It's worth highlighting there are numerous ways to create event listeners. You can use anonymous functions, or create named ones. You can use various shortcuts, like setting the `click` property, or using `addEventListener`. In our exercise we are going to focus on `addEventListener` and anonymous functions, as it's probably the most common technique web developers use. It's also the most flexible, as `addEventListener` works for all events, and the event name can be provided as a parameter. +> **NOTE:** It's worth highlighting there are numerous ways to create event listeners. You can use anonymous functions, or create named ones. You can use various shortcuts, like setting the `click` property, or using `addEventListener()`. In our exercise we are going to focus on `addEventListener()` and anonymous functions, as it's probably the most common technique web developers use. It's also the most flexible, as `addEventListener()` works for all events, and the event name can be provided as a parameter. ### Common events +While web browsers offer dozens of different events you can listen for, most interactive applications rely on just a handful of essential events. Understanding these core events will give you the foundation to build sophisticated user interactions. + There are [dozens of events](https://developer.mozilla.org/docs/Web/Events) available for you to listen to when creating an application. Basically anything a user does on a page raises an event, which gives you a lot of power to ensure they get the experience you desire. Fortunately, you'll normally only need a small handful of events. Here's a few common ones (including the two we'll use when creating our game): -- [click](https://developer.mozilla.org/docs/Web/API/Element/click_event): The user clicked on something, typically a button or hyperlink -- [contextmenu](https://developer.mozilla.org/docs/Web/API/Element/contextmenu_event): The user clicked the right mouse button -- [select](https://developer.mozilla.org/docs/Web/API/Element/select_event): The user highlighted some text -- [input](https://developer.mozilla.org/docs/Web/API/Element/input_event): The user input some text +| Event | Description | Common Use Cases | +|-------|-------------|------------------| +| `click` | The user clicked on something | Buttons, links, interactive elements | +| `contextmenu` | The user clicked the right mouse button | Custom right-click menus | +| `select` | The user highlighted some text | Text editing, copy operations | +| `input` | The user input some text | Form validation, real-time search | + +**Understanding these event types:** +- **Triggers** when users interact with specific elements on your page +- **Provides** detailed information about the user's action through event objects +- **Enables** you to create responsive, interactive web applications +- **Works** consistently across different browsers and devices ## Creating the game +Now that you understand how events work, let's put that knowledge into practice by building something engaging and useful. We'll create a typing speed game that demonstrates event handling while helping you develop a crucial developer skill. + We are going to create a game to explore how events work in JavaScript. Our game is going to test a player's typing skill, which is one of the most underrated skills all developers should have. We should all be practicing our typing! The general flow of the game will look like this: -- Player clicks on start button and is presented with a quote to type -- Player types the quote as quickly as they can in a textbox - - As each word is completed, the next one is highlighted - - If the player has a typo, the textbox is updated to red - - When the player completes the quote, a success message is displayed with the elapsed time +```mermaid +flowchart TD + A[Player clicks Start] --> B[Random quote displays] + B --> C[Player types in textbox] + C --> D{Word complete?} + D -->|Yes| E[Highlight next word] + D -->|No| F{Correct so far?} + F -->|Yes| G[Keep normal styling] + F -->|No| H[Show error styling] + E --> I{Quote complete?} + I -->|No| C + I -->|Yes| J[Show success message with time] + G --> C + H --> C +``` + +**Here's how our game will work:** +- **Starts** when the player clicks the start button and displays a random quote +- **Tracks** the player's typing progress word by word in real-time +- **Highlights** the current word to guide the player's focus +- **Provides** immediate visual feedback for typing errors +- **Calculates** and displays the total time when the quote is completed Let's build our game, and learn about events! ### File structure -We're going to need three total files: **index.html**, **script.js** and **style.css**. Let's start by setting those up to make life a little easier for us. +Organizing your project files properly from the start makes development smoother and helps you maintain clean, professional code. Our typing game will follow the standard web development structure that separates content, styling, and behavior. + +We're going to need three total files: `index.html`, `script.js` and `style.css`. Let's start by setting those up to make life a little easier for us. -- Create a new folder for your work by opening a console or terminal window and issuing the following command: +**Create a new folder for your work by opening a console or terminal window and issuing the following command:** ```bash # Linux or macOS @@ -51,29 +98,49 @@ mkdir typing-game && cd typing-game md typing-game && cd typing-game ``` -- Open Visual Studio Code +**Here's what these commands do:** +- **Creates** a new directory called `typing-game` for your project files +- **Navigates** into the newly created directory automatically +- **Sets up** a clean workspace for your game development + +**Open Visual Studio Code:** ```bash code . ``` -- Add three files to the folder in Visual Studio Code with the following names: - - index.html - - script.js - - style.css +**This command:** +- **Launches** Visual Studio Code in the current directory +- **Opens** your project folder in the editor +- **Provides** access to all the development tools you'll need + +**Add three files to the folder in Visual Studio Code with the following names:** +- `index.html` - Contains the structure and content of your game +- `script.js` - Handles all the game logic and event listeners +- `style.css` - Defines the visual appearance and styling ## Create the user interface +Building a user interface is like creating a blueprint for how users will interact with your application. We need to think about what elements users need to see and interact with, then structure them in a logical, accessible way. + If we explore the requirements, we know we're going to need a handful of elements on our HTML page. This is sort of like a recipe, where we need some ingredients: -- Somewhere to display the quote for the user to type -- Somewhere to display any messages, like a success message -- A textbox for typing -- A start button +| UI Element | Purpose | HTML Element | +|------------|---------|-------------| +| Quote Display | Shows the text to type | `
` with `id="quote"` | +| Message Area | Displays status and success messages | `
` with `id="message"` | +| Text Input | Where players type the quote | `` with `id="typed-value"` | +| Start Button | Begins the game | `