打字通小游戏制作教程:用HTML5和JavaScript提升打字速度

本文主要是介绍打字通小游戏制作教程:用HTML5和JavaScript提升打字速度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

🌟 前言

欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍

  • 🤖 洛可可白:个人主页

  • 🔥 个人专栏:✅前端技术 ✅后端技术

  • 🏠 个人博客:洛可可白博客

  • 🐱 代码获取:bestwishes0203

  • 📷 封面壁纸:洛可可白wallpaper

在这里插入图片描述

文章目录

  • 打字通小游戏制作教程:用HTML5和JavaScript提升打字速度
    • 体验地址
    • 准备工作
    • 创建HTML结构
    • 添加CSS样式
    • 编写JavaScript逻辑
    • 测试游戏
    • 全部代码
    • 🎉 结语
    • 🎉 往期精彩回顾

打字通小游戏制作教程:用HTML5和JavaScript提升打字速度

在这篇文章中,我们将一起学习如何使用HTML5和JavaScript来制作一个简单的打字通小游戏。这个小游戏可以帮助用户练习打字速度和准确性。通过这个教程,你将了解如何创建游戏界面、处理用户输入、实现倒计时以及计算得分。即使你是编程新手,也能跟随步骤完成这个项目。

体验地址

洛可可白⚡️打字通
在这里插入图片描述

准备工作

首先,确保你的计算机上安装了文本编辑器,如Notepad++、Sublime Text或Visual Studio Code。这些工具将帮助你编写和编辑代码。

创建HTML结构

打开你的文本编辑器,创建一个新的HTML文件,并输入以下代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>洛可可白打字通</title><style>/* 在这里添加CSS样式 */</style>
</head>
<body><div class="bigBox"><div class="container">你准备好了吗?</div><textarea placeholder="开始输入..." style="resize: none" cols="30" rows="10"></textarea><div class="operate"><button>开始</button><div id="timer">60</div></div></div><script>// 在这里添加JavaScript代码</script>
</body>
</html>

这是我们游戏的基本结构。<head>部分包含了页面的元数据和样式定义,<body>部分则是游戏的主要内容。

添加CSS样式

<style>标签内,我们将添加一些CSS样式来美化我们的打字通游戏。这包括游戏容器、文本区域、按钮和计时器的样式。

body {font-family: Arial, sans-serif;margin: 0;padding: 0;-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;-o-user-select: none;user-select: none;
}.bigBox {width: 50%;background-color: #ac8c3e;margin: 40px auto;box-sizing: border-box;padding: 20px;border-radius: 30px;box-shadow: 0px 0px 30px 9px #939393;
}.container {margin: 0 auto;text-align: center;padding: 20px;
}textarea {width: 100%;height: 200px;margin: 20px 0;font-size: 20px;border: none;
}.operate {width: 20%;margin: 0 auto;text-align: center;
}button {font-size: 24px;padding: 10px 20px;background-color: #007bff;color: #fff;border: none;border-radius: 5px;cursor: pointer;
}#timer {font-size: 48px;margin: 20px;
}

编写JavaScript逻辑

现在,我们将在<script>标签内添加JavaScript代码,这是游戏的核心部分。我们将创建游戏文本、初始化游戏、处理用户输入、实现倒计时以及计算得分。

const text = "Believe in yourself and all that you are..."; // 游戏文本
const container = document.querySelector(".container");
const input = document.querySelector("textarea");
const button = document.querySelector("button");
const timer = document.getElementById("timer");
input.value = "";let countdown;function startGame() {// 游戏开始后,禁用按钮button.disabled = true;// 显示文本container.textContent = text;// 启动倒计时countdown = setInterval(() => {const remainingTime = parseInt(timer.textContent) - 1;if (remainingTime === 0) {// 时间用完,游戏结束endGame();}timer.textContent = remainingTime;}, 1000);
}function endGame() {// 停止倒计时clearInterval(countdown);// 计算得分const score = calculateScore();const scoreMessage = `你的得分是 ${score} 分!`;container.textContent = scoreMessage;button.disabled = false;
}function calculateScore() {const userText = input.value.trim();const correctText = text.trim();const userWords = userText.split(" ");const correctWords = correctText.split(" ");let score = 0;for (let i = 0; i < userWords.length; i++) {if (userWords[i] === correctWords[i]) {score++;}}return score;
}// 添加按钮点击事件监听器
button.addEventListener("click", () => {// 设置倒计时时间timer.textContent = "60";// 清空输入框和输出文本区域input.value = "";container.textContent = "";// 启动游戏startGame();
});

在这个脚本中,我们首先定义了游戏的文本。然后,我们创建了开始游戏的函数startGame,它将显示游戏文本并启动倒计时。我们还定义了结束游戏的函数endGame,它将停止倒计时并计算得分。calculateScore函数用于计算用户的得分。最后,我们为开始按钮添加了一个点击事件监听器,当用户点击按钮时,游戏将开始。

测试游戏

保存你的HTML文件,并在浏览器中打开它。你应该能看到一个打字通游戏的界面。点击“开始”按钮,游戏将开始,你可以尝试在限定时间内尽可能准确地输入显示的文本。时间结束后,你的得分将被计算出来。

全部代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>洛可可白⚡️打字通</title><style>body {font-family: Arial, sans-serif;margin: 0;padding: 0;-moz-user-select: none;/*火狐*/-webkit-user-select: none;/*webkit浏览器*/-ms-user-select: none;/*IE10*/-khtml-user-select: none;/*早期浏览器*/-o-user-select: none;user-select: none;}.bigBox {width: 50%;background-color: #ac8c3e;margin: 40px auto;box-sizing: border-box;padding: 20px;border-radius: 30px;box-shadow: 0px 0px 30px 9px #939393;}.container {margin: 0 auto;text-align: center;padding: 20px;}textarea {width: 100%;height: 200px;margin: 20px 0;font-size: 20px;border: none;}.operate {width: 20%;margin: 0 auto;text-align: center;}button {font-size: 24px;padding: 10px 20px;background-color: #007bff;color: #fff;border: none;border-radius: 5px;cursor: pointer;}#timer {font-size: 48px;margin: 20px;}</style></head><body><div class="bigBox"><div class="container">你准备好了吗?</div><textareaname=""placeholder="开始输入..."id=""style="resize: none"cols="30"rows="10"></textarea><div class="operate"><button>开始</button><div id="timer">60</div></div></div><script>const text ="Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle. This quote by Christian D. Larson reminds us that we all have the power within us to overcome any obstacle we may face. When we have confidence in ourselves and our abilities, we can achieve great things. So, let's trust ourselves, believe in our dreams, and work hard to make them a reality.";const container = document.querySelector(".container");const input = document.querySelector("textarea");const button = document.querySelector("button");const timer = document.getElementById("timer");input.value = "";let countdown;function startGame() {// 游戏开始后,禁用按钮button.disabled = true;// 显示文本container.textContent = text;// 启动倒计时countdown = setInterval(() => {const remainingTime = parseInt(timer.textContent) - 1;if (remainingTime === 0) {// 时间用完,游戏结束endGame();}timer.textContent = remainingTime;}, 1000);}function endGame() {// 停止倒计时clearInterval(countdown);// 计算得分const score = calculateScore();const scoreMessage = `你的得分是 ${score} 分!`;container.textContent = scoreMessage;button.disabled = false;}function calculateScore() {const userText = input.value.trim();const correctText = text.trim();const userWords = userText.split(" ");const correctWords = correctText.split(" ");let score = 0;for (let i = 0; i < userWords.length; i++) {console.log(userWords[i], correctWords[i]);if (userWords[i] === correctWords[i]) {score++;}}return score;}// 添加按钮点击事件监听器button.addEventListener("click", () => {// 设置倒计时时间timer.textContent = "60";// 清空输入框和输出文本区域input.value = "";container.textContent = "";// 启动游戏startGame();});</script></body>
</html>

🎉 结语

恭喜你,你已经成功创建了一个打字通小游戏!这个教程涵盖了从创建基本的HTML结构到添加CSS样式,再到编写JavaScript交互逻辑的全过程。通过这个项目,你不仅学会了如何制作一个小游戏,还对前端开发有了基本的了解。随着你技能的提升,你可以尝试添加更多的功能,比如记录用户的最佳得分、添加音效或者实现更复杂的游戏逻辑。祝你编程愉快!
如果对你有帮助,点赞、收藏、关注是我更新的动力!👋🌟🚀

🎉 往期精彩回顾

  1. 拼图小游戏制作教程:用HTML5和JavaScript打造经典游戏
  • 234阅读 · 6点赞 · 8收藏
  1. Mock.js 基本语法与应用笔记
  • 215阅读 · 3点赞 · 8收藏
  1. 排序算法全景:从基础到高级的Java实现
  • 474阅读 · 22点赞 · 7收藏
  1. CentOS系统上安装Redis操作教程
  • 124阅读 · 4点赞 · 3收藏
  1. 打造你的HTML5打地鼠游戏:零基础入门教程
  • 1032阅读 · 17点赞 · 27收藏
  1. 打造你的贪吃蛇游戏:HTML、CSS与JavaScript的完美结合
  • 869阅读 · 24点赞 · 9收藏
  1. 快速上手:使用Hexo搭建并自定义个人博客
  • 579阅读 · 16点赞 · 17收藏
  1. 在Vue中处理接口返回的二进制图片数据
  • 582阅读 · 19点赞 · 15收藏
  1. 打造经典游戏:HTML5与CSS3实现俄罗斯方块
  • 1105阅读 · 31点赞 · 23收藏
  1. Spring Boot中Excel数据导入导出的高效实现
  • 1028阅读 · 23点赞 · 22收藏
  1. Spring Boot中实现图片上传功能的两种策略
  • 1187阅读 · 23点赞 · 13收藏
  1. CentOS上安装MySQL 5.7和MySQL 8.0教程
  • 806阅读 · 21点赞 · 13收藏
  1. Spring Boot工程集成验证码生成与验证功能教程
  • 1397阅读 · 38点赞 · 17收藏
  1. Spring Boot 3项目集成Swagger3教程
  • 771阅读 · 15点赞 · 8收藏
  1. CentOS上安装JDK的详细教程
  • 599阅读 · 12点赞 · 13收藏
  1. 解决前端项目中Node.js版本不一致导致的依赖安装错误
  • 845阅读 · 17点赞 · 16收藏
  1. 入门指南:使用uni-app构建跨平台应用
  • 1265阅读 · 29点赞 · 9收藏
  1. Vue项目中使用Mock.js进行API模拟
  • 650阅读 · 17点赞 · 7收藏
  1. Vue组件间通信实践
  • 863阅读 · 24点赞 · 18收藏
  1. CentOS上安装与配置Nginx
  • 649阅读 · 9点赞 · 6收藏
  1. Vue跳转页面传递参数
  • 263阅读 · 5点赞 · 4收藏
  1. vue项目如何下载使用gsap动画库
  • 544阅读 · 1点赞 · 0收藏
  1. VS Code上搭建React开发环境
  • 2263阅读 · 2点赞 · 10收藏
  1. vue命令式组件封装以及使用
  • 816阅读 · 2点赞 · 3收藏
  1. springboot项目常用配置
  • 379阅读 · 1点赞 · 0收藏
  1. 如何在Vue中使用百度地图API来创建地图应用程序。
  • 344阅读 · 3点赞 · 1收藏
  1. Nodejs搭建服务器
  • 167阅读 · 2点赞 · 1收藏
  1. 走入ES6
  • 201阅读 · 2点赞 · 1收藏
  1. Ajax技术包含Fetch和Axios
  • 175阅读 · 2点赞 · 2收藏
  1. Promise技术学这篇就够了
  • 82阅读 · 2点赞 · 1收藏
  1. 手把手教你CentOS下载Nginx配置使用
  • 463阅读 · 2点赞 · 3收藏
  1. 基于ubuntu的c语言编程简单易懂
  • 290阅读 · 3点赞 · 2收藏
  1. vue3 setup语法糖的三种书写方法
  • 2798阅读 · 5点赞 · 14收藏
  1. vue3中vuex 的使用基本使用和二次封装
  • 446阅读 · 3点赞 · 1收藏
  1. 初学Vue第一篇
  • 210阅读 · 1点赞 · 1收藏
  1. MySQL基础全套全网最详细讲解
  • 767阅读 · 3点赞 · 6收藏
  1. 数据结构之操作顺序表实战——C语言
  • 146阅读 · 3点赞 · 2收藏
  1. 前端开发之响应式布局,响应式 HTML, CSS and JavaScript 框架介绍;
  • 701阅读 · 3点赞 · 2收藏
  1. VS code搭建C/C++运行环境简单易上手
  • 2777阅读 · 5点赞 · 8收藏
  1. Vue.2&Vue.3项目引入Element-UI教程&踩坑
  • 9255阅读 · 22点赞 · 82收藏
  1. Vue项目引入Echarts可视化图表库教程&踩坑
  • 2198阅读 · 3点赞 · 5收藏
  1. VirtualBox虚拟机搭建CentOS系统教程
  • 4487阅读 · 4点赞 · 32收藏
  1. VS Code上搭建Vue开发环境
  • 10637阅读 · 13点赞 · 64收藏
  1. Color-UI 简介及使用教程
  • 5902阅读 · 2点赞 · 13收藏

这篇关于打字通小游戏制作教程:用HTML5和JavaScript提升打字速度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/802320

相关文章

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【 html+css 绚丽Loading 】000046 三才归元阵

前言:哈喽,大家好,今天给大家分享html+css 绚丽Loading!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦 💕 目录 📚一、效果📚二、信息💡1.简介:💡2.外观描述:💡3.使用方式:💡4.战斗方式:💡5.提升:💡6.传说: 📚三、源代码,上代码,可以直接复制使用🎥效果🗂️目录✍️