c语言小程序游戏
本代码是我大一刚学完c语言自己编写的,三个游戏分别是飞机竞速、走迷宫和魔塔,满满的都是回忆。
1、不可能的任务-飞机竞速
这种游戏在以前小时候玩的游戏机里面玩过,那时抱着个游戏机一玩就是一下午,如今感觉大型3A电脑游戏也不过如此,回首往事时才发现,那段无忧无虑的时光真是太奢侈了。
#include#include#includeint i,j,k,a,b,c,d=0,x=15,y=4,m=1;char ch,n[21][16]={ "###############","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","# #","###############"};void line(int z){for(i=0;i<z;i++)printf("\n");}void space(int z){for(j=0;j<z;j++)printf("\t");}void head(){space(1);printf("不可能完成的任务--飞行游戏你需要通过‘a’,‘d’两个键来控制战机不被敌方飞机坠毁\n");space(4);printf("你已躲过敌军%d战机的进攻\n",d);for(i=0;i<80;i++)printf("_");printf("\n");}void shape1(){n[x][y]='*'; n[x-1][y]='*';n[x][y-1]='*';n[x][y+1]='*';n[x+1][y]='*';n[x+2][y-1]='*';n[x+2][y+1]='*';}void shape2(){n[x][y]=' ';n[x-1][y]=' ';n[x][y-1]=' ';n[x][y+1]=' ';n[x+1][y]=' ';n[x+2][y-1]=' ';n[x+2][y+1]=' ';}void shape3(){n[a][b]='*';n[a+1][b]='*';n[a][b-1]='*';n[a][b+1]='*';n[a-1][b]='*';n[a-2][b-1]='*';n[a-2][b+1]='*';}void shape4(){n[a][b]=' ';n[a+1][b]=' ';n[a][b-1]=' ';n[a][b+1]=' ';n[a-1][b]=' ';n[a-2][b-1]=' ';n[a-2][b+1]=' ';}void trawmap(){for(i=0;i<21;i++){space(4);puts(n[i]);}}void move(){if(kbhit()!=0){ch=getch();if(ch=='d'&&n[x][y+4]!='#'){for(k=0;k<6;k++){shape2();y++;shape1();system("cls");head();trawmap();}}if(ch=='a'&&n[x][y-4]!='#'){for(k=0;k<6;k++){shape2();y--;shape1();system("cls");head();trawmap();}}}}void seat(int c,int x){if(c==0)a=x,b=4;if(c==1)a=x,b=10;}int main(){time_t t=time(NULL);srand(t);line(5);space(3);printf("请将输入法调成英文状态!!!");Sleep(3000);system("cls");line(5);space(3);printf("世界末日即将来临,外星人已经做好了攻打地球的准备了\n");Sleep(1000);space(3);printf("地球上最厉害的勇士,准备坐战机攻打敌人老巢\n");Sleep(1000);space(3);printf("然而在太空有许多的敌人,需要绕过他们才能到达敌军基地\n");Sleep(1000);space(3);printf("去吧!勇士,现在是时候来拯救地球啦!!!!!!!!!!\n");Sleep(1000);system("cls");seat(rand()%2,3); shape1();while(d<=50){head();trawmap();move();if(m==3&&d<=30)seat(rand()%2,3);if(m==3&&d>30&&d<=40)seat(rand()%2,7);if(m==3&&d>50)seat(rand()%2,8);m=1;shape4();a++;shape3();if(a==x-2&&b==y){m=2;break;}if(n[a+2][b]=='#'){shape4();m=3; d++;}if(d<=10)Sleep(50);else if(d<20)Sleep(25);else;system("cls");}if(m==2)printf("你方战斗机已被摧毁!!!\n");elseprintf("你已成功躲过敌军五十架战斗机的进攻!\n好样的你拯救了地球\n");while(1);return 0;}
2、走迷宫
一共有三个迷宫,每个迷宫都隐藏着不同种类的机关,快来探索一下吧。
#include#include#include#includeint i,x,y;//x,y为o点的位置坐标char ch,n[27][52];//输入字符在ch中来控制o点的移动,原始空地图void map1()//第一张地图{charm[27][52]={"#o#################################################","# ##### # ########### #","# # ###### ## # ### ######### ##### #","####### ## # ## # ## #### # # # #","# # # # ## # ## # # ##### ##### # ##### #","# # # # ## # ## # # ## ## # # # #","# # # ###### # ########## ########### # ##### # #","# # ## ## # # # # # # #","# # ########## ### ######## # # # ####### # ##### #","# ## ### ## # # # # #","###### ######### # #### # # ########### # #####","## # # #### # # # #","# ## # # #### ###### #### # ######### #### ######","## ## ################# ## # ##","## ######## ######## ## # # ## # # ##","## ## ##### ## ##### # # ## ## # ##","## # #### # ############## # # # ###### ###### # ##","## # #### # ## # ### ##","## ## # # # ############ ##### ########### ##","# ## ## ### # # #### # # # ### ####","# ## ## ## ##### # # ### # # ########","## ######### # # # ####### # ####","## # # ### # # ############ #","## ############### # # ##### # ### ########## #","## # # ## # #","#################################################$#"};printf("the one \n");Sleep(1000);system("cls");for(i=0;i<27;i++)strcpy(n[i],m[i]);//将地图复制到原始空地图里x=0;y=1;//定义o点的原始坐标}void map2()//第二张地图{char m[27][52]={"#o#################################################","# ##### # ########### #","# # ###### ## # ### ######### ##### #","####### ## # ## # ## #### # # # #","# # # # ## # ## # # ##### ##### # ##### #","# # # # ## # ## # # ## ## # # # #","# # # ###### # ########## ########### # ##### # #","# # ## ## # # # # # # #","# # ########## ### ######## # # # ####### # ##### #","# ## ### ## # # # # #","###### ######### # #### # # ########### # #####","## # # #### # # # #","# ## # # #### ###### #### # ######### #### ######","## ## ################# ## # ##","## ######## ######## ## # # ## # # ##","## ## #####+## ##### # # ## ## # ##","## # #### # ############## # # # ###### ###### # ##","## # #### # # ## # ### ##","## ## # # # ############ ##### ########### ##","# ## ## ### # # #### # # # ### ####","# ## ## ## ##### # # ##### ##########","## ######### # # # ####### #-###+ #","## # # ### # # ### ######## #","## ###############-# # ##### # ### ########## #","## # # ## # #","#################################################$#"};printf("the two \n");printf("当你走到‘-’位置时,会瞬间移动到‘+’位置!\n");Sleep(3000);system("cls");for(i=0;i<27;i++)strcpy(n[i],m[i]);x=0;y=1;}void map3()//第三张地图{char m[27][52]={"#o#################################################","# ##### # ########### #","# # ###### ## # ### ######### ##### #","####### #- # # ## # ## #### # # # #","# # # # ## # ## # # ##### ##### # ##### #","# # # # ## # ## # # ## ## # # # #","# # # ###### # ########## ########### # ##### # #","# # ## ## # # # # # # #","# # ########## ### ######## # # # ####### # ##### #","# ## ### ## # # # # #","###### ######### # #### # # ########### # #####","## # # #### # # # #","# ## # # #### ###### #### # ######### #### ######","## ## ################# ## # ##","## ######## ######## ## # # ## # # ##","## ## ##### ## ##### # # ## ## # ##","## # #### # ############## # # # ###### ###### # ##","## # #### # ## # ### ##","## ## # # # ############ ##### ########### ##","# ## ## ### # # #### # # # ### ####","# ## ## ## ##### # # ### # # ########","## ######### # # # ####### # ### +#","## # # ### # # ############ #","## ############### # # ##### # ### ########## #","## # # ## # #","#################################################$#"};printf("the three \n");printf("当你走到‘-’位置时,会瞬间移动到‘+’位置!\n");Sleep(2000);system("cls");for(i=0;i<27;i++)strcpy(n[i],m[i]);x=0;y=1;}void move()//通过'w','s','a','d'来控制o点的上下左右移动{ch=getch();//输入一个字符if(ch=='a'&&n[x][y-1]!='#'){n[x][y]=' ';y--;n[x][y]='o';}if(ch=='d'&&n[x][y+1]!='#'){n[x][y]=' ';y++;n[x][y]='o';}if(ch=='w'&&n[x-1][y]!='#'){n[x][y]=' ';x--;n[x][y]='o';}if(ch=='s'&&n[x+1][y]!='#'){n[x][y]=' ';x++;n[x][y]='o';}}void drawmap()//画出地图{for(i=0;i<27;i++)puts(n[i]);}void flicker(int x1,int y1,int x2,int y2)//瞬间移动{system("cls");n[x1][y1]=' ';x=x2;y=y2;n[x][y]='o';drawmap();}int main()//主函数开始{printf("欢迎来到走迷宫小游戏!\n你需要控制o点走到$位置!\n");Sleep(2000);system("cls");map1();//输出第一张地图while(1){drawmap();move();if(x==25&&y==49)break;system("cls");}printf("you win!\n");Sleep(1000);system("cls");map2();//输出第二张地图while(1){drawmap();move();if(x==23&&y==18)flicker(23,18,15,17);if(x==21&&y==34)//如果o点到达‘-’,这传送到‘+’点flicker(21,34,21,42);if(x==25&&y==49)break;system("cls");}printf("you win!\n");Sleep(1000);system("cls");map3();//输出第三张地图while(1){drawmap();move();if(x==3&&y==11)//如果o点到达‘-’,这传送到‘+’点{system("cls");n[x][y]=' ';x=21;y=49;n[x][y]='o';drawmap();}if(x==25&&y==49)break;system("cls");}printf("you win!\n");return 0;}
3、魔塔
魔塔是一种策略类游戏,需要多动脑筋,任何一个轻率的选择都可能导致游戏的失败。这个游戏是我呕心泣血之作,当年为了写出这款游戏,在上课的时候画了一堆图案,研究怎么才能写出只有一种胜利的方法,最后绞尽脑汁弄了出来。PS:当年学长为了追他女神,托我写了这么个游戏,隐藏彩蛋哦。
#include#include#includeint i,j,x,y,key=0,attack=0,life=5,money=0,floor,time[7]={0};int mk[8][8]={0},ma[8][8]={0},ml[8][8]={0},mA[8][8]={0},mB[8][8]={0},mC[8][8]={0},mD[8][8]={0},mM[8][8]={0};char ch,n[8][8];char n1[8][8],n2[8][8],n3[8][8],n4[8][8],n5[8][8],n6[8][8];void entryspace(int x){for(i=0;i<x;i++)printf("\t");}void entryline(int x){for(i=0;i<x;i++)printf("\n");}void line(){for(i=0;i<40;i++)printf("__");printf("\n");}void map1(){char m[8][8]={"#######","# ###$#","#*%#*K#","#AA#AA#","# K+K #","#o### >","#######"};x=5;y=1;for(i=0;i<8;i++)strcpy(n[i],m[i]);}void map2(){char m[8][8]={ "#######", "# #%# #", "##*+%##", "##* %##", "# #B# #", ">+ o<", "#######" };x=5;y=5;for(i=0;i<8;i++)strcpy(n[i],m[i]);}void map3(){char m[8][8]={ "#######", "#* BB%#", "#A# #%#", "# +%#B#", "# ###B#", "", "#######"};x=5;y=1;for(i=0;i<8;i++)strcpy(n[i],m[i]);}void map4(){char m[8][8]={"#>####","#C#C%##","# +B#*#","#B#+C%#","#B# ###","#%#B%o<","#######"};x=5;y=5;for(i=0;i<8;i++)strcpy(n[i],m[i]);}void map5(){char m[8][8]={"#<#####","#o### #","#D+C+D#","#C### #","#D#+D%#","> #%#*#","#######"};x=1;y=1;for(i=0;i<8;i++)strcpy(n[i],m[i]);}void map6(){char m[8][8]={"#######","##MPM##","##DMD##","#DM+ *#","# D#%*#","<oD#*%#","#######"};x=5;y=1;for(i=0;i<8;i++)strcpy(n[i],m[i]);}void head(){entryspace(5);printf("刘怀兵制作\n");line();printf("你需要通过‘w’,‘s’,‘a’,‘d’四个键开控制‘o’点\n");printf("K:钥匙\t+:关着的门\t&:开着的门\t*:生命值+5\t%%:攻击力+1\t>:向上的楼梯\n");printf("A:小怪兽(攻:5,金:1)B:中级怪兽(攻:10,金:2)C:大怪兽(攻:15,金:4)\n");printf("D:巨大怪兽 (攻:20,金:6) M:究极怪兽 (攻:40,金:5)$:商店 \n");line();printf("生存法则:当你攻击怪兽时,生命会减去你的攻击力减去怪兽的攻击力,生命为零即失败!\n");printf("key=%2dattack=%2dlife=%2dmoney=%2dfloor=%2d\n",key,attack,life,money,floor);line();}void trawmap(){entryline(2);for(i=0;i<8;i++){printf("\t\t\t\t\t");puts(n[i]);}}void move(){ch=getch();if(ch=='w'&&n[x-1][y]!='#'){if(n[x-1][y]=='+'){if(key>0){n[x][y]=' ';x--;n[x][y]='o';key--;}}else{n[x][y]=' ';x--;n[x][y]='o';}}if(ch=='s'&&n[x+1][y]!='#'){if(n[x+1][y]=='+'){if(key>0){n[x][y]=' ';x++;n[x][y]='o';key--;}}else{n[x][y]=' ';x++;n[x][y]='o';}}if(ch=='a'&&n[x][y-1]!='#'){if(n[x][y-1]=='+'){if(key>0){n[x][y]=' ';y--;n[x][y]='o';key--;}}else{n[x][y]=' ';y--;n[x][y]='o';}}if(ch=='d'&&n[x][y+1]!='#'){if(n[x][y+1]=='+'){if(key>0){n[x][y]=' ';y++;n[x][y]='o';key--;}}else{n[x][y]=' ';y++;n[x][y]='o';}}}void Key(int n,int k){if(x==n&&y==k&&mk[n][k]==0){mk[n][k]=1;key++;}}void Attack(int n,int k){if(x==n&&y==k&&ma[n][k]==0){ma[n][k]=1;attack++;}}void Life(int n, int k){if(x==n&&y==k&&ml[n][k]==0){ml[n][k]=1;life+=5;}}void A(int n,int k){if(x==n&&y==k&&mA[n][k]==0){mA[n][k]=1;if(attack<5)life-=5-attack;money++;}}void B(int n,int k){if(x==n&&y==k&&mB[n][k]==0){mB[n][k]=1;if(attack<10)life-=10-attack;money+=2;}}void C(int n,int k){if(x==n&&y==k&&mC[n][k]==0){mC[n][k]=1;if(attack<15) life-=15-attack;money+=4;}}void E(int n,int k){if(x==n&&y==k&&mD[n][k]==0){mD[n][k]=1;if(attack<20) life-=20-attack;money+=6;}}void M(int n,int k){if(x==n&&y==k&&mM[n][k]==0){mM[n][k]=1;if(attack<40) life-=40-attack;money+=5;}}void Door(int m,int k){if(n[m][k]==' ')n[m][k]='&';}void Store(int m,int k){if(x==m&&y==k){printf("欢迎来到商店\n按1 K$3\n按2 *$4\n按3 %%$5");ch=getch();if(ch=='1'){money-=3;if(money<0){printf("钱不够了");money+=3;key--;}key++;}if(ch=='2'){money-=4;if(money<0){printf("钱不够了");money+=4;life-=5;}life+=5;}if(ch=='3'){money-=5;if(money<0){printf("钱不够了");money+=5;attack--;}attack++;}}}void U(int k,int m){if(n[k][m]==' ')n[k][m]='>';}void D(int k,int m){if(n[k][m]==' ')n[k][m]='<';}void love(){char m[8][33]={"################################","# ### ## ## ## ### # # #","### # # # # # ## # # # #","### # # # ## ### # #","## # #### # #### #","# ### # ## # ### #","################################"};for(i=0;i<8;i++){puts(m[i]);}}int main(){printf("开始前请将输入法调成英文状态");entryline(5);printf("王子和公主在森林玩耍,突然大魔王来了,抓走了公主!!!");Sleep(2000);system("cls");entryline(5);printf("王子为了救公主,去了魔王所在的魔塔里,并决心与他一决生死!");Sleep(2000);system("cls");entryline(5);printf("现在你就是那位王子,已经在魔塔里了,去吧,动用你的智慧救出公主吧!");Sleep(2000);system("cls");floor=1;while(1){if(floor==1){head();if(time[1]==0){map1();}else{for(i=0;i<8;i++)strcpy(n[i],n1[i]);}while(1){trawmap();move();Key(4,2);Key(4,4);Key(2,5);Attack(2,2);Life(2,1);Life(2,4);A(3,1);A(3,2);A(3,4);A(3,5);Door(4,3);Store(1,5);if(life<=0){floor=0;printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");printf("关闭后重新调试即可\n");Sleep(3000);break;}if(n[1][5]==' ')n[1][5]='$';system("cls");U(5,6);if(x==5&&y==6){floor=2;n[5][6]='>';n[5][5]='o';x=5;y=5;for(i=0;i<8;i++)strcpy(n1[i],n[i]);time[1]=1;break;}head();}}if(floor==2){head();if(time[2]==0){map2();}else{ for(i=0;i<8;i++)strcpy(n[i],n2[i]);}while(1){trawmap();move();Life(2,2);Life(3,2);Attack(2,4);Attack(3,4);Attack(1,3);B(4,3);Door(5,3);Door(2,3);if(life<=0){floor=0;printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");printf("关闭后重新调试即可\n");Sleep(3000);break;}system("cls");U(5,0);D(5,6);if(x==5&&y==6){floor=1;n[5][6]='<';n[5][5]='o';x=5;y=5;for(i=0;i<8;i++)strcpy(n2[i],n[i]);time[2]=1;break;}if(x==5&&y==0){floor=3;n[5][0]='>';n[5][1]='o';x=5;y=1;for(i=0;i<8;i++)strcpy(n2[i],n[i]);time[2]=1;break;}head();}}if(floor==3){head();if(time[3]==0){map3();}else{for(i=0;i<8;i++)strcpy(n[i],n3[i]);}while(1){trawmap();move();Key(5,2);Attack(3,3);Attack(1,5);Attack(2,5);A(5,3);A(5,4);A(2,1);B(1,3);B(1,4);B(3,5);B(4,5);Life(1,1);Door(3,2);if(life<=0){floor=0;printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");printf("关闭后重新调试即可\n");Sleep(3000);break;}system("cls");U(5,6);D(5,0);if(x==5&&y==0){floor=2;n[5][0]='<';n[5][1]='o';x=5;y=1;for(i=0;i<8;i++)strcpy(n3[i],n[i]);time[3]=1;break;}if(x==5&&y==6){floor=4;n[5][6]='>';n[5][5]='o';x=5;y=5;for(i=0;i<8;i++)strcpy(n3[i],n[i]);time[3]=1;break;}head();}}if(floor==4){head();if(time[4]==0){map4();}else{for(i=0;i<8;i++)strcpy(n[i],n4[i]);}while(1){trawmap();move();Attack(5,4);Attack(1,4);Attack(3,5);Attack(5,1);B(5,3);B(2,3);B(3,1);B(4,1);C(1,1);C(1,3);C(3,4);Life(2,5);Door(2,2);Door(3,3);if(life<=0){floor=0;printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");printf("关闭后重新调试即可\n");Sleep(3000);break;}system("cls");U(0,1);D(5,6);if(x==5&&y==6){floor=3;n[5][6]='<';n[5][5]='o';x=5;y=5;for(i=0;i<8;i++)strcpy(n4[i],n[i]);time[4]=1;break;}if(x==0&&y==1){floor=5;n[0][1]='>';n[1][1]='o';x=1;y=1;for(i=0;i<8;i++)strcpy(n4[i],n[i]);time[4]=1;break;}head();}}if(floor==5){head();if(time[5]==0){map5();}else{for(i=0;i<8;i++)strcpy(n[i],n5[i]);}while(1){trawmap();move();Attack(5,3);Attack(4,5);Life(5,5);C(3,1);C(2,3);E(2,1);E(4,1);E(2,5);E(4,4);Door(2,2);Door(2,4);Door(4,3);if(life<=0){floor=0;printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");printf("关闭后重新调试即可\n");Sleep(3000);break;}system("cls");D(0,1);U(5,0);if(x==0&&y==1){floor=4;n[0][1]='<';n[1][1]='o';x=1;y=1;for(i=0;i<8;i++)strcpy(n5[i],n[i]);time[5]=1;break;}if(x==5&&y==0){floor=6;n[5][0]='<';n[5][1]='o';x=5;y=1;for(i=0;i<8;i++)strcpy(n5[i],n[i]);time[5]=1;break;}head();}}if(floor==6){head();if(time[6]==0){map6();}else{for(i=0;i<8;i++)strcpy(n[i],n6[i]);}while(1){trawmap();move();Attack(4,4);Attack(5,5);Life(3,5);Life(4,5);Life(5,4);M(1,2);M(1,4);M(3,2);M(2,3);Door(3,3);E(3,1);E(4,2);E(5,2);E(2,2);E(2,4);if(n[1][3]==' ')n[1][3]='P';if(n[1][2]!='M'&&n[1][4]!='M'&&n[2][3]!='M'){printf("我亲爱的公主,我终于找到你了!");love();Sleep(3000);floor=7;break;}if(life<=0){floor=0;printf("你被怪兽击败了!别气馁,再来一次,公主还在等着你救他呢!\n");printf("关闭后重新调试即可\n");Sleep(3000);break;}system("cls");D(5,0);if(x==5&&y==0){floor=5;n[5][0]='<';n[5][1]='o';x=5;y=1;for(i=0;i<8;i++)strcpy(n6[i],n[i]);time[6]=1;break;}head();}}if(floor==0||floor==7)break;}return 0;}