微信小程式 貪吃蛇

2021-09-27 03:22:18 字數 3269 閱讀 5869

效果圖:

貪吃蛇view

>

class

="score-box score-detail"

>

class

="scoredes"

>

得分text

>

class

="scorenum"

>

}text

>

view

>

class

="score-box score-detail"

>

class

="scoredes"

>

最高得分text

>

class

="scorenum"

>

}text

>

view

>

view

>

class

="ground"

>

wx:for

="}"

wx:for-item

="cols"

class

="rows"

>

wx:for

="}"

class

="box box_}"

>

view

>

view

>

view

>

class

="modal"

hidden

="}"

no-cancel

bindconfirm

="modalchange"

>

>

遊戲結束,重新開始嗎?view

>

modal

>

view

>

/* pages/snake/snake.wxss */

#snake

.score

.score-box

.title

.score-detail

.ground

.rows

.box

.box_1

.box_2

// pages/snake/snake.js

page(,

/** * 生命週期函式--監聽頁面載入

*/onload:

function

(options));

this

.initground

(this

.data.rows,

this

.data.cols)

;this

.initsnake(3

);this

.creatfood()

;this

.move()

;},//初始化操場

initground:

function

(rows, cols));

},//初始化蛇

initsnake:

function

(len)

this

.setdata()

},//生成食物

creatfood:

function()

)}, touchstart:

function

(e))},

touchmove:

function

(e));}

, touchend:

function()

else

if(math.

abs(x)

> math.

abs(y)

&& x <0)

else

if(math.

abs(x)

< math.

abs(y)

&& y <0)

else

this

.setdata()

},//計分器

storescore:

function()

) wx.

setstoragesync

('maxscore'

,this

.data.maxscore)}}

,//移動函式

move:

function()

,600)}

, changedir:

function

(dir)

let x = snakehead[0]

, y = snakehead[1]

;switch

(dir)

; snake[len -1]

=[x, y]

;this

.checkgame

(snaketail)

;for

(let i =

1; i < len; i++

)this

.setdata()

}}, checkgame:

function

(snaketail))}

if(snakehead[0]

==this

.data.food[0]

&& snakehead[1]

==this

.data.food[1]

));this

.storescore()

;this

.creatfood()

;}},

modalchange:

function()

);this

.onload()

;}})

完整**:

程式設計實現貪吃蛇小程式

include include include include include define key up w 向上方向鍵 define key down s 向下方向鍵 define key right d 向右方向鍵 define key left a 向左方向鍵 define key spac...

貪吃蛇小程式(功能完善)

經歷了幾個月對c和c 的學習,已經懂得基本原理的我準備自己寫乙個小專案了 先來look look成果吧!最後成品 一下操作均在vs2019中,vs低版本無礙,其他編譯器可能有出入 想寫乙個功能較為完備的貪吃蛇應該把它作為乙個專案去寫,而不僅僅是把所有 寫在乙個檔案裡,這應該是程式設計師最基本的意識了...

C 貪吃蛇程式

方向控制表 dirdirect dir 行進方向 可轉的方向0上 左右1下左右 2左上下3 右上下 include include include include include include include using namespace std int s 12,d 12,hard 3 dir...