turbo C 下的打字游戏

来源:互联网 发布:永久删除手机数据软件 编辑:程序博客网 时间:2024/05/18 02:59

在这里贴上最近自己忙活的用turbo C编写的“打字游戏”的源代码:

#include<graphics.h>
#include<conio.h>
#include<STDLIB.h>
#include<dos.h>
#define BK_COLOR BLACK
#define CHAR_COLOR WHITE
#define C_COLOR BLUE
#define num 10
#define SPEED 3000
#define Esc 27
#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 UpKey 72
#define DownKey 80
#define Enter 13

 

void  Choice(int c_x,int c_y,int color)
{
 setfillstyle(1,color);
 bar(c_x,c_y,c_x+ChoiceWidth,c_y+ChoiceH);
}

void Main_Menu()
{
 settextstyle(0,0,2);
 outtextxy(MenuX+20,MenuY+15,"Start Game");
 outtextxy(MenuX+20,MenuY+MenuSinH+15,"Game Steup");
 outtextxy(MenuX+40,MenuY+2*MenuSinH+15,"Exit");
}

void Option_Menu()
{
 settextstyle(0,0,2);
 outtextxy(MenuX+20,MenuY+15,"beginner");
 outtextxy(MenuX+20,MenuY+MenuSinH+15,"advancer");
 outtextxy(MenuX+40,MenuY+2*MenuSinH+15,"senior");
}

int ChooseMenu(int flag)
{
 int x,y;
 char C_key;
 x=ChoiceX;y=ChoiceY;
 do
 {
  kbhit();
  C_key=getch();
  if(C_key==Esc)
   return Esc;
  else
   if(C_key==Enter)
    return y;

  else
  switch(C_key)
  {
  case UpKey:
   if(y==ChoiceY)
    break;
   else
   {
    Choice(x,y,BK_COLOR);
    y-=MenuSinH;
    Choice(x,y,C_COLOR);
   }
   break;
  case DownKey:
   if (y==ChoiceY+2*MenuSinH)
    break;
   else
   {
    Choice(x,y,BLACK);
    y+=MenuSinH;
    Choice(x,y,BLUE);
   }
   break;
  default:
   break;
 }
 if (flag ==0)
  Main_Menu();
 else
 if(flag==1)
  Option_Menu();
 }while(C_key!=Esc);
}

void SetResult()
{
 int result;
 int speed;
 result=ChooseMenu(1);
 switch(result)
 {
  case Esc:
   return 0;
   break;
  case ChoiceY:
   speed=6000;
   break;
  case ChoiceY+MenuSinH:
   speed=3000;
   break;
  case ChoiceY+2*MenuSinH:
   speed=1000;
   break;
  default:
   break;
 }
}

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  DrawChar(int i,int j,char c)
{
 char ch[1];
 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(CHAR_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(CHAR_COLOR);
 DrawChar(x,y-1,c);
 delay(n);
}

char GenerateChar()
{
 int flag;
 char ch;
 randomize();
 flag=random(3);
 switch(flag)
 {
  case 0:
   ch='a'+ random(26);break;
  case 1:
   ch='A'+random(26);break;
  case 2:
   ch='0'+random(10);break;
  default:break;
 }
 return ch;
}
int getX()
{
 int xx=0;
 randomize();
 xx=random(500);
 return xx;
}

void Play()
{
 char c,key;
 int e=20,PreKey_Count=0,Hit_Count=0;
 int i,x,x1,y1,y,gd=DETECT,gm;
 setbkcolor(BK_COLOR);
 while(key!=Esc)
 {
  x=getX();
  c=GenerateChar();
  for(y=10;y<450;y++)
  {
    AutoDraw_Down(x,y,c,SPEED);
    if(kbhit())
    {
     key=getch();
     if(key==Esc)
      break;
     else
     {
      if(key==c)
      {
       x1=x;
       for(y1=400;y1<=y;y1--)
        AutoDraw_Up(x1,y1,key,SPEED/10);
       Hit_Count++;
      }
      else
      {
       x1=x+e;
       for(y1=400;y1<=10;y1--)
        AutoDraw_Up(x1,y1,key,SPEED/10);
      }
      PreKey_Count++;
     }
     break;
    }
   }
   cleardevice();
 }
 closegraph();
 printf("        PreKey:%d,      Hit:%d/n",PreKey_Count,Hit_Count);
 getch();
 }

main()
{
 int gd=DETECT,gm;
 int c_x,c_y,result;
 char C_Key;
 registerbgidriver(EGAVGA_driver);
 initgraph(&gd,&gm,"c:/tc/TC20H");
 c_x=ChoiceX;
 c_y=ChoiceY;
Begin:
 cleardevice();
 Interface();
 Choice(c_x,c_y,BLUE);
 Main_Menu();
 
 result=ChooseMenu(0);
 switch(result)
 {
  case Esc:
   exit(0);
   break;
  case ChoiceY:
   cleardevice();
   sleep(1);
   Play();
   goto Begin;
  
   break;
  case ChoiceY+MenuSinH:
   cleardevice();
   Interface();
   Choice(c_x,c_y,C_COLOR);
   Option_Menu();
   SetResult();
   goto Begin;
   break;
  case ChoiceY+2*MenuSinH:
   exit(0);
   break;
  default:
   break;
 }
}

 

        如果不对turbo C里面的 graphics函数进行编辑的话,在程序编译的时候就会出现“BGI Error: Graphics not initialized (use 'initgraph')”的错误,那么你就需要通过以下的操作来修改:

        为了使用方便, 应该建立一个不需要驱动程序就能独立运行的可执行图形程序,Turbo C中规定用下述步骤(这里以EGA、VGA显示器为例): 
    1. 在C:/TC子目录下输入命令:BGIOBJ EGAVGA 
    此命令将驱动程序EGAVGA.BGI转换成EGAVGA.OBJ的目标文件。 
    2. 在C:/TC子目录下输入命令:TLIB LIB/GRAPHICS.LIB+EGAVGA 
    此命令的意思是将EGAVGA.OBJ的目标模块装到GRAPHICS.LIB库文件中。 
    3. 在程序中initgraph()函数调用之前加上一句: 
       registerbgidriver(EGAVGA_driver):

在做了以上的修改后,再运行程序,就不会出错了。

现在你就可以在turbo C下玩这个打字游戏了!~~~

     不过,这段代码还只是实现了基本控制功能,还有一些效果没有加上去,比如击中的效果,还有就是背景音乐等...等待完善....