<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ~~~ package main import ( "fmt" "math/rand" "os" "time" ) /* #include <windows.h> #include <conio.h> // 使用了WinAPI來移動控制臺的光標 void gotoxy(int x,int y) { COORD c; c.X=x,c.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); } // 從鍵盤獲取一次按鍵,但不顯示到控制臺 int direct() { return _getch(); } */ import "C" // go中可以嵌入C語言的函數 // 表示光標的位置 type loct struct { i, j int } var ( area = [20][20]byte{} // 記錄了蛇、食物的信息 food bool // 當前是否有食物 lead byte // 當前蛇頭移動方向 head loct // 當前蛇頭位置 tail loct // 當前蛇尾位置 size int // 當前蛇身長度 ) // 隨機生成一個位置,來放置食物 func place() loct { k := rand.Int() % 400 return loct{k / 20, k % 20} } // 用來更新控制臺的顯示,在指定位置寫字符,使用錯誤輸出避免緩沖 func draw(p loct, c byte) { C.gotoxy(C.int(p.i*2+4), C.int(p.j+2)) fmt.Fprintf(os.Stderr, "%c", c) } func init() { // 初始化蛇的位置和方向、首尾;初始化隨機數 head, tail = loct{4, 4}, loct{4, 4} lead, size = 'R', 1 area[4][4] = 'H' rand.Seed(int64(time.Now().Unix())) // 輸出初始畫面 fmt.Fprintln(os.Stderr, ` #-----------------------------------------# | | | | | | | | | * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | #-----------------------------------------# `) // 我們使用一個單獨的go程來捕捉鍵盤的動作,因為是單獨的,不怕阻塞 go func() { for { // 函數只寫入lead,外部只讀取lead,無需設鎖 switch byte(C.direct()) { case 72: lead = 'U' case 75: lead = 'L' case 77: lead = 'R' case 80: lead = 'D' case 32: lead = 'P' } } }() } func main() { // 主程序 for { // 程序更新周期,400毫秒 time.Sleep(time.Millisecond * 400) // 暫停,還是要有滴 if lead == 'P' { continue } // 放置食物 if !food { give := place() if area[give.i][give.j] == 0 { // 食物只能放在空閑位置 area[give.i][give.j] = 'F' draw(give, '$') // 繪制食物 food = true } } // 我們在蛇頭位置記錄它移動的方向 area[head.i][head.j] = lead // 根據lead來移動蛇頭 switch lead { case 'U': head.j-- case 'L': head.i-- case 'R': head.i++ case 'D': head.j++ } // 判斷蛇頭是否出界 if head.i < 0 || head.i >= 20 || head.j < 0 || head.j >= 20 { C.gotoxy(0, 23) // 讓光標移動到畫面下方 break // 跳出死循環 } // 獲取蛇頭位置的原值,來判斷是否撞車,或者吃到食物 eat := area[head.i][head.j] if eat == 'F' { // 吃到食物 food = false // 增加蛇的尺寸,并且不移動蛇尾 size++ } else if eat == 0 { // 普通移動 draw(tail, ' ') // 擦除蛇尾 // 注意我們記錄了它移動的方向 dir := area[tail.i][tail.j] // 我們需要擦除蛇尾的記錄 area[tail.i][tail.j] = 0 // 移動蛇尾 switch dir { case 'U': tail.j-- case 'L': tail.i-- case 'R': tail.i++ case 'D': tail.j++ } } else { // 撞車了 C.gotoxy(0, 23) break } draw(head, '*') // 繪制蛇頭 } // 收尾了 switch { case size < 22: fmt.Fprintf(os.Stderr, "Faild! You've eaten %d $\\n", size-1) case size < 42: fmt.Fprintf(os.Stderr, "Try your best! You've eaten %d $\\n", size-1) default: fmt.Fprintf(os.Stderr, "Congratulations! You've eaten %d $\\n", size-1) } } //該片段來自于http://outofmemory.cn ~~~ ![](https://box.kancloud.cn/032176b8778dbc41f1ce6b79a83d59c1_900x350.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看