回 帖 发 新 帖 刷新版面

主题:[原创]贴一个最近写的小的赛车程序互相学习。

初步写了一下,还没有润色和详细改错。函数都是以中文命名应该不用说明了。
tc2.0编译。

/*This function is a race game, be use 'j,k,l' and 'i' to control the car.
  The 'e' means exiting the game, The 'r'means restart the race */

#include "graphics.h"
#include "conio.h"
#include "stdio.h"
enum shu {daolu, kongdi, qiche, qita};
enum setvalue {veryeasy, easy, normal, hard, veryhard};
enum setvalue set_sudu=veryeasy,set_nandu=veryeasy;

main()
{int gdriver, gmode;
void hua_lu();  void hua_che();  void sheng_lu();  void chushi_lu();
void hua_biankuang();  int ceshi_key();  void chongxin_bisai();
void chuli_che();    int ceshi_chugui();
enum shu matrix[128][96];
int chezhong[2]={64,86};
char keyvalue='a';
int kyh,shenming=100;
long i=0;
                                                            /*提示信息*/
kkkk:
printf("\n\n\t\t\tNAME:     MiniCar\n\t\t\tVERSION:  1.4\n\t\t\tCREATOR:  kanker\n\n");
printf("NARRATE:   This function is a race game, be use 'j,k,l' and 'i' to control the car. press 'e' means exiting the game, press 'r' means restart the race\n");
printf("\nTHE GAME SET:   Please input the value of speed and dificulty (0-4),");
printf("for example input '0,0' means speed=0 and dificulty=0,and so on.\n");
printf("\n\n\tGOOD LUCKY!\n\n\tplease input===>");
scanf("%d,%d",&set_sudu,&set_nandu);
if (set_sudu<5 &&set_nandu<5)
   { printf ("\nGOOD! SET OK!\n please waiting...");sleep(1.5);}
else
   { printf("\n\nINPUT ERROR,REINPUT\n");   goto kkkk; }


gdriver =9;    gmode =2;
initgraph(&gdriver,&gmode,"d:\\turboc2");

chushi_lu(matrix,set_nandu);                               /*初始化道路*/
for (;kyh!=10;i++)              
  { hua_biankuang();
    sheng_lu(matrix,set_sudu);
    hua_lu(matrix);
    hua_che(chezhong);
    if ( kbhit()==0 ) { keyvalue='c'; }
    else { keyvalue=getche();}                
    kyh=ceshi_key(keyvalue);                                /*处理键盘输入*/
    if (kyh==6) { nosound(); goto kkkk;shenming=100;i=0;}
    chuli_che(chezhong,kyh);
    shenming=shenming-ceshi_chugui(chezhong,matrix);
    sleep(0.75);  if(i>10) { sound(50+((int)(i/5))%100); }   /*赛车声音*/
    if (shenming<1)
      {setcolor(0);    outtextxy(280,25,"GAME START");
       setcolor(15);   outtextxy(280,175,"GAME OVER");  kyh=10;
      }
  }
nosound();
getch();
closegraph();
printf("\n\n Your score is:%ld\n\nTHANKS,BYEBYE!",i*(set_nandu+set_sudu+1));
sleep(5);
return 0;
}

void chushi_lu(enum shu aa[128][96],enum set nandu)
{int i,j;
for (i=20;i<=108;i++)
   { for (j=10;j<=86;j++)
       { if (i<80-nandu && i>50+nandu) { aa[i][j]=daolu; }
     else aa[i][j]=qita;
       }
   }
}

void sheng_lu(enum shu aa[128][96],enum set sudu)
{int i=0,j=0,k,kang;
kang=(int) rand() % 10;
for (i=20;i<=108;i++)
   { for (j=86;j>10+sudu;j--)
       {aa[i][j]=aa[i][j-1-sudu];
       }
   }
if (kang<5 && aa[20][10]==qita)
   {for (k=0;k<=sudu;k++)
      { for (i=20;i<108;i++)
          { aa[i][10+k]=aa[i+1][10+k]; }
          aa[108][10+k]=qita;
      }
   }
if (kang>=5 && aa[108][10]==qita)
   {for (k=0;k<=sudu;k++)
      { for (i=108;i>20;i--)
          { aa[i][10+k]=aa[i-1][10+k]; }
        aa[20][10+k]=qita;
      }
   }
}

void hua_lu(enum shu aa[128][96])
{int i,j;
for (i=20;i<=108;i++)
   { for (j=10;j<=86;j++)
       { if (aa[i][j] != daolu)
       { putpixel(i*5,j*5,7); }
     else
       { putpixel(i*5,j*5,0); }
       }
   }
}

void hua_che(int zhongxin[2])        /* the car's profile */
{int i,j,x,y;
x=zhongxin[0];  y=zhongxin[1];
for (i=0;i<5;i++)
   { for (j=0;j<8;j++)
       {
     { if (i!=0 && i!=4) putpixel ((x-4+i*2)*5,(y-j*2)*5,7);
       else
        {if (j!=7 && j!=3 && j!=4)
         putpixel ((x-4+i*2)*5,(y-j*2)*5,7);
        }
     }
       }
   }
}

void hua_biankuang ()
{enum shu bb[128][96];
setcolor(15);
rectangle(10,10,630,470);
rectangle(20*5,10*5,108*5,86*5);
outtextxy(280,25,"GAME START");
outtextxy(30,100,"SPEED:");
if (set_sudu+set_nandu<2) outtextxy(35,120,"VERYEASY");
if (set_sudu+set_nandu<4) outtextxy(35,120,"EASY");
if (set_sudu+set_nandu<6) outtextxy(35,120,"NORMAL");
if (set_sudu+set_nandu<8) outtextxy(35,120,"HARD");
if (set_sudu+set_nandu>=8) outtextxy(35,120,"VERYHARD");
                 /*can add some other text and line*/
}

int ceshi_key(char a)
{ int i=0;
  if (a=='j') i=1;
  if (a=='k') i=2;
  if (a=='l') i=3;
  if (a=='i') i=4;
  if (a=='r') i=6;
  if (a=='e') i=10;
  return i;
}

void chongxin_bisai()
{
}

void chuli_che(int zhongxin[2],int k)
{if (k==0 && zhongxin[1]<=84) zhongxin[1]=zhongxin[1];
if (k==1 && zhongxin[0]>=26) zhongxin[0]=zhongxin[0]-2;
if (k==3 && zhongxin[0]<=102) zhongxin[0]=zhongxin[0]+2;
if (k==2 && zhongxin[1]<=84) zhongxin[1]=zhongxin[1]+2;
if (k==4 && zhongxin[1]>=18) zhongxin[1]=zhongxin[1]-2;
if (k==6) chongxin_bisai();
}

int ceshi_chugui(int zhongxin[2],enum shu aa[128][96])
{int i,j,ming=0;
for (i=0;i<5;i++)
   {for (j=0;j<8;j++)
      {if (aa[zhongxin[0]-4+2*i][zhongxin[1]-j*2]!=daolu) ming++;
      }
   }
return ming;
}
[em1][em1]

回复列表 (共20个回复)

11 楼

这个程序运行的非常好``我好喜欢咯``以后多向你学习呀``

12 楼

2楼的兄弟的错误是你只要按1.2.3.4这几个级别的,不能用其它的字母``哈``

13 楼

郁闷!你们都能玩,我怎么不能玩?我编译通过了,我按楼上说的做了,一按回车就不行

14 楼

我也用win-tc运行的。
不错 ,不错。
刚学编程序,有这些源码,真好。
多谢楼主。

15 楼

kkkk:
printf("\n\n\t\t\tNAME:     MiniCar\n\t\t\tVERSION:  1.4\n\t\t\tCREATOR:  kanker\n\n");
printf("NARRATE:   This function is a race game, be use 'j,k,l' and 'i' to control the car. press 'e' means exiting the game, press 'r' means restart the race\n");
printf("\nTHE GAME SET:   Please input the value of speed and dificulty (0-4),");
printf("for example input '0,0' means speed=0 and dificulty=0,and so on.\n");
printf("\n\n\tGOOD LUCKY!\n\n\tplease input===>");
scanf("%d,%d",&set_sudu,&set_nandu);
if (set_sudu<5 &&set_nandu<5)
   { printf ("\nGOOD! SET OK!\n please waiting...");sleep(1.5);}
else
   { printf("\n\nINPUT ERROR,REINPUT\n");   goto kkkk; }


gdriver =9;    gmode =2;
initgraph(&gdriver,&gmode,"d:\\turboc2");

将这儿的 d:\\turboc2改为 自己的Tc的安装目录就可以了

16 楼

我已经按各位大哥的提示做了,还是不行
gdriver =9;    gmode =2;
initgraph(&gdriver,&gmode,"C:\Win-TC");
怎么会出现这种情况?

17 楼

真强啊

18 楼

好的,有空了改进一下。谢谢大家的意见!

19 楼

我用的是C++;怎么总说我有一个错误啊

20 楼

我的能运行了
但是不是很好玩啊!
支持楼主
收藏了

我来回复

您尚未登录,请登录后再回复。点此登录或注册