物件導向貪吃蛇遊戲原始碼

2021-10-11 17:40:31 字數 3944 閱讀 2543

話不多說,直接上**。物件導向純用js做乙個貪吃蛇

html**

<

!doctype html>

"en"

>

"utf-8"

>

"viewport" content=

"width=device-width, initial-scale=1.0"

>

document<

/title>

"stylesheet" href=

"./css/snake.css"

>

<

/head>

="content"

>

="btn startbtn"

>

<

/button>

="btn pausebtn"

>

<

/button>

"snakewarp"

>

<

/div>

<

/div>

"./js/snake.js"

>

<

/script>

<

/body>

<

/html>

css**部分

body

.content

.btn

.startbtn

.pausebtn

#snakewarp

#snakewarp div

.snakehead

.snakebody

.snakefood

##最重要的js檔案

//面相物件 寫貪吃蛇。

var sw =20,

//方塊的 寬高 跟列數

sh =20,

tr =30,

td =30;

var snake =

null

,//蛇的例項

food =

null

,//食物的例項

game =

null

;//遊戲的例項

//方塊建構函式。

//宣告乙個方塊 傳入三個引數 座標 跟 名字

function

square

(x, y, classnames)

//建立方塊dom元素

square.prototype.

create

=function()

square.prototype.

remove

=function()

;//建立乙個蛇類

function

snake()

, right:

, left:

, down:

}//蛇的下一步的方向

}//初始化乙個物件 init 初始化 蛇的一些屬性

snake.prototype.

init

=function()

//獲取 蛇頭下乙個位置對應的元素 ,根據元素 做一些不同的事情

snake.prototype.

getnextpos

=function()

});if

(selfcollide ==

true

)//下個點是牆 代表遊戲結束 撞到了圍牆

if(nextpos[0]

<

0|| nextpos[1]

<

0|| nextpos[0]

>

(td -1)

|| nextpos[1]

>

(tr -1)

)//下個點是 食物 就吃 這個條件成立說明 蛇頭下乙個點 就是食物 那個點 所以就執行吃

if(food && food.pos[0]

== nextpos[0]

&& food.pos[1]

== nextpos[1]

)//下個點啥都沒有 就走

this

.strategies.move.

call

(this);

//改變this指向 繼續讓 this指向例項

}//處理碰撞之後要做的事情

snake.prototype.strategies =

console.

log(

this);

console.

log(

'走了走了');

},eat:

function()

, die:

function()

}snake =

newsnake()

;// snake.init();

// snake.getnextpos();

//建立乙個食物 類

function

createfood()

});}

//生成食物

food =

newsquare

(x, y,

"snakefood");

food.pos =

[x, y]

;//儲存生成食物的座標用於 跟蛇頭要走的下乙個點 做對比

//資料結構單例模式 比較高效 只是改變 food的座標 並不是每次都生成乙個新的然後刪除原來的食物

var fooddom = document.

queryselector

('.snakefood');

if(fooddom)

else

// food.create();

}//建立乙個遊戲的規則

function

game()

game.prototype.

init

=function()

else

if(ev.which ==

38&& snake.direction != snake.directionnum.down)

else

if(ev.which ==

39&& snake.direction != snake.directionnum.left)

else

if(ev.which ==

40&& snake.direction != snake.directionnum.up)

}this

.start()

;}game.prototype.

start

=function()

,200

)//這個就可以控制設移動的速度

}game.prototype.

over

=function()

game.prototype.

pause

=function()

//遊戲開始

var startbtn = document.

queryselector

('.startbtn');

startbtn.

onclick

=function()

//暫停功能

var snakewarp = document.

getelementbyid

('snakewarp');

var pausebtn = document.

queryselector

('.pausebtn');

snakewarp.

onclick

=function()

pausebtn.

onclick

=function()

貪吃蛇遊戲(附原始碼)

貪吃蛇遊戲,現在還有很多bug。等待大家挑錯。難度 1最難,500最簡單。吃夠20個食物就可以過關了 呵呵。無聊時候玩玩吧 include include include include const int maxn 100 const int n 20 struct node int map ma...

貪吃蛇遊戲(附原始碼)

貪吃蛇遊戲,現在還有很多bug。等待大家挑錯。難度 1最難,500最簡單。吃夠20個食物就可以過關了 呵呵。無聊時候玩玩吧 include include include include const int maxn 100 const int n 20 struct node int map ma...

貪吃蛇原始碼

去年五一寫的乙個貪吃蛇遊戲,在tc3下執行成功,過幾天加點注釋 大家先湊和看吧.hoho.include include include include include include define vk esc 0x11b define vk up 0x4800 define vk down 0x...