搜索
查看: 746|回复: 9
打印 上一主题 下一主题

[转贴] C语言打字母游戏

[复制链接]
跳转到指定楼层
楼主
发表于 2014-9-27 12:55:49 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
WIN—TC可以运行
啊哈C运行会出毛病……
因为有一些函数只有WIN—TC里面有吗。。。

example.rar

8.84 KB, 下载次数: 14

沙发
 楼主| 发表于 2014-9-27 13:00:04 | 只看该作者
自古沙发归楼主
板凳
发表于 2014-9-27 20:54:59 | 只看该作者
TC............
地板
发表于 2014-9-27 21:27:08 | 只看该作者
顶楼主ddddddd
5#
发表于 2014-9-27 21:39:43 | 只看该作者
这不是随便改改就能移植的吗
6#
发表于 2014-9-27 21:45:13 | 只看该作者
本帖最后由 rosynirvana 于 2014-9-27 21:55 编辑

这是从哪里转的,fun_Esc写那么多,fun_Play居然塞在死循环里
7#
发表于 2014-9-27 22:33:57 | 只看该作者
移植版
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>

  6. int fun_Play();
  7. int fun_Esc();

  8. HANDLE hStdout, hStdin;
  9. CONSOLE_SCREEN_BUFFER_INFO csbi;
  10. const COORD startCoords = {0, 0};

  11. void init_console_functions(void)
  12. {
  13.         hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  14.         hStdin = GetStdHandle(STD_INPUT_HANDLE);

  15.         GetConsoleScreenBufferInfo(hStdout, &csbi);
  16. }

  17. void delay(unsigned int t)
  18. {
  19.         Sleep(t);
  20. }

  21. void gotoxy(int x, int y)
  22. {
  23.         COORD coord;

  24.         coord.X = x-1;
  25.         coord.Y = y;

  26.         SetConsoleCursorPosition(hStdout, coord);
  27. }

  28. void clrscr()
  29. {
  30.         DWORD discarded;
  31.         FillConsoleOutputCharacter(hStdout, ' ', csbi.dwSize.X * csbi.dwSize. Y,
  32.                 startCoords, &discarded);
  33.         gotoxy(0, 0);
  34. }


  35. char *kw[]={"Q W E R T Y U I O P [ ]","A S D F G H J K L ; '","Z X C V B N M , . / "};
  36. long AllCounter=0, RightCounter=0, WrongCounter=0;
  37. int main()
  38. {
  39.         int i,j;
  40.         init_console_functions();
  41.        
  42.         clrscr();
  43.         gotoxy(18,1);
  44.         printf("%s\n",kw[0]);
  45.         gotoxy(20,3);
  46.         printf("%s\n",kw[1]);
  47.         gotoxy(22,5);
  48.         printf("%s\n",kw[2]);
  49.         gotoxy(11,25);
  50.         for(i=0;i<60;i++)
  51.         {
  52.                 printf("=");
  53.         }
  54.         gotoxy(1,1);
  55.         printf("AllCh: %ld\nRight: %ld\nWrong: %ld",AllCounter,RightCounter,WrongCounter);
  56.         gotoxy(50,1);
  57.         printf("Press Esc to exit");
  58.         gotoxy(50,2);
  59.         printf("Enter to pause");
  60.         gotoxy(26,12);
  61.         printf("* * * * * * * * * * * * * * ");
  62.         gotoxy(26,13);
  63.         printf("* Press any key to start! *");
  64.         gotoxy(26,14);
  65.         printf("* * * * * * * * * * * * * * ");
  66.         gotoxy(51,13);

  67.         j = getch();
  68.         if(j==27)
  69.         {
  70.                 if(fun_Esc()==1)
  71.                 {
  72.                         clrscr();
  73.                         exit(0);
  74.                 }
  75.         }

  76.         gotoxy(23,12);
  77.         printf(" ");
  78.         gotoxy(23,13);
  79.         printf(" ");
  80.         gotoxy(23,14);
  81.         printf(" ");
  82.         while(1){
  83.         fun_Play();       
  84.         }
  85. }



  86. int fun_Play()
  87. {
  88.         int x,y,i,j;
  89.         unsigned int Timer;
  90.         char ch;
  91.         char cur;
  92.        
  93.         time_t t;
  94.         srand((unsigned)time(&t));
  95.         gotoxy(26,12);
  96.         printf("                              ");
  97.         gotoxy(26,13);
  98.         printf("                              ");
  99.         gotoxy(26,14);
  100.         printf("                              ");
  101.        
  102.         y = 6;
  103.         Timer = 1000;
  104.         i = rand()%3;
  105.         j = rand()%(9-i);
  106.         ch = kw[i][j*4];
  107.         x = 18+i*2+j*4;
  108.         while(y<=24)
  109.         {
  110.                 if(kbhit())
  111.                 {
  112.                         cur = getch();
  113.                         if(cur==ch || cur==ch+32)
  114.                         {
  115.                                 ch = '*'; Timer = 10;
  116.                         }
  117.                         else if(cur==27)
  118.                         {
  119.                                 if(fun_Esc()==1)
  120.                                 {
  121.                                         clrscr();
  122.                                         exit(0);
  123.                                 }
  124.                         }
  125.                         else if(cur=='\r')
  126.                         {
  127.                                 gotoxy(x,y-1);
  128.                                 printf(" ");
  129.                                 gotoxy(26,12);
  130.                                 printf("* * * * * * * * * * * * * * *");
  131.                                 gotoxy(26,13);
  132.                                 printf("* Press any key to continue *");
  133.                                 gotoxy(26,14);
  134.                                 printf("* * * * * * * * * * * * * * * ");
  135.                                 getch();
  136.                                 gotoxy(28,13);
  137.                                 printf(" ");
  138.                         }
  139.                         else
  140.                         {
  141.                                 WrongCounter++;
  142.                         }
  143.                 }
  144.                 if(y>6)
  145.                 {
  146.                         gotoxy(x,y-1);
  147.                         printf(" ");
  148.                 }
  149.                 gotoxy(x,y);
  150.                 printf("%c",ch);
  151.                 gotoxy(1,1);
  152.                 printf("AllCh: %ld\nRight: %ld\nWrong: %ld",AllCounter,RightCounter,WrongCounter);
  153.                 delay(Timer);
  154.                 y++;
  155.         }
  156.         AllCounter++;
  157.         if(ch == '*')
  158.         {
  159.                 RightCounter++;
  160.         }
  161.        
  162. }

  163. int fun_Esc()
  164. {
  165.         int key = '#';
  166.         gotoxy(26,12);
  167.         printf("* * * * * * * * * * * * * * * * ");
  168.         gotoxy(26,13);
  169.         printf("* Are you sure to exit? (Y/N) *");
  170.         gotoxy(26,14);
  171.         printf("* * * * * * * * * * * * * * * * ");
  172.         gotoxy(51,13);
  173.         while(key!='Y' && key!='y' && key!='N' && key!='n')
  174.         {
  175.                 key = getch();
  176.                 if(key=='Y' || key=='y')
  177.                 {
  178.                         return 1;
  179.                 }
  180.                 if(key=='N' || key=='n')
  181.                 {
  182.                         gotoxy(24,12);
  183.                         printf(" ");
  184.                         gotoxy(24,13);
  185.                         printf(" ");
  186.                         gotoxy(24,14);
  187.                         printf(" ");
  188.                         return 0;
  189.                 }
  190.         }
  191. }
复制代码

只做移植必要修改,另外我不懂为什么处理间隔是100秒(就是说100秒字母才下落一行),100秒的间隔没人会忍受,也完全称不上是一个游戏,所以我改成了1秒,另外正确后下落频率是10毫秒1行

会出现无法打印的字符,这是原作者在实现的时候出的疏漏
8#
发表于 2014-9-28 03:44:05 | 只看该作者
重构版本
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <windows.h>
  7. #include <ctype.h>

  8. #define ESC 0x1B

  9. void play();
  10. void print_blank_line();
  11. int case_insensitive_cmp(int a, int b);
  12. int quit();
  13. void del_box();
  14. void init_console_functions(void);
  15. void gotoxy(int x, int y);
  16. void clrscr();
  17. void print_head_info();


  18. HANDLE hStdout, hStdin;
  19. CONSOLE_SCREEN_BUFFER_INFO csbi;
  20. const COORD startCoords = {0, 0};

  21. char *keys[3];
  22. long all = 0, correct = 0, wrong = 0;

  23. int main()
  24. {
  25.         init_console_functions();

  26.         keys[0] = "QWERTYUIOP[]";
  27.         keys[1] = "ASDFGHJKL;'";
  28.         keys[2] = "ZXCVBNM,./";

  29.         print_head_info();
  30.        
  31.         gotoxy(26,12);
  32.         printf("* * * * * * * * * * * * * * ");
  33.         gotoxy(26,13);
  34.         printf("* Press any key to start! *");
  35.         gotoxy(26,14);
  36.         printf("* * * * * * * * * * * * * * ");
  37.         gotoxy(51,13);

  38.         if(getch() == ESC && quit() == 1) // 0x1B for Esc
  39.         {
  40.                 clrscr();
  41.                 exit(0);  
  42.         }

  43.         while(1)
  44.         play();       

  45.         return 0;
  46. }

  47. void play()
  48. {
  49.         int x,y,i,j;
  50.         unsigned int timer;
  51.         char ch;
  52.         char cur;
  53.        
  54.         srand(time(NULL));
  55.         del_box();
  56.        
  57.         y = 6;
  58.         timer = 700;
  59.         i = rand() % 3;
  60.         j = abs(rand() % 12 - i);
  61.         ch = keys[i][j];
  62.         x = 18 + i*2 + j*2;

  63.         while(y<=23)
  64.         {
  65.                 if(kbhit())
  66.                 {
  67.                         cur = getch();
  68.                         if(case_insensitive_cmp(cur, ch) == 0)
  69.                         {
  70.                                 ch = '*';
  71.                                 correct += 1;
  72.                                 timer = 10;
  73.                         }
  74.                         else if(cur == ESC && quit() == 1)
  75.                         {
  76.                                 clrscr();
  77.                                 exit(0);
  78.                         }
  79.                         else if(cur=='\r')
  80.                         {
  81.                                 gotoxy(x,y-1);
  82.                                 putchar(' ');
  83.                                 gotoxy(26,12);
  84.                                 printf("* * * * * * * * * * * * * * *");
  85.                                 gotoxy(26,13);
  86.                                 printf("* Press any key to continue *");
  87.                                 gotoxy(26,14);
  88.                                 printf("* * * * * * * * * * * * * * * ");
  89.                                 getch();
  90.                                 del_box();
  91.                         }
  92.                         else
  93.                                 wrong += 1;
  94.                 }

  95.                 if(y>6)
  96.                 {
  97.                         gotoxy(x,y-1);
  98.                         putchar(' ');
  99.                 }
  100.                 gotoxy(x,y);
  101.                 putchar(ch);
  102.                 gotoxy(1,1);
  103.                 printf("AllCh: %ld\nRight: %ld\nWrong: %ld", all, correct, wrong);
  104.                 Sleep(timer);
  105.                 y += 1;
  106.         }
  107.         all += 1;
  108. }


  109. int quit()
  110. {
  111.         int pressed_key;
  112.         gotoxy(26,12);
  113.         printf("* * * * * * * * * * * * * * * * ");
  114.         gotoxy(26,13);
  115.         printf("* Are you sure to exit? (Y/N) *");
  116.         gotoxy(26,14);
  117.         printf("* * * * * * * * * * * * * * * * ");
  118.         gotoxy(51,13);
  119.         do{
  120.                 pressed_key = getch();
  121.                 if(case_insensitive_cmp(pressed_key, 'y') == 0)
  122.                         return 1;
  123.                 else if(case_insensitive_cmp(pressed_key, 'n') == 0){
  124.                         del_box();
  125.                         return 0;
  126.                 }
  127.         }while(1);

  128.         //Impossible to reach here;
  129.         assert(0);
  130.         return -1;
  131. }

  132. void del_box()
  133. {
  134.         gotoxy(1, 12);
  135.         print_blank_line();
  136.         gotoxy(1, 13);
  137.         print_blank_line();
  138.         gotoxy(1, 14);
  139.         print_blank_line();
  140. }

  141. int case_insensitive_cmp(int a, int b)
  142. {
  143.         if(toupper(a) == toupper(b))
  144.                 return 0;
  145.         return 1;
  146. }

  147. void print_blank_line()
  148. {
  149.         printf("                                                             ");
  150. }

  151. void print_head_info()
  152. {
  153.         int i;
  154.         clrscr();
  155.         gotoxy(18,1);
  156.         for(i=0; keys[0][i]; ++i){
  157.                 putchar(keys[0][i]);
  158.                 putchar(' ');
  159.         }
  160.         putchar('\n');

  161.         gotoxy(20,3);
  162.         for(i=0; keys[1][i]; ++i){
  163.                 putchar(keys[1][i]);
  164.                 putchar(' ');
  165.         }
  166.         putchar('\n');
  167.         gotoxy(22,5);
  168.        
  169.         for(i=0; keys[2][i]; ++i){
  170.                 putchar(keys[2][i]);
  171.                 putchar(' ');
  172.         }
  173.         putchar('\n');
  174.         gotoxy(11,24);

  175.         for(i=0;i<60;i++)
  176.         {
  177.                 putchar('=');
  178.         }
  179.        
  180.         gotoxy(1,1);
  181.         printf("AllCh: %ld\nRight: %ld\nWrong: %ld", all, correct, wrong);
  182.         gotoxy(50,1);
  183.         printf("Press Esc to exit");
  184.         gotoxy(50,2);
  185.         printf("Enter to pause");
  186. }

  187. void init_console_functions(void)
  188. {
  189.         hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  190.         hStdin = GetStdHandle(STD_INPUT_HANDLE);

  191.         GetConsoleScreenBufferInfo(hStdout, &csbi);
  192. }


  193. void gotoxy(int x, int y)
  194. {
  195.         COORD coord;

  196.         coord.X = x-1;
  197.         coord.Y = y-1;

  198.         SetConsoleCursorPosition(hStdout, coord);
  199. }

  200. void clrscr()
  201. {
  202.         DWORD discarded;
  203.         FillConsoleOutputCharacter(hStdout, ' ', csbi.dwSize.X * csbi.dwSize. Y,
  204.                 startCoords, &discarded);
  205.         gotoxy(0, 0);
  206. }
复制代码


play还需要彻底重构,现在的主要缺陷是主要事件都卡在play的一个循环里面,检测间隔导致手感非常僵硬。
9#
 楼主| 发表于 2014-9-28 11:58:56 | 只看该作者
rosynirvana 发表于 2014-9-27 22:33
移植版

只做移植必要修改,另外我不懂为什么处理间隔是100秒(就是说100秒字母才下落一行),100秒的间 ...

delay();是以毫秒作为单位
10#
发表于 2014-9-28 12:20:41 | 只看该作者
4399APPLE 发表于 2014-9-28 11:58
delay();是以毫秒作为单位

你自己看看原来的源码默认多少毫秒……
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表