用js編寫簡(jiǎn)單的貪吃蛇小游戲
本文實(shí)例為大家分享了js編寫簡(jiǎn)單的貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
代碼如下:
HTML 5 部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>貪恰蛇</title>
<style>
#scence{
width: 800px;
height: 600px;
border: 1px solid #000;
margin: 50px auto;
background-color: aliceblue;
position: relative;
overflow: hidden;
}
.head{
position: absolute;
width: 20px;
height: 20px;
background-color: #000;
}
.tail{
position: absolute;
width: 20px;
height: 20px;
background-color: grey;
}
</style>
</head>
<body>
<div id="scence">
</div>
</body>
</html>
<script src="tools.js"></script>
<script src="../貪吃蛇/snake.js"></script>
<script src="food.js"></script>
<script src="game.js"></script>
js部分
tools.js
function getStyle(ele, styleObj) {
for (const key in styleObj) {
ele.style[key] = styleObj[key];
}
}
function getRandom(a, b) {
return Math.floor(Math.random() * (b - a) + a +1)
}
snake.js
function Snake() {
this.scence = document.querySelector('#scence');
this.body = [
[0, 0, 'grey', null],
[0, 1, 'grey', null],
[0, 2, '#000', null]
];
this.dir = 'right';
this.lastdir = 'right';
this.width = 20;
this.height = 20;
this.scence_w = scence.offsetWidth;
this.scence_h = scence.offsetHeight;
}
Snake.prototype.found = function () {
for (let i = 0; i < this.body.length; i++) {
if (this.body[i][3] == null) {
this.body[i][3] = document.createElement('div');
}
getStyle(this.body[i][3], {
width: this.width + 'px',
height: this.height + 'px',
position: 'absolute',
top: this.height * (this.body[i][0]) + 'px',
left: this.width * (this.body[i][1]) + 'px',
backgroundColor: this.body[i][2]
});
this.scence.appendChild(this.body[i][3]);
}
}
//運(yùn)動(dòng)函數(shù)
Snake.prototype.move = function () {
var length = this.body.length;
for (let i = 0; i < length - 1; i++) {
this.body[i][0] = this.body[i + 1][0];
this.body[i][1] = this.body[i + 1][1];
}
let snakehead = this.body[length - 1]
switch (this.dir) {
case 'right':
snakehead[1] += 1;
break;
case 'left':
snakehead[1] -= 1
break;
case 'up':
snakehead[0] -= 1
break;
case 'down':
snakehead[0] += 1
break;
}
this.lastdir = this.dir;
snake.found();
}
//計(jì)時(shí)運(yùn)動(dòng)
Snake.prototype.shift = function () {
document.onkeydown = (e) => {
e = e || window.event;
let key = e.keyCode;
switch (key) {
case 39:
if (this.lastdir == 'left') {
this.dir = 'left'
} else {
this.dir = 'right'
};
break;
case 37:
if (this.lastdir == 'right') {
this.dir = 'right'
} else {
this.dir = 'left'
};
break;
case 38:
if (this.lastdir == 'down') {
this.dir = 'down'
} else {
this.dir = 'up'
};
break;
case 40:
if (this.lastdir == 'up') {
this.dir = 'up'
} else {
this.dir = 'down'
};
break;
}
}
}
//游戲結(jié)束
Snake.prototype.over = function () {
let top = this.body[this.body.length - 1][0];
let left = this.body[this.body.length - 1][1];
let width = this.scence_w / this.width - 1;
let height = this.scence_w / this.height - 1;
if (top < 0 || left < 0 || top > width || left > height) {
clearInterval(timeid)
alert('game over');
}
for (let i = 0; i < this.body.length - 1; i++) {
if (top == this.body[i][0] && left == this.body[i][1]) {
clearInterval(timeid)
alert('game over');
}
}
}
let snake = new Snake();
snake.found();
snake.shift();
timeid = setInterval(function () {
snake.move();
food.eat();
snake.over()
}, 100)
food.js
function Food() {
this.scence = document.querySelector('#scence');
this.width = 20;
this.height = 20;
this.body = [-1, -1, 'red', null];
this.scence_w = scence.offsetWidth;
this.scence_h = scence.offsetHeight;
}
//食物生成
Food.prototype.crteate = function () {
this.body[1] = getRandom(0, this.scence_w / this.width-1);
this.body[0] = getRandom(0, this.scence_h / this.height-1);
this.body[3] = document.createElement('div');
getStyle(this.body[3], {
width: this.width + 'px',
height: this.height + 'px',
position: 'absolute',
top: this.height * (this.body[0] ) + 'px',
left: this.width * (this.body[1] ) + 'px',
backgroundColor: this.body[2],
borderRadius: ' 50%',
});
this.scence.appendChild(this.body[3]);
}
//蛇吃到食物
Food.prototype.eat=function(){
// const new=[0, 0, 'grey', null]
if(snake.body[snake.body.length-1][0]==this.body[0] && snake.body[snake.body.length-1][1]==this.body[1]){
this.scence.removeChild(this.body[3]);
this.crteate();
snake.body.unshift([-1,-1,"grey",null])
}
}
let food = new Food();
food.crteate();
food.eat();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
手把手教你用JS實(shí)現(xiàn)回車評(píng)論功能
最近在寫一個(gè)問(wèn)答功能,類似于評(píng)論,下面這篇文章主要給大家介紹了關(guān)于如何用JS實(shí)現(xiàn)回車評(píng)論功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
js實(shí)現(xiàn)遍歷含有input的table實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)遍歷含有input的table方法,結(jié)合實(shí)例形式分析了jsp讀取數(shù)據(jù)庫(kù)動(dòng)態(tài)生成table及JavaScript遍歷table的相關(guān)技巧,需要的朋友可以參考下2015-12-12
原生JS實(shí)現(xiàn)簡(jiǎn)單的無(wú)縫自動(dòng)輪播效果
輪播效果是老生常談的話題,今天小編通過(guò)實(shí)例代碼給大家分享原生JS實(shí)現(xiàn)簡(jiǎn)單的無(wú)縫自動(dòng)輪播效果,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2018-09-09
前端存儲(chǔ)后端響應(yīng)數(shù)據(jù)超詳細(xì)的實(shí)現(xiàn)方式和注意事項(xiàng)
前端通過(guò)多種方式向后端獲取數(shù)據(jù),下面這篇文章主要介紹了前端存儲(chǔ)后端響應(yīng)數(shù)據(jù)超詳細(xì)的實(shí)現(xiàn)方式和注意事項(xiàng),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
networkInformation.downlink測(cè)用戶網(wǎng)速方法詳解
這篇文章主要為大家介紹了networkInformation.downlink測(cè)用戶網(wǎng)速方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
JavaScript中獲取隨機(jī)數(shù)的幾種方法小結(jié)
本文總結(jié)了JavaScript中獲取隨機(jī)數(shù)的幾種方法,包括Math.random()、生成指定范圍的隨機(jī)數(shù)和從數(shù)組中隨機(jī)選擇一個(gè)元素,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
JS+DIV+CSS實(shí)現(xiàn)仿表單下拉列表效果
這篇文章主要介紹了JS+DIV+CSS實(shí)現(xiàn)仿表單下拉列表效果,涉及javascript鼠標(biāo)事件及頁(yè)面元素的動(dòng)態(tài)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
Redux中進(jìn)行異步操作(網(wǎng)絡(luò)請(qǐng)求)的示例方案
這篇文章主要介紹了Redux中進(jìn)行異步操作(網(wǎng)絡(luò)請(qǐng)求)的方案,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

