兴趣--学习之母

来源:互联网 发布:sql server附加数据库 编辑:程序博客网 时间:2024/05/02 16:19

我认为兴趣之于学习尤为重要的!个人对于用程序编写游戏总是很羡慕,这个可能是因为我是学习C++的用的是Microsoft Visual C++ 6.0来编程的。这个编程工具是比较好用的,但是有一个缺点就是图形化的东西都不能运行(如识别不了头文件<graphics.h>).别说是游戏了,就是一些简单的图像显示都不能。当然编写算法等还是很方便,也很好用。对此我是比较遗憾的,因为我在做C经典一百题中就有一些是图形的东西结果无法显示。所以我想到了再下载一个编译器。最终我选择了TC编译器。刚开始不会用的,因为都不知道要设置头文件的路径等。后来终于明白了TC的简单用法,然后用它来实现了C经典百题中关于图形的题。看着那些图形,感觉就是比较爽。有了这个工具我就想了却我的一个心愿——编写一个小游戏。但是这确实不是我现在能写的出来的。所以到图书管借了本关于编写游戏的书。今天终于写出了我的第一个小游戏——贪吃蛇游戏。呵呵,在游戏中加了我的名字进去。看着这个游戏,虽然他不是我想的,虽然我现在都不是很理解这个程序,但是还是很开心的。而且,对于编程我也更加的感兴趣了。

一下是游戏的代码,有兴趣的可以在TC上运行一下:

Code:
  1. #define N 200   
  2. #include <graphics.h>   
  3. #include <stdlib.h>   
  4. #include <dos.h>   
  5. #define LEFT 0x4b00   
  6. #define RIGHT 0x4d00   
  7. #define DOWN 0x5000   
  8. #define UP 0x4800   
  9. #define Esc 0x011b   
  10. int i,key;   
  11. int score=0;   
  12. int gamespeed=50000;   
  13. struct Food   
  14. {   
  15.     int x;   
  16.     int y;   
  17.     int yes;   
  18. }food;   
  19.   
  20. struct Snake   
  21. {   
  22.     int x[N];   
  23.     int y[N];   
  24.     int node;   
  25.     int direction;   
  26.     int life;   
  27. }snake;   
  28. void Init(void);   
  29. void Close(void);   
  30. void Drawk(void);   
  31. void GameOver(void);   
  32. void GamePlay(void);   
  33. void PrScore(void);   
  34.   
  35. void main(void)   
  36. {   
  37.     Init();   
  38.     Drawk();   
  39.     GamePlay();   
  40.     Close();   
  41. }   
  42.   
  43. void Init(void)   
  44. {   
  45.     int gd=DETECT,gm;   
  46.     initgraph (&gd,&gm,"d://tc");    /*zlknywt*/  
  47.     cleardevice();   
  48. }   
  49. void Drawk(void)   
  50. {   
  51.     setbkcolor(LIGHTGREEN);   
  52.     setcolor(11);   
  53.     setlinestyle(SOLID_LINE,0,THICK_WIDTH);   
  54.     for(i=50;i<=600;i+=10)   
  55.     {   
  56.         rectangle(i,40,i+10,49);   
  57.         rectangle(i,451,i+10,460);   
  58.     }   
  59.     for(i=40;i<=450;i+=10)   
  60.     {   
  61.         rectangle(50,i,59,i+10);   
  62.         rectangle(601,i,610,i+10);   
  63.     }   
  64. }   
  65. void GamePlay(void)   
  66. {   
  67.     randomize();   
  68.     food.yes=1;   
  69.     snake.life=0;   
  70.     snake.direction=1;   
  71.     snake.x[0]=100;   
  72.     snake.y[0]=100;   
  73.     snake.x[1]=110;   
  74.     snake.y[1]=100;   
  75.     snake.node=2;   
  76.     PrScore();   
  77.     while(1)   
  78.     {   
  79.         while(!kbhit())   
  80.         {   
  81.             if(food.yes==1)   
  82.             {   
  83.                 food.x=rand()%400+60;   
  84.                 food.y=rand()%350+60;   
  85.                 while(food.x%10!=0)   
  86.                     food.x++;   
  87.                 while(food.y%10!=0)   
  88.                     food.y++;   
  89.                 food.yes=0;   
  90.             }   
  91.             if(food.yes==0)   
  92.             {   
  93.                 setcolor(GREEN);   
  94.                 rectangle(food.x,food.y,food.x+10,food.y-10);   
  95.             }   
  96.             for(i=snake.node-1;i>0;i--)   
  97.             {   
  98.                 snake.x[i]=snake.x[i-1];   
  99.                 snake.y[i]=snake.y[i-1];   
  100.             }   
  101.             switch(snake.direction)   
  102.             {   
  103.             case 1:   
  104.                 snake.x[0]+=10;break;   
  105.             case 2:   
  106.                 snake.x[0]-=10;break;   
  107.             case 3:   
  108.                 snake.y[0]-=10;break;   
  109.             case 4:   
  110.                 snake.y[0]+=10;break;   
  111.             }   
  112.             for(i=3;i<snake.node;i++)   
  113.             {   
  114.                 if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])   
  115.                 {   
  116.                     GameOver();   
  117.                     snake.life=1;   
  118.                     break;   
  119.                 }   
  120.             }   
  121.             if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)   
  122.             {   
  123.                 GameOver();   
  124.                 snake.life=1;   
  125.             }   
  126.             if(snake.life==1)   
  127.                 break;   
  128.             if(snake.x[0]==food.x&&snake.y[0]==food.y)   
  129.             {   
  130.                 setcolor(0);   
  131.                 rectangle(food.x,food.y,food.x+10,food.y-10);   
  132.                 snake.x[snake.node]=-20;   
  133.                 snake.y[snake.node]=-20;   
  134.                 snake.node++;   
  135.                 food.yes=1;   
  136.                 score+=10;   
  137.                 PrScore();   
  138.             }   
  139.             setcolor(4);   
  140.             for(i=0;i<snake.node;i++)   
  141.                 rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);   
  142.             delay(gamespeed);   
  143.             setcolor(0);   
  144.             rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);   
  145.         }   
  146.         if(snake.life==1)   
  147.             break;   
  148.         key=bioskey(0);   
  149.         if(key==Esc)   
  150.             break;   
  151.         else if(key==UP&&snake.direction!=4)   
  152.             snake.direction=3;   
  153.         else if(key==RIGHT&&snake.direction!=2)   
  154.             snake.direction=1;   
  155.         else if(key==LEFT&&snake.direction!=1)   
  156.             snake.direction=2;   
  157.         else if(key==DOWN&&snake.direction!=3)   
  158.             snake.direction=4;   
  159.      }/*endwhile(1)*/  
  160. }   
  161.   
  162. void GameOver(void)   
  163. {   
  164.     cleardevice();   
  165.     PrScore();   
  166.     setcolor(RED);   
  167.     settextstyle(0,0,4);   
  168.     outtextxy(200,200,"GAME OVER");   
  169.     getch();   
  170. /*  clrscr();  
  171.     cleardevice();  
  172.     setcolor(RED);  
  173.     settextstyle(0,0,4);  
  174.     outtextxy(200,200,"AWEN MAKE");  
  175. */  
  176. }   
  177.   
  178. void PrScore(void)   
  179. {   
  180.     char str[10];   
  181.     setfillstyle(SOLID_FILL,YELLOW);   
  182.     bar(50,15,220,35);   
  183.     setcolor(6);   
  184.     settextstyle(0,0,2);   
  185.     sprintf(str,"score:%d",score);   
  186.     outtextxy(55,20,str);   
  187. }   
  188.   
  189. void Close(void)   
  190. {   
  191.     getch();   
  192.     closegraph();   
  193. }   
  194.   
  195.   

敲这段代码,只是因为个人兴趣而已。再次声明这段代码不是我设计的,呵呵。但是也很满足了。希望想学习的朋友都能从学习中找到快乐。

原创粉丝点击