| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1960 人关注过本帖
标题:全面改写小C游戏(已较完整),欢迎试玩玩儿
只看楼主 加入收藏
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
 问题点数:0 回复次数:19 
全面改写小C游戏(已较完整),欢迎试玩玩儿

(建议用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, &regs, &regs); }

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编辑过]

搜索更多相关主题的帖子: 游戏 欢迎 
2005-01-15 01:12
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
得分:0 
小说明

小说明

一, 玩法: 1,一般玩法:

双击car文件,就可运行了。

我电脑的CPU是733Mhz,是在这个频率下用C语言编写的(有兴趣可参看“程序源代码“文件),由于不同电脑的CPU的频率高低不同(较新的电脑的CPU在2000Mhz左右,就是2Ghz),引起游戏运行速度不同,不过可以通过选择最后一项"自定义方式"运行:(本游戏的快慢主要通过delaytime(延迟时间)决定的,延迟时间越大,游戏就越慢,反之,就越快。建议的基本范围:100 到 10000 。不过,如果CPU过高或过低的话, 也可从 0 到 更高。。。当然也可根据个人的喜好)

选择游戏速度的窗口如下:

h....(high) 高速 a....(average) 平均 l....(low) 低速 c....(coustomize) 自定义速度

可直接按下相应的字母确定,也可先用上下箭头键(或间隔键)选择,再按下回车键确定。 其中,自定义方式可另弹出窗口提示说明(内容就是上面提到的) 在光标处输入从100 到10000 之间的数字,按下回车键确定。(如按下其他键 可能死机,不过不要紧,可同时按ALT和回车键回到小窗口,再关闭。 以后再完善此处的程序。。)

2,另类玩法:(可能有点不协调) 如果游戏速度过快,除了上面提到的自定义方式外,也可用一个程序来使在运行这个游戏的范围里给CPU将速(由于程序是在系统的虚拟DOS窗口运行的,程序运行时跟系统真实的硬件包括CPU等等是毫无关系的。这里的给CPU降速只是在虚拟环境中的,因此不会对真实的CPU作任何改变,更谈不上损伤了。所以请放心。),程序名叫slowdown.

为了运行方便,我编写了运行程序的批处理文件play, 可根据情况,自由设定运行程序的CPU频率。

具体方法: 1),右键play程序名,编辑,slowdown /p:30 处的30是百分比数,表示以30%的CPU频率运行程序,你可自由设定,当然不能超过100 !

2),保存后,双击即可运行。

没结束时要退出,可同时按ALT和回车键回到小窗口,再关闭。 (以后,再完善程序,使得在游戏中按下键关闭。)

二, 怎样赢?

小赛车虽然可以上下左右移动,但只是象征性的,本游戏只是小习作,不具有三维立体仿真功能,只是二维图形游戏。所以,当小赛车移动时,游戏画面不会跟着延伸。游戏的通关要求仅仅是:不撞车,行车里程达到10000公里。所以,当你移动小赛车时(尤其是向前(也就是向上)),你要高度集中,很可能马上就有过路车撞上你了! 哈哈。。

三, 界面语言

由于显示中文教烦琐,要好多文件配合才能运行中文字库,所以选择简洁的英语说明,只要一个程序就可以运行了。

四, 程序的遗憾

本程序只有电脑自带的小扬声器发出很小的声音,由于是DOS程序,要配上声卡上的逼真的音效,难度较大,不是每个制作程序的编译器都提供音频播放函数。目前正在想办法。。。

五,联系方式

有建议可EMAIL: tdaim@sina.com 另,欢迎访问我的个人小主页(草版): http://mykool.icpcn.com

[此贴子已经被作者于2005-6-28 17:32:29编辑过]


有缘相识!我中文系的 3月拿下C/C++/VC 5天写游戏 2月写游戏系统 有空去http://mykool.go2.
2005-01-16 02:29
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
不能用VC 开发吗?

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-19 20:57
lyw505
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2004-12-2
收藏
得分:0 
2005-02-23 14:35
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
得分:0 
用VC?
这种小玩意儿 还用VC?
杀鸡焉用宰牛刀呀!
嘿嘿...

有缘相识!我中文系的 3月拿下C/C++/VC 5天写游戏 2月写游戏系统 有空去http://mykool.go2.
2005-03-05 01:53
thekey008
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2005-3-16
收藏
得分:0 
你们是好啊.我C还不懂多少呢!

到处看看!
2005-03-20 21:04
BlueDreame
Rank: 1
等 级:新手上路
帖 子:545
专家分:2
注 册:2004-12-16
收藏
得分:0 
回去研究一下。

2005-03-21 10:40
叶枫706
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-11
收藏
得分:0 
我怎么在vc下编译出错呢,你在哪个编译器下编译的啊?
2005-06-11 21:16
叶枫706
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-11
收藏
得分:0 
tc下的程序搬到vc下编译,行不?改哪些东西?
2005-06-11 21:43
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
得分:0 
可能不行,有些图形函数不兼容. 用WIN-TC挺方便的!

有缘相识!我中文系的 3月拿下C/C++/VC 5天写游戏 2月写游戏系统 有空去http://mykool.go2.
2005-07-01 22:20
快速回复:全面改写小C游戏(已较完整),欢迎试玩玩儿
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018292 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved