用C语言写一个会动的小人代码
时间: 2023-12-29 20:21:30 浏览: 73
好的,以下是一个简单的用C语言实现的会动的小人代码:
```c
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void gotoxy(int x, int y) { // 定义光标移动函数
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int main() {
int x = 40, y = 10; // 小人的初始位置
int i;
while (1) { // 无限循环
gotoxy(x, y); // 将光标移动到小人的位置
printf(" O \n"); // 输出小人的头部
gotoxy(x, y+1);
printf(" /|\\ \n"); // 输出小人的身体
gotoxy(x, y+2);
printf(" / \\ \n"); // 输出小人的脚部
Sleep(1000); // 停留1秒钟
for (i = 0; i < 5; i++) { // 将小人向右移动5个字符位置
x++;
gotoxy(x, y);
printf(" O \n");
gotoxy(x, y+1);
printf(" /|\\ \n");
gotoxy(x, y+2);
printf(" / \\ \n");
Sleep(200); // 停留0.2秒钟
}
for (i = 0; i < 5; i++) { // 将小人向左移动5个字符位置
x--;
gotoxy(x, y);
printf(" O \n");
gotoxy(x, y+1);
printf(" /|\\ \n");
gotoxy(x, y+2);
printf(" / \\ \n");
Sleep(200); // 停留0.2秒钟
}
}
return 0;
}
```
这段代码会在控制台中输出一个会动的小人,小人会不断地向左右移动。在代码中,我们使用了Windows API中的`SetConsoleCursorPosition`函数将光标移动到指定位置,使用了`Sleep`函数来实现停留的效果。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)