zoukankan      html  css  js  c++  java
  • c++实现推箱子游戏

    代码实现


      1 #include<stdio.h>
      2 #include<windows.h>
      3 #include<conio.h>
      4 char* playerPosition, *boxPosition[2];
      5 /*初始化地图
      6     map:地图数组
      7     n:    地图的行数
      8     m:    地图的列数*/
      9 void csh(char map[15][15], int n, int m)
     10 {
     11 
     12     for (int i = 0;i < n;i++)
     13         for (int j = 0;j < m;j++)
     14             map[i][j] = ' ';
     15 
     16 }
     17 /*控制角色向上移动
     18     map:地图数组*/
     19 void move(int moveX, int moveY)
     20 {
     21     int i = 0;
     22 
     23     moveY = moveY * 15;
     24     if (*(playerPosition + (moveX + moveY)) != '@')
     25     {
     26         if (*(playerPosition + (moveX + moveY)) == '*' )
     27         {
     28             if (*(playerPosition + 2* (moveX + moveY)) != '@')
     29             {
     30                 if (playerPosition + (moveX + moveY) == boxPosition[0])
     31                     i = 0;
     32                 else
     33                     i = 1;
     34                 *boxPosition[i] = ' ';
     35                 boxPosition[i] += (moveX + moveY);
     36                 *boxPosition[i] = '*';
     37                 *playerPosition = ' ';
     38                 playerPosition += (moveX + moveY);
     39                 *playerPosition = '8';
     40             }
     41         }
     42         else
     43         {
     44             *playerPosition = ' ';
     45             playerPosition += (moveX+moveY);
     46             *playerPosition = '8';
     47         }
     48     }
     49 
     50 }
     51 /*输出结果
     52     map:地图数组
     53     n:    地图的行数
     54     m:    地图的列数*/
     55 void sc(char map[15][15],int n,int m)
     56 {
     57     for (int i = 0;i < n;i++)
     58     {
     59         for (int j = 0;j < m;j++)
     60             printf("%c ", map[i][j]);
     61         printf("
    ");
     62     }
     63 }
     64 /*进行重开、跳关、移动的判断,进入相应的函数,并传参给相应函数
     65     map:地图数组*/
     66 char pd(char map[15][15])
     67 {
     68     char pd;
     69 
     70     pd = _getch();
     71     switch (pd)
     72     { 
     73         case 'w':move(0,-1);break;
     74         case 's':move(0, 1);break;
     75         case 'a':move(-1, 0);break;
     76         case 'd':move(1, 0);break;
     77         case 13:return 1;break;
     78         case 32:return 2;
     79     }
     80     return 0;
     81 }
     82 
     83 /*第一关*/
     84 void diyiguan()
     85 {
     86     int cnt = 0;
     87     char map[15][15],cz;
     88     boolean isEnd = false, isAgain = false, isNext = false;
     89 
     90     csh(map, 8, 6);
     91     //生成地图
     92     map[0][1] = map[0][2] = map[0][0] = map[1][0] = map[2][0] = map[3][0] = map[4][0] = map[5][0] = map[6][0] = map[7][0] = map[7][1] = map[7][2] = map[7][3] = map[6][3] = map[5][3] = map[4][3] = map[4][4] = map[4][5] = map[2][3] = map[2][4] = map[2][5] = map[3][5] = map[1][2] = map[2][2] = '@';
     93     map[6][2] = 'O';
     94     playerPosition = &map[3][1];
     95     *playerPosition = '8';
     96     boxPosition[0] = &map[4][2];
     97     *boxPosition[0] = '*';
     98 
     99 
    100     //开始游戏
    101     sc(map, 8, 6);
    102     printf("第一关");
    103     while (!isEnd && !isAgain && !isNext)
    104     {
    105         cz = pd(map);
    106         cnt++;
    107         if (map[6][2] == ' ')
    108             map[6][2] = 'O';
    109         if (cz == 1) isAgain = true;
    110         if (cz == 2) isNext = true;
    111         system("cls");
    112         sc(map, 8, 6);
    113         printf("已走 % d步
    第二关, 点击回车重开本关,点击空格跳过本关",cnt);
    114         if (map[6][2] == '*')
    115             isEnd = true;        
    116     }
    117     system("cls");
    118     if (cz == 1)
    119         diyiguan();
    120         
    121     
    122 
    123 }
    124 /*第二关*/
    125 void dierguan()
    126 {
    127     int cnt = 0;
    128     char map[15][15], cz;
    129     boolean isEnd = false, isAgain = false, isNext = false;
    130 
    131     csh(map, 8, 6);
    132     //生成地图
    133     for (int i = 0;i <= 5; i++)
    134         map[0][i] = map[7][i]='@';
    135     for (int i = 0;i <= 7; i++)
    136         map[i][0] = map[i][5] = '@';
    137     map[4][4]='@';
    138     map[6][2] = 'O';
    139     map[6][4] = 'O';
    140     playerPosition = &map[3][1];
    141     *playerPosition = '8';
    142     boxPosition[0] = &map[4][2];
    143     *boxPosition[0] = '*';
    144     boxPosition[1] = &map[3][3];
    145     *boxPosition[1] = '*';
    146 
    147 
    148     //开始游戏
    149     sc(map, 8, 6);
    150     printf("第二关");
    151     while (!isEnd && !isAgain && !isNext)
    152     {
    153         cz = pd(map);
    154         cnt++;
    155         if (map[6][2] == ' ')map[6][2] = 'O';
    156         if (map[6][4] == ' ')map[6][4] = 'O';
    157         if (cz == 1) isAgain = true;
    158         if (cz == 2) isNext = true;
    159         system("cls");
    160         sc(map, 8, 6);
    161         printf("已走%d步
    第二关,点击回车重开本关,点击空格跳过本关",cnt);
    162         if (map[6][2] == '*'&&map[6][4]=='*')
    163             isEnd = true;
    164     }
    165     system("cls");
    166     if (cz == 1)
    167         dierguan();
    168 }
    169 
    170 void main()
    171 {    
    172     diyiguan();
    173     dierguan();
    174     printf("
    恭喜你成功通关(这么简单的关卡真的有什么好恭喜的吗?)
    ");
    175 }

    运行截图


  • 相关阅读:
    vue系列【element ui table中render函数加if的使用】
    vue系列【watch的使用方法及immdiate、deep解决组件传值中数据改变视图不更新等常见问题】
    vue系列【element ui 处理图片流及实现多张图片轮播】
    webpack系列【webpack的基本使用及配置项】
    vue系列【element ui 行内三元表达式的使用及实现行内switch按钮加文字】
    vue系列【vue路由$route.go(1)返回使用KeepAlive保持原页面数据不更新】
    vue系列【post 和 get 请求书写及使用】
    vue系列【vue+element 三元表达式多条件实现功能】
    vue系列【sessionStorage的setItem和getItem使用】
    vue系列【vue swiper插件实现图片轮播效果】
  • 原文地址:https://www.cnblogs.com/hbsblog/p/12992362.html
Copyright © 2011-2022 走看看