EGE推箱子如何移动

为什么移动之后,原来的派大星还在那里,而且只能移动一次,再按就没有反应了。

img

#include <iostream>
#include "graphics.h"
#include <cstdio>
#include <ctime>
#define WIDTH 500
#define HEIGHT 500
#define LEFT 0
#define TOP 0
#define UNIT 50
#define M 10
#define N 10

void drawmap(int Map[][N], PIMAGE pi[], ege_rect r[][N])
{
    int i, j;
    setcolor(BLACK);
    for (i = 0;i < M;i++)
    {
        for (j = 0;j < N;j++)
        {
            r[i][j].x = LEFT + j * UNIT;
            r[i][j].y = TOP + i * UNIT;
            r[i][j].w = UNIT;
            r[i][j].h = UNIT;
            if (Map[i][j] == 0)
                rectangle(r[i][j].x, r[i][j].y, r[i][j].x + r[i][j].w, r[i][j].y + r[i][j].h);
            else
                putimage(r[i][j].x, r[i][j].y, r[i][j].w, r[i][j].h, pi[Map[i][j] - 1], 0, 0);
        }
    }

}

void movestep(int Map[][N], int ay, int ax, int px, int py)
{
    int i, j, num;
    //    for(i=0;i<M;i++)
    //    {
    //        for(j=0;j<N;j++)
    //        {
    //            px=i;
    //            py=j;
    //        }
    //    }
        //1、人的前面是空地
    if (Map[py + ay][px + ax] == 0)
    {
        Map[py + ay][px + ax] = 4;//空地改为人
        //原来人的位置还原
        Map[py][px] = 0;

        py += ay;
        px += ax;
    }
    //2、人的前面是目的地
  /* else if(Map[py+ay][px+ax] == 2)
    {

            Map[py][px] = 0;


        py += ay;
        px += ax;
    }

    */
    //人的前面是箱子
    else if (Map[py + ay][px + ax] == 3)
    {
        //a、箱子的前面是空地
        if (Map[py + 2 * ay][px + 2 * ax] == 0)
        {
            Map[py + 2 * ay][px + 2 * ax] = 3;
            //原来人的位置还原
            Map[py + ay][px + ax] = 4;
            Map[py][px] = 0;

        }

        //b、箱子的前面是目的地
        else if (Map[py + 2 * ay][px + ax * 2] == 2)
        {
            Map[py + ay][px + ax] = 4;
            Map[py + 2 * ay][px + ax * 2] = 3;
            //原来人的位置还原
            Map[py][px] = 0;


        }
        py += ay;
        px += ax;
    }

}




int main(int argc, char** argv) {
    initgraph(WIDTH, HEIGHT);
    //绘制背景

    ege_rect r[M][N];

    int ax, ay, i, px, py;
    
    px = 5, py = 4;        //人的初始位置 
    
    PIMAGE pi[4];
    for (i = 0;i < 4;i++)
    {
        pi[i] = newimage();
    }
    getimage(pi[0], "qiang.png");
    getimage(pi[1], "star.png");
    getimage(pi[2], "box.png");
    getimage(pi[3], "pai.png");

    int Map[M][N] =
    {
        {0,0,0,1,1,1,0,0,0,0},
        {0,0,0,1,2,1,0,0,0,0},
        {0,0,0,1,0,1,1,1,1,0},
        {0,1,1,1,3,3,0,2,1,0},
        {0,1,2,0,3,4,1,1,1,0},
        {0,1,1,1,1,3,1,0,0,0},
        {0,0,0,0,1,0,1,0,0,0},
        {0,0,0,0,1,2,1,0,0,0},
        {0,0,0,0,1,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
    };
    drawmap(Map, pi, r);


    int flag;
    for (;is_run();delay_fps(60))
    {
        //键盘操作 
        while(kbhit())//如果有键盘字符输入
        {
            key_msg msg = getkey();
            //           int ch = getch();
            //            if(ch!=key_up&&ch!=key_left&&ch!=key_down&&ch!=key_right)
            //            {
            //               ch = 1;
            //                continue;
            //            }
            switch (msg.key)
            {
            case key_up:
                movestep(Map, -1, 0, px, py);
                break;
            case key_left:
                movestep(Map, 0, -1, px, py);
                break;
            case key_down:
                movestep(Map, 1, 0, px, py);
                break;
            case key_right:
                movestep(Map, 0, 1, px, py);
                break;
            }
            drawmap(Map, pi, r);
        }
    }

    getch();
    closegraph();
    return 0;
}