求有意思的程序
大家有没有一些好玩的程序,求上传,我想让自己更热爱编程,寻找一样热爱编程的人
程序代码:/*
* 实现一个小孩随便跑程序
* 设计效果 :
* 小孩用*打印,假设界面是30 * 30的数组矩阵。程序开始时,小孩的位置随机
* 产生,w / W(上)s / S(下)a / A(左)d / D(右)控制小孩的移动方向,每击键(wsad)
* 一次按一下回车键,小孩向对应方向移动1下(1字符)。当跑出边缘,则小孩会从相反方
* 向的边缘进入界面,因而小孩由始至终都在界面上乱跑。按下q / Q键退出,程序结束。
*/
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define GRAPH_SIZE 20
void Init();
void gotoXY(int, int);
int X, Y;
int main()
{
char ch;
Init();
while (1)
{
gotoXY(X, Y);
putchar(0xa3), putchar('*' + 0x80);
while (!_kbhit());
gotoXY(X, Y);
putchar(' '), putchar(' ');
ch = _getch();
if ('q' == ch)
{
break;
}
switch (ch)
{
case 'a':
X = (X + GRAPH_SIZE - 2) % GRAPH_SIZE + 1;
break;
case 's':
Y = Y % GRAPH_SIZE + 1;
break;
case 'd':
X = X % GRAPH_SIZE + 1;
break;
case 'w':
Y = (Y + GRAPH_SIZE - 2) % GRAPH_SIZE + 1;
break;
default: break;
}
}
return 0;
}
void Init()
{
int i;
CONSOLE_CURSOR_INFO cursor_info;
//光标不显示
cursor_info.bVisible = FALSE;
cursor_info.dwSize = 20;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
//画界面
for (i = 0; i <= GRAPH_SIZE + 1; i++)
{
printf("%s", "█");
}
for (i = 1; i <= GRAPH_SIZE; i++)
{
gotoXY(0, i), puts("█");
gotoXY(GRAPH_SIZE + 1, i), puts("█");
}
for (i = 0; i <= GRAPH_SIZE + 1; i++)
{
printf("%s", "█");
}
//小孩位置初始化
srand(time(NULL));
X = rand() % GRAPH_SIZE;
Y = rand() % GRAPH_SIZE;
}
void gotoXY(int x, int y)
{
COORD coord = { 2 * x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
