#include"判断方向.h" #include<iostream> #include<conio.h> #include<Windows.h> #include"蛇的移动.h" using namespace std; void updateWithInput(int(*canvas)[30], int Length, int Width, int moveDirection, int moveDirection1, int& newDirection) { char input; while (_kbhit()) { input = _getch(); } if (_kbhit())// 判断是否有输入 { input = _getch(); //根据用户的不同输入来移动,不必输入回车 if (input == 'w') { newDirection = 1;// 位置上移 moveDirection = 2; } else if (input == 's') { newDirection = 2; // 位置下移 moveDirection = 1; } else if (input == 'a') { newDirection = 3; // 位置左移 moveDirection = 4; } else if (input == 'd') { newDirection = 4; // 位置右移 moveDirection = 3; } if (newDirection != 0 && newDirection != moveDirection) { moveDirection = newDirection; } moveSnakeByDirection(canvas, Length, Width, moveDirection); } }严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C2061 语法错误: 标识符“Ckbhit” 贪吃蛇 F:\大一下\实训\贪吃蛇\贪吃蛇\判断方向.cpp 9
时间: 2024-03-12 15:43:37 浏览: 70
这段代码中出现了语法错误,编译器无法识别" _kbhit()"函数。这个函数是conio.h库中的函数,需要在代码开头加上这个头文件,如下所示:
```c++
#include"判断方向.h"
#include<iostream>
#include<conio.h> // 加上这个头文件
#include<Windows.h>
#include"蛇的移动.h"
using namespace std;
void updateWithInput(int(*canvas)[30], int Length, int Width, int moveDirection, int moveDirection1, int& newDirection) {
char input;
while (_kbhit())
{
input = _getch();
}
if (_kbhit())// 判断是否有输入
{
input = _getch(); //根据用户的不同输入来移动,不必输入回车
if (input == 'w')
{
newDirection = 1;// 位置上移
moveDirection = 2;
}
else if (input == 's') {
newDirection = 2; // 位置下移
moveDirection = 1;
}
else if (input == 'a') {
newDirection = 3; // 位置左移
moveDirection = 4;
}
else if (input == 'd') {
newDirection = 4; // 位置右移
moveDirection = 3;
}
if (newDirection != 0 && newDirection != moveDirection) {
moveDirection = newDirection;
}
moveSnakeByDirection(canvas, Length, Width, moveDirection);
}
}
```
阅读全文