純JS單頁面賽車游戲制作代碼分享
分享一個(gè)以前寫的小游戲,純js游戲,代碼很簡單。歡迎大家來噴呦!
效果圖:
代碼展示://直接復(fù)制到html文件即可 支持IE9+版本
<!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> <title>Best Fly Car</title> <style> input { border: 1px solid yellow; margin-left: 20px; margin-top: 20px; } .way-content { margin-left: auto; margin-right: auto; width: 1024px; height: 700px; position: relative; border: 1px solid burlywood; background-color: darkolivegreen; } .car { color:black; width: 20px; height: 20px; border: 1px solid brown; background: greenyellow; position: absolute; left: 502px; top: 678px; text-align: center; z-index: 99; } .C-car { color:black; width: 20px; height: 15px; border: 1px solid brown; background: red; position: absolute; left: 502px; top: 0px; text-align: center; z-index: 98; } .way-grid { margin: 0 0; margin-left: 412px; background-color: grey; border: none; border-right: 1px solid brown; border-left: 1px solid brown; min-width: 100px; max-width: 1024px; width: 200px; height: 10px; color: white; font-size: 5px; position: relative; text-align: center; } .go { background: greenyellow; width: 100px; height: 50px; text-align: center; } </style> </head> <body> <div id="way-content" class="way-content"> <div id="car" class="car">++</div> </div> <br /> The width of the road:<input id="wayWhite" type="number" value="200" /> <br /> Keyboard sensitivity: <input type="number" id="LMD" value="10" /><br /> The game difficulty:<input type="number" id="ND" value="5" /><br /> <input class="go" id="go" type="button" value="go" /><br /> <script type="text/javascript"> var wayLeft = [], cO = null, LC = null, RC = null, st = null, clickLR = false, s = null, LMD = 5, wayWhite = 200, ND = 15, //n*2+1; NDArry = [0, 1, -1], gridStr = 'way-grid-', PX = 'px', length = 70, NDIndex = 30, waysetIndex = 10, waysetValue = (1024 - wayWhite) / 2, delvalue = 0, n = 5, waitvalue = 10 / n, checkOut = function () { var index = wayLeft[wayLeft.length - 1].wayLeft, CCarvalue = wayLeft[wayLeft.length - 1].CCarvalue + index, carIndex = parseInt(document.getElementById('car').offsetLeft) + 10; if (carIndex < index + wayWhite && carIndex > index && (CCarvalue == 0 || !(CCarvalue < carIndex && CCarvalue + 20 > carIndex))) { return true; } else { clearInterval(st); clearInterval(cO); clearInterval(LC); clearInterval(RC); var e = new Date(); alert('totle :' + parseInt((e.getTime() - s.getTime()) / 600) + 's'); document.onkeydown = null; document.onkeyup = null; clearInterval(cO); clearInterval(LC); clearInterval(RC); } }, //初始道路坐標(biāo) getFitstArray = function () { waysetValue = (1 - wayWhite) / 2; for (var i = 0; i < length; i++) { wayLeft[i] = {}; wayLeft[i].wayLeft = (1024 - wayWhite) / 2; } }, //初始化道路 setWay = function () { var docElem = document.createDocumentFragment(); for (var i = 0; i < length; i++) { var grid = document.createElement('div'); grid.className = 'way-grid'; grid.id = gridStr + i; grid.textContent = '|'; docElem.appendChild(grid); } document.getElementById('way-content').appendChild(docElem); }, //生成下一個(gè)道路 getNextL = function (firstL) { var CCarvalue = 0; //漸變道路 if (waysetIndex > 1) { waysetIndex -= 1; waysetValue = firstL + delvalue; } else if (waitvalue > 0) { //直線緩沖道路 waitvalue--; waysetValue = firstL; } else { //漸變規(guī)則 delvalue = NDArry[parseInt(Math.random() * 3)] * parseInt(n / 2 + 1); CCarvalue = parseInt(Math.random() * wayWhite); waysetIndex = NDIndex; waitvalue = 100 / n; waysetValue = firstL + delvalue; } return waysetValue >= 0 && waysetValue <= (1024 - wayWhite) ? { wayLeft: waysetValue, CCarvalue: CCarvalue } : { wayLeft: firstL, CCarvalue: CCarvalue }; }, //根據(jù)數(shù)組刷新道路 getWayByArry = function () { var CCarvalueList = document.getElementsByClassName('C-car'); while (CCarvalueList[0]) { CCarvalueList[0].remove(); } for (var i = 0; i < wayLeft.length; i++) { var grid = document.getElementById(gridStr + i); grid.style['width'] = wayWhite + PX; grid.style['margin-left'] = wayLeft[i].wayLeft + PX; if (wayLeft[i].CCarvalue) { var CCarvalue = document.createElement('div'); CCarvalue.id = 'CCarvalue' + i; CCarvalue.className = 'C-car'; CCarvalue.textContent = '+'; CCarvalue.style['left'] = wayLeft[i].CCarvalue + PX; grid.appendChild(CCarvalue); } } }, //左鍵事件 lClick = function () { if (document.onkeydown) { var a = document.getElementById('car') a.style['left'] = (a.offsetLeft - LMD) + PX; } }, //右鍵事件 rClick = function () { if (document.onkeydown) { var a = document.getElementById('car') a.style['left'] = (a.offsetLeft + LMD) + PX; } }, //確定事件 goClick = function () { clearInterval(st); clearInterval(cO); clearInterval(LC); clearInterval(RC); document.onkeydown = null; document.onkeyup = null; LMD = parseInt(document.getElementById('LMD').value) / 4; wayWhite = parseInt(document.getElementById('wayWhite').value); ND = parseInt(document.getElementById('ND').value) * 1 + 1; NDIndex = 30; getFitstArray(); getWayByArry(); s = new Date(); document.onkeydown = function (evt) { var evt = window.event ? window.event : evt; if (clickLR) { } else if (evt.keyCode == 37) { //鎖定當(dāng)前按鍵 clickLR = true; LC = setInterval(lClick, 10); } else if (evt.keyCode == 39) { //鎖定當(dāng)前按鍵 clickLR = true; RC = setInterval(rClick, 10); } }; document.onkeyup = function (evt) { //清除鎖定 clickLR = false; clearInterval(LC); clearInterval(RC); } document.getElementById('car').style['left'] = 502 + PX; st = setInterval(stratBC, 100 / ND); cO = setInterval(checkOut, 10); }, stratBC = function () { setWayArray(); getWayByArry(); }, setWayArray = function () { var firstL = wayLeft[0], nextL = (1024 - wayWhite) / 2; nextL = getNextL(firstL.wayLeft); for (var i = 0; i < wayLeft.length; i++) { firstL = wayLeft[i]; wayLeft[i] = nextL; nextL = firstL; } }; getFitstArray(); setWay(); getWayByArry(); document.getElementById('go').onclick = goClick; </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
momentjs獲取上周、上月、前三個(gè)月的起始和結(jié)束時(shí)間(附完整代碼)
這篇文章主要給大家介紹了關(guān)于momentjs獲取上周、上月、前三個(gè)月的起始和結(jié)束時(shí)間的相關(guān)資料,在需要你進(jìn)行日期處理的地方,必然少不了moment.js的使用,需要的朋友可以參考下2023-07-07js實(shí)現(xiàn)簡單的倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡單的倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01使用TypeScript?V8來改進(jìn)您的JavaScript代碼
TypeScript?V8是一個(gè)強(qiáng)大的JavaScript類型系統(tǒng),它可以幫助你發(fā)現(xiàn)JavaScript代碼中的錯(cuò)誤和潛在問題,并在編譯時(shí)捕獲它們,以便您可以解決它們,TypeScript?V8為JavaScript提供一系列的類型注釋,包括自定義類型和其他高級功能2023-08-08使用TypeScript接口優(yōu)化數(shù)據(jù)結(jié)構(gòu)的示例詳解
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)結(jié)構(gòu)的設(shè)計(jì)至關(guān)重要,它直接影響到程序的性能和可維護(hù)性,TypeScript 作為一種靜態(tài)類型的超集,為 JavaScript 帶來了類型系統(tǒng),本文將探討如何利用 TypeScript 的接口(Interfaces)來優(yōu)化數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2024-09-09一文帶你搞懂JavaScript中轉(zhuǎn)義字符的使用
說起轉(zhuǎn)義字符,大家最先想到的肯定是使用反斜杠,這也是我們最常見的,很多編程語言都支持。除了反斜杠以外,在前端開發(fā)中,還有其他幾種轉(zhuǎn)義字符,也是較常見的,本文將對這些做一個(gè)總結(jié)2023-02-02JavaScript仿淘寶實(shí)現(xiàn)固定右側(cè)側(cè)邊欄
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)仿淘寶固定右側(cè)側(cè)邊欄效果,文中示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2022-03-03