1.代码运行出现错误:Id returned 1 exit status。
2.运行环境Dev-C++(能在Visual C++ 6.0 下运行更好)
3.希望保留代码中的功能(数字雨、音乐播放、方向键控制速度)
4.代码如下:
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#include<conio.h>
#include<stdlib.h>
typedef struct
{
int x,y;
char ch;
}STU;
STU st[100];
//设置光标位置,决定要在什么位置输出
void gotoxy(int x, int y)
{
HANDLE hout;
COORD pos;
pos.X = x;
pos.Y = y;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
}
/*隐藏光标*/
void show_cursor(int hide)
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hout;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleCursorInfo(hout, &cciCursor))
{
cciCursor.bVisible = hide;
SetConsoleCursorInfo(hout, &cciCursor);
}
}
/*设置颜色*/
void set_color(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
}
/*读取方向键*/
void keyDown()
{
int pre_key,key;
double speed;
pre_key = key;//记录前一个按键的方向
if (_kbhit())//如果用户按下了键盘中的某个键
{
fflush(stdin);//清空缓冲区的字符
//getch()读取方向键的时候,会返回两次,第一次调用返回0或者224,第二次调用返回的才是实际值
key = _getch();//第一次调用返回的不是实际值
key = _getch();//第二次调用返回实际值
}
switch (key)
{
case 72://如果读取上方向键
speed-=100;
break;
case 80://如果读取下方向键
speed+=100;
break;
}
}
int main()
{
int i,t;
double speed;
speed=500;
show_cursor(0);
srand(time(NULL));
//初始化结构体
for (i=0;i<100;i++)
{
st[i].x = rand()%80;
st[i].y = rand()%20;
st[i].ch = rand()%(56-47)+48;
}
PlaySound (TEXT("D:\\暂时\\UNICORN.wav"), NULL, SND_ASYNC | SND_NODEFAULT);
while (1)
{
for (i=0;i<100;i++)
{
gotoxy(st[i].x,st[i].y);
set_color(0x2);
//putchar把参数char指定的字符写入到标准输出stdout中
putchar(st[i].ch);
gotoxy(st[i].x,st[i].y-5);
putchar(' ');
st[i].y++;
st[i].ch = rand()%(56-47)+48;
if (st[i].y-5>=18)
{
gotoxy(st[i].x,st[i].y);
putchar(' ');
gotoxy(st[i].x,st[i].y-1);
putchar(' ');
gotoxy(st[i].x,st[i].y-2);
putchar(' ');
gotoxy(st[i].x,st[i].y-3);
putchar(' ');
gotoxy(st[i].x,st[i].y-4);
putchar(' ');
gotoxy(st[i].x,st[i].y-5);
putchar(' ');
}
if (st[i].y > 23)
{
st[i].x = rand()%80;
st[i].y = rand()%20;
}
gotoxy(st[i].x,st[i].y);
set_color(0xA);
putchar(st[i].ch);
}
Sleep(speed);
exit(0);
}
}