C语言基础——打字游戏

#include

#include

#include

#include

#include

#include

#define xLine 70

#define yLine 20

#define full 100

#define true 1

#define false 0

main()

{

voidprintScreen(int level,int right,int sum,char p[yLine][xLine]);

int start();

void leave();

intlevelChoice(int level);

int newWord(int sum,char p[yLine][xLine]);

int moving(int miss,char p[yLine][xLine]);

int wordHit(char p[yLine][xLine]);

int result(intright);

void clrscr();

 char p[yLine] [xLine];

int i,j,level=0,right,sum,n,m,miss;

srand(time(NULL));

start();

for(i=0;i

{

        for(j=0;j

               if(j==0||j==xLine-1)

                      p[i][j]='|';            

               else

                      p[i][j]='';

}

level=levelChoice(level);

for(;;)

{

        sum=0;

        right=0;

       miss=0;

        printf("按任意键开始游戏!");

        m=getch();

        printScreen(level,right,sum,p);

        for(n=0,m=4;;n++)

        {

               Sleep(10);

               if(m%4==0)

              

               {

                      sum=newWord(sum,p);

                      m=5;

                      printScreen(level,right,sum,p);

               }

        if(wordHit(p)==true)

       

        {

               right++;

               Beep(440,1500);

               printScreen(level,right,sum,p);

        }

        if(n==(37-4*level))

       

        {

               n=0;

               m++;

               miss=moving(miss,p);

               printScreen(level,right,sum,p);

       

        }

        if(right+miss==full) break;

       

        }

       

        if(result(right)==true && level<9)

        level++;

}

}

//清空屏幕(刷新)

void clrscr()

{

system("cls");

}

 

//刷新屏幕的输出图像

void printScreen(int level,int right,int sum,char p[yLine][xLine])

{

int i,j;

clrscr();

printf("等级:%d           0退出;1暂停     ;2返回菜单      输入正确字符%d\n                                           游戏总字符%d\n",level,right,sum);

printf("======================================================================\n");

for(i=0;i

{

        for(j=0;j

               printf("%c",p[i][j]);

        printf("\n");

}

printf("======================================================================\n");

 }

 

 //结束程序

void leave()

 {

       clrscr();

       printf("\n\n\n\n感谢使用!!!");

 Sleep(1);

 exit(0);

  }

 

//选择等级

int levelChoice(int level)

  {

     

      while(true)

      {

             clrscr();

             printf("请从1-9中选择打字速度!\n");

             level=getch();

             level=level-48;

             if(level>0 && level<10)return(level);

             else if(level==0)

                    leave();

             else

                    printf("请输入正确号码!\n");

  }

   }

  

//随机生成字母

int newWord(int sum,char p[yLine][xLine])

{

int j,w;

if(sum!=full)

{

        j=(rand()%(xLine-2))+1;                   //随机生成字母出现的位置

        w=(rand()%26)+65;                   //随机生成字母

        p[0][j]=w;

        sum++;

}

return(sum);

 }

 

//字母下降

int moving(int miss,char p[yLine][xLine])

{

int i,j;

char w;

for(j=1,i=yLine-1;j//游历一遍最后一行,不是空格miss加1

{

        if(p[i][j]!='')

        {

               miss++;

               p[i][j]='';

        }

}

for(i=yLine-2;i>=0;i--)         //游历倒数第二行,如果该行有字母就移到下一行(字母的下降)

{

        for(j=xLine-2;j>0;j--)

        {

               if(p[i][j]!='')

               {

                      w=p[i][j];

                      p[i][j]='';

                      p[i+1][j]=w;

               }

        }

}

return(miss);                  //返回用户打字游戏打错的字母的个数

 }

 //主菜单

int start()

{

char c;

while(true)

{

clrscr();

printf("\n\n\n\n\n欢迎来到打字游戏! \n\n\n1.开始游戏\n2.游戏说明\n0.退出游戏\n");

c=getch();

switch(c)

{

        case '0':leave();

        case '2':

        {

               clrscr();

               printf("\n\n\n\n\n在字母从屏幕消失前,从键盘上输出对应字符。\n\n1.开始游戏\n2.返回菜单\n0.退出游戏\n");

               while(true)

               {

                      c=getch();

                      if(c=='0')leave();

                      if(c=='2')start();

                      if(c=='1')return(true);

               }

        }

        case'1' :return(true);

}

}

}

//用户输入后 与屏幕字符对比

int wordHit(char p[yLine][xLine])

{

int i,j;

char key;

if(kbhit()) //判断用户是否从键盘输入字符。有输入kbhit返回值不为0

{

        key=getch();

        putch(key);

}

if(key)

{

        if(key=='0')leave();

               if(key=='2')start();

               if(key=='1')

        {

               clrscr(); 

        printf("按任意键继续!\n");

        getch();

        }

        for(i=yLine;i>0;i--) //从最后一行的最后一个字母开始游历

        {

               for(j=xLine;j>0;j--)

               {

                      if(key-32==p[i-1][j-1])

                      {

                      p[i-1][j-1]=''; //如果一输入的字符,则把对应的置空

                      return(true);

                      }

               }

        }

       }

       return (false);

}

 

//输出结果 询问用户是否继续

int result(int right)

{

int score;

char yn;

score=right*100/full;

clrscr();

if(score==100)

        printf("非常棒!\n");

        elseif(score>=85)

               printf("!\n");

elseif(score>=70)

        printf("继续努力!\n");

else

        printf("成绩不理想!\n");

printf("是否继续?YN\n");

for(;;)

{

        yn=getch();

        switch(yn)

        {

               case'y':

               {

                      if(score>=70)

                      {

                      return(true);

                      }

                      elsereturn(false);

               }

               case'n':leave();

               desault:printf("请输入一个正确的选择:");

        }

}

 }


C语言基础——打字游戏_第1张图片

你可能感兴趣的:(C语言基础,c语言,游戏,初学者打字游戏)