|
发表于 2021-1-16 12:13:12
|
显示全部楼层
本帖最后由 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) ;
- }
- }
- }
- }
复制代码 |
|