C语言设计推箱子小游戏(课程设计)

   日期:2020-07-17     浏览:86    评论:0    
核心提示:C语言设计推箱子小游戏(课程设计)代码如下:#include#include get_keyboard.hint 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}

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;
}



我的资源中有免费资源哦!!!

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服