c语言课程设计经典例题推箱子

更新时间:2024-01-23 03:57:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

贪吃蛇源代码完美运行,结尾附图 #include #include #include #include

int printMap(int map[][16]);

void moveBox(int map[][16],int x,int y,int select,int map2[][16]); int insert(int map[][16],int map2[][16],int sum); void UI(); void img1(); void img2(); void img3(); void img4(); void img5(); void img6();

void color(int b) //颜色函数 {

HANDLE hConsole =

GetStdHandle((STD_OUTPUT_HANDLE)) ; SetConsoleTextAttribute(hConsole,b) ; }

void img1() {

int map2[14][16]; int i,j,sum=0;

int map[14][16]={

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,3,1,0,0,0,0,0,0,0},

{0,0,0,1,1,1,1,4,1,0,0,0,0,0,0,0}, {0,0,0,1,3,2,4,6,1,1,1,0,0,0,0,0}, {0,0,0,1,1,1,4,2,4,3,1,0,0,0,0,0}, {0,0,0,0,0,1,2,1,1,1,1,0,0,0,0,0}, {0,0,0,0,0,1,3,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };

for(i=0;i<14;i++)

for(j=0;j<16;j++){

map2[i][j]=map[i][j];

if(map[i][j] == 3 || map[i][j] == 5) sum++; }

insert(map,map2,sum); img2(); }

void img2() {

int map2[14][16]; int i,j,sum=0;

int map[14][16]={

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0}, {0,0,0,0,1,2,6,3,3,3,1,0,0,0,0,0}, {0,0,0,0,1,2,2,2,2,2,1,1,0,0,0,0}, {0,0,0,1,1,1,4,2,2,2,2,1,0,0,0,0}, {0,0,0,1,2,2,2,2,2,2,2,1,0,0,0,0}, {0,0,0,1,2,4,2,1,4,1,2,1,0,0,0,0}, {0,0,0,1,2,2,2,1,2,2,2,1,0,0,0,0},

{0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };

for(i=0;i<14;i++)

for(j=0;j<16;j++){

map2[i][j]=map[i][j];

if(map[i][j] == 3 || map[i][j] == 5) sum++; }

insert(map,map2,sum); img3(); }

void img3() {

int map2[14][16]; int i,j,sum=0;

int map[14][16]={

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,1,2,2,6,1,0,0,0,0,0,0,0}, {0,0,0,0,1,2,4,4,1,0,1,1,1,0,0,0}, {0,0,0,0,1,2,4,2,1,0,1,3,1,0,0,0}, {0,0,0,0,1,1,1,2,1,1,1,3,1,0,0,0}, {0,0,0,0,0,1,1,2,2,2,2,3,1,0,0,0}, {0,0,0,0,0,1,2,2,2,1,2,2,1,0,0,0}, {0,0,0,0,0,1,2,2,2,1,1,1,1,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };

for(i=0;i<14;i++)

for(j=0;j<16;j++){

map2[i][j]=map[i][j];

if(map[i][j] == 3 || map[i][j] == 5) sum++; }

insert(map,map2,sum); img4(); }

void img4() {

int map2[14][16]; int i,j,sum=0;

int map[14][16]={

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,2,2,1,0,0,0,0,0,0}, {0,0,0,0,0,1,1,2,2,1,0,0,0,0,0,0}, {0,0,0,0,0,1,6,4,2,1,0,0,0,0,0,0}, {0,0,0,0,0,1,1,4,2,1,1,0,0,0,0,0}, {0,0,0,0,0,1,1,2,4,2,1,0,0,0,0,0}, {0,0,0,0,0,1,3,4,2,2,1,0,0,0,0,0}, {0,0,0,0,0,1,3,3,5,3,1,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };

for(i=0;i<14;i++)

for(j=0;j<16;j++){

map2[i][j]=map[i][j];

if(map[i][j] == 3 || map[i][j] == 5) sum++;

}

insert(map,map2,sum); img5(); }

void img5() {

int map2[14][16]; int i,j,sum=0;

int map[14][16]={

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0}, {0,0,1,1,1,1,2,2,2,2,2,1,0,0,0,0}, {0,0,1,2,2,2,3,1,1,1,2,1,0,0,0,0}, {0,0,1,2,1,2,1,2,2,2,2,1,1,0,0,0}, {0,0,1,2,1,2,4,2,4,1,3,2,1,0,0,0}, {0,0,1,2,1,2,2,5,2,2,1,2,1,0,0,0}, {0,0,1,2,3,1,4,2,4,2,1,2,1,0,0,0}, {0,0,1,1,2,2,2,2,1,2,1,2,1,1,1,0}, {0,0,0,1,2,1,1,1,3,2,2,2,2,6,1,0}, {0,0,0,1,2,2,2,2,2,1,1,2,2,2,1,0}, {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };

for(i=0;i<14;i++)

for(j=0;j<16;j++){

map2[i][j]=map[i][j];

if(map[i][j] == 3 || map[i][j] == 5) sum++; }

insert(map,map2,sum); img5(); }

void img6() {

int map2[14][16]; int i,j,sum=0;

int map[14][16]={

{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,1,3,2,3,2,3,1,0,0,0,0}, {0,0,0,0,0,1,2,4,4,4,2,1,0,0,0,0}, {0,0,0,0,0,1,3,4,6,4,3,1,0,0,0,0}, {0,0,0,0,0,1,2,4,4,4,2,1,0,0,0,0}, {0,0,0,0,0,1,3,2,3,2,3,1,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };

for(i=0;i<14;i++)

for(j=0;j<16;j++){

map2[i][j]=map[i][j];

if(map[i][j] == 3 || map[i][j] == 5) sum++; }

insert(map,map2,sum); }

int insert(int map[][16],int map2[][16],int sum) {

int i,j;

int x,y; //人的位置

char op; //所按下的方向键

int num = 0; //还剩下几个箱子没有推向目的地

while(1){

for(i=0;i<14;i++){

for(j=0;j<16;j++)

if(map[i][j] == 6) goto found; found:

} x = i; y = j;

system(\

num = printMap(map); if(num == sum){

printf(\你通关了....\\n\ return 0; } else{

op = getch();

if((op == 'Q') || (op =='q')){ printf(\退出....\\n\ return 2; }

op = getch(); }

switch(op) {

case 72: //上箭头

moveBox(map,x,y,1,map2); break;

case 75: //左箭头

moveBox(map,x,y,2,map2); break;

case 77: //右箭头

moveBox(map,x,y,3,map2);

break;

case 80: //下箭头

moveBox(map,x,y,4,map2); break; default:

break; } } }

int printMap(int map[][16]) {

int i,j;

int count = 0;

for(i=0;i<14;i++){

for(j=0;j<16;j++){

switch(map[i][j]){

case 0: printf(\ \ break;

case 1: color(13); printf(\■\ break;

case 2: 地

printf(\ \ break;

case 3: color(11); printf(\×\ break;

case 4: color(8);

//空地 //墙 //内部空//目的地//箱子 printf(\◎\ break;

case 5: //箱子推到目的地后显示

count++; color(9); printf(\☆\ break;

case 6: //小人 color(10); printf(\♀\ break;

} }

printf(\ }

printf(\

printf(\按方向键操作游戏,按Q退出!\\n\ return count; }

void moveBox(int map[][16],int x,int y,int select,int map2[][16]) {

int x1,y1; //下个位置的坐标 int x2,y2; //下下个位置的坐标 switch(select) {

case 1: //向上移动 x1 = x - 1; y1 = y; x2 = x - 2; y2 = y; break;

case 2: //向左移动

x1 = x; y1 = y - 1; x2 = x; y2 = y - 2; break;

case 3: //向右移动 x1 = x; y1 = y + 1; x2 = x; y2 = y + 2; break;

case 4: //向下移动 x1 = x + 1; y1 = y; x2 = x + 2; y2 = y; break; default:

break; }

//operate map

switch(map[x1][y1]){

case 1: //墙,不动 break;

case 2: if(map2[x][y] == 3){//空格,分情况变换

map[x1][y1] = 6; map[x][y] = 3; } else{

map[x1][y1] = 6; map[x][y] = 2; } break;

case 3:

if(map2[x][y] == 3){//目的地,分情况变换 map[x1][y1] = 6; map[x][y] = 3; } else{

map[x1][y1] = 6; case 4: 6)

map2[x][y] == 6)

case 5:

map[x][y] = 2; }

break;

if(map[x2][y2] == 3){ map[x2][y2] = 5; map[x1][y1] = 6;

if(map2[x][y] == 2 || map2[x][y] == map[x][y] = 2; else if(map2[x][y] == 3) map[x][y] = 3; else

map[x][y] = 2; }

else if(map[x2][y2] == 2 || map2[x][y] == 6){ map[x2][y2] = 4; map[x1][y1] = 6; if(map2[x][y] == 2 || map[x][y] = 2; else if(map2[x][y] == 3) map[x][y] = 3; else

map[x][y] = 2; }

break; if((map2[x][y] == 2 || map2[x][y] == 6) && map2[x2][y2] == 3) {

map[x2][y2] = 5; map[x1][y1] = 6; map[x][y] = 2; }

else if(map2[x][y] == 3 && map[x2][y2] == 3){

map[x2][y2] = 5; map[x1][y1] = 6; map[x][y] = 3; }

else if(map2[x][y] == 5 && map[x2][y2] == 3){

map[x2][y2] = 5; map[x1][y1] = 6; map[x][y] = 3; map2[x][y] = 3; }

else if((map2[x][y] == 2 || map2[x][y] == 6) && map2[x2][y2] == 2){

map[x2][y2] = 4; map[x1][y1] = 6; map[x][y] = 2; map2[x1][y1] = 3; }

//箱子推到目的地后 break; default:

break; } }

void UI()

{

char ch,num; color(11);

printf(\开始--------------|\\n\ printf(\选关--------------|\\n\ printf(\退出--------------|\\n\ ch=getch(); if(ch == '1')

img1(); if(ch == '2'){

printf(\请输入关数(1--6):\ num=getch(); switch(num){

case '1' : img1();break; case '2' : img2();break; case '3' : img3();break; case '4' : img4();break; case '5' : img5();break; case '6' : img6();break; default : printf(\error!\\n\ } }

if(ch == '3'){

system(\ exit(0); } }

void main() {

UI(); }

本文来源:https://www.bwwdw.com/article/tt7o.html

Top