2025年3月26日 星期三 甲辰(龙)年 月廿五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > C语言

C语言实现打字游戏

时间:01-03来源:作者:点击数:103

该程序在TC3.0环境下编译通过,共有9个函数。分别是printScreen,start,leave,levelChoice,newWord,moving,wordHit,result和main。

其功能分别是:

printScreen:刷新屏幕输出的图像。

Start:用户进入程序时选择开始,退出和求助。

Leave:用户离开程序事提示感谢使用

levelChoice:用户开始之前选择等级

newWord:生成一个新的字母并将其置于首行。

moving:将屏幕上的所有字母向下移动一行

wordHit:判断用户是否输入字母,如果输入,且屏幕上有该字母则将该字母所在位置置空。

result:判断该用户的得分,提示其是否进入下一等级。

main:总体控制以上各函数,判断其是否运行。

压缩包下载: 打字游戏 (19.71 KB)

-------------------------------------------------------------以下是程序源码----------------------------------------------------

  • #include "stdio.h"
  • #include "time.h"
  • #include "stdlib.h"
  • #include "conio.h"
  • #include "dos.h"
  • #define xLine 70
  • #define yLine 20
  • #define full 100
  • #define true 1
  • #define false 0
  • /*---------------------------------------------------------------------*/
  • void printScreen(int level,int right,int sum,char p[yLine][xLine])/* 刷新屏幕的输出图像 */
  • {
  • int i,j;
  • clrscr();
  • printf("level:%d Press 0 to exit;1 to pause score:%d/%d\n",level,right,sum);/* 输出现在的等级,击中数和现在已下落总数 */
  • printf ("----------------------------------------------------------------------\n");
  • for (i=0;i<yLine;i++)
  • {
  • for(j=0;j<xLine;j++)
  • printf ("%c",p[i][j]);
  • printf("\n");
  • }/* for (i) */
  • printf ("----------------------------------------------------------------------\n");
  • }/* printScreen */
  • /*---------------------------------------------------------------------*/
  • void leave()/* 离开程序时,调用该函数结束程序。 */
  • {
  • clrscr();
  • printf ("\n\n\n\nThank you for playing.");
  • delay (2500);
  • exit (0);
  • }
  • /*----------------------------------------------------------------------*/
  • int levelChoice(int level)/* 进入游戏时选择游戏等级 */
  • {
  • while (true)/* void */
  • {
  • clrscr ();
  • printf("please input 1-9 to choice level.choice 0 to return.\n");
  • level=getch();
  • level=level-48;
  • if (level>0&&level<10) return (level);
  • else if (level==0)
  • leave ();
  • else
  • printf ("Please input a correct number!\n");
  • }/* while (true) */
  • }/* levelChoice */
  • /*---------------------------------------------------------------------*/
  • 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;
  • return (++sum);
  • }/* if */
  • return (sum);
  • }/* newWord */
  • /*---------------------------------------------------------------------*/
  • int moving(int miss,char p[yLine][xLine])/* 将最后一行置空,并使所有在数组中其他行的字符下降一行 */
  • {
  • int i,j;
  • char w;
  • for (j=1,i=yLine-1;j<xLine-1;j++)/* 遍历最后一行的所有字符,如果该字符非空则将其置空并使miss加一 */
  • {
  • 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;
  • }/* if */
  • }/* for(j) */
  • }/* for(i) */
  • return (miss);
  • }/* moving */
  • /*---------------------------------------------------------------------*/
  • int wordHit(char p[yLine][xLine])
  • /*判断是否有字符从键盘键入。
  • 如果有,则从最后一行的最后一个元素开始遍历该数组,找出该字符,并把对应位置置空,且返回1。
  • 如果有输入,但屏幕上无对应项,或无输入则返回0*/
  • {
  • int i,j;
  • char key;
  • if(kbhit())/* 判断用户是否从键盘键入字符。如果kbhit返回值为 */
  • key=getch();
  • if(key)
  • {
  • if (key=='0') leave();
  • if (key=='1')
  • {
  • clrscr();
  • printf ("Press any key to continue.");
  • 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);
  • }/* if */
  • }/* for (j) */
  • }/* for (i) */
  • sound(300);
  • }/* if (key) */
  • return (false);
  • }/* wordHit */
  • /*---------------------------------------------------------------------*/
  • int result(int right)
  • /*判断该次的成绩并输出对应的结果,询问用户是否继续,若继续,判断是否可以进入下一级别。*/
  • {
  • int score;
  • char yn;
  • score=right*100/full;
  • nosound();
  • clrscr();
  • if (score==100)
  • printf("perfect!\n");
  • else if (score>=85)
  • printf("good!\n");
  • else if (score>=70)
  • printf("That's OK!\n");
  • else
  • printf("you need to play again.\n");
  • printf("do you want to continue? Y/N\n");
  • for (;;)/* void */
  • {
  • yn=getch();
  • switch (yn)
  • {
  • case 'y' :
  • {
  • if (score>=70) return(true);
  • else return(false);
  • }/* case */
  • case 'n' : leave ();
  • default : printf("Please input a correct choice:");
  • }/* switch */
  • }/* for */
  • }/* result */
  • /*---------------------------------------------------------------------*/
  • int start()/* 进入程序时调用该函数,提示其操作。 */
  • {
  • char c;
  • while (true)
  • {
  • clrscr ();
  • printf ("\n\n\n\n\n Welcome to type game!\n\n\n 1.start\n 2.How to play\n 0.Exit");
  • c=getch();
  • switch (c)
  • {
  • case '0' : leave();/* 用户选择退出,退出主程序 */
  • case '2' :
  • {
  • clrscr();
  • printf ("\n\n\n\n\n Hit the type when you see it on the screen.\n Press 0 to exit.\n Press 1 to pause\n 1.start\n 0.exit");
  • while (true)
  • {
  • c=getch ();
  • if (c=='0') leave();
  • if (c=='1') return (true);
  • }
  • }
  • case '1' : return (true);
  • }
  • }
  • }
  • /*----------------------------------------------------------------------*/
  • void main()
  • {
  • /*---------------------------------------------------------------------*/
  • /*函数声明*/
  • void printScreen(int level,int right,int sum,char p[yLine][xLine]);
  • void leave();
  • int levelChoice(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(int right);
  • int start();
  • /*---------------------------------------------------------------------*/
  • char p[yLine][xLine];
  • int i,j,level,right,sum,n,m,miss;
  • srand(time(NULL));
  • start();
  • for (i=0;i<yLine;i++)/* 初始化屏显数组 */
  • {
  • for (j=0;j<xLine;j++)
  • if (j==0||j==xLine-1)
  • p[i][j]=140;
  • else
  • p[i][j]=' ';
  • }/* for (i) */
  • level=levelChoice(level);
  • for (;;)/* 开始运行主程序 */
  • { sum=0;
  • right=0;
  • miss=0;
  • printf("Press any key to start!");
  • m=getch();
  • printScreen(level,right,sum,p);
  • for (n=0,m=4;;n++)
  • {
  • delay(20);/* 延迟 */
  • nosound();
  • if(m%4==0)/* 当m为4的整数倍,即上一个字母下落3行时生成一个新的字母在首行并刷新屏幕 */
  • {
  • sum=newWord(sum,p);
  • m=5;
  • printScreen(level,right,sum,p);
  • }/*if(newWord)*/
  • if (wordHit(p)==true)/* 如果用户输入了字符并且正确,则使答对的数加一,并刷新屏幕 */
  • {
  • right++;
  • sound (1500);
  • printScreen(level,right,sum,p);
  • }/* if(wordHit) */
  • if(n==(37-4*level))/* 当n=37-4*level时,屏幕上的字母下落一行,并刷新屏幕,若最后一行有字母,则使错过数加一 */
  • {
  • n=0;
  • m++;
  • miss=moving(miss,p);
  • printScreen(level,right,sum,p);
  • }/* if (moving)*/
  • if(right+miss==full) break;/* 当正确数加错误数等于预置的总字母个数时跳出该循环 */
  • }
  • if (result (right)==true&&level<9)/* 当用户成绩在70以上,并且等级小于9时,等级加一,即进入下一级别 */
  • level++;
  • }/* for(sum,right) */
  • }

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门