打字游戏代码

作者在 2010-04-17 12:32:19 发布以下内容
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
#define BK_COLOR BLACK  /*定义背景颜色*/
#define C_COLOR BLUE   /*定义画笔颜色*/
#define UpKey 72    
#define DownKey 80
#define Esc 27
#define Enter 13
#define MenuX 200      /*定义目录的横坐标*/
#define MenuY 110
#define MenuWidth 200   /*定义目录每行的宽度*/
#define MenuSinH 40
#define ChoiceX MenuX+15  /*定义选择条的位置*/
#define ChoiceY MenuY+10
#define ChoiceWidth 170
#define ChoiceH 25
#define ViewLeft 20
#define ViewTop 5
#define ViewRight 520
#define ViewBottom 460
#define DataViewX ViewRight
#define DataViewY ViewTop+40
#define DataH 40
int nCount=10;
int speed=6000;
struct PlayData/*定义结构体名result,用来存储游戏数据*/
{
    int time;/*时间;个数;没打到的;命中率*/
    int Hit;
    int Lose;
    double ratio;
}result;
void Interface()
{
    int i,j;
    setcolor(CYAN);/*定义画笔颜色为青绿色*/
    for(i=0;i<60;i++)
    {
        line(i,0,i,500);
        line((getmaxx()-i),0,(getmaxx()-i),500);/*画两边的青色柱子*/
    }
    setcolor(YELLOW);
    for(i=60,j=0;i<85;i++,j++)
    {
        line(i,j,i,500);
        line((getmaxx()-i),j,(getmaxx()-i),500);/*画黄色的门*/
    }
    setcolor(WHITE);
    rectangle(MenuX,MenuY,MenuX+MenuWidth,MenuY+3*MenuSinH);/*画矩形框*/
    line(MenuX,MenuY+MenuSinH,MenuX+MenuWidth,MenuY+MenuSinH);/*画两条横线*/
    line(MenuX,MenuY+2*MenuSinH,MenuX+MenuWidth,MenuY+2*MenuSinH);
}
void Main_Menu()
{
    settextstyle(0,0,2);
    outtextxy(MenuX+20,MenuY+5,"START GAME");
    outtextxy(MenuX+20,MenuY+MenuSinH+15,"GAME SETUP");
    outtextxy(MenuX+20,MenuY+2*MenuSinH+15,"EXIT");
}
void Option_Menu()
{
    settextstyle(0,0,2);/*对文本的模式设定,白色字,2号大小的字*/
    outtextxy(MenuX+20,MenuY+15,"beginner");/*在各行把相应的字输出*/
    outtextxy(MenuX+20,MenuY+MenuSinH+15,"advancer");
    outtextxy(MenuX+20,MenuY+2*MenuSinH+15,"senior");
}
void Choice(int c_x,int c_y,int color)
{
    setfillstyle(1,color);
    bar(c_x,c_y,c_x+ChoiceWidth,c_y+ChoiceH);/*画选择条*/
}
int ChooseMenu(int flag)/*选择菜单功能*/
{
    int x,y;
    char C_Key;
    x=ChoiceX,y=ChoiceY;/*定义选择条位置x,y*/
    do
    {
        kbhit();/*键盘按键输入函数*/
        C_Key=getch();/* 获得输入的字符*/
        if(C_Key==Esc)
        {
            sound(1000);/*打开声音系统,延时6秒,关闭生意系统,返回Esc*/
            delay(6000);
            nosound();
            return Esc;
        }
        else
            if(C_Key==Enter)
            {
                sound(1000);
               delay(10000);
                 nosound();
                return y;
                }
        else
        {
            switch(C_Key)/*如果输入的上下键*/
            {
            case UpKey:
                sound(1000);
                delay(10000);
                                nosound();
                if(y==ChoiceY)/*当选择条已经在最上面时没有反应*/
                    break;
                else
                {
                    Choice(x,y,BK_COLOR);/*覆盖原来的选择条,向下移动一格,出现选择条*/
                    y-=MenuSinH;
                    Choice(x,y,BLUE);
                }
                break;
            case DownKey:
                sound(1000);
                delay(10000);
                nosound();
                if(y==(ChoiceY+2*MenuSinH))
                    break;
                else
                {
                    Choice(x,y,BK_COLOR);
                    y+=MenuSinH;
                    Choice(x,y,BLUE);
                }
                break;
            default:
                break;
            }
            if(flag==0)
                Main_Menu();/*如果flag是0,则选择画面出现在主菜单,1则副菜单*/
            else
                if(flag==1)
                    Option_Menu();
        }

    }while(C_Key!=Esc);
}
void Setup()
{
    int result;
    result=ChooseMenu(1);/*从副菜单获得选的下落速度与打击数量*/
    switch(result)
    {
    case Esc:
        break;
    case ChoiceY:
        speed=6000;
        nCount=30;
        break;
    case ChoiceY+MenuSinH:
        speed=3000;
        nCount=60;
        break;
    case ChoiceY+2*MenuSinH:
        speed=1000;nCount=100;
        break;
    default:
        break;
    }
}
void Loading()/*出现游戏进入界面*/
{
    int i,j,l;
    Interface();
    setfillstyle(1,0);
    bar(200,110,400,230);/*出现矩形框覆盖原来的菜单*/
    settextstyle(0,0,1);
    outtextxy(250,207,"Loading");
    for(l=0;l<6;l++)
    {
        setcolor(15);
        rectangle(237+l*15,255,244+l*15,235);/*出现空格*/
    }
    l=0;
    j=1;
    while(j!=480-j)
    {
        j++;
        if(j%40==0)
        {
            setfillstyle(1,RED);
            bar(237+15*l,255,244+15*l,235);/*出现红色实心框*/
            l++;
        }
        delay(1000);
    }
    delay(60000);
    clearviewport();/*清屏*/
}


void DrawChar(int i,int j,char c)/*移动到(x,y)输出C*/
{
    char ch[2];
    ch[0]=c;
    ch[1]='\0';
    moveto(i,j);
    settextstyle(0,0,2);
    outtext(ch);
}
void AutoDraw_Down(int x,int y,char c,int n)/*覆盖前一个,向下输出下一个*/
{
    setcolor(BK_COLOR);
    DrawChar(x,y,c);
    setcolor(C_COLOR);
    DrawChar(x,y+1,c);
    delay(n);
}void AutoDraw_Up(int x,int y,char c,int n)/*覆盖前一个,向上输出*/
{
    setcolor(BK_COLOR);
    DrawChar(x,y,c);
    setcolor(C_COLOR);
    DrawChar(x,y-1,c);
    delay(n);
}
char GenerateChar()/*随机获得并返回*/
{
    int flag;
    char c;
    flag=random(3);
    switch(flag)
    {
    case 0:c='a'+random(26);/*小写字母*/
        break;
    case 1:c='A'+random(26);/*大写字母*/
        break;
    case 2:c='0'+random(10);/*数字*/
        break;
    default:break;
    }
    return c;
}
void PlayViewPort()/*游戏界面,数据画面*/
{
    int i;
    cleardevice();
    setcolor(CYAN);
    for(i=0;i<3;i++)
    rectangle(ViewLeft-10-i,ViewTop-i,ViewRight+100+i,ViewBottom+i);
    settextstyle(0,0,0);
    rectangle(DataViewX,DataViewY,DataViewX+100,DataViewY+DataH
    );
    rectangle(DataViewX,DataViewY+DataH,DataViewX+100,DataViewY+2*DataH);
    rectangle(DataViewX,DataViewY+2*DataH,DataViewX+100,DataViewY+3*DataH);
    outtextxy(DataViewX+10,DataViewY+10,"Hit");
    outtextxy(DataViewX+10,DataViewY+DataH+10,"Lose");
    outtextxy(DataViewX+10,DataViewY+2*DataH+10,"PreKey");
}
void DrawLaugh(int x,int y,int r)/*笑脸*/
{
    int c1=YELLOW,c2=BLACK,c3=WHITE;
    int d=r/5;
    setcolor(c1);
    circle(x,y,r);
    setfillstyle(1,c1);
    floodfill(x,y,c1);
    setcolor(c2);
    line(x-d,y-3*d,x-2*d,y-4*d);
    line(x-2*d,y-4*d,x-2.9*d,y-4*d);
    line(x+d,y-3*d,x+2*d,y-4*d);
    line(x+2*d,y-4*d,x+2.9*d,y-4*d);

    line(x-3.5*d,y-2*d,x-2.5*d,y-2.6*d);
    line(x-2.5*d,y-2.6*d,x-1.5*d,y-2*d);
    line(x+3.5*d,y-2*d,x+2.5*d,y-2.6*d);
    line(x+2.5*d,y-2.6*d,x+1.5*d,y-2*d);

    pieslice(x,y,180,360,4*d);
    setfillstyle(1,c3);
    floodfill(x,y+d,c2);

    line(x-2*d,y,x-2*d,y+3.5*d);
    line(x+2*d,y,x+2*d,y+3.5*d);
    line(x,y,x,y+4*d);
    line(x+3.5*d,y,x+3.5*d,y+1.8*d);
}
void Laugh(int x,int y)/*出现笑脸上线跳动*/
{
    int i,d,r,flag=0;
    d=6;
    i=0;
    r=10;
    while(!kbhit())
    {
        DrawLaugh(x,y+i,r);
        delay(10000);
        setfillstyle(1,BK_COLOR);
        setcolor(BK_COLOR);
        pieslice(x,y+i,0,360,r);
        if(i==d)/*限定跳动的范围*/
            flag=1;
        if(i==0)
            flag=0;
        if(flag==0)
            i++;
        else
            i--;
    }
}
void Smile(int x,int y)/*微笑*/
{
    int r=10,c1=YELLOW,c2=BLACK;
    int d=r/5,i;
    setcolor(c1);
    circle(x,y,r);
    setfillstyle(1,c1);
    floodfill(x,y,c1);
    setcolor(c2);
    setfillstyle(1,c2);
    sector(x-2.5*d,y-2.5*d,0,360,1,1);
    sector(x+2.5*d,y-2.5*d,0,360,1,1);
    setcolor(c2);
    arc(x,y,200,350,3.5*d);
}
void Weep(int x,int y)/*沮丧的表情*/
{
    int r=10,c1=YELLOW,c2=BLACK;
    int d=r/5;
        setcolor(c1);
         circle(x,y,r);
        setfillstyle(1,c1);
        floodfill(x,y,c1);
        setcolor(c2);
        sector(x-2.5*d,y-2.5*d,0,360,1,1);
            sector(x+2.5*d,y-2.5*d,0,360,1,1);
            setcolor(c2);
            arc(x,y+5*d,30,150,4*d);
             delay(10000);
        

}
void ShowFace(int flag)/*表情的表现位置*/
{
    int x,y;
    x=DataViewX+50;y=DataViewY-20;
    switch(flag)
    {
    case 1:
        Laugh(x,y);
        break;
    case 2:
        Smile(x,y);
        break;
    case 3:
        Weep(x,y);
        break;
    default:
        break;
    }
}
void ShowData(int nHit,int nLose,int nPre)/*数据框*/
{
    char *str;
    str=(char *)malloc(sizeof(*str));
    setfillstyle(1,BK_COLOR);
    bar(DataViewX+1,DataViewY+30,DataViewX+99,DataViewY+DataH-1);
    bar(DataViewX+1,DataViewY+DataH+30,DataViewX+99,DataViewY+2*DataH-1);
    bar(DataViewX+1,DataViewY+2*DataH+30,DataViewX+99,DataViewY+3*DataH-1);
    settextstyle(0,0,0);
    setcolor(C_COLOR);
    sprintf(str,"%d",nHit);
    outtextxy(DataViewX+10,DataViewY+30,str);
    sprintf(str,"%d",nLose);
    outtextxy(DataViewX+10,DataViewY+DataH+30,str);
    sprintf(str,"%d",nPre);
    outtextxy(DataViewX+10,DataViewY+2*DataH+30,str);
}
void ShowResult(int t,int Hit,int Lose,double ratio)/*结果显示*/
{
    int x,y,temp,Char_Count;
    int flag;
    char *str;
    str=(char *)malloc(sizeof(*str));
    Char_Count=Hit+Lose;
    x=ViewLeft+150;
    y=ViewTop+100;
    setcolor(WHITE);
    rectangle(x,y,x+200,y+120);
    line(x,y+30,x+200,y+30);
    line(x,y+60,x+200,y+60);
    line(x,y+90,x+200,y+90);
    settextstyle(0,0,0);
    setcolor(YELLOW);
    sprintf(str,"Total Teme :%d s.",t);
    outtextxy(x+20,y+10,str);
    sprintf(str,"    Hit    :%d s.",Hit);
    outtextxy(x+20,y+40,str);
    sprintf(str,"    Lose   :%d s.",Lose);
    outtextxy(x+20,y+70,str);
    temp=(int)(ratio*100.00);
    sprintf(str," Hit ratio :%d\%",temp);
    outtextxy(x+20,y+100,str);
    settextstyle(1,0,2);
    setcolor(WHITE);
    if(temp==100&&Char_Count==nCount)
    {
        flag=1;
        outtextxy(x+50,y+150,"Pretty Good!You have a gift!");
    }
    else
    if(temp>=89)
    {
            flag=1;
            outtextxy(x+50,x+150,"Well Done !");
    }
    else
    if(temp>=80)
    {
        flag=2;
        outtextxy(x+70,y+150,"Good!");
    }
    else
    if(temp>=60)
    {
        flag=2;
        outtextxy(x+50,y+150,"Not Bad!");
    }
    else
    {
        flag=3;
        outtextxy(x-50,y+150,"sorry! You did a bad work!");
    }
    ShowFace(flag);
}
void Play()/*开始游戏*/
{
    int x,y,x1,y1,i,j,Char_Count=0;
    int e=20,PreKey_Count=0,Hit_Count=0;
    char c,key,*str;
    int Start_T,End_T;
    setbkcolor(BK_COLOR);
    PlayViewPort();
    ShowData(Hit_Count,(Char_Count-Hit_Count),PreKey_Count);
    Start_T=time(NULL);
    kbhit();
    key=getch();
    while(key!=Esc&&Char_Count<nCount)
    {
        randomize();/*随机初始化,使得出来的数更随机*/
        x=ViewLeft+40+random(ViewRight-ViewLeft-60)-30;/*字符出现的位子随机*/
        c=GenerateChar();
        Char_Count++;
        for(y=ViewTop+31;y<ViewBottom-30;y++)
        {
            AutoDraw_Down(x,y,c,speed);
            if(kbhit())
            {
                key=getch();
                if(key==Esc)
                {
                    sound(600);
                       delay(10000);
                    nosound();
                    Char_Count--;
                    break;
                       }
                else
                {
                    if(key==c)
                    {
                     sound(100);
                      delay(10000);
                      nosound();
                        x1=x;
                        for(y1=ViewBottom-31;y1>=y;y1--)
                        {
                            AutoDraw_Up(x1,y1,key,100);
                        }
                        Hit_Count++;
                        setcolor(RED);
                        for(i=0;i<=30;i++)
                        {
                            circle(x,y1,i);
                            delay(1000);
                        }
                    }
                    else
                    {
                       sound(600);
                       delay(10000);
                       nosound();
                        x1=x+e;
                        for(y1=ViewBottom-31;y1>=ViewTop+15;y1--)
                        {
                            AutoDraw_Up(x1,y1,key,100);
                        }
                    }
                    PreKey_Count++;
                }
                break;
            }
        }
        ShowData(Hit_Count,(Char_Count-Hit_Count),PreKey_Count);
        setfillstyle(1,0);
        bar(ViewLeft+1,ViewTop+1,ViewRight-1,ViewBottom-1);
    }
    End_T=time(NULL);
    result.time=Start_T-End_T;
    result.Hit=Hit_Count;
    result.Lose=Char_Count-Hit_Count;
    if(Char_Count==0)
        result.ratio=0.0;
    else
        result.ratio=Hit_Count/(float)Char_Count;
    ShowResult(Char_Count,result.Hit,result.Lose,result.ratio);
    getch();
    sound(3000);
    delay(10000);
    nosound();
}
main()
{
    int gd=DETECT,gm;
    int c_x,c_y,result;
    char C_Key;
    initgraph(&gd,&gm,"D:\\TC201E\\BGI");/*开起图像系统*/
    c_x=ChoiceX;
    c_y=ChoiceY;
Begin:
    cleardevice();
    Interface();
    Choice(c_x,c_y,BLUE);
    Main_Menu();
    result=ChooseMenu(0);
    switch(result)
    {
    case Esc:
        closegraph();
        exit(0);
    case ChoiceY:
        cleardevice();
        Loading();
        Play();
        goto Begin;
    case ChoiceY+MenuSinH:
    {
        cleardevice();
        Interface();
        Choice(c_x,c_y,C_COLOR);
        Option_Menu();
        Setup();
        goto Begin;
    }
    case ChoiceY+2*MenuSinH:
        closegraph();
        exit(0);
        break;
    default:
        break;
    }
}

C语言 | 阅读 1157 次
文章评论,共4条
woaiyunyun
2010-04-17 12:36
1
怎么玩啊?
h646028147
2010-04-18 08:40
2
Cannot open include file: 'graphics.h': No such file or directory<br />
‘graphics’是不是该自己建立?
一口三个汉堡(作者)
2010-04-19 22:18
3
<div class="quote"><span class="q"><b>h646028147</b>: Cannot open include file: 'graphics.h': No such file or directory<br />
‘graphics’是不是该自己建立?</span></div>那个头文件是TC特有的,只有在TC中编译才可以成功
一口三个汉堡(作者)
2010-04-19 22:18
4
<div class="quote"><span class="q"><b>woaiyunyun</b>: 怎么玩啊?</span></div>把代码复制到TC中,编译,出来的EXE就可以玩了
游客请输入验证码