为自己的那个打字游戏附上完整代码

#include<graphics.h>
#include<conio.h>
#include<STDLIB.h>
#include<dos.h>
#include<time.h>

#define BK_COLOR BLACK
#define CHAR_COLOR WHITE
#define C_COLOR BLUE
#define num 10
#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
#define ViewLeft 20
#define ViewRigeht 520
#define ViewTop 15
#define ViewBotton 464
#define DateViewX ViewRigeht
#define DateViewY ViewTop+40
#define DateH 40
/*静态变量*/
int nCount=30;
int speed=6000;

struct PlayDate
{
 int time;
 int Hit;
 int Lose;
 double ratio;
}result;

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;
 result=ChooseMenu(1);
 switch(result)
 {
  case Esc:
   return 0;
   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 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 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,225,244+l*15,235);
 }
 l=0;
 j=1;
 while(j!=480-j)
 {
  j++;
  if(j%40==0)
  {
   setfillstyle(1,RED);
   bar(237+l*15,225,244+l*15,235);
   l++;
  }
  setfillstyle(1,0);
  bar(0,0,getmaxx(),j);
  bar(0,480,getmaxx(),480-j);
  delay(1000);
 }
  delay(60000);
  clearviewport();
}

void PlayViewPort()
{
 int i;
 cleardevice();
 setcolor(CYAN);
 for(i=0;i<3;i++)
  rectangle(ViewLeft-10-i,ViewTop-10-i,ViewRigeht+110+i,ViewBotton+10+i);
 /*利用循环画矩形外框,目的是为了让外框显示有厚度*/
 rectangle(ViewLeft,ViewTop,ViewRigeht+100,ViewBotton);
 /*画内框,与外框相差10像素*/
 setfillstyle(XHATCH_FILL,YELLOW);/*设置填充样式和颜色*/
 floodfill(ViewLeft-1,ViewTop-1,CYAN);
 setcolor(WHITE);
 for(i=0;i<3;i++)
  rectangle(ViewLeft-i,ViewTop-i,ViewRigeht+100+i,ViewBotton+i);
 /*用和填充外框厚度的方法填充内框的厚度*/
 line(ViewRigeht,ViewTop,ViewRigeht,ViewBotton);
 /*用直线吧视图区和数据区分开*/
 settextstyle(0,0,0);
 /*对数据区各个区域的内容进行描述*/
 rectangle(DateViewX,DateViewY,DateViewX+100,DateViewY+DateH );
 rectangle(DateViewX,DateViewY+DateH ,DateViewX+100,DateViewY+2*DateH );
 rectangle(DateViewX,DateViewX+2*DateH ,DateViewX+100,DateViewY+3*DateH );
 outtextxy(DateViewX+10,DateViewY+10,"Hit");
 outtextxy(DateViewX+10,DateViewY+DateH +10,"Lose");
 outtextxy(DateViewX+10,DateViewY+DateH *2+10,"PreKey");
}

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(ViewRigeht-ViewLeft-30);
 return xx+ViewLeft+20;
}

void BombShow(int x,int y,char c)
{
 int c1,c2,r1=15,r2=8;
 char ch;
 c1=YELLOW;
 c2=BLACK;
 setcolor(c1);
 circle(x,y,r1);
 setfillstyle(1,c1);
 floodfill(x,y,c1);
 setcolor(c2);
 circle(x,y,r2);
 setfillstyle(1,c2);
 floodfill(x,y,c2);
 settextstyle(0,0,2);
 outtextxy(x-2,y-2,c);
 delay(1000);
}


void ShowDate(int x,int y,int z)
{
 char *str;
 setfillstyle(1,BLACK);
 bar(DateViewX+60,DateViewY+2,DateViewX+90,DateViewY-2+DateH);
 bar(DateViewX+60,DateViewY+2+DateH,DateViewX+90,DateViewY-2+DateH*2);
 bar(DateViewX+60,DateViewY+2+DateH*2,DateViewX+90,DateViewY-2+DateH*3);
 settextstyle(0,0,0);
 setcolor(CHAR_COLOR);
 sprintf(str,"%d",x);
 outtextxy(DateViewX+60,DateViewY+10,str);
 sprintf(str,"%d",y);
 outtextxy(DateViewX+60,DateViewY+DateH+10,str);
 sprintf(str,"%d",z);
 outtextxy(DateViewX+60,DateViewY+DateH*2+10,str);
}

void DrawLaugh(int x,int y,int r)/*x=ViewRigeht+50,y=ViewTop+20*/
{
 int d,c1,c2,c3;
 c1=YELLOW;
 c2=BLACK;
 c3=WHITE;
 d=r/5;

 setcolor(c1);
 circle(x,y,r);
 setfillstyle(1,c1);
 floodfill(x,y,c1);

 setcolor(c2);
 line(x-d,y-d*3,x-2*d,y-4*d);
 line(x-2*d,y-4*d,x-2.9*d,y-4*d);
 line(x+d,y-d*3,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)/*x=ViewRigeht+50,y=ViewTop+20*/
{
 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)/*x=ViewRigeht+50,y=ViewTop+20*/
{
 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)/*x=ViewRigeht+50,y=ViewTop+20*/
{
 int r=10,c1=YELLOW,c2=BLACK;
 int d=r/5,i=0;
 while(!kbhit())
 {
  setcolor(c1);
  circle(x,y,r);
  setfillstyle(1,c1);
  floodfill(x,y,c1);
  setcolor(c2);
  line(x-2.5*d-1,y-2.5*d,x-2.5*d-1,y-2.5*d+i);
  line(x-2.5*d,y-2.5*d,x-2.5*d,y-2.5*d+i);
  line(x-2.5*d+1,y-2.5*d,x-2.5*d+1,y-2.5*d+i);
  line(x+2.5*d-1,y-2.5*d,x+2.5*d-1,y-2.5*d+i);
  line(x+2.5*d,y-2.5*d,x+2.5*d,y-2.5*d+i);
  line(x+2.5*d+1,y-2.5*d,x+2.5*d+1,y-2.5*d+i);
  arc(x,y+5*d,30,150,4*d);
  delay(10000);
  i++;
  if(i==(3*d))
  {
   i=0;
   setfillstyle(1,c2);
   pieslice(x,y,0,360,r);
  }
 }
}

void ShowFace(int flag)
{
 int x,y;
 x=DateViewX+50;
 y=DateViewY-20;
 switch(flag)
 {
  case 1:
   Laugh(x,y);
   break;
  case 2:
   Smile(x,y);
   break;
  case 3:
   Weep(x,y);
   break;
  default :
   break;
 }
}
 
void ShowResult(int t,int Hit,int Lose,float ratio)
{
 int x,y,temp,Char_Count;
 int flag;
 char *str;
 Char_Count=Hit+Lose;
 x=ViewLeft+150;
 y=ViewRigeht+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 Time: %d s",t);
 outtextxy(x+20,y+10,str);
 sprintf(str,"Hit  : %d ",Hit);
 outtextxy(x+20,y+40,str);
 sprintf(str,"Lose : %d ",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,y+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=2;
  outtextxy(x-50,y+150,"Sorry!You did a bad work!");
  }
 ShowFace(flag);
}

void Play()
{
 char c,key,*str;
 int e=20,PreKey_Count=0,Hit_Count=0,Char_Count=0;
 int i,x,x1,y1,y,gd=DETECT,gm;
 int Start_T,End_T;
 float f;
 setbkcolor(BK_COLOR);
 PlayViewPort();
 ShowDate(Hit_Count,(Char_Count-Hit_Count),PreKey_Count);
 Start_T=time(NULL);
 while(key!=Esc&&Char_Count<nCount)
 {
  x=getX();
  c=GenerateChar();
  Char_Count++;
  for(y=16;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=ViewBotton-10;y1>=y;y1--)/*从屏幕低端飞出相同字符直到掉下的字符*/
       AutoDraw_Up(x1,y1,key,speed/10);
      Hit_Count++;
      setcolor(RED);
      for(i=10;i<=30;i++)
      {
       circle(x1,y1,i);
       delay(1000);
      }
      /*BombShow(x,y,c);*/
     }
     else
     {
      x1=x+e;
      for(y1=ViewBotton-10;y1>=y;y1--)
      AutoDraw_Up(x1,y1,key,speed/10);
     }
     PreKey_Count++;
    }
    break;
   }
  }
  ShowDate(Hit_Count,(Char_Count-Hit_Count),PreKey_Count);
  setfillstyle(1,0);
  bar(ViewLeft+1,ViewTop+1,ViewRigeht-1,ViewBotton-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;
 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;
 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);
   Loading();
   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;
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的使用Scratch编写的打字游戏代码。 首先,你需要在Scratch的舞台上创建一个文本框,用于显示要输入的文字。然后,你需要创建一个变量,用于记录玩家输入的字母数量。接下来,你需要创建一个列表,包含你想要玩家输入的文本。 在角色部分,你需要创建两个角色,一个是游戏场景,另一个是玩家的小球。在游戏场景角色中,你需要添加以下代码: 当开始被点击时 清空文本框 设置变量 [字母数量] 为 0 当 [空格 v] 键被按下时 如果 <文本框的内容> = <列表的第一个元素>,那么 删除列表的第一个元素 改变 [字母数量] 以 1 结束如果 如果 <字母数量> = <(列表的所有元素的数量)> ,那么 显示对话框 “恭喜!你已完成打字游戏!” 清空变量 [字母数量] 结束如果 接下来,你需要在玩家小球角色中添加以下代码: 当开始被点击时 要求 [准备好开始打字游戏吗?] 并等待 等待 2 秒钟 运动 10 步 现在,你可以在Scratch中尝试运行这个打字游戏代码了。当你准备好开始游戏时,点击玩家小球角色,然后在文本框中输入正确的字母,并按下空格键进行检查。每输入一个正确的字母,列表中的第一个元素将会被删除,并且字母数量变量会增加1。当你输入完所有的字母后,会弹出一个对话框,显示你已经完成了打字游戏。 希望这个简单的代码能帮助你开始编写你自己的Scratch打字游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值