IT评测·应用市场-qidao123.com技术社区

标题: HTMlCSS实现切分方块小游戏 [打印本页]

作者: 愛在花開的季節    时间: 6 天前
标题: HTMlCSS实现切分方块小游戏
本内容来自网络,仅学习使用
这段代码构建了一个 HTML5 切积木益智游戏的前端界面,联合了 HTML、CSS 技能。HTML 部分界说了游戏的根本结构,包括游戏画布、游戏信息显示区(HUD)和菜单体系;CSS 部分则负责页面的样式设计,如配景、字体、按钮效果等。
代码详细分析

1. HTML 部分

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>HTML5切积木益智游戏</title>
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">
  7.     <link rel="stylesheet" href="css/style.css">
  8. </head>
  9. <body>
  10.     <!-- Game canvas -->
  11.     <canvas id="c"></canvas>
  12.     <!-- Gameplay HUD -->
  13.     <div class="hud">
  14.         <div class="hud__score">
  15.             <div class="score-lbl"></div>
  16.             <div class="cube-count-lbl"></div>
  17.         </div>
  18.         <div class="pause-btn"><div></div></div>
  19.         <div class="slowmo">
  20.             <div class="slowmo__bar"></div>
  21.         </div>
  22.     </div>
  23.     <!-- Menu System -->
  24.     <div class="menus">
  25.         <div class="menu menu--main">
  26.             <h1>切块</h1>
  27.             <button type="button" class="play-normal-btn">开始游戏</button>
  28.             <button type="button" class="play-casual-btn">休闲模式</button>
  29.         </div>
  30.         <div class="menu menu--pause">
  31.             <h1>暂停</h1>
  32.             <button type="button" class="resume-btn">重新开始</button>
  33.             <button type="button" class="menu-btn--pause">主菜单</button>
  34.         </div>
  35.         <div class="menu menu--score">
  36.             <h1>游戏结束</h1>
  37.             <h2>分数:</h2>
  38.             <div class="final-score-lbl"></div>
  39.             <div class="high-score-lbl"></div>
  40.             <button type="button" class="play-again-btn">再玩一次</button>
  41.             <button type="button" class="menu-btn--score">主菜单</button>
  42.         </div>
  43.     </div>
  44.     <script  src="js/index.js"></script>
  45. </body>
  46. </html>
复制代码

2. CSS 部分
  1. body {
  2.     margin: 0;
  3.     background-color: #000;
  4.     background-image: radial-gradient(ellipse at top, #335476 0.0%, #31506e 11.1%, #304b67 22.2%, #2f4760 33.3%, #2d4359 44.4%, #2c3f51 55.6%, #2a3a4a 66.7%, #293643 77.8%, #28323d 88.9%, #262e36 100.0%);
  5.     height: 100vh;
  6.     overflow: hidden;
  7.     font-family: monospace;
  8.     font-weight: bold;
  9.     letter-spacing: 0.06em;
  10.     color: rgba(255, 255, 255, 0.75);
  11. }
  12. #c {
  13.     display: block;
  14.     touch - action: none;
  15.     transform: translateZ(0);
  16. }
  17. /*/
  18. //        HUD        //
  19. /*/
  20. .hud__score,
  21. .pause-btn {
  22.     position: fixed;
  23.     font-size: calc(14px + 2vw + 1vh);
  24. }
  25. .hud__score {
  26.     top: 0.65em;
  27.     left: 0.65em;
  28.     pointer - events: none;
  29.     user - select: none;
  30. }
  31. .cube-count-lbl {
  32.     font-size: 0.46em;
  33. }
  34. .pause-btn {
  35.     position: fixed;
  36.     top: 0;
  37.     right: 0;
  38.     padding: 0.8em 0.65em;
  39. }
  40. .pause-btn>div {
  41.     position: relative;
  42.     width: 0.8em;
  43.     height: 0.8em;
  44.     opacity: 0.75;
  45. }
  46. .pause-btn>div::before,
  47. .pause-btn>div::after {
  48.     content: '';
  49.     display: block;
  50.     width: 34%;
  51.     height: 100%;
  52.     position: absolute;
  53.     background-color: #fff;
  54. }
  55. .pause-btn>div::after {
  56.     right: 0;
  57. }
  58. .slowmo {
  59.     position: fixed;
  60.     bottom: 0;
  61.     width: 100%;
  62.     pointer - events: none;
  63.     opacity: 0;
  64.     transition: opacity 0.4s;
  65.     will - change: opacity;
  66. }
  67. .slowmo::before {
  68.     content: 'SLOW - MO';
  69.     display: block;
  70.     font-size: calc(8px + 1vw + 0.5vh);
  71.     margin-left: 0.5em;
  72.     margin-bottom: 8px;
  73. }
  74. .slowmo::after {
  75.     content: '';
  76.     display: block;
  77.     position: fixed;
  78.     bottom: 0;
  79.     width: 100%;
  80.     height: 1.5vh;
  81.     background-color: rgba(0, 0, 0, 0.25);
  82.     z - index: -1;
  83. }
  84. .slowmo__bar {
  85.     height: 1.5vh;
  86.     background-color: rgba(255, 255, 255, 0.75);
  87.     transform - origin: 0 0;
  88. }
  89. /*/
  90. //       MENUS       //
  91. /*/
  92. .menus::before {
  93.     content: '';
  94.     pointer - events: none;
  95.     position: fixed;
  96.     top: 0;
  97.     right: 0;
  98.     bottom: 0;
  99.     left: 0;
  100.     background-color: #000;
  101.     opacity: 0;
  102.     transition: opacity 0.2s;
  103.     transition - timing - function: ease - in;
  104. }
  105. .menus.has - active::before {
  106.     opacity: 0.08;
  107.     transition - duration: 0.4s;
  108.     transition - timing - function: ease - out;
  109. }
  110. .menus.interactive - mode::before {
  111.     opacity: 0.02;
  112. }
  113. /* Menu containers */
  114. .menu {
  115.     pointer - events: none;
  116.     position: fixed;
  117.     top: 0;
  118.     right: 0;
  119.     bottom: 0;
  120.     left: 0;
  121.     display: flex;
  122.     flex - direction: column;
  123.     justify - content: center;
  124.     align - items: center;
  125.     user - select: none;
  126.     text - align: center;
  127.     color: rgba(255, 255, 255, 0.9);
  128.     opacity: 0;
  129.     visibility: hidden;
  130.     transform: translateY(30px);
  131.     transition - property: opacity, visibility, transform;
  132.     transition - duration: 0.2s;
  133.     transition - timing - function: ease - in;
  134. }
  135. .menu.active {
  136.     opacity: 1;
  137.     visibility: visible;
  138.     transform: translateY(0);
  139.     transition - duration: 0.4s;
  140.     transition - timing - function: ease - out;
  141. }
  142. .menus.interactive - mode .menu.active {
  143.     opacity: 0.6;
  144. }
  145. .menus:not(.interactive - mode) .menu.active>* {
  146.     pointer - events: auto;
  147. }
  148. /* Common menu elements */
  149. h1 {
  150.     font - size: 4rem;
  151.     line - height: 0.95;
  152.     text - align: center;
  153.     font - weight: bold;
  154.     margin: 0 0.65em 1em;
  155. }
  156. h2 {
  157.     font - size: 1.2rem;
  158.     line - height: 1;
  159.     text - align: center;
  160.     font - weight: bold;
  161.     margin: -1em 0.65em 1em;
  162. }
  163. .final - score - lbl {
  164.     font - size: 5rem;
  165.     margin: -0.2em 0 0;
  166. }
  167. .high - score - lbl {
  168.     font - size: 1.2rem;
  169.     margin: 0 0 2.5em;
  170. }
  171. button {
  172.     display: block;
  173.     position: relative;
  174.     width: 200px;
  175.     padding: 12px 20px;
  176.     background: transparent;
  177.     border: none;
  178.     outline: none;
  179.     user - select: none;
  180.     font - family: monospace;
  181.     font - weight: bold;
  182.     font - size: 1.4rem;
  183.     color: #fff;
  184.     opacity: 0.75;
  185.     transition: opacity 0.3s;
  186. }
  187. button::before {
  188.     content: '';
  189.     position: absolute;
  190.     top: 0;
  191.     right: 0;
  192.     bottom: 0;
  193.     left: 0;
  194.     background - color: rgba(255, 255, 255, 0.15);
  195.     transform: scale(0, 0);
  196.     opacity: 0;
  197.     transition: opacity 0.3s, transform 0.3s;
  198. }
  199. /* No `:focus` styles because this is a mouse/touch game! */
  200. button:active {
  201.     opacity: 1;
  202. }
  203. button:active::before {
  204.     transform: scale(1, 1);
  205.     opacity: 1;
  206. }
  207. .credits {
  208.     position: fixed;
  209.     width: 100%;
  210.     left: 0;
  211.     bottom: 20px;
  212. }
  213. a {
  214.     color: white;
  215. }
  216. /* Only enable hover state on large screens */
  217. @media (min - width: 1025px) {
  218.     button:hover {
  219.         opacity: 1;
  220.     }
  221.     button:hover::before {
  222.         transform: scale(1, 1);
  223.         opacity: 1;
  224.     }
  225. }
复制代码

总结

这段代码通过 HTML 构建了游戏的根本结构,包括游戏画布、信息显示区和菜单体系;通过 CSS 设置了页面的样式,包括配景、字体、按钮效果等,为游戏提供了一个美观且交互性良好的界面。而游戏的焦点逻辑则由外部 JavaScript 文件 js/index.js 实现。
源码可联系QQ:3341742487
 效果如图

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4