用C语言做一个圣诞树代码,适合linux系统,其中\033[xxm是ANSI转义字符,xx表示颜色
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include<unistd.h>
#include<stdlib.h>
//圣诞树数组,存放坐标对应的颜色数字
char m[25][18]=
{
{7,7,7,7,7,7,7,7,7,3,7,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,3,3,3,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,2,3,2,7,7,7,7,7,7,7},
{7,7,7,7,7,7,1,2,2,2,2,2,7,7,7,7,7,7},
{7,7,7,7,7,7,2,3,2,2,2,2,2,7,7,7,7,7},
{7,7,7,7,7,2,2,2,4,5,6,2,2,2,7,7,7,7},
{7,7,7,7,7,7,7,7,2,2,2,4,7,7,7,7,7,7},
{7,7,7,7,7,7,1,2,2,2,2,2,3,7,7,7,7,7},
{7,7,7,7,7,7,2,5,4,2,2,2,2,7,7,7,7,7},
{7,7,7,7,2,2,2,2,2,6,2,2,2,2,2,7,7,7},
{7,7,7,7,4,7,7,2,2,5,4,2,7,7,7,7,7,7},
{7,7,7,7,7,3,2,2,2,2,2,3,6,7,7,7,7,7},
{7,7,7,7,7,2,6,2,2,2,2,2,2,2,7,7,7,7},
{7,7,7,7,2,2,2,1,4,2,2,2,2,2,2,7,7,7},
{7,7,2,2,2,2,2,2,0,3,5,6,2,2,2,2,2,7},
{7,7,7,7,7,7,2,2,0,0,0,2,4,7,7,7,7,7},
{7,7,7,7,7,7,7,7,0,0,0,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,0,0,0,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,0,0,0,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,0,0,0,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,0,0,0,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,0,0,0,0,0,7,7,7,7,7,7},
{7,7,7,7,7,7,0,0,0,0,0,0,0,7,7,7,7,7},
{7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7},
{7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}
};
int type=0,t=700,n=0,c=0;
struct {
int x;
int y;
} p[30]= { 0};//存放灯带的位置
void Gb( int x, int y)
{
printf("\033[%d;%dH",y,x);
}
void cls()
{
printf("\033[2J\033[1;1H");
fflush(stdout);
}
void findColor()//把灯带中方块的位置按顺序存进数组
{
int isTop,topx,topy;
for(int y=3; y<25; y++)
for(int x=0; x<17; x++)
{
int col=m[y][x];
if(col>0&&col<7&&col!=2)
{
int isTop=1;
for(int i=0; i<c; i++)
if(p[i].x==x&&p[i].y==y)
{ isTop=0;
break;
}
if(isTop)
{
p[c].x=x,p[c++].y=y,topx=x,topy=y;
while(1)
{
struct {
int x;
int y;
} t[3]={{x+1,y},{x,y+1},{x+1,y+1}};
int flag= 1;
for(int i=0; i<3; i++)
{
int v=m[t[i].y][t[i].x];
if(v>0&&v<7&&v!=2)//找到一条灯带中的下一个灯块
{
p[c].x=t[i].x,p[c++].y=t[i].y;
x=t[i].x,y=t[i].y;
flag=0;
break;
}
}
if(x>16||y>24||flag)
break;
}
x=topx,y=topy;
}
}
}
}
void menu()
{
int speed[5]= { 1500,700,400,220,90 }
,ch=0;
while(ch<7&&ch>=0)
{
cls();
Gb(2,13);
printf("\033[35m闪烁速度:1慢 2较慢 3正常 4快 5超快");
printf("\n灯光模式:6随机 7流动(填序号)\033[15;2H\033[34m按回车开始,运行中按回车返回菜单");
ch=getch()-49;
if(ch==-29)
return;
else if(ch<0||ch>6)
continue;
if(ch>=0&&ch<=4)
t=speed[ch];
else type=ch-5;
Gb(15,16);
printf("\033[32m设置成功!\033[35m");
fflush(stdout);
usleep(700000);
}
cls();
}
//画方块,颜色数字0到7分别是黑红绿黄蓝紫青白
void print_block()
{
printf("\033[1;1H");
int color;
if(type)//循环流动模式,灯光依次等于下一个灯光
{
int top=m[p[0].y][p[0].x];
for(int i=0; i<c-1; i++)
m[p[i].y][p[i].x]=m[p[i+1].y][p[i+1].x];
m[p[c-1].y][p[c-1].x]=top;
for(int y=0; y<25; y++)
{
for(int x=0; x<18; x++)
printf("\033[4%dm ",m[y][x]);
printf("\033[40m\n");
}
}
else{
static int n[5]= { 1,3,4,5,6 },color=7;
for(int i=0,last=7; i<25; i++)
{
for(int j=0; j<18; j++)
{
if(m[i][j]==7||m[i][j]==2||m[i][j]==0||i<3)
color=m[i][j];
else {
do color=n[rand()%5];
while(color==last);
last=color;
}
printf("\033[4%dm ",color);
}
printf("\033[40m\n");
}
}
fflush(stdout);
}
int main()
{
printf("\033[?25l");
srand(time(0));
menu();
if(type)
findColor();
while(1)
{
if(_kbhit()&&getch()=='\n')
menu();//检测键盘有没有被按下和是否是回车
print_block();
usleep(t*1000);
}
}