C语言设计推箱子小游戏(课程设计)
代码如下:
#include<stdio.h>
#include "get_keyboard.h"
int boards[7][8] =
{
{0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,1},
{1,3,0,1,1,2,0,1},
{1,0,3,3,2,0,0,1},
{1,0,0,1,2,0,0,1},
{1,0,0,4,0,1,1,1},
{1,1,1,1,1,0,0,0}
};
//记录小老鼠的位置 控制移动 改变小老鼠在地图中的位置
int row = 0;
int col = 0;
int cnt = 0;
int pos = 0;
void print_boards(){
int i,j;
for(i = 0;i<7;i++){
for(j = 0;j<8;j++){
switch(boards[i][j]){
case 0:printf(" ");break;
case 1:printf("#");break;
case 2:printf("@");break;
case 3:printf("0");break;
case 4:printf("&");break;
}
}
printf("\n");
}
}
void left(){
switch(boards[row][col-1]){
case 0:boards[row][col-1] = 4;
posi();
col -= 1;
pos = 0;
break;
case 1:break;
case 2:if(boards[row][col-2]==0){
boards[row][col-1] = 4;
boards[row][col-2] = 2;
posi();
pos = 0;
col -= 1;
}else if(boards[row][col-2]==3){
boards[row][col-1] = 4;
boards[row][col-2] = 0;
posi();
pos = 0;
col -= 1;
cnt++;
}
break;
case 3:boards[row][col-1] = 4;
posi();
pos = 3;
col -= 1;
break;
}
}
void right(){
switch(boards[row][col+1]){
case 0:boards[row][col+1] = 4;
posi();
col += 1;
pos = 0;
break;
case 1:break;
case 2:if(boards[row][col+2]==0){
boards[row][col+1] = 4;
boards[row][col+2] = 2;
posi();
col += 1;
pos = 0;
}else if(boards[row][col+2]==3){
boards[row][col+1] = 4;
boards[row][col+2] = 0;
posi();
col += 1;
pos = 0;
cnt++;
}
break;
case 3:boards[row][col+1] = 4;
posi();
col += 1;
pos = 3;
break;
}
}
void up(){
switch(boards[row-1][col]){
case 0:boards[row-1][col] = 4;
posi();
row -= 1;
pos = 0;
break;
case 1:break;
case 2:if(boards[row-2][col]==0){
boards[row-1][col] = 4;
boards[row-2][col] = 2;
posi();
row -= 1;
pos = 0;
}else if(boards[row-2][col]==3){
boards[row-1][col] = 4;
boards[row-2][col] = 0;
posi();
row -= 1;
cnt++;
pos = 0;
}
break;
case 3:boards[row-1][col] = 4;
posi();
row -= 1;
pos = 3;
break;
}
}
void down(){
switch(boards[row+1][col]){
case 0:boards[row+1][col] = 4;
posi();
row += 1;
pos = 0;
break;
case 1:break;
case 2:if(boards[row+2][col] == 0){
boards[row+1][col] = 4;
boards[row+2][col] = 2;
posi();
row += 1;
pos = 0;
}else if(boards[row+2][col]==3){
boards[row+1][col] = 4;
boards[row+2][col] = 0;
posi();
row += 1;
pos = 0;
cnt++;
}
break;
case 3:boards[row+1][col] = 4;
posi();
row += 1;
pos = 3;
break;
}
}
void posi(){
switch(pos){
case 0:boards[row][col] = 0;break;
case 1:boards[row][col] = 1;break;
case 2:boards[row][col] = 2;break;
case 3:boards[row][col] = 3;break;
}
return;
}
int main(){
int i,j;
for(i = 0;i<7;i++){
for(j = 0;j<8;j++){
if(boards[i][j] == 4){
row = i;
col = j;
break;
}
}
}
print_boards();
while(cnt!=3){
int move = get_keyboard();
system("clear");
switch(move){
case KEY_UP:up();print_boards();break;
case KEY_DOWN:down();print_boards();break;
case KEY_RIGHT:right();print_boards();break;
case KEY_LEFT:left();print_boards();break;
case KEY_ENTER:;print_boards();break;
}
}
return 0;
}
我的资源中有免费资源哦!!!