当前位置:   article > 正文

c++代码雨代码可复制无错误

c++代码雨

今天给大家带来的是代码雨的代码,希望支持。 `#include <time.h>
#include <Windows.h>
#include <stdio.h>

typedef struct //记录雨滴的结构体
{
int x, y;
char ch;
}RAINDROP;

const int BUFFER_SIZE = 100;
int WIDTH = 80;
int HEIGHT = 30;
const int RAIN_LENGTH = 18;

RAINDROP raindropLine[BUFFER_SIZE];
HANDLE HOUT = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准输出的句柄

void gotoxy(int x, int y)
{
COORD pos; //定义表示一个字符在控制台屏幕上的坐标的对象
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(HOUT, pos); //设置控制台标准输出光标位置
}

void show_cursor(BOOL hide)
{
CONSOLE_CURSOR_INFO cciCursor;
if (GetConsoleCursorInfo(HOUT, &cciCursor)) //获得光标信息
{
cciCursor.bVisible = hide; //隐藏光标
SetConsoleCursorInfo(HOUT, &cciCursor); //重新设置光标
}
}

void set_color(int color)
{
SetConsoleTextAttribute(HOUT, color); //设置输出颜色
}

int main()
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(HOUT, &info); //获得控制台窗体信息
HEIGHT = info.srWindow.Bottom; //根据控制台的宽高设置显示的宽高
WIDTH = info.srWindow.Right;

show_cursor(FALSE);
srand((unsigned int)time(NULL));
for (int i=0; i<BUFFER_SIZE; i++)			//随机设置雨滴下落的位置
{
	raindropLine[i].x = rand()%WIDTH;
	raindropLine[i].y = rand()%HEIGHT;
	raindropLine[i].ch = rand() %2 + 48;				//设置雨滴内容0或1
}

while(true)				
{
	GetConsoleScreenBufferInfo(HOUT, &info);	//当窗体大小变化时,重新设置宽高信息
	HEIGHT = info.srWindow.Bottom;
	WIDTH = info.srWindow.Right;
	for (int i=0; i<BUFFER_SIZE; ++i)
	{
		if (raindropLine[i].y <= HEIGHT)
		{
			gotoxy(raindropLine[i].x, raindropLine[i].y);
			set_color(FOREGROUND_GREEN);		//设置雨滴颜色
			putchar(raindropLine[i].ch);
		}
		gotoxy(raindropLine[i].x, raindropLine[i].y - RAIN_LENGTH);	//擦除过长的雨滴
		putchar(' ');
		raindropLine[i].y++;
		raindropLine[i].ch = rand() % 2 + 48;
		if (raindropLine[i].y > HEIGHT + RAIN_LENGTH)		
		{
			raindropLine[i].x = rand() % WIDTH;
			raindropLine[i].y = rand() % HEIGHT;
		}
		if ( raindropLine[i].y <= HEIGHT)
		{
			gotoxy(raindropLine[i].x, raindropLine[i].y);
			set_color(FOREGROUND_GREEN|FOREGROUND_INTENSITY);	//高亮最下方的雨滴
			putchar(raindropLine[i].ch);
		}
	}
	Sleep(50);
}
getchar();
return 0;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

}`

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/452789
推荐阅读
相关标签
  

闽ICP备14008679号