帮我用c写一个下雪的代码
时间: 2024-06-06 15:06:18 浏览: 34
一个用C写的下雪程序
好的,下面是一个简单的用 C 写的下雪代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 80
#define HEIGHT 25
void draw(int x, int y, char c)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
putchar(c);
}
int main()
{
srand(time(NULL));
int x, y, i, j;
char snowflakes[HEIGHT][WIDTH] = {0};
while (1) {
x = rand() % WIDTH;
y = rand() % HEIGHT;
snowflakes[y][x] = '*';
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
draw(j, i, snowflakes[i][j]);
snowflakes[i][j] = (i == 0 || snowflakes[i-1][j] == '*' || rand() % 15 == 0) ? '*' : ' ';
}
}
Sleep(50);
}
return 0;
}
阅读全文