推箱子(c++)

#include<iostream>
#include<conio.h>
#include<windows.h>
#include<iomanip>
using namespace std;
#define R 10//行
#define C 10//列 
void Game_Menu();//游戏菜单
void Game_description();//操作介绍
int DrawMap();//绘制地图 
void Move();//移动 
int finish();//判断是否结束 
void setmap(int n);//跳转地图 
void color(int m);//设置文本颜色
bool flag=true;//可以随时退出 
/*
    1.墙
    3.目的地 
    4.箱子
    5.人
    0.空地 
*/
int map[R][C]={0};
//地图1: 
int map1[10][10]={
    {0,0,1,1,1,0,0,0},
    {0,0,1,3,1,0,0,0},
    {1,1,1,4,1,1,1,1},
    {1,0,0,0,0,4,3,1},
    {1,3,4,0,0,1,1,1},
    {1,1,1,5,4,1,0,0},
    {0,0,0,1,3,1,0,0},
    {0,0,0,1,1,1,0,0},
};
//地图2:
int map2[10][10]={
    {1,1,1,1,1,0,0,0,0,0},
    {1,5,0,0,1,0,0,0,0,0},
    {1,0,4,4,1,0,1,1,1,0},
    {1,0,4,0,1,0,1,3,1,0},
    {1,1,1,0,1,1,1,3,1,0},
    {0,1,1,0,0,0,0,3,1,0},
    {0,1,0,0,0,1,0,0,1,0},
    {0,1,0,0,0,1,1,1,1,0},
    {0,1,1,1,1,1,0,0,0,0},
}; 
//地图3:
int map3[10][10]={
    {0,0,0,1,1,1,1,1,1,1},
    {0,0,1,1,0,0,1,0,5,1},
    {0,0,1,0,0,0,1,0,0,1},
    {0,0,1,4,0,4,0,4,0,1},
    {0,0,1,0,4,1,1,0,0,1},
    {1,1,1,0,4,0,1,0,1,1},
    {1,3,3,3,3,3,0,0,1,0},
    {1,1,1,1,1,1,1,1,1,0},
};
int map4[10][10]={
    {1,1,1,1,1,1,1,1,1,1},//1
    {1,0,0,0,0,0,0,0,1,1},//2
    {1,0,1,0,0,1,0,1,1,1},//3
    {1,0,0,0,0,0,0,0,0,1},//4
    {1,1,1,1,1,5,1,1,1,1},//5
    {1,1,1,1,1,0,0,0,0,1},//6
    {1,4,4,4,0,4,0,4,4,1},//7
    {1,0,0,4,0,0,0,0,4,1},//8
    {1,3,3,3,3,3,3,3,3,1},//9
    {1,1,1,1,1,1,1,1,1,1},//10
};
/*1.墙
    3.目的地 
    4.箱子
    5.人
    0.空地
*/
int pass=1;//初始第一关 
void full_screen()
{   
    HWND hwnd = GetForegroundWindow();
    int cx = GetSystemMetrics(SM_CXSCREEN);            /* 屏幕宽度 像素 */
    int cy = GetSystemMetrics(SM_CYSCREEN);            /* 屏幕高度 像素 */

    LONG l_WinStyle = GetWindowLong(hwnd,GWL_STYLE);   /* 获取窗口信息 */
    /* 设置窗口信息 最大化 取消标题栏及边框 */
    SetWindowLong(hwnd,GWL_STYLE,(l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);

    SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}
int main(){
    char c,d;
    full_screen();
    Game_Menu();
    Game_description();
    c=getch();
    setmap(pass);
    switch(c){
        case 'Q': 
        case 'q':
        return 0; 
        case 'S':
        case 's':
        while(flag){
            system("cls");
            DrawMap();
            Move();
            if(finish()){
                if(pass<=4){
                    system("cls");
                    DrawMap();
                    printf("游戏胜利!\n");
                    system("pause");
                    pass++;
                    setmap(pass); 
                }
                else{
                    cout<<"你获得了最终胜利!!!";
                    system("pause");
                    return 0;
                }
            }
        }
        break;
    }
    return 0;
} 

void Game_Menu(){
    system("cls");
    cout<<"|---------------------|\n";
    cout<<"|                     |\n";
    cout<<"|      经典小游戏     |\n";
    cout<<"|        推箱子       |\n";
    cout<<"|    按S或者s键开始   |\n";
    cout<<"|    按Q或者q键退出   |\n";
    cout<<"|                     |\n";
    cout<<"|---------------------|\n";
    Sleep(500);
}
void Game_description(){
    cout<<"\\*********************/\n";
    cout<<"*                     *\n";
    cout<<"*      操作提示       *\n";
    cout<<"*   操作上移:W w ↑  *\n";
    cout<<"*   操作下移:S s ↓  *\n";
    cout<<"*   操作左移:A a ←  *\n";
    cout<<"*   操作右移:D d →  *\n";
    cout<<"*                     *\n";
    cout<<"*   退  出:Q q       *\n";
    cout<<"*                     *\n";
    cout<<"|---------------------|\n";
    _getch();
}

int DrawMap(){
    cout<<"\n关卡:"<<pass<<endl;
    for(int i=0;i<R;i++){
        for(int j=0;j<C;j++){
            switch(map[i][j]){
                case 0:
                    color(0xF);
                    cout<<"  ";
                    break;
                case 1:
                    color(8);
                    cout<<"■";
                    break; 
                case 3:
                    color(0xE);
                    cout<<"☆";
                    break;
                case 4:
                    color(4);
                    cout<<"□";
                    break;
                case 5:
                    color(3);
                    cout<<"♀";
                    break;
                case 7:
                    color(6);
                    cout<<"★";
                    break;
                case 8:
                    color(6);
                    cout<<"♀";
                    break;
                default:    
                break;
            }
        }
        cout<<endl;
    } 
    cout<<"\n重新开始本关请按p\n";
    return 0;
} 
void Move(){//判断移动 
    int r,c;
    for(int i=0;i<R;i++){//判断当前坐标 
        for(int j=0;j<C;j++){
            if(map[i][j]==5||map[i][j]==8){
                r=i;
                c=j;
            }
        }
    }
    cout<<"您当前的坐标为:("<<r<<","<<c<<")"<<endl;
    int ch;
    ch=_getch();
    switch(ch){
        case 'W':
        case 'w':
        case 72: 
        if(map[r-1][c]==0||map[r-1][c]==3){
            map[r-1][c]+=5;
            map[r][c]-=5;
        }
        else if(map[r-1][c]==4||map[r-1][c]==7){
            if(map[r-2][c]==0||map[r-2][c]==3){
                map[r-2][c]+=4;
                map[r-1][c]+=1;
                map[r][c]-=5;
            }
        }
        break;
        case 'S':
        case 's':
        case 80:
        if(map[r+1][c]==0||map[r+1][c]==3){
            map[r+1][c]+=5;
            map[r][c]-=5;
        }
        else if(map[r+1][c]==4||map[r+1][c]==7){
            if(map[r+2][c]==0||map[r+2][c]==3){
                map[r+2][c]+=4;
                map[r+1][c]+=1;
                map[r][c]-=5;
            }
        }
        break;
        case 'A':
        case 'a':
        case 75:
        if(map[r][c-1]==0||map[r][c-1]==3){
            map[r][c-1]+=5;
            map[r][c]-=5;
        }
        else if(map[r][c-1]==4||map[r][c-1]==7){
            if(map[r][c-2]==0||map[r][c-2]==3){
                map[r][c-2]+=4;
                map[r][c-1]+=1;
                map[r][c]-=5;
            }
        }
        break;
        case 'D':
        case 'd':
        case 77:
        if(map[r][c+1]==0||map[r][c+1]==3){
            map[r][c+1]+=5;
            map[r][c]-=5;
        }
        else if(map[r][c+1]==4||map[r][c+1]==7){
            if(map[r][c+2]==0||map[r][c+2]==3){
                map[r][c+2]+=4;
                map[r][c+1]+=1;
                map[r][c]-=5;
            }
        }
        break;
        case 'Q':
        case 'q':
            flag=false;
            break;
        case 'p':
            setmap(pass); 
            break;  
        default:
            break;
    }
}
int finish(){//判断游戏是否结束 
    for(int i=0;i<R;i++){
        for(int j=0;j<C;j++){
            if(map[i][j]==4){
                return 0;
            }
        }
    }
    return 1;
}
void setmap(int n){//复制地图 
    if(n==1){
        memcpy(map,map1,sizeof(map1));
    }
    if(n==2){
        memcpy(map,map2,sizeof(map2));
    }
    if(n==3){
        memcpy(map,map3,sizeof(map3));
    }
    if(n==4){
        memcpy(map,map4,sizeof(map4));
    }
}
void color(int m){//颜色设置 
    HANDLE consolehend;
    consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(consolehend,m);
    return ;
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值