func initialize() { // 初始化蛇的位置和方向、首尾;初始化随机数 head, tail = loct{4, 4}, loct{4, 4} lead, size = 'R', 1 area[4][4] = 'H' rand.Seed(int64(time.Now().Unix())) defer syscall.FreeLibrary(kernel32) SetConsoleTextAttribute(hCon, 0x0075) // 输出初始画面 fmt.Println(" ^0^$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$^0^ ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" || || ") fmt.Println(" ^0^$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$^0^ ") // 使用一个单独的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 init() { // 初始化蛇的位置和方向、首尾;初始化随机数 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' } } }() }