JavaScript实现简单的打字游戏

╰半夏微凉° 2022-12-23 00:38 281阅读 0赞

完整项目下载:https://download.csdn.net/download/weixin_44893902/13131694

演示地址:https://url\_777.gitee.io/typing-games/

效果图:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDg5MzkwMg_size_16_color_FFFFFF_t_70

实现代码:

目录结构:

20201120224913592.png

HTML:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>打字游戏</title>
  6. <link rel="stylesheet" href="css/new_file.css" />
  7. <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
  8. <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
  9. </head>
  10. <body>
  11. <!-- 背景音乐 -->
  12. <audio id="mus">
  13. <source src="music/music_bg.mp3"></source>
  14. </audio>
  15. <!-- 主体部分 -->
  16. <div id="box">
  17. <div id="">
  18. </div>
  19. <!-- 左右的生命和音量图标 -->
  20. <img src="./img/生命.png" style="float: left; margin: 45px;" width="45" height="38" id="life" />
  21. <img src="./img/volume.png" style="float: right; margin: 45px;" width="45" height="38" id="volume" />
  22. <!-- 计分模块 -->
  23. <div class="score"><font>0</font></div>
  24. <div id="game"></div>
  25. <!-- 重新开始界面 -->
  26. <div class="reload">
  27. <input type="button" class="btn squick" value="重新开始" />
  28. <a href="http://www.baidu.com"> <input type="button" class=" btn tu" value="退出游戏" /></a>
  29. </div>
  30. <!-- 按钮 -->
  31. <input type="button" class=" btn start" value="开 始 游 戏" />
  32. <input type="button" class=" btn stop" value="暂 停 游 戏" />
  33. <input type="button" class=" btn quick" value="增 加 难 度" />
  34. </div>
  35. </html>

CSS

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .btn{
  6. width: 170px;
  7. height: 70px;
  8. border-radius: 20px;
  9. border: 1px solid greenyellow;
  10. outline: medium;
  11. background: url(../img/background.jpg) no-repeat;
  12. background-size:170px 70px;
  13. float: left;
  14. margin-left: 20px;
  15. margin-top: 300px;
  16. text-align: center;
  17. /* cursor: pointer; */
  18. /* user-select: none; */
  19. font-weight: bold;
  20. font-size: 30px;
  21. color:white;
  22. }
  23. .btn:hover{
  24. border: 3px solid greenyellow;
  25. }
  26. #box{
  27. width: 100%;
  28. height: 100vh;
  29. background:url(../img/background.jpg) no-repeat;
  30. background-size:100% 100%;
  31. overflow: hidden;
  32. }
  33. .score{
  34. width: 250px;
  35. height: 355px;
  36. background: url(../img/score.png) no-repeat;
  37. background-size:100% 100%;
  38. position: absolute;
  39. right:10px;
  40. /* bottom: -80px; */
  41. font-size: 60px;
  42. text-align: center;
  43. line-height: 280px;
  44. color: white;
  45. cursor: pointer;
  46. user-select: none;
  47. margin-top: 395px;
  48. }
  49. .startorstop{
  50. width: 100%;
  51. height: 50px;
  52. position: absolute;
  53. bottom: 20px;
  54. }
  55. .reload{
  56. width: 500px;
  57. height: 350px;
  58. background: url(../img/reload.gif) no-repeat;
  59. background-size:350px 270px;
  60. margin: 0 auto;
  61. position: relative;
  62. top: -650px;
  63. }
  64. .squick{
  65. width: 130px;
  66. height: 50px;
  67. margin-top: 280px;
  68. margin-left: 40px;
  69. }
  70. .tu{
  71. width: 130px;
  72. height: 50px;
  73. margin-top: -49px;
  74. margin-left: 210px;
  75. }
  76. #game img{
  77. position: absolute;
  78. }
  79. #music_btn{
  80. width: 70px;
  81. height: 70px;
  82. position: absolute;
  83. left: 10px;
  84. top: 20px;
  85. cursor: pointer;
  86. user-select: none;
  87. }
  88. .play-tips{
  89. width: 500px;
  90. height: 400px;
  91. margin: 0 auto;
  92. margin-top: 150px;
  93. border: 1px solid red;
  94. }
  95. #buttons1{
  96. width: 700px;
  97. height: 200px;
  98. margin-bottom: 100px;
  99. }

JavaScript

  1. $(function(){
  2. var chars =['A','B','C','D','E','F','G','H','T','J','K','L','M','N','O','P','Q','R','S','G','U','V','W','X','Y','Z'];
  3. //积分
  4. var score = 0;
  5. //默认游戏的状态
  6. var flag = true;
  7. //声明刚开始游戏的状态
  8. var f = true;
  9. //开始游戏的标识
  10. var speed = 1;
  11. var start = $(".start");
  12. var createImgInterval;
  13. var downImgInterval;
  14. //默认游戏的状态
  15. //点击开始游戏按钮所执行的函数
  16. start.click(function(){
  17. play(speed);
  18. var music=document.getElementById("mus");
  19. music.play();
  20. flag = true;
  21. if(f){
  22. f = false;
  23. }
  24. });
  25. //点击暂停按钮所执行的函数
  26. $(".stop").click(function(){
  27. if(flag){
  28. flag = false;
  29. $(this).val("继 续 游 戏")
  30. var music=document.getElementById("mus");
  31. music.pause();
  32. }else{
  33. flag = true;
  34. $(this).val("暂 停 游 戏")
  35. var music=document.getElementById("mus");
  36. music.play();
  37. }
  38. });
  39. //增加难度
  40. $(".quick").click(function(){
  41. if(!f){
  42. speed += 0.5;
  43. play(speed);
  44. }
  45. });
  46. //重新开始游戏
  47. $(".reload").click(function(){
  48. $(this).animate({top:"-350px"});
  49. $("#game").children().remove();
  50. score = 0;
  51. $(".score").html(score);
  52. flag = true;
  53. f = true;
  54. speed = 1;
  55. play(speed);
  56. console.log(speed)
  57. });
  58. //创建图片
  59. function createImg(){
  60. if(flag){
  61. //随机创建
  62. var random = randomScope(0,25);
  63. var img = chars[random];
  64. var Dwidth = $(document).width();//获取浏览器的宽度
  65. var left = randomScope(Dwidth-100,100);
  66. $("#game").append("<img src='img/"+img+".png' width='80' height='100' style='left:"+left+"px; top:-100px;' />");
  67. }
  68. }
  69. //加载图片
  70. function downImg(){
  71. if(flag){
  72. var imgs = $("#game").children();//获取生成的所有字母
  73. for(var i=0;i<imgs.length;i++){
  74. var img = imgs[i];
  75. //当前字母
  76. var Top = parseInt(img.style.top);
  77. //当前字母距离顶部的值
  78. var Height = $("#box").height()-200;
  79. if(Top<=Height){
  80. img.style.top=(Top+2)+"px";
  81. }else{
  82. img.remove();
  83. // error.play();
  84. if(score==0){
  85. score=0
  86. }else{
  87. score -= 10;
  88. }
  89. $(".score").html(score);
  90. if(score <= 0){
  91. flag = false;
  92. $(".reload").animate({"top":"30px"});
  93. // gameOver.play();
  94. window.clearInterval(createImgInterval);
  95. window.clearInterval(downImgInterval);
  96. return ;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. $(window).keyup(function(e){
  103. var eve = window.event || e;//获取事件对象
  104. var imgs = $("#game").children();//获取所生成的字母
  105. var code = eve.keyCode;//获取用户按下的键
  106. if(flag){
  107. for(var i =0;i<imgs.length;i++){
  108. var img = imgs[i];
  109. var imgSrc = img.src.split("/");
  110. var name = imgSrc[imgSrc.length-1].split(".")[0];
  111. if(name == chars[code-65]){
  112. img.remove();
  113. score+=10;
  114. $(".score").html(score);
  115. if(score <= 0){
  116. // $(".tupian").animate({"bottom":"0px"});
  117. window.clearInterval(createImgInterval);
  118. window.clearInterval(downImgInterval);
  119. }
  120. return;
  121. }
  122. }
  123. }
  124. });
  125. //定时器
  126. function play(speed){
  127. console.log(speed)
  128. createImgInterval = window.setInterval(createImg,1000-speed*50);
  129. downImgInterval = window.setInterval(downImg,20-speed/2);
  130. }
  131. //随机范围方法
  132. function randomScope(minScope,maxScope){
  133. return Math.floor(Math.random()*(maxScope-minScope+1)+minScope);
  134. }
  135. var music=document.getElementById("volume");
  136. music.onclick = function(){
  137. var i = true;
  138. var mus=document.getElementById("mus");
  139. if (mus.paused) {
  140. mus.play();
  141. // 暂停中
  142. } else {
  143. // 播放中
  144. mus.pause();
  145. }
  146. }
  147. });

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读

    相关 WPF打字游戏

    本文的通过C\利用WPF编辑打字游戏,实现按下指定按键飞机打出一颗子弹并且产生厌恶,击中字母后子弹以及字幕消失,代码如下 编辑器界面对整体的背景以及大小的设定,同时导入按钮

    相关 C语言实现简单打字游戏

    我们实现这样一道编程题,简单的打字游戏,在程序启动后,随机生成一段英文字母,然后用户输入英文字母,输入完毕后,显示用户打字时间和正确率。 代码如下: define