github: game-of-tetris
一圖勝千言

tetris
緣起
群里有人問(wèn), Sublime能不能做點(diǎn)好玩的東西?
當(dāng)然能啊!
技術(shù)分析
本質(zhì)上是一個(gè)Sublime的插件, 用現(xiàn)有插件的API, 來(lái)實(shí)現(xiàn)一個(gè)小游戲.
輸入
- 首先創(chuàng)建一個(gè)游戲的View, 在view.setting里面加一個(gè)游戲標(biāo)識(shí)(
isGameTetris). - 監(jiān)聽(tīng)鍵盤(pán)事件. 如果
"key": "setting.isGameTetris"等于("operator": "equal")真("operand": true), 那么觸發(fā)tetris_operation.
{ "keys": ["up"], "command": "tetris_operation", "args": {"operation": "up"}, "context":[
{ "key": "setting.isGameTetris", "operator": "equal", "operand": true }
]},
{ "keys": ["down"], "command": "tetris_operation", "args": {"operation": "down"}, "context":[
{ "key": "setting.isGameTetris", "operator": "equal", "operand": true }
]},
{ "keys": ["left"], "command": "tetris_operation", "args": {"operation": "left"}, "context":[
{ "key": "setting.isGameTetris", "operator": "equal", "operand": true }
]},
{ "keys": ["right"], "command": "tetris_operation", "args": {"operation": "right"}, "context":[
{ "key": "setting.isGameTetris", "operator": "equal", "operand": true }
]},
-
tetris_operation其實(shí)就是tetris.py里面定義的class TetrisOperation(sublime_plugin.WindowCommand):
輸出
- 游戲的渲染, 是
tetris.py里面的class TetrisRender(sublime_plugin.TextCommand): - 其原理就是把view里面的東西刪除, 根據(jù)游戲數(shù)據(jù), 重新insert一個(gè)新的內(nèi)容進(jìn)去.
- 目前是很戳的字符集, 后期可能用
MiniHTML來(lái)替換這個(gè)部分.
其他則是游戲的實(shí)現(xiàn)過(guò)程, 和Sublime無(wú)關(guān)(略)
EOF