(建议用WIN-TC编译,会自动加载图形驱动,TC2就要处理了,用VC6不行,有些图形函数不兼容) 我刚较大的改写完善了书上《C语言游戏编程》的一个小C游戏,原来的赛车只能向左或向右,不能前进和后退,而且过路车只有一辆卡车,只有一种颜色,很单调,我增加了赛车的前进和后退,又加了一辆小货车,并加了选择游戏难度的菜单和里程显示,还使过路车的色彩五彩缤纷, 并且,如果赛车从左右两侧撞上过路车,也能反映失败.相比之下,原来的撞车就很单一了,.. 希望大家有时间玩一玩,,,如果美工好的,也可自己改成逼真的车型... 另,也准备好了能独立运行的可执行程序,双击就可玩的...(哎呀! 不能上传吗?) 原代码: #include <math.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <dos.h> #include <bios.h> #include <string.h> #include <ctype.h>
#define BORDER 1 #define ESC 27 #define REV_VID 0x1e #define NORM_VID 0x40
void light(void); void chineseputs(int x, int y, char *p, int attrib); void display_menu(char *menu[], int x, int y, int count); void disp_box(int x1, int y1, int x2, int y2, int attrib); int get_resp(int x, int y, int count, char *menu[], char *keys); int popup(char *menu[], char *keys, int count, int x, int y, int border); void goto_xy(int x, int y); int coustomize(); main() { int gdriver,gmode,c,d,e,b,i,l,j,k,u,w,course,s,h,bcolor,scolor,pcolor,ccolor,time; int *p[]={400,40,310,200,610,140}; int poly[14]; char speed; char *main_menu[]={".h...(high)",".a...(average)",".l...(low)",".c...(coustomize)"};
c=1;d=0;u=0; gdriver=DETECT; registerbgidriver(EGAVGA_driver); initgraph(&gdriver,&gmode,""); setwritemode(XOR_PUT);
setfillstyle(HATCH_FILL,4); drawpoly(4,p); line(380,50,600,160); setcolor(14); setbkcolor(0); settextstyle(GOTHIC_FONT,HORIZ_DIR,6); outtextxy(350,70,"JUST START MY COURSE !!"); setcolor(5); settextstyle(TRIPLEX_FONT,VERT_DIR,8); outtextxy(550,120,"Our goal is just waiting for your hands !"); setcolor(8); settextstyle(TRIPLEX_FONT,HORIZ_DIR,3); outtextxy(80,400,"Written by Dai ...");
getch(); closegraph();
speed=popup(main_menu, "halc", 4, 5, 16, BORDER); if(speed==0) time=400; if(speed==1) time=1000; if(speed==2) time=1500; if(speed==3) time=coustomize(); if(speed==5) exit (0);
initgraph(&gdriver,&gmode,""); setwritemode(XOR_PUT); setcolor(YELLOW); outtextxy(250,350,"Are you ready?"); while(kbhit()==0); /* getch() will eat the previous ENTER */
cleardevice(); light();
setcolor(15); for(h=0;h<4;h++) line(150+h*100,0,150+h*100,472);
for(h=0;h<3;h++) { setlinestyle(3,0,1); line(200+h*100,0,200+h*100,472); settextstyle(1,HORIZ_DIR,3); } settextstyle(1,HORIZ_DIR,4); setcolor(RED); outtextxy(40,40,"Just do it !"); outtextxy(40,80,"Yeah !!");
setcolor(10); setlinestyle(SOLID_LINE,0,3); rectangle(150,400,450,460); outtextxy(160,410,"Ready,Begin!");
setcolor(BLUE); rectangle(180+c*100,400,220+c*100,440); rectangle(170+c*100,390,230+c*100,400); rectangle(190+c*100,370,210+c*100,390); rectangle(170+c*100,440,230+c*100,450); setcolor(5); line(190+c*100,400,190+c*100,440); line(200+c*100,400,200+c*100,440); line(210+c*100,400,210+c*100,440); line(200+c*100,350,200+c*100,370); line(185+c*100,350,215+c*100,350);
course=0; w=-50; loop: bcolor=rand()%15,scolor=rand()%15,pcolor=rand()%15,ccolor=rand()%15,i=rand()%3,l=rand()%3; if(w>=800) w=-50; for(j=-20;j<=90;j++,w++) { setcolor(bcolor+1); rectangle(170+i*100,j*10,230+i*100,60+j*10); rectangle(160+i*100,70+j*10,240+i*100,260+j*10); line(180+i*100,70+j*10,180+i*100,260+j*10); line(200+i*100,70+j*10,200+i*100,260+j*10); line(220+i*100,70+j*10,220+i*100,260+j*10);
if(j>=20) { k=j-35; setcolor(scolor+1); rectangle(170+l*100,k*10,230+l*100,60+k*10); } setcolor(10);
line(85,-5+u*15+w,85,55+u*15+w); line(95,-5+u*15+w,95,55+u*15+w); line(105,-5+u*15+w,105,55+u*15+w); line(115,-5+u*15+w,115,55+u*15+w); line(75,11+u*15+w,75,39+u*15+w); line(125,11+u*15+w,125,39+u*15+w);
setcolor(pcolor+1); poly[0] = 530; poly[1] = u*15+j*30;
poly[2] =515; poly[3] = 25+u*15+j*30;
poly[4] =485; poly[5] =25+u*15+j*30 ;
poly[6] =470; poly[7] =u*15+j*30 ;
poly[8] =485; poly[9] =-25+u*15+j*30;
poly[10] =515; poly[11] =-25+u*15+j*30 ;
poly[12] = poly[0]; poly[13] = poly[1];
drawpoly(7,poly);
delay(time);
setcolor(bcolor+1); rectangle(170+i*100,j*10,230+i*100,60+j*10); rectangle(160+i*100,70+j*10,240+i*100,260+j*10); line(180+i*100,70+j*10,180+i*100,260+j*10); line(200+i*100,70+j*10,200+i*100,260+j*10); line(220+i*100,70+j*10,220+i*100,260+j*10); if(j>=20) { setcolor(scolor+1); rectangle(170+l*100,k*10,230+l*100,60+k*10);
}
setcolor(10);
line(85,-5+u*15+w,85,55+u*15+w); line(95,-5+u*15+w,95,55+u*15+w); line(105,-5+u*15+w,105,55+u*15+w); line(115,-5+u*15+w,115,55+u*15+w); line(75,11+u*15+w,75,39+u*15+w); line(125,11+u*15+w,125,39+u*15+w);
setcolor(pcolor+1); poly[0] = 530; poly[1] = u*15+j*30;
poly[2] =515; poly[3] = 25+u*15+j*30;
poly[4] =485; poly[5] =25+u*15+j*30 ;
poly[6] =470; poly[7] =u*15+j*30 ;
poly[8] =485; poly[9] =-25+u*15+j*30;
poly[10] =515; poly[11] =-25+u*15+j*30 ;
poly[12] = poly[0]; poly[13] = poly[1];
drawpoly(7,poly);
sound(j*10); delay(50); nosound();
course++; setcolor(ccolor+1); settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
outtextxy(580,410,"10000");
if(course>=1000) outtextxy(580,50,"1000"); if(course>=1500) outtextxy(580,70,"1500"); if(course>=2000) outtextxy(580,90,"2000"); if(course>=2500) outtextxy(580,110,"2500"); if(course>=3000) outtextxy(580,130,"3000"); if(course>=3500) outtextxy(580,150,"3500"); if(course>=4000) outtextxy(580,170,"4000"); if(course>=4500) outtextxy(580,190,"4500"); if(course>=5000) outtextxy(580,210,"5000"); if(course>=5500) outtextxy(580,230,"5500"); if(course>=6000) outtextxy(580,250,"6000"); if(course>=6500) outtextxy(580,270,"6500"); if(course>=7000) outtextxy(580,290,"7000"); if(course>=7500) outtextxy(580,310,"7500"); if(course>=8000) outtextxy(580,330,"8000"); if(course>=8500) outtextxy(580,350,"8500"); if(course>=9000) outtextxy(580,370,"9000"); if(course>=9500) outtextxy(580,390,"9500"); if(course>=10000) outtextxy(580,410,"10000");
if(course>=10000) {
sound(800); delay(1000); nosound();
closegraph(); window(10,5,60,10); textbackground(RED); textcolor(YELLOW); cprintf("\n\rYou are EXCELLENT !\n\r"); cprintf(" You have WON !\n\r"); cprintf(" The mileage you have covered\n\ris:"); textcolor(BLUE); cprintf("%d",course); won: while(bioskey(1)==0); if(bioskey(0)==0x1c0d) /* or if((char)bioskey(0)==13) */ /*getch(); getch();*/ exit (0); goto won; } if(bioskey(1)!=0) { b=bioskey(0);
setcolor(BLUE); rectangle(180+c*100,400-d*10,220+c*100,440-d*10); rectangle(170+c*100,390-d*10,230+c*100,400-d*10); rectangle(190+c*100,370-d*10,210+c*100,390-d*10); rectangle(170+c*100,440-d*10,230+c*100,450-d*10); setcolor(5); line(190+c*100,400-d*10,190+c*100,440-d*10); line(200+c*100,400-d*10,200+c*100,440-d*10); line(210+c*100,400-d*10,210+c*100,440-d*10); line(200+c*100,350-d*10,200+c*100,370-d*10); line(185+c*100,350-d*10,215+c*100,350-d*10);
if((b==0x4b00)&&(c>0)) c--; else if((b==0x4d00)&&(c<2)) c++;
else if(b==0x4800) d++; else if(b==0x5000) d--;
setcolor(BLUE);
setcolor(BLUE); rectangle(180+c*100,400-d*10,220+c*100,440-d*10); rectangle(170+c*100,390-d*10,230+c*100,400-d*10); rectangle(190+c*100,370-d*10,210+c*100,390-d*10); rectangle(170+c*100,440-d*10,230+c*100,450-d*10); setcolor(5); line(190+c*100,400-d*10,190+c*100,440-d*10); line(200+c*100,400-d*10,200+c*100,440-d*10); line(210+c*100,400-d*10,210+c*100,440-d*10); line(200+c*100,350-d*10,200+c*100,370-d*10); line(185+c*100,350-d*10,215+c*100,350-d*10); }
if(((c==i)&&(j>=9-d&&j<=45-d))||((c==l)&&(k>=29-d&&k<=45-d)))
{
for(i=0;i<2000;i++) { sound(1500); delay(10); nosound(); } closegraph(); window(35,18,75,23); textbackground(WHITE); textcolor(DARKGRAY); cprintf("\n\r You made a car ACCIDENT !\n\r"); cprintf(" You have LOST !\n\r"); cprintf("The mileage you have covered\n\ris:"); textcolor(MAGENTA); cprintf("%d",course); lost: while(bioskey(1)==0); if(bioskey(0)==0x1c0d) /* or if((char)bioskey(0)==13) */ /* getch(); getch(); */ exit (0); goto lost; }
} goto loop; }
void goto_xy(int x, int y) { union REGS regs; regs.h.ah=2; regs.h.dl=y; regs.h.dh=x; regs.h.bh=0; int86(0x10, ®s, ®s); }
void display_menu(char *menu[], int x, int y, int count) { register int i; for(i=0;i<count;i++,x++) { goto_xy(x, y); cprintf(menu[i]); } }
void disp_box(int x1, int y1, int x2, int y2, int attrib) { unsigned char *boxchar[6]={"H","H","H","H","H","H"}; register int i; for(i=x1;i<=x2;i++) { chineseputs(i, y1, boxchar[1],attrib); chineseputs(i, y2, boxchar[1],attrib); } for(i=y1;i<=y2;i++) { chineseputs(x1, i, boxchar[3],attrib); chineseputs(x2, i, boxchar[3],attrib); } chineseputs(x1, y1, boxchar[0],attrib); chineseputs(x2, y1, boxchar[2],attrib); chineseputs(x2, y2, boxchar[5],attrib); chineseputs(x1, y2, boxchar[4],attrib); }
void chineseputs(int x, int y, char *p, int attrib) { union REGS r; while(*p) { r.h.ah=2; r.h.dl=x++; r.h.dh=y; int86(0x10, &r, &r); r.h.ah=9; r.h.bh=0; r.x.cx=1; r.h.al=*p++; r.h.bl=attrib; int86(0x10, &r, &r); } }
int get_resp(int x, int y, int count, char *menu[], char *keys) { union inkey { char ch[2]; int i; }c; int arrow_choice=0, key_choice; y++; goto_xy(x, y); chineseputs(y, x, menu[0], REV_VID); for(;;) { while(!bioskey(1)); c.i=bioskey(0); goto_xy(x+arrow_choice, y); chineseputs(y, x+arrow_choice, menu[arrow_choice], NORM_VID); if(c.ch[0]) { key_choice=is_in(keys, tolower(c.ch[0])); if(key_choice) return key_choice-1; switch(c.ch[0]) { case '\r': return arrow_choice; case ' ': arrow_choice++; break; case ESC: return 5; } }
else { switch(c.ch[1]) { case 72: arrow_choice--; break; case 80: arrow_choice++; break; } } if(arrow_choice==count) arrow_choice=0; if(arrow_choice<0) arrow_choice=count-1; goto_xy(x+arrow_choice, y); chineseputs(y, x+arrow_choice, menu[arrow_choice], REV_VID); } }
int is_in(char *s, char c) { register int i; for(i=0;*s;i++) if(*s++==c) return i+1; return 0; }
int popup(char *menu[], char *keys, int count, int x, int y, int border) { register int i, len; int endx, endy, choice; unsigned *p; if((x>24)||(x<0)||(y>79)||(y<0)) { printf("range error"); return -2; } len=10; for(i=0;i<count;i++) if(strlen(menu[i])>len) len=strlen(menu[i]); endy=len+1+y; endx=count+1+x; if((endx+1>24)||(endy+1>79)) { printf("menu dont'fit"); return -2; } /* p=(unsigned int*)malloc((endx-x+1) *(endy-y+1)); if(!p) exit(1); save_video(x, endx+1, y, endy+1, p); */ if(border) disp_box(y, x, endy, endx, YELLOW); display_menu(menu, x+1, y+1, count); choice=get_resp(x+1, y, count, menu, keys); /* restore_video(x, endx+1, y, endy+2, (unsigned char *)p); free (p); */ return choice; }
int coustomize() { int delaytime; delaytime=0; window(36,10,76,20); textcolor(GREEN); textbackground(BLUE); clrscr(); cprintf("\n Please input the delaytime (in milli-\n\r second):\n\r The greater the delaytime is,the slower the game is. It depends on your PC CPU frequency as well as your own preferenc es.The suggested num is:\n\n\r"); textcolor(RED); cprintf(" *** from 100 to 10000 ***\n\r"); textcolor(GREEN); cprintf(" input here:"); cscanf("%d",&delaytime); return delaytime; }
void light(void) { rectangle(535,65,565,155); /* floodfill(545,100,BLUE); */ setfillstyle(1,RED); fillellipse(550,80,10,10);
sound(650); delay(300); nosound();
delay(100); setfillstyle(1,YELLOW); fillellipse(550,110,10,10); sound(650); delay(300); nosound(); delay(100); setfillstyle(1,GREEN); fillellipse(550,140,10,10); sound(650); delay(300); nosound(); delay(100); setcolor(10); setlinestyle(SOLID_LINE,0,3); rectangle(150,400,450,460); outtextxy(160,410,"Ready,Begin!"); }
[此贴子已经被作者于2005-6-28 17:36:03编辑过]