|  | 
 
 
 楼主|
发表于 2022-3-15 23:30:56
|
显示全部楼层 
| 复制代码int direct(char ch) {                // 方向键设置 
        if(ch == -32) {                        
                ch = getch();
                if(ch == 72) return 1;        
                if(ch == 80) return 3;
                if(ch == 75) return 2;
                if(ch == 77) return 4; 
        }
        return 0;                                // ?? 这个返回的 0 会被怎么处理呢 
}
 复制代码// --------------------------------------------------
void key(snake *head, snake *p) {                // 改变蛇的方向与暂停功能的实现
        char ch = 0;
        int i, j, a;
        i = j = a = 0;
        if(_kbhit()) {                                                // 如果发生了键盘输入,就执行函数体 
                ch = getch();
                if(ch == -32) {                                        // (problem) -32 是什么                         
                        i = direct(ch);                                
                        j = p->direction;                        
                        if((i == 1 && j == 3) || (i == 2 && j == 4) || (i == 3 && j == 1) || (i == 4 && j == 2));        // 判断是否与原有方向冲突 
                        else p->direction = i;
                }
                else if(ch == 32) {                                // 32 是空格的编码                        
                        cls();                                                // 这个是清屏                         
                        gotoxy(36, 10);
                        printf("暂停中");
                        while (1) {
                                if(kbhit() && getch() == 32) {
                                        gotoxy(36, 10);
                                        printf("      ");                // 擦除“暂停中” 
                                        draw(head);                                // 恢复蛇 
                                        gotoxy(F.x, F.y);                // 恢复食物 
                                        c(12);
                                        printf("o");                        
                                        for(a = 0; a < ai; ++a) {
                                                gotoxy(F1[a].x, F1[a].y);
                                                c(10);
                                                printf("#");
                                        }
                                        break;
                                }
                        }
                }
        } 
}
 | 
 |