该程序正在检测右键盘键,但是当我尝试通过按键盘上的箭头移动对象时,但是当我这样做时,无论我按哪个箭头,它都会进入同一行。我正在寻求帮助以将这个对象移动到不同的位置。
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
COORD coord={0, 0};
struct Ship{
int x,y;
}Ship;
struct Ship S;
void gotoxy (int x, int y){
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void print()
{
system("CLS");
coord.X = 0;
coord.Y = 0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
printf (">");
}
int main(){
time_t last_change = clock();
int game=1;
int speed=300;
print();
int x=0, y=0;
while (game==1){
if (kbhit()){
int c = getch();
//printf("%d",c);
if (c==224){
c = getch();
//printf("%d",c);
switch (c){
case 72: {y--;printf(">");}
break;
case 80: {y++;printf(">");}
break;
case 77: {x++;printf(">");}
break;
case 75: {x--;printf(">");}
break;
}
}
};
last_change= clock();
}
}