本文主要是介绍[C] 大一课设-简易推箱子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大一时写的课设,今天翻到,在此保存。
若有帮到您最好,不足之处请多多包涵,欢迎指正错误。
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<string.h>
int step_num=0,askx=1,asky=1,nowx=1,nowy=1,sign=1;//记下箱子的位置
int num[20][75],out=1;
void form(void);//形成框架
void start(void);//游戏开始
void gotoxy(int x,int y);
void go(char way);//讲要去的地方
int judge(char);//判断是否可以移动
void move(int);//移动
void winorfail(void);
//1为障碍物,100为12 99为30void main() {if ( sign==0 ) return ;step_num=0;srand((unsigned) time(NULL) );form();start();
}void form(void) {int i,j,k;nowx=1,nowy=1;memset(num,0,sizeof(num));system("mode con cols=75 lines=24");for ( i=0; i<20; i++) {num[i][0]=1;num[i][74]=1;}//框架for ( i=0; i<75; i++) {num[0][i]=1;num[19][i]=1;}//框架for ( i=1; i<19; i++)for ( j=2; j<70; j++)num[i][j]=rand()%5;//随机障碍物for ( i=1; i<19; i++)for ( j=72; j<74; j++)num[i][j]=rand()%20;//随机障碍物num[1][1]=100;//人物初始位置k=1+rand()%18;num[k][74]=0;out=k;//出口i=1+rand()%18;j=50+rand()%73;num[i][j]=99;asky=i;askx=j;//箱子坐标for ( i=0; i<20; i++)for ( j=0; j<75; j++) {if ( num[i][j]==1 ) putchar('*');else if ( num[i][j]==100 ) putchar(12);else if ( num[i][j]==99 ) putchar(30);else putchar(' ');}
}
void gotoxy(int x, int y)
{COORD pos;pos.X = x; pos.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void start(void) {int i;char way;printf("操控w s a d或者方向键,操控%c将%c送出最右边的出口!按任意键开始游戏!",12,30);getch();gotoxy(0,20);for (i=0; i<75; i++) putchar(' ');gotoxy(0,20);printf("可按enter键换图!按ESC键退出游戏!");gotoxy(61,22);printf("By:summer_dew");way=getch();//得到一个字符while (1) {go(way);//把操控键传入gowinorfail();/*if ( askx==74&&asky==out ) {gotoxy(50,15);printf("You are win!");gotoxy(50,16);printf("输入任意键继续游戏");getch();main();}*/gotoxy(0,20);printf("可按enter键换图!按ESC键退出游戏!当前步数%d",step_num);way=getch();for ( i=0; i<90; i++) putchar(' ');}
}
void go(char way) {switch (way) {case 27:exit(0);break;//按ESC退出游戏 关闭窗口case '\r':main();break;case 80:move( judge('s') );break;case 's':move( judge('s') );break;case 72:move( judge('w') );break;case 'w':move( judge('w') );break;case 75:move( judge('a') );break;case 'a':move( judge('a') );break;case 77:move( judge('d') );break;case 'd':move( judge('d') );break;default:{gotoxy(0,21);printf("请输入w,s,a,d或者方向键操控方向!");}}
}
int judge(char a) {int j=nowx,i=nowy;//i存X,j为yif ( a=='s' ) {if ( num[i+1][j]==1 ) return 0;//前方障碍物else if ( num[i+1][j]==99 ) {//前方是箱子if ( num[i+2][j]==1 ) return 0;//箱子的前方是障碍物else return 2;//前方为箱子,箱子前方为空}else return 1;//前方为空}if ( a=='w' ) {if ( num[i-1][j]==1 ) return 0;//前方障碍物else if ( num[i-1][j]==99 ) {//前方是箱子if ( num[i-2][j]==1 ) return 0;//箱子的前方是障碍物else return 4;//前方为箱子,箱子前方为空}else return 3;//前方为空}if ( a=='a' ) {if ( num[i][j-1]==1 ) return 0;//前方障碍物else if ( num[i][j-1]==99 ) {//前方是箱子if ( num[i][j-2]==1 ) return 0;//箱子的前方是障碍物else return 6;//前方为箱子,箱子前方为空}else return 5;//前方为空}if ( a=='d' ) {if ( num[i][j+1]==1 ) return 0;//前方障碍物else if ( num[i][j+1]==99 ) {//前方是箱子if ( num[i][j+2]==1 ) return 0;//箱子的前方是障碍物else return 8;//前方为箱子,箱子前方为空}else return 7;//前方为空}
}
void move(int a) {if ( a==1||a==3||a==5||a==7 ) {step_num++;gotoxy(nowx,nowy);putchar(' ');if ( a==1 ){gotoxy(nowx,++nowy);putchar(12);return;}if ( a==3 ){gotoxy(nowx,--nowy);putchar(12);return;}if ( a==5 ){gotoxy(--nowx,nowy);putchar(12);return;}if ( a==7 ){gotoxy(++nowx,nowy);putchar(12);return;}}if ( a==2||a==4||a==6||a==8 ) {step_num++;gotoxy(nowx,nowy);putchar(' ');if ( a==2 ){++nowy;gotoxy(nowx,nowy);putchar(12);num[asky][askx]=50;asky=nowy+1;askx=nowx;num[asky][askx]=99;gotoxy(askx,asky);putchar(30);return;}if ( a==4 ){--nowy;gotoxy(nowx,nowy);putchar(12);num[asky][askx]=50;asky=nowy-1;askx=nowx;num[asky][askx]=99;gotoxy(askx,asky);putchar(30);return;}if ( a==6 ){--nowx;gotoxy(nowx,nowy);putchar(12);num[asky][askx]=50;askx=nowx-1;asky=nowy;num[asky][askx]=99;gotoxy(askx,asky);putchar(30);return;}if ( a==8 ){++nowx;gotoxy(nowx,nowy);putchar(12);num[asky][askx]=50;askx=nowx+1;asky=nowy;num[asky][askx]=99;gotoxy(askx,asky);putchar(30);return;}}
}
void winorfail(void) {int yes=0;if ( askx==74&&asky==out ) {gotoxy(50,15);printf("You are win!");gotoxy(50,16);printf("输入任意键继续游戏");getch();main();}else if (1) {//错误(箱子位置周围有2个地方是障碍物) 应该是 上左 上右 左下 下右if (num[asky-1][askx]==1&&num[asky][askx-1]==1) yes++;//上左if (num[asky-1][askx]==1&&num[asky][askx+1]==1) yes++;//上右if (num[asky][askx-1]==1&&num[asky+1][askx]==1) yes++;//左下if (num[asky+1][askx]==1&&num[asky][askx+1]==1) yes++;//下右if (asky==1||asky==18||askx==1) yes+=1;//在边框}if ( yes>=1 ) {gotoxy(50,15);printf("游戏失败!");gotoxy(50,16);printf("输入任意键继续游戏");getch();main();}
}
这篇关于[C] 大一课设-简易推箱子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!