目录背景需求构思逻辑构思代码结构小结 背景 题主现在是PHP程序员, 学了一周的golang, 深刻的感受到了其特性的优雅及功能的强大, 为了增强熟练度, 决定来写个贪吃蛇来践行下.
题主现在是PHP程序员, 学了一周的golang, 深刻的感受到了其特性的优雅及功能的强大, 为了增强熟练度, 决定来写个贪吃蛇来践行下.(底部有GitHub项目链接)
1. 确定元素
- 蛇
- 墙
- 食物
- 分数
- 基本的提示信息
2. 用户故事
- 蛇撞墙, 死亡
- 蛇吃蛋分数加1, 身体增加一格长度.
- 点击键盘左键, 蛇向左走
- 点击键盘右键, 蛇向右走
- 点击键盘上键, 蛇向上走
- 点击键盘下键, 蛇向下走
- 点esc, 退出游戏
元素及用户故事都确定了, 就要开始写代码吗? 写项目不是这样的!要践行以终为始(很重要!, 否则可能会造成代码的荒草丛生), 先去思考一下我们的代码结构是什么样子的.
以手持游戏机为例.
游戏机其实就是一个服务(Service), 然后屏幕和键盘统一由游戏机调配.
然后我们细分一下屏幕和键盘控制的元素:
//game control 游戏数据结构
type game struct {
//控制
control *control
//屏幕
screen *screen
}
//control 键盘控制
type control struct {
moveChannel chan int
quitChannel chan int
playGameStatusChannel chan bool
gameOver bool
direction int
}
//screen 屏幕相关参数
type screen struct {
snakes *snake
foodPoint *scope
width int
height int
score int
}
//NewGameService 实例化游戏服务
func NewGameService() *gameService {
return &gameService{screenApp: newScreenApp(), monitorApp: newMonitorApp()}
}
//newScreenApp 屏幕实例化
func newScreenApp() *screenApp {
return &screenApp{Screen: initScreenHandle()}
}
//newMonitorApp 实例化
func newMonitorApp() *monitorApp {
return &monitorApp{Monitor: initMonitor()}
}
个人认为项目的代码的结构写的还算清晰,所以不放过多代码了, 只是把一个全局的结构图景放到这里, 留给你去探索. 这个小项目的代码逻辑肯定还不完善,你如果有什么想法或者吐槽, 可以在下方留言,每个我都会认真阅读和回复.?
最后放上项目链接(?????? 感觉不错, 别忘star哦 ??????): github.com/TheOnlines/…
--结束END--
本文标题: golang 手写贪吃蛇示例实现
本文链接: https://www.lsjlt.com/news/164847.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0