之前的文章《教你使用HTML、CSS和JS創建響應式可過濾的遊戲(附代碼)》中,跟大家介紹怎麼使用JS創建響應式可過濾的遊戲。以下這篇文章跟大家介紹怎麼使用Three.js的噴火龍小遊戲,夥伴們來看看。
現場示範
如果你想了解這個噴火龍小遊戲是如何運作的,那你可以嘗試下面的示範。在這裡,我提供了所需的原始程式碼,以便你可以複製程式碼並在你自己的學習(開小差)、工作(摸魚)中使用它。
##如你在上圖所看到的,這裡我使用HTML、CSS 和JavaScript 製作了一個簡單的噴火龍小遊戲。 HTML程式碼示範網址:http://haiyong.site/penhuolong(用瀏覽器開啟)
<body> <div id="world"></div> <div id="instructions"> <span class="lightInstructions">你点击的时间越长,它打喷嚏的力度越大</span><br /> <span class="lightInstructions">- 按住并拖动可转身 -</span></div> <div id="credits"> <p>Prints on <a class="society6" href="https://juejin.cn/user/2040341402229751" target="blank">haiyong</a> | <a href="http://haiyong.site/game" target="blank">Game</a> | <a href="http://haiyong.site" target="blank">haiyong.site</a> </p> </div> <div id="power">00</div> </body>
div,
world的樣式
#world { background: #652e37; position: absolute; width: 100%; height: 100%; overflow: hidden; }
你點擊的時間越長,它打噴嚏的力度越大#按住並拖曳可轉身-為了讓它適應小螢幕,字體不會那麼小,將元素在小螢幕中分開佈局,我在這裡設定了媒體查詢。#instructions { position: absolute; width: 100%; top: 50%; margin: auto; margin-top: 120px; font-family: "Open Sans", sans-serif; color: #fdde8c; font-size: 0.8em; text-transform: uppercase; text-align: center; line-height: 1.5; user-select: none; } .lightInstructions { color: #f89a78; font-size: 1.6em; }登入後複製
@media screen and (max-width:600px) { #instructions { top: 50%; } .lightInstructions { font-size: 1.5em; } } @media screen and (max-width:470px) { #instructions { top: 60%; } .lightInstructions { font-size: 1.3em; } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
function init() { powerField = document.getElementById("power"); scene = new THREE.Scene(); scene.fog = new THREE.Fog(0x652e37, 350, 500); HEIGHT = window.innerHeight; WIDTH = window.innerWidth; aspectRatio = WIDTH / HEIGHT; fieldOfView = 60; nearPlane = 1; farPlane = 2000; camera = new THREE.PerspectiveCamera( fieldOfView, aspectRatio, nearPlane, farPlane ); camera.position.x = -300; camera.position.z = 300; camera.position.y = 100; camera.lookAt(new THREE.Vector3(0, 0, 0)); renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(WIDTH, HEIGHT); renderer.shadowMapEnabled = true; container = document.getElementById("world"); container.appendChild(renderer.domElement); windowHalfX = WIDTH / 2; windowHalfY = HEIGHT / 2; window.addEventListener("resize", onWindowResize, false); document.addEventListener("mouseup", handleMouseUp, false); document.addEventListener("touchend", handleTouchEnd, false); //* controls = new THREE.OrbitControls(camera, renderer.domElement); controls.minPolarAngle = -Math.PI / 2; controls.maxPolarAngle = Math.PI / 2; controls.noZoom = true; controls.noPan = true; //*/ }
HTML/CSS影片教學、JS影片教學
以上是一招教你使用HTML/CSS和Three.js的噴火龍小遊戲(程式碼分享)的詳細內容。更多資訊請關注PHP中文網其他相關文章!