github: game-of-tetris
一圖勝千言
tetris
緣起
群里有人問, Sublime能不能做點好玩的東西?
當(dāng)然能啊!
技術(shù)分析
本質(zhì)上是一個Sublime的插件, 用現(xiàn)有插件的API, 來實現(xiàn)一個小游戲.
輸入
- 首先創(chuàng)建一個游戲的View, 在view.setting里面加一個游戲標(biāo)識(
isGameTetris
). - 監(jiā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
其實就是tetris.py
里面定義的class TetrisOperation(sublime_plugin.WindowCommand):
輸出
- 游戲的渲染, 是
tetris.py
里面的class TetrisRender(sublime_plugin.TextCommand):
- 其原理就是把view里面的東西刪除, 根據(jù)游戲數(shù)據(jù), 重新insert一個新的內(nèi)容進去.
- 目前是很戳的字符集, 后期可能用
MiniHTML
來替換這個部分.
其他則是游戲的實現(xiàn)過程, 和Sublime無關(guān)(略)
EOF