24小时热门版块排行榜    

查看: 4161  |  回复: 0

2356482573

铜虫 (小有名气)

[求助] 用C++编写滚动字幕效果

#include   //标准输入输出流头文件
#include
#include
#include   //控制台输入输出
#include
#include    //时间
using namespace std;

string font="";//所选用的字体
int color=4;//所选用的字体颜色,默认选择4(红色)
string hz_string="";//输入的不超过4个汉字的内容;
int hz_move;//效果,默认选择1(向右移动)
int hz_num;//输入的汉字个数
int define_array[16][64];


//用于把光标移动到指定的位置
void gotoxy(int x,int y)  
{  
  COORD pos;
  pos.X=x;  
  pos.Y=y;  
  ::SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);  
}

//在控制台写汉字的函数
void write(char *s)
{
        FILE *fp;           //FILE 是变量类型,实际上是C语言定义的标准数据结构,用于文件。FILE *fp 是声明,声明fp是指针,用来指向FILE类型的对象。fp=fopen("yssysj.txt","r";  fopen标准函数,打开磁盘文件yssysj.txt, 用于读,送返指针,指向FILE类型对象。fscanf 用于从文件读入数据,scanf 从键盘读入数据。书写格式上,fscanf 多一个参数,就是 fp,其它两者一样:fscanf(fp,"%f",&x[j]); scanf("%f",&x[j]);
        char buffer[32]; /* 32(个字符)大小的字模缓冲区 */
        register int i,j,k;//register:优化
        unsigned char qh,wh;
        unsigned long location;

        const char *p = (char *)font.c_str();//c_str()返回指向字符串的指针  
        if((fp=fopen(p,"rb")==NULL)
        {
                printf(p);
                printf("无法打开字体库!";HANDLE),7);
        font="";
        cout<<"请输入不超过4个汉字的内容:";
        cin>>hz_string;
        getch();
       

        int hz_font;
        cout<<"请选择您想要的字体(1/2/3/4):"<         cin>>hz_font;
        getch();
                _getch();
                exit(0);
        }
        else
        {//已经测试成功了,printf("字体库打开成功!"}
                printf("字体库打开成功!";
                int xxx=0;//表示汉字的数量
                while(*s)
                {
                        qh=*s-0xa0;
                        wh=*(s+1)-0xa0;
                        location=(94*(qh-1)+(wh-1))*32L; /* 计算汉字字模在文件中的位置 */
                        fseek(fp,location,SEEK_SET);
                        fread(buffer,32,1,fp);
                        xxx++;
                        hz_num=xxx;
                        for(i=0;i<16;i++)
                        {
                                int yyy=(xxx-1)*16;
                                for(j=0;j<2;j++)
                                {
                                        for(k=0;k<8;k++)
                                        {
                                                if(((buffer[i*2+j]>>(7-k))&0x1)!=NULL)
                                                {
                                                        define_array[yyy]=1;
                                                }
                                                else
                                                {
                                                        define_array[yyy]=0;
                                                }
                                                yyy++;
                                        }
                                }
                        }
                        s+=2;
                }
                fclose(fp);
        }
}
//利用已经存好的汉字数组以一定的形式输出到控制台
void hz_write(int x,int y,int index_color,int index_array[16][64]){
        //先设置在屏幕上输出字体的颜色
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),index_color);
        for(int i = 0;i<16;i++){
                gotoxy(x,y+i);//一行一行的输出,每一行输出前将光标进行定位
                for(int j=0;j                         if(index_array[j]==1){       
                                cout<<"*";
                        }else{  
                                cout<<" ";
                        }
                }
                cout<         }
}
//一个一个字的输出(hz_write的重载)
void hz_write(int x,int y,int index_color,int index_array[16][64],int ex){
        //先设置在屏幕上输出字体的颜色
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),index_color);
        while(true){
                if(kbhit()==1){
                        break;
                }
                system("cls";
                for(int i=0;i                         for(int j=0;j<16;j++){
                                gotoxy(x+16*i,j+y);
                                Sleep(40);
                                for(int z=x+16*i;z<16+x+16*i;z++)
                                if(index_array[j][z]==1){       
                                        cout<<"*";
                                }else{  
                                        cout<<" ";
                                }
                        }
                        cout<                         Sleep(100);//每写一个字要停顿100毫秒
                }
                Sleep(1000);//两次完全书写之间相隔1s
        }       
}
       

//向右移动效果的函数
void myMove(int x,int y){
        int middle=0;
        while(true){
                if(kbhit()==1){
                        break;
                }
                Sleep(20);
                system("cls";
                hz_write(x+middle,0,color,define_array);
                middle+=5;
                if(middle+x>=80){
                        middle=0;
                }
        }
}

//用于随机改变字体颜色的函数
void myColor(int x,int y){
        while(true){

                if(kbhit()==1){
                        break;
                }

                srand((unsigned)time(0));//随即种子
                int my_num = rand()%10+1;//随即产生1到9间的随机数
                Sleep(1000);//停止1S ,保证随即种子的更换
                system("cls";
                color = my_num;
                hz_write(0,0,color,define_array);
        }
}
//用于实现反弹运动的函数
void myResponse(int x,int y){
        int speed_x=1;//水平方向上的速度
        int speed_y=1;//垂直方向上的速度
        int x_right;
        int y_down ;
        while(true){

                if(kbhit()==1){
                        break;
                }

                x+=speed_x;
                x_right =x+hz_num*16;
                y+=speed_y;
                y_down =y+16;
                if(x_right<=79 && x>=0){
                        if(y>=0 && y_down<=30){
                                hz_write(x,y,color,define_array);
                        }else{
                                speed_y=-speed_y;
                        }
                }else{
                        speed_x=-speed_x;
                }
                Sleep(20);
                system("cls";
        }
}

//用于实现轮播的函数
void myPlay(int x,int y){
        system("cls";
        hz_write(x,y,color,define_array,1);
}
//用于初始化的函数
void init(){       

        system("cls";
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_
        //根据输入选择汉字字体  
        switch(hz_font){
                case 1:
                        font+="宋体.bin";
                        break;
                case 2:
                        font+="隶书.bin";
                        break;
                case 3:
                        font+="仿宋.bin";
                        break;
                case 4:
                        font+="华文琥珀.bin";
                        break;
                default:
                        font+="";
                        break;
        }
        write((char *)hz_string.c_str());
        cout<<"请选择动画效果: "<         cin>>hz_move;
        getch();

        if(hz_move != 2){
                int hz_color;
                cout<<"请选择您想要的字体的颜色:"<                 cin>>hz_color;
                getch();
        //根据用户输入选择相应颜色
                switch(hz_color){
                        case 1:
                                color=4;//对应红色
                                break;
                        case 2:
                                color=1;//对应蓝色
                                break;
                        case 3:
                                color=2;//对应绿色
                                break;
                        default:
                                color=4;//默认就是红色
                                break;       
                }
        }
}
//在初始化之后根据选择的效果情况开始动画
void hz_go(){
        switch(hz_move){
                case 1:
                        myMove(0,0);
                        break;
                case 2:
                        myColor(0,0);
                        break;
                case 3:
                        myResponse(0,0);
                        break;
                case 4:
                        myPlay(0,0);
                default:
                        break;
        }
}
int main(){
        while(true){
        init();
        //根据用户输入情况选择动画效果
        hz_go();
        getch();
        }
}


这个程序有一定的问题,能帮忙改下吗,,,还有能不能帮忙写个程序说明文档,,,多谢!
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

找到一些相关的精华帖子,希望有用哦~

科研从小木虫开始,人人为我,我为人人
相关版块跳转 我要订阅楼主 2356482573 的主题更新
信息提示
请填处理意见