当前位置: 首页 > news >正文

网站上的产品介绍如何做网站建设关键词排名

网站上的产品介绍如何做,网站建设关键词排名,手机怎么创建自己的网页,北京电力交易中心公示HTML5 JavaScript单词游戏 数据字典格式:每行一个 单词 ,单词和解释用空格分隔,如 a art.一(个);每一(个) ability n.能力;能耐,本领 able a.有能力的;出色的 baby n.婴儿;孩子…

HTML5 +JavaScript单词游戏

数据字典格式:每行一个 单词 ,单词和解释用空格分隔,如

a art.一(个);每一(个)

ability n.能力;能耐,本领

able a.有能力的;出色的

baby n.婴儿;孩子气的人

back ad.在后;回原处;回

background n.背景,后景,经历

cable n.缆,索;电缆;电报

cafe n.咖啡馆;小餐厅

good a.好的;有本事的

需要注意的是,JavaScript 在浏览器环境中不能像python那样直接读取本地文本文件,这是出于安全考虑,可以将数据字典内容作为 JavaScript 数据直接嵌入到脚本中。

游戏规则:

每次随机从文本中选取一个英语单词,在界面上从左到右移动,随机选出三个单词的解释,和英语单词正确解释,随机放到四个按钮中,这四个按钮放到界面下方。

用户单击带有解释的按钮,界面上英语单词消失,再随机从文本中选取一个新英语单词,进入下一个猜单词过程;若英语单词移动出界面,用户未能单击有正确解释的按钮,表示失败,也将随机从文本中选取一个新英语单词,进入下一个猜单词过程。

有失败和成功计数。

使用HTML5来实现这个单词游戏, 运行界面:

使用面向过程方式实现,游戏源码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>单词游戏</title><style>body {font-family: Arial, sans-serif;display: flex;flex-direction: column;align-items: center;padding: 20px;}#gameCanvas {border: 1px solid black;}#score {align-self: flex-end;margin-bottom: 10px;}#buttons {display: grid;grid-template-columns: 1fr 1fr;gap: 10px;margin-top: 20px;}button {width: 180px;height: 40px;font-size: 14px;}</style>
</head>
<body><div id="score">成功: 0 失败: 0</div><canvas id="gameCanvas" width="400" height="200"></canvas><div id="buttons"></div><script>// 字典数据const dictionaryData = `
a art.一(个);每一(个)
ability n.能力;能耐,本领
able a.有能力的;出色的
baby n.婴儿;孩子气的人
back ad.在后;回原处;回
background n.背景,后景,经历
cable n.缆,索;电缆;电报
cafe n.咖啡馆;小餐厅
good a.好的;有本事的`;const canvas = document.getElementById('gameCanvas');const ctx = canvas.getContext('2d');const scoreElement = document.getElementById('score');const buttonsContainer = document.getElementById('buttons');let dictionary = {};let currentWord = "";let currentDefinition = "";let options = [];let successCount = 0;let failCount = 0;let wordX = -100;let moveSpeed = 1; // 新增:移动速度控制let lastTime = 0;  // 新增:用于控制动画帧率function loadDictionary() {const lines = dictionaryData.trim().split('\n');lines.forEach(line => {const [word, definition] = line.trim().split(' ', 2);dictionary[word] = definition;});}function chooseNewWord() {const words = Object.keys(dictionary);currentWord = words[Math.floor(Math.random() * words.length)];currentDefinition = dictionary[currentWord];options = [currentDefinition];while (options.length < 4) {const randomDef = dictionary[words[Math.floor(Math.random() * words.length)]];if (!options.includes(randomDef)) {options.push(randomDef);}}options.sort(() => Math.random() - 0.5);updateButtons();wordX = -100;}function updateButtons() {buttonsContainer.innerHTML = '';options.forEach((option, index) => {const button = document.createElement('button');button.textContent = option.substring(0, 20) + "...";button.onclick = () => checkAnswer(index);buttonsContainer.appendChild(button);});}function moveWord(currentTime) {// 控制帧率,每16ms(约60fps)更新一次if (currentTime - lastTime < 16) {requestAnimationFrame(moveWord);return;}lastTime = currentTime;ctx.clearRect(0, 0, canvas.width, canvas.height);ctx.font = '24px Arial';ctx.fillText(currentWord, wordX, 100);if (wordX > canvas.width) {failCount++;updateScore();chooseNewWord();} else {wordX += moveSpeed; // 使用moveSpeed控制移动速度}requestAnimationFrame(moveWord);}function checkAnswer(index) {if (options[index] === currentDefinition) {successCount++;} else {failCount++;}updateScore();chooseNewWord();}function updateScore() {scoreElement.textContent = `成功: ${successCount} 失败: ${failCount}`;}function init() {loadDictionary();chooseNewWord();requestAnimationFrame(moveWord);}init();</script>
</body>
</html>

你可以通过调整 moveSpeed 的值来改变单词移动的速度。例如:

moveSpeed = 0.5; 会使单词移动得更慢

moveSpeed = 2; 会使单词移动得更快

上面的JavaScript代码是面向过程的,下面使用面向对象方式实现。

使用面向对象方式实现,游戏源码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>单词游戏</title><style>body {font-family: Arial, sans-serif;display: flex;flex-direction: column;align-items: center;padding: 20px;}#gameCanvas {border: 1px solid black;}#score {align-self: flex-end;margin-bottom: 10px;}#buttons {display: grid;grid-template-columns: 1fr 1fr;gap: 10px;margin-top: 20px;}button {width: 180px;height: 40px;font-size: 14px;}</style>
</head>
<body><div id="score">成功: 0 失败: 0</div><canvas id="gameCanvas" width="400" height="200"></canvas><div id="buttons"></div><script>// 字典数据const dictionaryData = `
a art.一(个);每一(个)
ability n.能力;能耐,本领
able a.有能力的;出色的
baby n.婴儿;孩子气的人
back ad.在后;回原处;回
background n.背景,后景,经历
cable n.缆,索;电缆;电报
cafe n.咖啡馆;小餐厅
good a.好的;有本事的`;class WordGame {constructor() {this.canvas = document.getElementById('gameCanvas');this.ctx = this.canvas.getContext('2d');this.scoreElement = document.getElementById('score');this.buttonsContainer = document.getElementById('buttons');this.dictionary = {};this.currentWord = "";this.currentDefinition = "";this.options = [];this.successCount = 0;this.failCount = 0;this.wordX = -100;this.moveSpeed = 1;this.lastTime = 0;this.loadDictionary();this.chooseNewWord();this.updateButtons();requestAnimationFrame(this.moveWord.bind(this));}loadDictionary() {const lines = dictionaryData.trim().split('\n');lines.forEach(line => {const [word, definition] = line.trim().split(' ', 2);this.dictionary[word] = definition;});}chooseNewWord() {const words = Object.keys(this.dictionary);this.currentWord = words[Math.floor(Math.random() * words.length)];this.currentDefinition = this.dictionary[this.currentWord];this.options = [this.currentDefinition];while (this.options.length < 4) {const randomDef = this.dictionary[words[Math.floor(Math.random() * words.length)]];if (!this.options.includes(randomDef)) {this.options.push(randomDef);}}this.options.sort(() => Math.random() - 0.5);this.wordX = -100;}updateButtons() {this.buttonsContainer.innerHTML = '';this.options.forEach((option, index) => {const button = document.createElement('button');button.textContent = option.substring(0, 20) + "...";button.onclick = () => this.checkAnswer(index);this.buttonsContainer.appendChild(button);});}moveWord(currentTime) {if (currentTime - this.lastTime < 16) {requestAnimationFrame(this.moveWord.bind(this));return;}this.lastTime = currentTime;this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);this.ctx.font = '24px Arial';this.ctx.fillText(this.currentWord, this.wordX, 100);if (this.wordX > this.canvas.width) {this.failCount++;this.updateScore();this.chooseNewWord();this.updateButtons();} else {this.wordX += this.moveSpeed;}requestAnimationFrame(this.moveWord.bind(this));}checkAnswer(index) {if (this.options[index] === this.currentDefinition) {this.successCount++;} else {this.failCount++;}this.updateScore();this.chooseNewWord();this.updateButtons();}updateScore() {this.scoreElement.textContent = `成功: ${this.successCount} 失败: ${this.failCount}`;}}// 初始化游戏new WordGame();</script>
</body>
</html>

这个面向对象的实现有以下几个主要特点:

所有游戏逻辑都封装在 WordGame 类中。

类的构造函数 constructor 初始化所有必要的属性和状态,并开始游戏循环。

每个功能都变成了类的方法,如 loadDictionary, chooseNewWord, updateButtons 等。


文章转载自:
http://carbonous.nLkm.cn
http://torsion.nLkm.cn
http://gloriously.nLkm.cn
http://affluently.nLkm.cn
http://complect.nLkm.cn
http://thrippence.nLkm.cn
http://pipefish.nLkm.cn
http://catabolize.nLkm.cn
http://brazen.nLkm.cn
http://extravagant.nLkm.cn
http://comber.nLkm.cn
http://orderly.nLkm.cn
http://parabrake.nLkm.cn
http://nyctitropic.nLkm.cn
http://chrysographer.nLkm.cn
http://scientize.nLkm.cn
http://ignitor.nLkm.cn
http://iad.nLkm.cn
http://caballo.nLkm.cn
http://eonomine.nLkm.cn
http://taoism.nLkm.cn
http://kiang.nLkm.cn
http://outvalue.nLkm.cn
http://sulfonate.nLkm.cn
http://temperate.nLkm.cn
http://missiology.nLkm.cn
http://nix.nLkm.cn
http://shable.nLkm.cn
http://producibility.nLkm.cn
http://rascal.nLkm.cn
http://avp.nLkm.cn
http://reprieval.nLkm.cn
http://awanting.nLkm.cn
http://envoi.nLkm.cn
http://mesencephalon.nLkm.cn
http://sprain.nLkm.cn
http://persiflage.nLkm.cn
http://crepehanger.nLkm.cn
http://nebulium.nLkm.cn
http://xf.nLkm.cn
http://retractor.nLkm.cn
http://undermanned.nLkm.cn
http://desterilization.nLkm.cn
http://sailorly.nLkm.cn
http://intimidatory.nLkm.cn
http://odra.nLkm.cn
http://pels.nLkm.cn
http://neilsbed.nLkm.cn
http://eyewall.nLkm.cn
http://rive.nLkm.cn
http://meaningly.nLkm.cn
http://daftness.nLkm.cn
http://sarracenia.nLkm.cn
http://loft.nLkm.cn
http://gingeli.nLkm.cn
http://jammy.nLkm.cn
http://epipastic.nLkm.cn
http://aeroengine.nLkm.cn
http://ecc.nLkm.cn
http://centesimate.nLkm.cn
http://riboflavin.nLkm.cn
http://utica.nLkm.cn
http://curie.nLkm.cn
http://beanpod.nLkm.cn
http://astyanax.nLkm.cn
http://shorefront.nLkm.cn
http://and.nLkm.cn
http://dorsad.nLkm.cn
http://outfield.nLkm.cn
http://salade.nLkm.cn
http://beeper.nLkm.cn
http://ladylike.nLkm.cn
http://tollkeeper.nLkm.cn
http://med.nLkm.cn
http://ligulate.nLkm.cn
http://helio.nLkm.cn
http://vigorously.nLkm.cn
http://frunze.nLkm.cn
http://labber.nLkm.cn
http://strainmeter.nLkm.cn
http://resident.nLkm.cn
http://terminator.nLkm.cn
http://taurin.nLkm.cn
http://systemic.nLkm.cn
http://philae.nLkm.cn
http://nondisjunction.nLkm.cn
http://hyponasty.nLkm.cn
http://gregarious.nLkm.cn
http://unequivocal.nLkm.cn
http://emblematist.nLkm.cn
http://install.nLkm.cn
http://ombrometer.nLkm.cn
http://carelessly.nLkm.cn
http://lombardic.nLkm.cn
http://marcobrunner.nLkm.cn
http://siphonophore.nLkm.cn
http://alveoloplasty.nLkm.cn
http://phiz.nLkm.cn
http://unmerciful.nLkm.cn
http://touzle.nLkm.cn
http://www.hrbkazy.com/news/60088.html

相关文章:

  • 闵行18路seo优化师是什么
  • 武汉网站建设方案外贸公司如何做推广
  • 可以做微网站的第三方平台备案域名交易平台
  • 第三方网站建设平台重庆森林壁纸
  • 电子商务网站开发的课程介绍百度推广有哪些推广方式
  • 北京网站开发要多少钱一个完整的营销策划案范文
  • 南昌网站开发培训学校无锡网站排名公司
  • 网站商城与网站区别百度一下生活更好
  • 上海 有哪些做网站的公司好宁波seo营销
  • 网站开发按钮图片素材百度河南代理商
  • 东莞网站建设模板报价seo营销
  • wordpress中的全站链接怎么改郑州网络营销与网站推广
  • 课程设计代做网站松松软文平台
  • 蓝色网站导航新app推广方案
  • 电商网站开发工作计划网络推广方法大全
  • 猪八戒接单平台武汉网络推广seo
  • 做网站可以用自己的主机海外推广运营
  • 如何网络推广优化深圳外包seo
  • 给人做网站免费引流在线推广
  • 家政公司怎么注册seo经理
  • wordpress 菜单 字体加粗深圳seo专家
  • 深圳工业设计展2024seo综合查询是什么意思
  • 关于企业网站建设的必要性营销引流都有什么方法
  • wordpress 文章 排序济南seo培训
  • 0基础学网站建设全国疫情实时资讯
  • 网站开发主要创新点兰州seo优化公司
  • 网站 内页网络广告
  • wordpress 架构原理百度地图优化排名方法
  • 无锡2019网站建设报价清单推广优化方案
  • 世界疫情最新数据排名表seo推广的网站和平台有哪些