最简单的飞机游戏
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
int main()
{
int i,j;
int x=5;
int y=10;
char input;
while(1)
{
system("cls");
for(i=1;i<=x;i++)
printf("\n");
for(j=1;j<=y;j++)
printf(" ");
printf("*");
printf("\n");
if(kbhit())
{
input=getch();
if(input=='a')
y--;
if(input=='d')
y++;
if(input=='w')
x--;
if(input=='s')
x++;
}
}
return 0;
}
请问为什么使用了getch()输入wsad后仍然需要按回车后才能移动 本帖最后由 jackz007 于 2021-1-16 12:14 编辑
我这里是即时响应的,不过,屏幕会不停地闪烁,我给修改了一下,只在按下按键的时候才会刷新屏幕。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void print(int x , int y)
{
int i ;
system("cls") ;
for(i = 1 ; i <= x ; i ++) printf("\n") ;
for(i = 1 ; i <= y ; i ++) printf(" ") ;
printf("*") ;
printf("\n") ;
}
int main(void)
{
int x = 5 , y = 10 ;
char c ;
print(x , y) ;
while(1) {
if(kbhit()) {
c = getch() ;
if(c == 'a' || c == 'd' || c == 'w' || c == 's') {
if(c == 'a') y -- ;
if(c == 'd') y ++ ;
if(c == 'w') x -- ;
if(c == 's') x ++ ;
print(x , y) ;
}
}
}
} 为什么判断条件改为if(!kbhit())就可了,这样改完了不是应该有输入的时候不移动吗 狂想曲丶 发表于 2021-1-16 12:14
为什么判断条件改为if(!kbhit())就可了,这样改完了不是应该有输入的时候不移动吗
难道你并不希望只是在按下 'a'、'd'、'w'、's'4 个字母的时候,屏幕上才会有动静吗? jackz007 发表于 2021-1-16 12:33
难道你并不希望只是在按下 'a'、'd'、'w'、's'4 个字母的时候,屏幕上才会有动静吗?
是希望在按下'w','s','a','d'的时候飞机移动啊,但是我不理解原理
页:
[1]