用C++实现推箱子(小人和推着箱子能过地板版)

简介: 用C++实现推箱子(小人和推着箱子能过地板版)

源代码:

#include <iostream>
#include <Windows.h>
#include <graphics.h>
#include <conio.h>
#include "box_man.h"
void changeMap(struct _POS *pos,enum _RES prop)
{
  map[pos->x][pos->y]=prop;
  putimage(START_X+pos->y*RATIO,START_Y+pos->x*RATIO,&pic[prop]);
}
struct _POS man;
void gameCotrol(enum _DIRECT direct)
{
  struct _POS next_pos=man;
  struct _POS next_next_pos=man;
  switch(direct)
  {
    case UP:
      next_pos.x--;
      next_next_pos.x-=2;
      break;
    case DOWN:
      next_pos.x++;
      next_next_pos.x+=2;
      break;
    case LEFT:
      next_pos.y--;
      next_next_pos.y-=2;
      break;
    case RIGHT:
      next_pos.y++;
      next_next_pos.y+=2;
      break;
  }
  if(isValid(next_pos)&&map[next_pos.x][next_pos.y]==FLOOR) //小人前是地板
  {
    if(hold==0)
    {
      changeMap(&next_pos,MAN);
      changeMap(&man,FLOOR);
      man=next_pos;
    }else
    {
      changeMap(&next_pos,MAN);
      changeMap(&man,BOX_DES);
      man=next_pos;
      hold=0;
    }
  }else if(isValid(next_pos)&&map[next_pos.x][next_pos.y]==BOX_DES) //小人前是箱子目的地
  {
    changeMap(&next_pos,MAN);
    changeMap(&man,FLOOR);
    man=next_pos;
    hold=1;
  }else if(isValid(next_pos)&&map[next_pos.x][next_pos.y]==BOX)   //小人前是箱子
  {
    if(isValid(next_next_pos)&&map[next_next_pos.x][next_next_pos.y]==BOX_DES)  
    {
      changeMap(&next_next_pos,HIT);
      changeMap(&next_pos,MAN);
      changeMap(&man,FLOOR);
      man=next_pos;
    }else if(isValid(next_next_pos)&&map[next_next_pos.x][next_next_pos.y]==FLOOR)    //箱子前面是地板
    {
      if(hold==1)
      {
        changeMap(&next_next_pos,BOX);
        changeMap(&next_pos,MAN);
        changeMap(&man,BOX_DES);
        man=next_pos;
        hold=0;
      }else
      {
        changeMap(&next_next_pos,BOX);
        changeMap(&next_pos,MAN);
        changeMap(&man,FLOOR);
        man=next_pos;
      }
    }
  }else if(isValid(next_next_pos)&&map[next_pos.x][next_pos.y]==HIT)    //小人前方箱子和目的地重合
    {
        changeMap(&next_next_pos,BOX);
        changeMap(&next_pos,MAN);
        changeMap(&man,FLOOR);
        man=next_pos;
        hold=1;
    }
}
int main(void)
{
  IMAGE bk_ground;
  initgraph(WEIGHT,HEIGHT);
  loadimage(&bk_ground,L"blackground.bmp",WEIGHT,HEIGHT,true);
  putimage(0,0,&bk_ground);
  loadimage(&pic[WALL],L"wall.bmp",RATIO,RATIO,true);
  loadimage(&pic[FLOOR],L"floor.bmp",RATIO,RATIO,true);
  loadimage(&pic[BOX_DES],L"des.bmp",RATIO,RATIO,true);
  loadimage(&pic[MAN],L"man.bmp",RATIO,RATIO,true);
  loadimage(&pic[BOX],L"box.bmp",RATIO,RATIO,true);
  loadimage(&pic[HIT],L"box.bmp",RATIO,RATIO,true);
  for(int i=0;i<LINE;i++)
  {
    for(int j=0;j<COLUMN;j++)
    {
      if(map[i][j]==MAN)
      {
        man.x=i;
        man.y=j;
      }
      putimage(START_X+j*RATIO,START_Y+i*RATIO,&pic[map[i][j]]);
    }
  }
  bool quit=true;
  do
  {
    if(_kbhit())
    {
      char ch=_getch();
      if(ch==KEY_UP)
      {
        gameCotrol(UP);
      }else if(ch==KEY_DOWN)
      {
        gameCotrol(DOWN);
      }else if(ch==KEY_LEFT)
      {
        gameCotrol(LEFT);
      }else if(ch==KEY_RIGHT)
      {
        gameCotrol(RIGHT);
      }else if(ch==KEY_QUIT)
      {
        quit=false;
      }
    }
  }while(quit==true);
  closegraph();
  system("pause");
  return 0;
}

头文件:

#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'
#define START_X 100   //x轴偏移
#define START_Y 150   //y轴偏移
#define LINE 9    
#define COLUMN 12
#define WEIGHT 960    //舞台长
#define HEIGHT 768    //舞台宽
#define RATIO 61
#define isValid(pos) pos.x>=0&&pos.x<LINE&&pos.y>=0&&pos.y<COLUMN   //判断小人在地图行走后是否有效
using namespace std;
int hold=0;
enum _RES
{
  WALL,
  FLOOR,
  BOX_DES,
  MAN,
  BOX,
  HIT,
  ALL
};
int map[LINE][COLUMN]={ 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0},
    { 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 0, 0},
    { 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0},
    { 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0},
    { 0, 1, 1, 1, 0, 3, 1, 1, 1, 4, 1, 0},
    { 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0},
    { 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0},
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
IMAGE pic[ALL];   //资源图片
enum _DIRECT
{
  UP,
  DOWN,
  LEFT,
  RIGHT
};
struct _POS   //小人坐标
{
  int x;
  int y;
};
相关文章
|
7月前
|
算法 测试技术 C++
C++二分算法习题:判断是否是完全平方数[容易]和排列箱子[容易]
C++二分算法习题:判断是否是完全平方数[容易]和排列箱子[容易]
|
5月前
|
机器学习/深度学习 算法 测试技术
C++前缀和算法的应用:从仓库到码头运输箱子原理、源码、测试用例
C++前缀和算法的应用:从仓库到码头运输箱子原理、源码、测试用例
|
存储 C++
【数据结构】C++用链表实现一个箱子排序附源代码详解
【数据结构】C++用链表实现一个箱子排序附源代码详解
116 0
【数据结构】C++用链表实现一个箱子排序附源代码详解
|
8天前
|
存储 编译器 C++
c++的学习之路:6、类和对象(2)
c++的学习之路:6、类和对象(2)
22 0
|
8天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
23 0
|
8天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
21 0
|
1天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
1天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
1天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
1天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现