c语言小游戏编程
C语言实现简单扫雷小游戏
C语⾔实现简单扫雷⼩游戏本⽂实例为⼤家分享了C语⾔实现扫雷⼩游戏的具体代码,供⼤家参考,具体内容如下#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <windows.h>#include <time.h>/*⽤ C 语⾔写⼀个简单的扫雷游戏*/// 1.写⼀个游戏菜单 Menu()// 2.开始游戏// 1.初始化⼆维数组 Init_Interface()// 2.打印游戏界⾯ Print_Interface()// 3.玩家掀起指定位置 Play() --> 指定输⼊坐标(判断合法性)// 1.判断该位置是否是雷// 2.判断是否掀掉所有空地// 3.如果掀开的是空地,则判断该空地周围是否有雷// 1.如果周围有雷,则统计周围雷的个数// 2.如果周围没有雷,则掀开周围除了雷的所有空地,并且统计所掀开空地周围雷的个数// 4.更新地图// 5.继续 3 的循环//定义全局变量://定义扫雷地图的长和宽#define MAX_ROW 9#define MAX_COL 9//定义默认的雷数#define DEFAULT_MINE 9//定义两个⼆维数组,分别存放初始地图和雷阵char show_map[MAX_ROW + 2][MAX_COL + 2];char mine_map[MAX_ROW + 2][MAX_COL + 2];//写⼀个游戏菜单int Menu() {printf("=========\n");printf("1.开始游戏\n");printf("0.结束游戏\n");printf("=========\n");printf("请选择游戏菜单选项:");int choice = 0;while (1) {scanf("%d", &choice);if (choice != 0 && choice != 1) {printf("您的输⼊有误, 请重新输⼊\n");continue;}break;}return choice;}//开始游戏//初始化数组void Init_Interface() {for (int row = 0; row < MAX_ROW + 2; row++) {for (int col = 0; col < MAX_COL + 2; col++) {show_map[row][col] = '*';}}for (int row = 0; row < MAX_ROW + 2; row++) {for (int col = 0; col < MAX_COL + 2; col++) {mine_map[row][col] = '0';}}int mine_count = DEFAULT_MINE;while (mine_count > 0) {int row = rand() % MAX_ROW + 1;int col = rand() % MAX_COL + 1;if (mine_map[row][col] == '1') { //将雷设置为 1//此处已经有雷continue;}mine_count--;mine_map[row][col] = '1';}}//打印初始界⾯void Print_Interface(char map[MAX_ROW + 2][MAX_COL + 2]) {printf(" ");for (int col = 1; col <= MAX_COL; col++) {printf("%d ", col);}printf("\n ");for (int col = 1; col <= MAX_COL; col++) {printf("--");}printf("\n");for (int row = 1; row <= MAX_ROW ; row++) {printf("%02d |", row);for (int col = 1; col <= MAX_COL; col++) {printf("%c ", map[row][col]);}printf("\n");}}//写⼀个统计周围雷数个数的函数int Around_Mine_count(int row, int col) {return (mine_map[row - 1][col - 1] - '0'+ mine_map[row - 1][col] - '0'+ mine_map[row - 1][col + 1] - '0'+ mine_map[row][col - 1] - '0'+ mine_map[row][col + 1] - '0'+ mine_map[row + 1][col - 1] - '0'+ mine_map[row + 1][col] - '0'+ mine_map[row + 1][col + 1] - '0');}//写⼀个判断该位置周围是否有雷的函数int No_Mine(int row, int col) {if (Around_Mine_count(row, col) == 0) {return 1;}return 0;}//写⼀个掀开该位置周围空地的函数void Open_Blank(int row, int col) {show_map[row - 1][col - 1] = '0' + Around_Mine_count(row - 1, col - 1); show_map[row - 1][col] = '0' + Around_Mine_count(row - 1, col);show_map[row - 1][col + 1] = '0' + Around_Mine_count(row - 1, col + 1); show_map[row][col - 1] = '0' + Around_Mine_count(row, col - 1);show_map[row][col + 1] = '0' + Around_Mine_count(row, col + 1);show_map[row + 1][col - 1] = '0' + Around_Mine_count(row + 1, col - 1); show_map[row + 1][col] = '0' + Around_Mine_count(row + 1, col);show_map[row + 1][col + 1] = '0' + Around_Mine_count(row + 1, col + 1); }//写⼀个判断游戏结束的函数int Success_Sweep(char show_map[MAX_ROW + 2][MAX_COL + 2]) { int count = 0;for (int row = 1; row <= MAX_ROW; row++) {for (int col = 1; col <= MAX_COL; col++) {if (show_map[row][col] == '*') {count++;}}}if (count == DEFAULT_MINE) {return 1;}return 0;}//开始游戏void StartGame() {while (1) {printf("请输⼊您要掀开的坐标:");int row = 0;int col = 0;while (1) {scanf("%d %d", &row, &col);if (row < 1 || row > MAX_ROW || col < 1 || col > MAX_COL) {printf("您的输⼊有误,请重新输⼊!\n");continue;}if (show_map[row][col] != '*') {printf("该位置已被掀开,请重新选择\n");continue;}break;}//判断该地⽅是否有雷if (mine_map[row][col] == '1') {Print_Interface(mine_map);printf("该地⽅有雷,游戏结束\n");break;}if (No_Mine(row, col)) {show_map[row][col] = '0';Open_Blank(row, col);}show_map[row][col] = '0' + Around_Mine_count(row, col);//判断是否掀开所有空地if (Success_Sweep(show_map) == 1) {Print_Interface(mine_map);printf("您已成功扫雷\n");break;}system("cls");//更新地图Print_Interface(show_map);}}int main() {if (Menu() == 0) {exit(0);}srand((unsigned int)time(NULL));Init_Interface();Print_Interface(show_map);StartGame();system("pause");return 0;}效果图:数字代表周围雷的个数以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
C语言简易骰子游戏代码
printf("\n\n您的积分为:%d 电脑的积分为:%d\n",f,g);
}
}
}
else
printf("\n对不起!\n您下注的分数不能超过您的积分!\n请重新下注!\n");
if(f<=10)
{
if(b==0)
break;
switch(b)
{
case 1 :printf("\n我买大\n");break;
case 2 :printf("\n我买小\n");break;
case 3 :printf("\n我买豹子\n");break;
default :printf("\n请选择0~3之间的数\n");
}
if(b>0&&b<=3)
{
srand((unsigned)time(NULL));
printf("\n电脑出:");
for(i=0;i<3;i++)
{
t[i]=rand()%6+1;
printf("\n\n您的积分为:%d 电脑的积分为:%d\n",f,g);
}
}
if(b==1&&c<10||b==2&&c>=10||b==3&&(t[0]!=t[1]||t[0]!=t[2]))
{
e++;
printf("\n您总共赢了%d局!输了%d局!",d,e);
c语言课程设计 综合型小游戏
#include<stdio.h>#include<stdlib.h>#include<time.h>int money1=10000,money2=10000,money=10000;int main(){void game1(int put);void game2(int put);int put,game,i;printf("单人模式请输入1,双人模式请输入2.\n");scanf("%d",&put);if(put==1)printf("你的本钱有一万元,你的任务是翻一倍,达到两万元则游戏胜利\n");if(put==2)printf("最后金钱多者为胜者\n");system("pause");system("cls");for(i=0;i<=1000;i++){printf("请选择游戏:1.思维风暴2.猜数字3.退出\n");scanf("%d",&game);if(game==1){game1(put);}if(game==2){game2(put);}if(game==3){break;}}if(put==1){if(money>=20000)printf("恭喜你通关了\n");if(money>=10000&&money<20000)printf("很遗憾未能通关,不过至少没亏本了\n");}if(put==2){if(money1>money2)printf("恭喜玩家一,你实在太强势了\n");if(money1<money2)printf("恭喜玩家二,简直是虐菜啊\n");if(money1==money2)printf("二位简直势均力敌啊,真是好基友\n");}system("pause");}void game1(int put){int JudgeA(int a[4],int b[4]),JudgeB(int a[4],int b[4]);int a[4],b[4];int c,i,j,m,n,k,l,under,under1,under2;printf("游戏规则:系统将随机产生一个四位不重复数字,你输入猜想的数字后\n");printf("系统将判断你猜对的数字个数和正确位置数,系统将以-A-B的形式提示,其中A 前面的数字表示位置正确的数的个数");printf("而B前的数字表示数字正确而位置不对的数的个数,如正确答案为5234,而猜的人猜5346,则是1A2B.\n **记住你只有八次机会**\n");system("pause");system("cls");if(put==1){for(l=0;l<100;l++){printf("请压底,最高为五千\n");for(m=0;m<=20;m++){scanf("%d",&under);if(under>5000||under<=0){printf("超过上限,请重新输入\n");continue;}elsebreak;}printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;//产生首位随机数,对10取模得0~9的数字}while(a[0]==0);//若首位为零则重新选择for(i = 1;i < 4; i++){do{a[i]=rand()%10;//产生其它几位随机数for(j = 0; j < i; j++){if(a[i]==a[j])//若与前几位相同则跳出,重置a[i]{k=0;break;}elsek=1;//若不同,则该位有效,置标记k为1}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money=money-under*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money=money+under*2;break;}elsecontinue;}printf("your money:%d\n重玩请输入1,返回请输入2\n",money); scanf("%d",&c);if(c==1)continue;if(c==2)break;}}if(put==2){printf("请play1压底,最高为五千\n");scanf("%d",&under1);printf("请play2压底\n");scanf("%d",&under2);for(m=0;m<=10;m++){if(under1>5000||under2>5000){printf("超过上限,请重新输入\n");continue;}elsebreak;}printf("play1's turn\n");printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;}while(a[0]==0);for(i = 1;i < 4; i++){do{a[i]=rand()%10;for(j = 0; j < i; j++){if(a[i]==a[j]){k=0;break;}elsek=1;}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money1=money1-under1*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money1=money1+under1*2;break;}elsecontinue;}printf("play1's money:%d\n",money1);system("pause");printf("play2's turn\n");printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;}while(a[0]==0);for(i = 1;i < 4; i++){do{a[i]=rand()%10;for(j = 0; j < i; j++){if(a[i]==a[j]){k=0;break;}elsek=1;}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money2=money2-under2*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money2=money2+under2*2;break;}elsecontinue;}printf("play2's money:%d\n",money2);}}int JudgeA(int a[4],int b[4]){int i,result1=0;for(i=0;i<4;i++){if(b[i]==a[i]) result1++;}return result1;}int JudgeB(int a[4],int b[4]){int i,j,result=0;for(i=0;i<4;i++){for(j=0;j<4;j++){if(a[i]==b[j]&&i!=j)result++;}}return result;}void game2(int put){int i,j,k,l,a,num,down,down1,down2,random;int nu[6];int *p;p=nu;system("pause");system("cls");printf("游戏规则:单人模式为猜数,双人模式为比大小\n");if(put==1){for(i=0;i<=100;i++){for(k=0;k<=100;k++){printf("请下注,最高为500\n");scanf("%d",&down);if(down>0&&down<=500)break;else{printf("超过上线,请重新下注\n");continue;}}printf("请输入所猜数\n");for(j=0;j<100;j++){scanf("%d",&num);if(num>0&&num<=6)break;else{printf("错误,请重新输入\n");continue;}}for(l=0;l<100;l++){srand((unsigned)(time(NULL)));random = rand()%6+1;if(random>0&&random<=6)break;}printf("正确数为%d,继续玩请输入1,返回菜单输入2\n",random);if(num==random){printf("***********************YOUWIN************************\n");money=money+down*2;}else{printf("***********************YOULOST***********************\n");money=money-down*2;}printf("你的金钱为%d\n",money);scanf("%d",&a);if(a==1)continue;if(a==2)break;}}if(put==2){for(i=0;i<100;i++){printf("游戏规则:玩家分别得到三次随机数字,总和大者胜利\n");printf("请下注,最高为一千\n");scanf("%d",&down);system("pause");for(j=1;j<=6;j++){if(j%2==1){srand((unsigned)(time(NULL)));p[j-1] = rand()%6+1;printf("玩家一第%d次得数为%d\n",j/2+1,p[j-1]);}else{srand((unsigned)(time(NULL)));p[j-1] = rand()%6+1;printf("玩家二第%d次得数为%d\n",j/2,p[j-1]);}system("pause");}printf("玩家一总得数为%d\n玩家二总得数为%d\n",p[0]+p[2]+p[4],p[1]+p[3]+p[5]);if(p[0]+p[2]+p[4]>p[1]+p[3]+p[5]){printf("玩家一获胜\n");money1=money1+down*2;money2=money2-down*2;}if(p[0]+p[2]+p[4]<p[1]+p[3]+p[5]){printf("玩家二获胜\n");money1=money1-down*2;money2=money2+down*2;}if(p[0]+p[2]+p[4]==p[1]+p[3]+p[5]){printf("恭喜\n");money1=money1+down*2;money2=money2+down*2;}printf("玩家一金钱为%d\n玩家二金钱为%d\n重玩输入1,返回输入2\n",money1,money2);scanf("%d",&a);if(a==1)continue;if(a==2)break;}}}。
c语言小游戏
outtextxy(50,200,"Let's play!!");
sleep(1);
}
/*开机动画*/
kjdh()
{
setbkcolor(1);
cleardevice();
setcolor(13);
}
n++;
}
}
/*目标小旗*/
mbxq(int x,int y)
{
setlinestyle(0,0,1);
setcolor(4);
moveto(x,y);
lineto(x,y-20);
lineto(250,400);
lineto(300,450);
lineto(580,200);
lineto(640,200);
setcolor(3);
rectangle(210,370,240,400);
rectangle(610,170,640,200);
lineto(x+10,y-20);
lineto(x,y-10);
}
/*地图1*/
map1()
{
setlinestyle(0,0,3);
setbkcolor(0);
setcolor(2);
moveto(50,480);
lineto(200,400);
{
x=random(640);
y=random(480);
z=random(16);
putpixel(x,y,z);
}
}
/*画旗*/
C语言编程游戏代码
#include <stdio.h>#include <stdlib.h>#include <dos.h>#include <conio.h>#include <time.h>#define L 1#define LX 15#define L Y 4static struct BLOCK{int x0,y0,x1,y1,x2,y2,x3,y3;int color,next;intb[]={{0,1,1,1,2,1,3,1,4,1},{1,0,1,3,1,2,1,1,4,0},{1,1,2,2,1,2,2,1,1,2},{0,1,1,1,1,0,2,0,2,4}, {0,0,0,1,1,2,1,1,2,3},{0,0,1,0,1,1,2,1,3,8},{1,0,1,1,2,2,2,1,2,5},{0,2,1,2,1,1,2,1,2,6},{0,1,0,2,1,1,1 ,0,3,9},{0,1,1,1,1,2,2,2,3,10},{1,1,1,2,2,1,2,0,3,7},{ 1,0,1,1,1,2,2,2,7,12},{0,1,1,1,2,1,2,0,7,13},{0 ,0,1,2,1,1,1,0,7,14},{0,1,0,2,1,1,2,1,7,11},{0,2,1,2,1,1,1,0,5,16},{0,1,1,1,2,2,2,1,5,17},{1,0,1,1,1, 2,2,0,5,18},{0,0,0,1,1,11,2,1,5,15},{0,1,1,1,1,0,2,1,6,2,0},{0,1,1,2,1,1,1,0,6,21},{0,1,1,2,1,1,2,1,6 ,22},{1,0,1,1,1,2,2,1,6,19}};static int d[10]={33000,3000,1600,1200,900,800,600,400,300,200};int Llevel,Lcurrent,Lnext,Lable,lx,ly,Lsum;unsigned Lpoint;int La[19][10],FLAG,sum;unsigned ldelay;void scrint(),datainit(),dispb(),eraseeb();void throw(),judge(),delayp(),move(0,note(0,show();int Ldrop(),Ljudge(),nextb(),routejudge();}main(){char c;datainit();Label=nextb();Label=Ldrop();while(1){delayp();if(Label!=0){Ljudge();Lable=nextb();}ldelay--;if(ldelay==0){Label=Ldrop();ldelay=d[0];}if(FLAG!=0) break;while(getch()!='\r');goto xy(38,16);cputs("again?");c=getch();while(c!='n'&&c!='N')c lscr();}int nextb(){if(La[(b[Lnext].y0)][(3+b[Lnext].x0)]!=0||La[(b[Lnext].y1)][(3+b[Lnext].x1)]!=0|| La[(b[Lnext].y2)][(3+b[Lnext].x2)]!=0||La[(b[Lnext].y3)][3+b[Lnext].x3)]!=0) {FLAG=L;return (-1);}erase b(0,3,5,Lnext);Lcurrent=Lnext;lx=3;ly=0;Label=0;ldelay=d[0];Lsum ++;Lpoint+=1;Lnext=random(23);dispb(0,3,5,Lnext);text color(7);goto xy(3,14);printf("%#5d",Lsum);goto xy(3,17);printf("%#5d",Lpoint);return(0);}void delayp(){char key;if(kbhit()!=0){key=getch();move(key);if(key=='\\')getch();}}void move(funkey)char funkey;{int tempcode;case 'k';if(lx+b[current].x0>0){if(La[ly+(b[Lcurrent].y0)][lx-1+(b[Lcurrent].x0)]==0&&La[(ly+b[current].y1)][(lx-1+b[curr ent].x1]==0&&La[ly+b[current].y2)][lx-1+b[Lcurrent].x2)]==0&&La[ly+(b[current].y3)][lx-1+(b [Lcurrent].x3)]==0){eraseb(L,lx,lyLcurrent);lx--;dispb(L,lx,ly,Lcurrent);}}break;case 0x20;tempcode=b[Lcurrent].next;if (lx+b[tempcode].x0>=0 && lx+b[tempcode].x3<=9 && ly+b[tempcode].y1<=19 && ly+b[tempcode].y2<=19){if(routejudge()!+-1){if(La+(b[tempcode].y0)][lx+(b[tempcode].x0)]==0 && La[ly+(b[tempcode].y1)][lx+(b[tempcode].x1)]==0 && La[ly+(b[tempcode].y2)][lx+(b[tempcode].x2)]==0 && La[ly+(b[tempcode].y3)][lx+(b[tempcode].x3)]==0)eraseb(L,lx,ly,Lcurrent);Lcurrent=tempcode;dispb(L,lx,ly,Lcurrent);}}break;case 'M';if(lx+b[Lcurrent].x3<9){if(La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 &&La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 && La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 && La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0){eraseb(L,lx,ly,Lcurrent);lx++;disb(L,lx,ly,Lcurrent);}}break;case 'p';throw();break;case 0x1b;clrscr();exit(0);break;default:break;}void throw(){int tempy;tempy=ly;while(ly+b[Lcurrent].y1<19 && ly+b[current].y2<19&&La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 && La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 && La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 && La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0)ly++;ly--;eraseb(L,lx,tempy,Lcurrent);dispb(L,lx,ly,Lcurrent);La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=La[l y+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].c olor;Label=-1;}int routejudge(){int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)if(La[ly+i][lx+j]!=0)return(-1);else return(1);}int Ldrop(){if(ly+b[Lcurrent].y1>=18||ly+b[Lcurrent].y2>=18{La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=3;La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=1;La[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=5;La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].color;return(-1);}if(La(ly+1+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]!=0||La(ly+1+(b[Lcurrent].y1)][lx+(b[Lcur rent].x1)]!=0||La(ly+1+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]!=0||La(ly+1+(b[Lcurrent].y3)][lx+( b[Lcurrent].x3)]!=0){La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[cu rrent].x1)]=0BLa[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurren t].color){return(-1);eraseb(L,lx,ly,Lcurrent);dispb(L,lx,++ly,Lcurrent);return(0);}}int Ljudge(){int i,j,k,lines,f;static int p[5]={0,1,3,6,10};lines=0;for(k=0;k<=3;k++){f=0;if((ly+k)>18)continue;for(i=0;i<10;i++){if(La[ly+k]==0);i>0;i--){f++;break;}if(f==0){movetext(LX,L Y,LX+19,L Y+ly+k-1,LX,L Y+1);for(i=(ly+k);i>0;i--){for(j=0;j<10;j++)La[j]=La[i-1][j];{for(j=0;j<10;j++)La[0][j]=0;lines++;}}}}}Lpoint+=p[lines]*10;return(0);}void scrint(){int i;char lft[20];textbackground(1);clrscr();goto xy(30,9);cputs("enter your name");scanf("%s",lft);goto xy(25,14);scanf("%s",lft);textbackground(0);clrscr();goto xy(17,1);printf("%s",lft);goto xy(5,3);puts("next");goto xy(4,13);cputs("block");goto xy(4,16);cputs("point");for(i=0;i<19;i++){goto xy(LX-2,L Y+1);cputs("** **");}goto xy(LX-2,L Y+19);cputs("**********************");void datainit(){int i,j;)for(i=0;i<19;i++){for(j=0;j<10;j++){La[j]=0;Label=0;FLAG=0;ldelay=d[0];Lsum=0;Lpoint=0;randomize();Lnext=random(23);}}}void dispb(LRflag,x,y,blockcode){int realx,realy;if(LRflag==L){realx=LX+x*2;realy=L Y+y;}realx=x;raly=y;textcolor(b[blockcode].color);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0); cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1); cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2); cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3); cputs("**");}void eraseb(LRflag,x,y,blockcode)int LRflag,x,y,blockcode;int realx,realy;if(LRflag==L){realx=LX+x*2;realy=L Y+y;}else{realx=Lx+x*2;realy=L Y+y;}textcolor(0);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0);cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1);cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2);cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3);cputs("**");}。
c语言21点小游戏设计
user=user+usercurrent;
switch(usercurrent)
{
case 1:printf("you have got a %cA\n",3);break;
case 2:printf("you have got a %c2\n",3);break;
case 38:printf("computer have got * %cQ\n",5);break;
case 39:printf("computer have got * %cK\n",5);break;
case 40:printf("computer have got * %cA\n",6);break;
case 29:printf("computer have got * %c3\n",5);break;
case 30:printf("computer have got * %c4\n",5);break;
case 31:printf(");break;
case 6:printf("you have got * %c6\n",3);break;
case 7:printf("you have got * %c7\n",3);break;
case 8:printf("you have got * %c8\n",3);break;
case 44:printf("computer have got * %c5\n",6);break;
简单的迷宫小游戏C语言程序源代码
简单的迷宫小游戏C语言程序源代码#include <stdio.h>#include <conio.h>#include <windows.h>#include <time.h>#define Height 31 //迷宫的高度,必须为奇数#define Width 25 //迷宫的宽度,必须为奇数#define Wall 1#define Road 0#define Start 2#define End 3#define Esc 5#define Up 1#define Down 2#define Left 3#define Right 4int map[Height+2][Width+2];void gotoxy(int x,int y) //移动坐标{COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HA NDLE ), coord );}void hidden()//隐藏光标{HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci); cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}void create(int x,int y) //随机生成迷宫{int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向int i,j,t;//将方向打乱for(i=0;i<4;i++){j=rand()%4;t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;}map[x][y]=Road;for(i=0;i<4;i++)if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) {map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]);}}int get_key() //接收按键{char c;while(c=getch()){if(c==27) return Esc; //Escif(c!=-32)continue;c=getch();if(c==72) return Up; //上if(c==80) return Down; //下if(c==75) return Left; //左if(c==77) return Right; //右}return 0;}void paint(int x,int y) //画迷宫{gotoxy(2*y-2,x-1);switch(map[x][y]){case Start:printf("入");break; //画入口case End:printf("出");break; //画出口case Wall:printf("※");break; //画墙case Road:printf(" ");break; //画路}}void game(){int x=2,y=1; //玩家当前位置,刚开始在入口处int c; //用来接收按键while(1){gotoxy(2*y-2,x-1);printf("☆"); //画出玩家当前位置if(map[x][y]==End) //判断是否到达出口{gotoxy(30,24);printf("到达终点,按任意键结束"); getch();break;}c=get_key();if(c==Esc){gotoxy(0,24);break;}switch(c){case Up: //向上走if(map[x-1][y]!=Wall){paint(x,y);x--;}break;case Down: //向下走if(map[x+1][y]!=Wall){paint(x,y);x++;}break;case Left: //向左走if(map[x][y-1]!=Wall){paint(x,y);y--;}break;case Right: //向右走if(map[x][y+1]!=Wall){paint(x,y);y++;}break;}}}int main(){int i,j;srand((unsigned)time(NULL)); //初始化随即种子hidden(); //隐藏光标for(i=0;i<=Height+1;i++)for(j=0;j<=Width+1;j++)if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫map[i][j]=Road;else map[i][j]=Wall;create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数for(i=0;i<=Height+1;i++) //边界处理{map[i][0]=Wall;map[i][Width+1]=Wall;}for(j=0;j<=Width+1;j++) //边界处理{map[0][j]=Wall;map[Height+1][j]=Wall;}map[2][1]=Start; //给定入口map[Height-1][Width]=End; //给定出口for(i=1;i<=Height;i++)for(j=1;j<=Width;j++) //画出迷宫paint(i,j);game(); //开始游戏getch();return 0;}。
C语言小游戏源代码
b=(a+2)%8; /*得到下第二个的数组下标*/
change(a,b); /*交换*/
}
update();
}
update(){ /*在屏幕上画出整个九格图像,关于定位的问题自己分析*/
pos=GetTheNull(); /*得到空格*/
switch(key){ /*测试按键*/
case 72: /*按下*/
if(pos<=5) change(pos,pos+3);
int i;
for(i=0;i<=8;i++){
if (num[i]==0)
return i;
}
}
change(int a,int b){ /*交换num[a],num[b]*/
int temp;
}
if(key==27) break; /*退出*/
}
clrscr(); /*清屏*/
}
newGame(){ /*新建游戏,实际上就是把有数字的八个方格打散,
这种效果的实现就是随机的两两交换,而怎么不与空的那个交换呢?
而这些的前提是其中的0不能与其第奇数个作奇数次交换.
为了省事,不处理0...
其中产道理,请自己思考
*/
int i,a,b;
for(i=0;i<8;i++) num[i]=i+1;
num[8]=0; /*将数组复原*/
for(i=0;i<300;i++){
这涉及有没有解的问题
不管怎么样,九格游戏最后的结果只有两个:
C语言小游戏源代码《打砖块》
C 语言小游戏源代码《打砖块》#include "graphics.h"#include "stdio.h"#include "conio.h" /* 所需的头文件*/int on; /* 声明具有开关作用的全局变量*/ static int score;/* 声明静态的记分器变量*//* 定义开始界面函数*/int open(){setviewport(100,100,500,380,1); /* 设置图形窗口区域*/setcolor(4); /* 设置作图色*/rectangle(0,0,399,279); /* 以矩形填充所设的图形窗口区域*/ setfillstyle(SOLID_FILL,7); /* 设置填充方式*/floodfill(50,50,4); /* 设置填充范围*/setcolor(8);settextstyle(0,0,9); /* 文本字体设置*/outtextxy(90,80,"BALL"); /* 输出文本内容*/settextstyle(0,0,1);outtextxy(110,180,"version 1.0");outtextxy(110,190,"made by ddt");setcolor(128);settextstyle(0,0,1);outtextxy(120,240,"Press any key to continue "); }/* 定义退出界面函数*/int quitwindow(){char s[100]; /* 声明用于存放字符串的数组*/setviewport(100,150,540,420,1);setcolor(YELLOW);rectangle(0,0,439,279);setfillstyle(SOLID_FILL,7);floodfill(50,50,14);setcolor(12);settextstyle(0,0,8);outtextxy(120,80,"End");settextstyle(0,0,2);outtextxy(120,200,"quit? Y/N");sprintf(s,"Your score is:%d",score);/* 格式化输出记分器的值*/outtextxy(120,180,s);on=1; /* 初始化开关变量*/}/* 主函数*/main(){int gdriver,gmode;gdriver=DETECT; /* 设置图形适配器*/gmode=VGA; /* 设置图形模式*/registerbgidriver(EGAVGA_driver); /* 建立独立图形运行程序*/ initgraph(&gdriver,&gmode,""); /* 图形系统初试化*/ setbkcolor(14);open(); /* 调用开始界面函数*/getch(); /* 暂停*/while(1) /* 此大循环体控制游戏的反复重新进行*/{int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=- 2,dy=-2;/* 初始化小球相关参数*/intleft[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][10 0];/*方砖阵列相关参数*/static int pp;static int phrase; /* 一系列起开关作用的变量*/ int oop=15;pp=1;score=0;driver=DETECT;mode=VGA;registerbgidriver(EGAVGA_driver);initgraph(&driver,&mode,"");setbkcolor(10);cleardevice(); /**/ clearviewport(); /* 清除现行图形窗口内容*/b=t+6;r=l+60;setcolor(1);rectangle(0,0,639,479);setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+2,t+2,4);for(i=0,k=0;i<=6;i++) /* 此循环绘制方砖阵列*/ {top[i]=k;bottom[i]=top[i]+20;k=k+21;oop--;for(j=0,m=0;j<=7;j++){left[j]=m;right[j]=left[j]+80;m=m+81;setcolor(4);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,j+oop); floodfill(left[j]+1,top[i]+1,4);num[i][j]=pp++;}}while(1) /* 此循环控制整个动画*/ {while(!kbhit()){x=x+dx; /* 小球运动的圆心变量控制*/ y=y+dy;if(x+r1>r||x+r1<r)phrase=0;} {if((x-r1<=r||x+r1<=r)&&x+r1>=l){if(y<t)phrase=1;if(y+r1>=t&&phrase==1){dy=-dy;y=t-1-r1;}}if(off==0)continue;for(i=0;i<=6;i++) /*for(j=0;j<=7;j++) 此循环用于判断、控制方砖阵列的撞击、擦除* /if((x+r1<=right[j]&&x+r1>=left[j])||(x-r1<=right[j]&&x- r1>=left[j])){if(( y-r1>top[i]&&y-r1<=bottom[i])||(y+r1>=top[i]&&y+r1<=bottom[i] )) {if(num[i][j]==0){continue; }setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10); dy=-dy;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}if((y+r1>=top[i]&&y+r1<=bottom[i])||(y-r1>=top[i]&&y-r1<=bottom[i])){if((x+r1>=left[j]&&x+r1<right[j])||(x-r1<=right[j]&&x-r1>left[j])){if(num[i][j]==0){ continue;}setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dx=-dx;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}}if(x+r1>639) /* 控制小球的弹射范围*/{dx=-dx;x=638-r1;}if(x<=r1){dx=-dx;x=r1+1;}if(y+r1>=479){off=0;quitwindow();break;}if(y<=r1){dy=-dy;y=r1+1;}if(score==560){off=0;quitwindow();break;} setcolor(6); circle(x,y,r1);setfillstyle(SOLID_FILL,14);floodfill(x,y,6);delay(1000);setcolor(10);circle(x,y,r1);setfillstyle(SOLID_FILL,10);floodfill(x,y,10); }a=getch();setcolor(10);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,10);floodfill(l+2,t+2,10);if(a==77&&l<=565) /* 键盘控制设定*/ {dl=20;l=l+dl;}if(a==75&&l>=15){dl=-20;l=l+dl;}if(a=='y'&&on==1)break;if(a=='n'&&on==1)break;if(a==27){quitwindow();off=0;}r=l+60;setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+5,t+5,4);delay(100);}if(a=='y'&&on==1) /* 是否退出游戏*/ {break;}if(a=='n'&&on==1){ continue;}}closegraph();}。
推箱子(C语言小游戏代码)
#include<stdio.h> #include<stdlib.h>#include<windows.h>#include<conio.h>//打印地图void DrawMap();//设置颜色void SetColor(int nColor);//获取玩家坐标POINT GetGamerPosition();//上void Up();//下void Down();//左void Left();//右void Right();//获取空箱子个数int GetSpareBox();//地图//0:空地;3箱子的目的地4箱子// 6人7箱子与目的地的集合//1:墙壁;9人在箱子的目的地int g_map[2][10][12]={{{0,0,0,0,1,1,1,0,0,0,0,0 },{0,0,0,0,1,3,1,0,0,0,0,0 },{0,0,0,0,1,0,1,1,1,1,1,1 },{1,1,1,1,1,4,0,4,0,0,3,1 },{1,3,0,0,0,4,6,1,1,1,1,1 },{1,1,1,1,1,1,4,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,3,1,0,0,0,0 },{0,0,0,0,0,1,1,1,0,0,0,0 },},{{1,1,1,1,1,0,0,0,0,0,0,0 },{1,0,0,0,1,0,1,1,1,0,0,0 },{1,0,4,0,1,0,1,1,1,1,1,1 },{1,0,4,6,1,0,1,0,0,0,3,1 },{1,1,1,4,1,1,1,0,0,0,3,1 },{1,1,1,0,0,0,0,0,0,0,3,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,1,1,1,1,1,1 },{1,1,1,1,1,1,0,0,0,0,0,0 },}};int g_nCurrentLevel = 0;//当前管卡int main(){//设置窗口大小system("mode con cols=26 lines=11");//设置标题//SetConsoLeTitle("推箱子");char nInput = 0;//输入字符while(1){//如果箱子推完了过关if (0==GetSpareBox())g_nCurrentLevel++;//清屏system("cls");//如果没有关卡,那就恭喜你成功通关。
C语言小游戏
C语言小游戏#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<Windows.h>//延时语句#include<conio.h>//getch字符输入但不显示int i=0;int x=0;int ii=0;char zhanghao[10]={0};char mima0[10]={0};char zh[10]={0};char mima1[10]={0};int a1=0,a2=0,a3=0;//结构体/*结构体*/struct xin{char nicheng[30];char zhongzu[30];char xingbie[10];}ling[100];/*主菜单*/int x0(){x=0;printf("\t*************&*********&前尘~=~往世&*********&**************\n\n");while(1){printf("\t\t\t1 . 进入轮回注册\n");printf("\t\t\t2 . 进入轮回世界\n"); printf("\t\t\t3 . 查看规则\n"); printf("\t\t\t4 . 关于前尘往世有系\n");printf("\t\t\t5 . 结束游戏\n");printf("\n\n\t\t\t提示:\n\t\t\t 输入所选项,并回车。
\n\t\t\t 账号首字符不可为零。
\n");printf("\n\t请选择: ");scanf("%d",&x);if(x>0&&x<=5){system("cls");break;}else{system("cls"); printf("\t*************&*********&前尘~=~往世&*********&**************\n\n");printf("\t\t输入有误,请重新输入~~\n\t\t请在已有项中选择:\n");continue;}}return(x);}//主菜单/*进入轮回注册*/int x1(){char aa0=0;=>请注册轮回账号<= ***************\n"); printf("\t\t*************** printf("\n请创建轮回编号(8位): ");scanf("%s",zhanghao); printf("\n请创建轮回密码(8位): ");fflush(stdin);//清除缓存区for(ii=0;ii<10;ii++){mima0[ii]=getch();//逐次赋值但不显示if(mima0[ii]=='\x0d') {mima0[ii]='\0';break;}printf("*");}system("cls");printf("\t\t*************** =>请注册轮回账号<= ***************\n"); printf("\n请输入前世姓名: ");scanf("%s",ling[i].nicheng);printf("\n请输入前世种族: ");scanf("%s",ling[i].zhongzu);printf("\n请输入今世性别: ");scanf("%s",ling[i].xingbie);printf("\t\t已注册成功轮回编号~~");printf("\n\n\n\t按任意键进入轮回查询,回车键返回主菜单...\n"); fflush(stdin);aa0=getch();system("cls");if(aa0=='\x0d'){}else{x2();}return 0;}/*进入轮回世界*/int x2(){int ii=0;cuo=0; intint i1=0;char x2c=0;printf("\t\t***************&欢迎进入前生测试游戏&***************\n"); while(1){if(zhanghao[0]==0){char aa0=0;printf("\t\t请先创建轮回编号~~");printf("\n\n\n\t按任意键进入轮回编号创建,回车键返回主菜单...\n");fflush(stdin);aa0=getch();system("cls");if(aa0=='\x0d'){x0();//主菜单}else{x1();//进入注册break;}}else{if(cuo==1){}printf(" 请输入轮回编号(8位): "); scanf("%s",zh);printf(" 请输入轮回密码(8位): "); fflush(stdin);//清除缓存区for(ii=0;ii<10;ii++){mima1[ii]=getch();//逐次赋值但不显示 if( mima1[ii]=='\x0d') {mima1[ii]='\0';break;}printf("*");}for(i=0;i<10;i++){if((zhanghao[i]==zh[i])&&(mima0[i]== mima1[i])){if(zhanghao[i]=='\0'||zh=='\0'||mima0[i]=='\0'||mima1[i]=='\0'){printf("\n\n\n\t\t\t输入正确,进入轮回系统中。
憋死牛游戏(C语言小游戏)
#include<stdio.h>#include<windows.h>#include<stdlib.h>char qipan[23][23];void csh();//初始化棋盘void printfqipan();//输出棋盘void yxjs();//游戏介绍int main(){int a,b,c,d,p,q;char e;//用于存储谁先走bool who;int x=1;int n=0;static bool t=true;//定义静态变量,递归调用时不会被初始化SetConsoleTitle("石家庄学院---“憋死牛”儿时游戏"); // 设置控制台标题system("mode con cols=78 lines=43");//设置控制台缓冲区大小system("color 02");//设置字体颜色yxjs(); //输出游戏介绍if(t)//再来一局时不会执行{printf("按回车键开始游戏!");getchar();t=false;}csh();//初始化棋盘printfqipan();//输出棋盘printf("请输入谁先开始:");while(x)//获取先走的一方{fflush(stdin);//清空键盘缓冲区e=getchar();fflush(stdin);//清空键盘缓冲区if(e=='a'||e=='A'){who=true;x--;}//获取到有效值则终止循环else if(e=='b'||e=='B'){who=false;x--;}//获取到有效值则终止循环elseprintf("请输入A或B:");}printf("输入要走棋子的坐标,使其补到空位。
C语言代码实现简单2048游戏
C语⾔代码实现简单2048游戏最近玩2048上瘾,于是尝试⽤C++写了⼀个2048⼩游戏操作⽅法很简单,通过wasd控制⽅块的⽅向,数据的上限为65536代码如下#include<bits/stdc++.h>#include<conio.h>#include <windows.h>void color(short x){if(x>=0 && x<=15)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);elseSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);}using namespace std;int qp[4][4]={0};long long int gread=0;int pd(){int i,j;for(i=0;i<4;i++){for(j=0;j<4;j++){if(qp[i][j]==0){return 0;}if(i==0&&j==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j+1]){return 0;}}else if(i==0&&j==3){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j-1]){return 0;}}else if(i==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j+1]||qp[i][j]==qp[i][j-1]){return 0;}}else if(i==3&&j==0){if(qp[i][j]==qp[i][j+1]||qp[i][j]==qp[i-1][j]){return 0;}}else if(j==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j+1]){return 0;}}else if(i==3&&j==3){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]||qp[i][j]==qp[i][j+1]) {return 0;}}else if(j==3){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]||qp[i][j]==qp[i+1][j]) {return 0;}}}}return 1;}int sjs(){int num = rand() % 100 + 1;if(num<=5){return 4;}else{return 2;}}int sc(){for(;;){int n=rand()%4;int m=rand()%4;if(qp[n][m]==0){qp[n][m]=sjs();return 0;}}}void dy(int n){if(n==0){cout<<" ";}else if(n==2){color(7);cout<<" "<<n<<" ";color(7);}else if(n==4){color(14);cout<<" "<<n<<" ";color(7);}else if(n==8){color(6);cout<<" "<<n<<" ";color(7);}else if(n==16){color(12);cout<<" "<<n<<" ";cout<<" "<<n<<" ";color(7);}else if(n==64){color(13);cout<<" "<<n<<" ";color(7);}else if(n==128){color(5);cout<<" "<<n<<" ";color(7);}else if(n==256){color(9);cout<<" "<<n<<" ";color(7);}else if(n==512){color(3);cout<<" "<<n<<" ";color(7);}else if(n==1024){color(11);cout<<n<<" ";color(7);}else if(n==2048){color(10);cout<<n<<" ";color(7);}else if(n==4096){color(2);cout<<n<<" ";color(7);}else{color(15);cout<<n;color(7);}}int main(){srand(time(NULL));int i,j;cout<<"Game start!(输⼊w a s d进⾏控制)"<<endl; sc();sc();cout<<"-------------------------"<<endl;cout<<"|";dy(qp[0][0]);cout<<"|";dy(qp[0][1]);cout<<"|";dy(qp[0][2]);cout<<"|";dy(qp[0][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl;cout<<"|";dy(qp[1][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[2][0]);cout<<"|";dy(qp[2][1]);cout<<"|";dy(qp[2][2]);cout<<"|";dy(qp[2][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[3][0]);cout<<"|";dy(qp[3][1]);cout<<"|";dy(qp[3][2]);cout<<"|";dy(qp[3][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; for(;;){char n;n=getch();if(n=='w'){int g=0;for(i=0;i<4;i++){for(j=1;j<4;j++){if(qp[j][i]!=0){int k=j;while(qp[k-1][i]==0&&k!=0){k--;}qp[k][i]=qp[j][i];if(k!=j){qp[j][i]=0;g=1;}}}if(qp[0][i]==qp[1][i]&&qp[0][i]!=0) {qp[0][i]=qp[0][i]*2;gread+=qp[0][i];qp[1][i]=qp[2][i];qp[2][i]=qp[3][i];qp[3][i]=0;g=1;}if(qp[1][i]==qp[2][i]&&qp[1][i]!=0) {qp[1][i]=qp[1][i]*2;gread+=qp[1][i];qp[2][i]=qp[3][i];qp[3][i]=0;g=1;}if(qp[2][i]==qp[3][i]&&qp[2][i]!=0) {qp[2][i]=qp[2][i]*2;{cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='d'){int g=0;for(i=0;i<4;i++){for(j=2;j>=0;j--){if(qp[i][j]!=0){int k=j;while(qp[i][k+1]==0&&k!=3){k++;}qp[i][k]=qp[i][j];if(k!=j){qp[i][j]=0;g=1;}}}if(qp[i][3]==qp[i][2]&&qp[i][3]!=0) {qp[i][3]=qp[i][3]*2;gread+=qp[i][3];qp[i][2]=qp[i][1];qp[i][1]=qp[i][0];qp[i][0]=0;g=1;}if(qp[i][2]==qp[i][1]&&qp[i][2]!=0) {qp[i][2]=qp[i][2]*2;gread+=qp[i][2];qp[i][1]=qp[i][0];qp[i][0]=0;g=1;}if(qp[i][1]==qp[i][0]&&qp[i][1]!=0) {qp[i][1]=qp[i][1]*2;gread+=qp[i][1];qp[i][0]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='s'){int g=0;for(i=0;i<4;i++)while(qp[k+1][i]==0&&k!=3){k++;}qp[k][i]=qp[j][i];if(k!=j){qp[j][i]=0;g=1;}}}if(qp[3][i]==qp[2][i]&&qp[3][i]!=0) {qp[3][i]=qp[3][i]*2;gread+=qp[3][i];qp[2][i]=qp[1][i];qp[1][i]=qp[0][i];qp[0][i]=0;g=1;}if(qp[2][i]==qp[1][i]&&qp[2][i]!=0) {qp[2][i]=qp[2][i]*2;gread+=qp[2][i];qp[1][i]=qp[0][i];qp[0][i]=0;g=1;}if(qp[1][i]==qp[0][i]&&qp[1][i]!=0) {qp[1][i]=qp[1][i]*2;gread+=qp[1][i];qp[0][i]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='a'){int g=0;for(i=0;i<4;i++){for(j=1;j<4;j++){if(qp[i][j]!=0){int k=j;while(qp[i][k-1]==0&&k!=0){k--;}qp[i][k]=qp[i][j];if(k!=j){qp[i][j]=0;g=1;}}}if(qp[i][0]==qp[i][1]&&qp[i][0]!=0)qp[i][2]=qp[i][3];qp[i][3]=0;g=1;}if(qp[i][1]==qp[i][2]&&qp[i][1]!=0){qp[i][1]=qp[i][1]*2;gread+=qp[i][1];qp[i][2]=qp[i][3];qp[i][3]=0;g=1;}if(qp[i][2]==qp[i][3]&&qp[i][2]!=0){qp[i][2]=qp[i][2]*2;gread+=qp[i][2];qp[i][3]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl;continue;}else{system("cls");}}else{cout<<"请输⼊w、a、s、d"<<endl; continue;}sc();cout<<"分数:"<<gread<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[0][0]);cout<<"|";dy(qp[0][1]);cout<<"|";dy(qp[0][2]);cout<<"|";dy(qp[0][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[1][0]);cout<<"|";dy(qp[1][1]);cout<<"|";dy(qp[1][2]);cout<<"|";dy(qp[1][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[2][0]);cout<<"|";dy(qp[2][1]);cout<<"|";dy(qp[2][2]);cout<<"|";dy(qp[2][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[3][0]);cout<<"|";dy(qp[3][1]);cout<<"|"<<endl;cout<<"-------------------------"<<endl;if(pd()==1){break;}}cout<<"Game over~"<<endl;cout<<"请输⼊“quit”并回车退出游戏"<<endl;for(;;){char s[10000];cin>>s;if(strcmp(s,"quit")==0){break;}}return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
c语言小游戏代码
c语言小游戏代码#include <stdio.h>#include <stdlib.h>#include <windows.h>// 定义元素类型#define ELEMENT char// 游戏行数#define ROW 10// 游戏显示延迟#define SLEEPTIME 100int main(int argc, char *argv[]){// 定义游戏的棋盘,用数组存放ELEMENT array[ROW][ROW];// 定义获胜条件int winCondition = 5;// 初始化,把棋盘清空system("cls");int i,j;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){array[i][j] = ' ';}}// 循环游戏,当有一方满足胜利条件时终止int tmp;int count = 0; // 存放棋子数while(1){// 依次取出玩家记录的棋子int x, y;// 如果已经有子落下,则计算是第几步if(count > 0){printf("第%d步:\n", count);}// 显示游戏棋盘for(i = 0; i < ROW; i++){printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n|");for(j = 0; j < ROW; j++){printf("%c |", array[i][j]);}printf("\n");}printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n");// 要求玩家输入放下棋子的位置printf("请玩家输入要放弃棋子的位置(1-%d)\n", ROW); printf("横坐标:");scanf("%d", &x);printf("纵坐标:");scanf("%d", &y);// 判断棋子位置是否有效if(x < 1 || x > ROW || y < 1 || y > ROW || array[x-1][y-1] != ' '){printf("输入错误!\n");system("pause");system("cls");continue;}// 把棋子记录,并计数if(count % 2 == 0){array[x-1][y-1] = 'X';}else{array[x-1][y-1] = 'O';}count++;// 判断是否有获胜者int i, j, k;int tempx, tempy;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){if(array[i][j] == 'X' || array[i][j] == 'O') {// 判断横向是否有获胜者tmp = 1;for(k = 1; k < winCondition; k++){// 注意边界,必须验证范围有效if(j + k > ROW - 1) break;// 如果和前一个位置的棋子相同,则计数加1,否则跳出if(array[i][j+k] == array[i][j])tmp++;else break;}// 如果计数满足获胜条件,则显示获胜者if(tmp >= winCondition){printf("玩家 %c 获胜!\n", array[i][j]);system("pause");return 0;}// 判断纵向是否有获胜者tmp = 1;for(k。
c语言入门猜数字游戏(解释每行代码)
c语言入门猜数字游戏(解释每行代码)这是一个“猜数字”的小游戏,玩家需要猜出随机生成的数字。
#include <stdio.h> // 1. 引入标准输入输出库#include <stdlib.h> // 2. 引入标准库#include <time.h> // 3. 引入时间相关库int main() { // 4. 主函数开始int num,guess; // 5. 定义两个整型变量: num和guesssrand(time(NULL)); // 6. 初始化随机数种子num = rand() % 100 + 1; // 7. 生成1到100之间的随机整数printf("请猜一个1~100之间的整数:\n"); // 8. 提示玩家输入do { // 9. 进入循环体scanf("%d", &guess); // 10. 接收玩家的输入if (guess > num) { // 11. 判断玩家猜的数是否太大printf("你猜的数字太大了!\n继续猜:\n");} else if (guess < num) { // 12. 判断玩家猜的数是否太小printf("你猜的数字太小了!\n继续猜:\n");}} while (guess != num); // 13. 如果猜错了就继续循环printf("恭喜你,猜对了!\n"); // 14. 玩家猜对了,输出结果return 0; // 15. 返回0表示程序结束}各行代码解释如下:1. 引入`stdio.h`库,提供了输入和输出的函数和符号常量。
2. 引入`stdlib.h`库,提供一些标准函数功能,如内存分配等。
3. 引入`time.h`库,提供了处理时间的函数。
4. `int main()`是程序的起点,主函数开始执行。
C语言小游戏源代《俄罗斯方块》
C语言小游戏源代码《俄罗斯方块》#include <stdlib.h>#include <stdio.h>#include <graphics.h>#define ESC 27#define UP 328#define DOWN 336#define LEFT 331#define RIGHT 333#define BLANK 32#define BOTTOM 2#define CANNOT 1#define CAN 0#define MAX 30#define F1 315#define ADD 43#define EQUAL 61#define DEC 45#define SOUNDs 115#define SOUNDS 83#define PAUSEP 80#define PAUSEp 112void Init();void Down();void GoOn();void ksdown();void Display(int color);void Give();int Touch(int x,int y,int dx,int dy);int GeyKey();void Select();void DetectFill();void GetScores();void Fail();void Help();void Quit();void DrawBox(int x,int y,int Color);void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch);void DrawNext(int Color);int Heng=12,Shu=20; /*横竖*/int Position[MAX][MAX];int middle[MAX][MAX];int ActH,ActS;int Act,Staus;int i,j,k;int Wid=10;int NoPass=CAN;float Delays=15000;int BeginH=250,BeginS=7;float Seconds=0;int Scores=0;int flag=1;int Sounds=CAN;int PreAct,NextAct;int a[8][4][4][4]={{{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}},{{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}},{{1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},{{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}},{{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}},{{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}}, {{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}, {1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0}, {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0}}, {{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}; int b[4][4];main(int argc,char *argv[]){if (argc!=1){if (argv[1]!="")Heng=atoi(argv[1]);if (argv[2]!="")Shu=atoi(argv[2]);}Init(); /*初始化界面*/PreAct=random(8); /*取得当前的方块*/ for(;;) /*以下是游戏流程*/{NextAct=random(8); /*取得下一个方块*/ DrawNext(1); /*画出下一个方块*/Act=PreAct;if (Heng%2==0) ActH=Heng/2;else ActH=(Heng-1)/2;ActS=0; /*方块开始从游戏空间的中间下落*/Staus=0; /*取开始的状态*/NoPass=CAN; /*物体可以下落*/Give(); /*取得当前的方块*/Display(Act+1); /*显示当前的方块,每种方块的颜色不同*/ GoOn(); /*游戏的算法精髓所在*/PreAct=NextAct; /*方块下落完毕,取得下一个方块*/ DrawNext(0);}}void Init(){int GraphDriver=DETECT,GraphMode;registerbgidriver(EGAVGA_driver);initgraph(&GraphDriver,&GraphMode,"");if (kbhit()) Sounds=CANNOT;setcolor(1);OutTextXY(10,10,"Tetris");OutTextXY(30,30,"Version 2.0");OutTextXY(10,120,"Help:");OutTextXY(20,140,"+ :Faster");OutTextXY(20,160,"- :Slower");OutTextXY(20,180,"Esc :Quit");OutTextXY(20,200,"F1 :Help");OutTextXY(10,310,"Copyright(c) 1998.2.22");OutTextXY(10,320,"By Mr. Unique");outtextxy(10,250,"Score: 00000");rectangle(BeginH-3,BeginS-3,BeginH+Heng*(Wid+2)+2,BeginS+Shu*(Wid+2)+2);rectangle(BeginH-5,BeginS-5,BeginH+Heng*(Wid+2)+4,BeginS+Shu*(Wid+2)+4);rectangle(BeginH+(Heng+4)*(Wid+2)-2,BeginS+10,BeginH+(Heng+8)*(Wid+2)+2,BeginS+12+4*( Wid+2));for (i=0;i<MAX;i++)for (j=0;j<MAX;j++){Position[i][j]=1;middle[i][j]=-1;}for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)Position[i][j]=0;for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)DrawBox(i,j,0);randomize();}void GoOn(){for(;;){Seconds+=0.2; /*控制方块的下落速度*/if (Seconds>=Delays){Down();Seconds=0;if (NoPass==BOTTOM){DetectFill();middle[ActH][ActS]=Act;if (ActS==0)Fail();return;}}if (kbhit())Select();}}void Down() /*方块下降*/{Display(0);if (Touch(ActH,ActS,0,1)==CAN) ActS++;elsemiddle[ActH][ActS]=Act; Display(Staus+1);}int Touch(int x,int y,int dx,int dy) {NoPass=CAN;for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+dx+i][y+dy+j]+=b[i][j];for (i=0;i<MAX;i++)for (j=0;j<MAX;j++)if (Position[i][j]>1)NoPass=CANNOT;for (i=0;i<4;i++)for (j=0;j<4;j++){Position[x+dx+i][y+dy+j]-=b[i][j]; middle[x+dx+i][y+dy+j]=Act;}if (NoPass==CANNOT && dx==0 && dy==1) {for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+i][y+j]+=b[i][j];NoPass=BOTTOM;}return NoPass;}int GetKey(void){int Ch,Low,Hig;Ch=bioskey(0);Low=Ch&0x00ff;Hig=(Ch&0xff00)>>8;return(Low==0?Hig+256:Low);}void Select(){int OldStaus,acts=ActS;switch(GetKey()){case ESC :Quit();break;case DOWN :Seconds+=14500;break;case LEFT :Display(0);if (ActH>0 && Touch(ActH,ActS,-1,0)==CAN) { ActH--;}Display(Act+1);break;case RIGHT :Display(0);if (ActH<Heng && Touch(ActH,ActS,1,0)==CAN) { ActH++;}Display(Act+1);break;case BLANK : Display(0);ksdown();Display(Act+1);break;case F1 :Help();break;case EQUAL :case ADD :if (Delays>300) Delays-=100;break; case DEC :if (Delays<3000) Delays+=100;break; case PAUSEP :case PAUSEp :getch();break;case SOUNDS :case SOUNDs :if (Sounds==CAN)Sounds=CANNOT;elseSounds=CAN;break;case UP :if(Act==7){while(acts<Shu-1&&Position[ActH][acts]!=1) acts++;Position[ActH][acts]=0;DrawBox(ActH,acts,0);acts=ActS;break;}else{Display(0);OldStaus=Staus;switch(Act){case 0:case 3:case 4:if (Staus==1) Staus=0;else Staus=1;break; case 1:break;case 2:case 5:case 6:if (Staus==3) Staus=0;else Staus++;break; }Give();if (Touch(ActH,ActS,0,0)==CANNOT){Staus=OldStaus;Give();}Display(Act+1);break;}}}void ksdown(){while(flag){if(Touch(ActH,ActS,0,0)==CAN){ActS++;}else {ActS--;flag=0;}}flag=1;}void Quit(){int ch,TopScore;FILE *fp;if ((fp=fopen("Russian.scr","r+"))!=NULL) {fscanf(fp,"%d",&TopScore);if (Scores>TopScore){setcolor(1);outtextxy(470,80,"Hello !");outtextxy(470,100,"In all the players,"); outtextxy(470,120,"You are the First !"); outtextxy(470,140,"And your score will"); outtextxy(470,160,"be the NEW RECORD !"); fseek(fp,0L,0);fprintf(fp,"%d",Scores);}fclose(fp);}setcolor(1);OutTextXY(470,220,"Are You Sure (Yes/no)?");ch=getch();if (ch=='y'||ch=='Y'){closegraph();delay(20);exit(0);}setcolor(0);outtextxy(470,220,"Are You Sure (Yes/no)?"); }void OutTextXY(int x,int y,char *String) {int i=0;char a[2];moveto(x,y);a[1]='\0';while (*(String+i)!='\0'){a[0]=*(String+i);outtext(a);if (Sounds==CAN && a[0]!=' '){sound(3000);delay(50);nosound();}i++;}}void Help(){unsigned Save;void *Buf;Save=imagesize(160,120,500,360);Buf=malloc(Save);getimage(160,120,500,360,Buf);setfillstyle(1,1);bar(160,120,500,280);setcolor(0);OutTextXY(170,130," About & Help");OutTextXY(170,150," # # # ########## # # # "); OutTextXY(170,160," # ## # # # # # # ###### ### "); OutTextXY(170,170," ########## ########## ## # # "); OutTextXY(170,180," # # # # # # # ## #### "); OutTextXY(170,190," # ## # #### ## # # # "); OutTextXY(170,200," # ## # # # # # ## # # # "); OutTextXY(170,210," # # # ## ## # ###### # # # "); OutTextXY(170,220," ## # ## # ## # # # # "); OutTextXY(170,230," # ## # #### # ## # "); OutTextXY(170,260," Good Luckly to You !!! ");getch();putimage(160,120,Buf,0);free(Buf);}void GetScores(){int Sec10000,Sec1000,Sec100,Sec10,Sec1; setfillstyle(0,1);bar(60,250,109,260);Sec1=Scores%10;Sec10=(Scores%100-Scores%10)/10;Sec100=(Scores%1000-Scores%100)/100;Sec1000=(Scores%10000-Scores%1000)/1000; Sec10000=(Scores%100000-Scores%10000)/10000; DispScore(60,250,'0'+Sec10000);DispScore(70,250,'0'+Sec1000);DispScore(80,250,'0'+Sec100);DispScore(90,250,'0'+Sec10);DispScore(100,250,'0'+Sec1);DispScore(110,250,'0');DispScore(120,250,'0');}void DispScore(int x,int y,char Ch){char a[2];a[1]='\0';a[0]=Ch;outtextxy(x,y,a);void Give(){for (i=0;i<4;i++)for (j=0;j<4;j++)b[i][j]=a[Act][Staus][i][j];}void Display(int color){for (i=0;i<4;i++)for (j=0;j<4;j++)if (b[i][j]==1) DrawBox(ActH+i,ActS+j,color); }void DrawBox(int x,int y,int Color){x=BeginH+x*(Wid+2);y=BeginS+y*(Wid+2);setfillstyle(1,Color);bar(x+2,y+2,x+Wid-1,y+Wid-1);if (Color==0)setcolor(9);elsesetcolor(Act+1);rectangle(x+4,y+4,x+Wid-4,y+Wid-4);}void DrawNext(int Color)for (i=0;i<4;i++)for (j=0;j<4;j++)if (a[NextAct][0][i][j]==1) DrawBox(Heng+4+i,1+j,Color); }void DetectFill(){int Number,Fall,FallTime=0;for (i=Shu-1;i>=0;i--){Number=0;for (j=0;j<Heng;j++)if (Position[j][i]==1) Number++;if (Number==Heng){FallTime++;if (Sounds==CAN){sound(500);delay(500);nosound();}for (Fall=i;Fall>0;Fall--)for (j=0;j<Heng;j++){Position[j][Fall]=Position[j][Fall-1];middle[j][Fall]=middle[j][Fall-1];if (Position[j][Fall]==0) DrawBox(j,Fall,0); else DrawBox(j,Fall,middle[j][Fall]+1);}i++;}}switch(FallTime){case 0:break;case 1:Scores+=1;break;case 2:Scores+=3;break;case 3:Scores+=6;break;case 4:Scores+=10;break;}if (FallTime!=0){GetScores();if (Scores%100==0) Delays-=100;}}void Fail(){if (Sounds==CAN){for (k=0;k<3;k++){sound(300);delay(200);nosound();}}setcolor(1);OutTextXY(440,200,"Game over!"); Quit();closegraph();exit(0);}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
程序设计实践大作业学号:********XXXX姓名:XXX班级:信息10-3班实验题目:射击类飞机游戏成绩:一.实验题目:射击类飞机游戏二.实验目的:通过c语言编写一个射击类的打飞机小游戏,可以通过键盘来进行游戏,操作方法是“a”“d”“w”或者“←”“↑”“→”来控制,击中敌机可获得积分,被敌机撞中死亡一次,每次游戏有3次生还机会,游戏结束后可选择是否重新开始游戏……三.对游戏的改进:这个游戏是我对一个小游戏进行的改造升级版,添加了颜色函数、终止函数,选择类函数,使游戏实现了可以终止,不再是分数、死亡数一直增加但是没有结束的情况;增加了颜色函数,使得游戏看起来更加的舒适;增加了终止函数,使游戏在死亡三次后自动结束游戏,并且可以选择是否重新开始游戏;另外增添了设置函数,使得可以对游戏进行设置,改变游戏大小,调整飞机运行速度等等,是游戏更加的人性化……四.实验内容编写出c语言代码,运行程序,并调试程序,最终实现游戏的功能。
本程序主要包含游戏控制函数、设置是否继续游戏函数、输出函数、子弹移动函数、敌机移动函数、设置函数、菜单函数等7个主要函数,包含了不同的功能,对原来的程序作出了很大的改进,用到的主要语句有getche语句、for语句、while语句、printf语句、switch语句等等,添加了颜色函数,实现了诸多功能。
可以在页面上显示制作人的主要信息等等……【流程图见打印版】五.源程序:#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <time.h>#define N 35#define up 72#define left 75#define right 77void run();//游戏控制函数void yn();//设置是否继续游戏函数void print(int [][N]);//输出函数void movebul(int [][N]);//子弹移动函数void movepla(int [][N]);//敌机移动函数void setting(void);//设置函数void menu(void);//菜单函数int scr[22][N]={0},pl=9,width=24,speed=3,density=30,score=0,death=0;//全局变量:界面、我机初始位、界面宽度、敌机速度、敌机密度、得分、死亡void main(){ menu();run();}void print(int a[][N])//输出函数{system("cls");int i,j;for(i=0;i<22;i++){a[i][width-1]=4;for(j=0;j<width;j++){if(a[i][j]==0)printf(" ");if(a[i][j]==1)printf("\5");//输出我机的符号if(a[i][j]==2)printf("^");//子弹if(a[i][j]==3)printf("\3"); //输出敌机符号if(a[i][j]==4)printf("\2");if(i==1&&j==width-1)printf("您成功杀敌:%d 架",score);//右上角显示得分if(i==2&&j==width-1)printf("设置:Esc");if(i==4&&j==width-1)printf("您已经死亡了:%d 次",death);//右上角显示死亡次数if(i==9&&j==width-1)printf(" 你还剩余%d 条命!",3-death);if(i==18&&j==width-1)printf("制作人:XXX");if(i==19&&j==width-1)printf("班级:信息10-3班");if(i==20&&j==width-1)printf("学号:20100302xxxx");}printf("\n");}}void movebul(int a[][N]){int i,j;for(i=0;i<22;i++)for(j=0;j<width;j++){if(i==0&&a[i][j]==2)a[i][j]=0;if(a[i][j]==2){if(a[i-1][j]==3)//加分{score+=1;printf("\7");}a[i][j]=0,a[i-1][j]=2;}}}void movepla(int a[][N]){int i,j;for(i=21;i>=0;i--)//从最后一行往上是为了避免把敌机直接冲出数组。
for(j=0;j<width;j++){if(i==21&&a[i][j]==3)a[i][j]=0;//消除敌机,在最低层if(a[i][j]==3)a[i][j]=0,a[i+1][j]=3;//敌机移动}if(a[20][pl]==3&&a[21][pl]==1)death++;//死亡}void yn(){system("cls");system("color 2e");printf("\n");printf("\n");printf("\t\t\t\t *****GAME OVER*****\n\a");printf("\n");printf("\n");printf("\t\t *****游***戏***结***束***** \n\n\n");printf("\t\t\t按y键继续游戏,n键退出游戏(y/n)?\n");printf("\t\t\t");switch(getch())//提示是否要继续游戏{case 'y':case 'Y':death=0,score=0,run();break;case 'n':case 'N':break;default :exit(0);break;}}void setting(void){int sw=0,i,j;system("cls");do{sw=0;printf("\n 游戏界面的大小:1.大2.小>> ");switch(getche()){case '1':width=34;break;case '2':width=24;;break;default:printf("\n 错误,请重新选择...\n");sw=1;}}while(sw);do{sw=0;printf("\n 请选择敌机密度:1.大2.中3.小>> "); switch(getche()){case '0':density=10;break;case '1':density=20;break;case '2':density=30;break;case '3':density=40;break;default:printf("\n 错误,请重新选择...\n");sw=1;}}while(sw);do{sw=0;printf("\n 敌机的飞行速度:1.快2.中3.慢>> "); switch(getche()){case '1':speed=2;break;case '2':speed=3;break;case '3':speed=4;break;default:printf("\n 错误,请重新选择...\n");sw=1;}}while(sw);for(i=0;i<22;i++)for(j=0;j<45;j++)scr[i][j]=0;scr[21][pl=9]=1;printf("\n 按任意键保存...");getch();}void run(){system("color 2e");//设置背景颜色int i=0,j=0;scr[21][pl]=1; //我方飞机初始位置scr[0][5]=3;while(death<3) //限制死亡次数{if(kbhit())switch(getch())//控制左右移动{case left:case 'a':case 'A':if(pl>0)scr[21][pl]=0,scr[21][--pl]=1;break;case right:case 'd':case 'D':if(pl<width-2)scr[21][pl]=0,scr[21][++pl]=1;break;case up:case 'W':case 'w':scr[20][pl]=2;break;case 27 :exit(0);break;}if(++j%density==0)//控制生产敌机的速度{j=0;srand(time(NULL));//产生随机数scr[0][rand()%width]=3;//生成随机敌方飞机}if(++i%speed==0)//控制敌机移动速度,相对于子弹移动速度movepla(scr);//飞机移动movebul(scr);//子弹移动print(scr);//绘制游戏画面}yn();}void menu(void){system("color 2e");//设置背景颜色printf("说明:按N M 控制我机左右飞行,Z 发射子弹\n 设置:请按Esc\n 开始游戏:任意键");if(getch()==27)setting();}实验结果与分析:菜单界面退出时显示界面六.实验反思:通过本次试验,使我对c语言有了更深一层次的了解,对getche语句、for语句、while语句、printf语句、switch语句、颜色函数等等的了解及使用更加详细了一些,明白了这些语句的用法,明确了c语言的使用环境,功能。