,:增强熟练度,决定来写个贪吃蛇来践行下.

背景

题主如今是php程序员,学了一周的golang,深刻的感遭到了其特点的高贵及功能的强悍,为了提高熟练度,决定来写个贪吃蛇来弘扬下.(顶部有github项目链接)

需求构思

1. 确定元素
    --- 食物
    - 分数
    - 基本的提示信息
2. 用户故事
    - 蛇撞墙, 死亡
    - 蛇吃蛋分数加1, 身体增加一格长度.
    - 点击键盘左键, 蛇向左走
    - 点击键盘右键, 蛇向右走
    - 点击键盘上键, 蛇向上走
    - 点击键盘下键, 蛇向下走
    - 点esc, 退出游戏

逻辑构思

元素及用户故事都确定了,就要开始写代码吗?写项目不是这样的!要弘扬以终为始(很重要!,否则可能会导致代码的浓荫丛生),先去思索一下我们的代码结构是哪些样子的.

以手持游戏机为例.

之后我们细分一下屏幕和鼠标控制的元素:

代码结构

//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哦):

本作品采用《CC合同》,转载必须标明作者和本文链接

© 版权声明
THE END
喜欢就支持一下吧
点赞157赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容