【无标题】C语言圣诞树代码

这篇文章介绍了如何使用C语言编写一个在Linux系统下运行的圣诞树程序,利用ANSI转义字符控制颜色,包括灯带位置的查找和动态颜色模式,用户可以通过菜单选择不同的闪烁速度和灯光模式。
摘要由CSDN通过智能技术生成

用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);

    }
}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值