Go + C 一款简单的贪吃蛇
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Go + C 一款简单的贪吃蛇
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                前言
這是一款運行在window上簡單貪吃蛇,需要電腦上gcc編譯C語言代碼,可以參考win10下安裝gcc/g++
代碼【下載】
package mainimport ("fmt""math/rand""os""snake/Clib""strconv""time" )// 全局常量 const (WIDTH = 40 // 地圖寬度HEIGHT = 20 // 地圖高度LENGTH = 2 // 蛇長度DIRECTION = 'R' // 方向 )// 全局變量 var (food Food // 食物delay time.Duration = time.Second / 2 // 速度score int = 0 // 分?jǐn)?shù) )// 初始化父類坐標(biāo) type Position struct {X intY int }// 初始化蛇子類 type Snake struct {len int // 長度dir int // 方向pos [WIDTH * HEIGHT]Position // 定義數(shù)組,存儲每一節(jié)蛇的坐標(biāo) }// 初始化食物子類 type Food struct {Position }// 初始化游戲地圖 func MapInit() {fmt.Fprintln(os.Stderr, `貪吃蛇#----------------------------------------#| || || || || || || || || || || || || || || || || || || || |#----------------------------------------# `) }// 初始化蛇信息 func (s *Snake) SnakeInit() {s.len = LENGTH // 蛇長度s.dir = DIRECTION // 蛇方向// 蛇位置s.pos[0].X = WIDTH / 2s.pos[0].Y = HEIGHT / 2s.pos[1].X = WIDTH/2 - 1s.pos[1].Y = HEIGHT / 2s.snake() // 繪制蛇s.getInput() // 接受鍵盤輸入 }// 繪制蛇 func (s *Snake) snake() {for i := 0; i < s.len; i++ {var ch byte// 區(qū)分蛇頭和身體if i == 0 {ch = '@'} else {ch = '*'}Show(s.pos[i].X, s.pos[i].Y, ch)} }// 接受指定鍵盤按鍵輸入 func (s *Snake) getInput() {go func() {for {switch Clib.Direction() {case 80, 68, 100:if s.dir != 'U' {s.dir = 'D'}case 72, 85, 117:if s.dir != 'D' {s.dir = 'U'}case 77, 82, 114:if s.dir != 'L' {s.dir = 'R'}case 75, 76, 108:if s.dir != 'R' {s.dir = 'L'}case 32:s.dir = 'P'}}}() }// 蛇運動 func (s *Snake) Play() {// 游戲流程控制for {FLAG:time.Sleep(delay) // 延遲調(diào)用if s.dir == 'P' {goto FLAG}if s.check() == false { // 生命檢測break}s.eating() // 享受美食s.changeSpeed() // 改變速度s.updatePos() // 更新蛇位置s.snake() // 繪制蛇的UI} }// 生命檢測 func (s *Snake) check() bool {// 蛇和墻碰撞if s.pos[0].X <= 2 || s.pos[0].X >= WIDTH+3 || s.pos[0].Y <= 2 || s.pos[0].Y >= HEIGHT+3 {return false}// 蛇頭和身體碰撞for i := 1; i < s.len; i++ {if s.pos[0].X == s.pos[i].X && s.pos[0].Y == s.pos[i].Y {return false}}return true }// 更新位置 func (s *Snake) updatePos() {Show(s.pos[s.len-1].X, s.pos[s.len-1].Y, ' ') // 將末尾置空// 更新蛇身體坐標(biāo)for i := s.len - 1; i > 0; i-- {s.pos[i].X = s.pos[i-1].Xs.pos[i].Y = s.pos[i-1].YShow(s.pos[i].X, s.pos[i].Y, '*')}// 根據(jù)方向改變蛇頭位置switch s.dir {case 'U':s.pos[0].Y += -1case 'D':s.pos[0].Y += 1case 'L':s.pos[0].X += -1case 'R':s.pos[0].X += 1}Show(s.pos[0].X, s.pos[0].Y, '@') // 繪制蛇頭}// 享受美食 func (s *Snake) eating() {if s.pos[0].X == food.X && s.pos[0].Y == food.Y {s.len++ // 身體增加s.randomFood() // 創(chuàng)建隨機食物score ++s.showScore() // 顯示等分} }// 根據(jù)得分改變速度 func (s *Snake) changeSpeed() {delay = time.Second / (time.Duration(score/20) + 3) }// 隨機食物 func (s *Snake) randomFood() {// 隨機食物for i := 1; i < s.len; i++ {food.X = RandInt(3, WIDTH+3)food.Y = RandInt(3, HEIGHT+3)if food.X == s.pos[i].X && food.Y == s.pos[i].Y {continue}}Show(food.X, food.Y, '$') // 顯示食物 }// 顯示信息 func Show(X int, Y int, ch byte) {Clib.GotoPosition(X, Y) // 調(diào)用C語言代碼設(shè)置控制光標(biāo)位置fmt.Fprintf(os.Stderr, "%c", ch) // 將字符繪制在界面中 }// 兩者之間隨機數(shù) 左閉右開 func RandInt(min, max int) int {if min >= max || min == 0 || max == 0 {return max}return rand.Intn(max-min) + min }// 顯示等分 func (s *Snake) showScore() {Clib.GotoPosition(2, 24)fmt.Fprintf(os.Stderr, "%s", "分?jǐn)?shù):"+strconv.Itoa(score)) }// 游戲結(jié)束 func (s *Snake) gameOver() {// 擦除食物Clib.GotoPosition(food.X, food.Y)fmt.Fprintf(os.Stderr, "%s", " ")// 顯示Game OverClib.GotoPosition(18, 10)fmt.Fprintf(os.Stderr, "%s", "Game Over")// 顯示等分Clib.GotoPosition(22, 12)fmt.Fprintf(os.Stderr, "%s", strconv.Itoa(score))// 暫停關(guān)閉time.Sleep(time.Second * 10) }func main() {rand.Seed(time.Now().UnixNano()) // 創(chuàng)建隨機數(shù)種子Clib.HideCursor() // 隱藏控制臺光標(biāo)MapInit() // 初始化地圖var s Snake // 創(chuàng)建蛇對象s.SnakeInit() // 初始化蛇信息s.randomFood() // 隨機食物s.Play() // 開始游戲s.gameOver() // 游戲結(jié)束 } package Clib/* #include<windows.h> #include<conio.h> //使用WInAPI來移動控制臺光標(biāo) void gotoxy(int x,int y){COORD c;c.X=x,c.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); } //從鍵盤獲取一次按鍵但不顯示在控制臺 int direct(){return _getch(); } //去控制臺光標(biāo) void hideCursor(){CONSOLE_CURSOR_INFO cci;cci.bVisible=FALSE;cci.dwSize=sizeof(cci);SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cci); } */ import "C" // go可以嵌入C語言的函數(shù)// 設(shè)置控制臺光標(biāo)位置 func GotoPosition(X int, Y int) {C.gotoxy(C.int(X), C.int(Y)) }// 無顯示鍵盤輸入字符 func Direction() (key int) {key = int(C.direct())return }// 隱藏鼠標(biāo) func HideCursor() {C.hideCursor() }?
備注:代碼學(xué)習(xí)參考黑馬《go語言與區(qū)塊鏈精品入門課程視頻》視頻課程
總結(jié)
以上是生活随笔為你收集整理的Go + C 一款简单的贪吃蛇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: LaTex用模板的时候图片的captio
 - 下一篇: 在.NET中进行AutoCAD二次开发(