2048方块的移动问题

我现在想要实现的是这个64号图片在这个4*4格子内的移动,要求是:①移动一次后,第二次移动时不会回到初始位置,②并且在每一行或者每一列最后一个格子时,能回到当时行或列的第一个格子。

img

我的代码如下:

#include <graphics.h>
#include <stdio.h>
#define I 15

int main()
{
initgraph(500, 500, 0);

//创建图片对象
PIMAGE pimg = newimage();
PIMAGE pimg2 = newimage();

getimage(pimg, "resource/background.jpg");
getimage(pimg2, "resource/block.jpg");
putimage(0, 0, pimg);
putimage(15, 15, pimg2);

for(;is_run();delay_fps(60))
{
    while(kbmsg())
    {
        key_msg msg = getkey ();
        if (msg.msg ==key_msg_down){
            if (msg.key == 'D') {
                for(int i=0;i<=120;i++){
                    cleardevice();
                    putimage(0, 0, pimg);
                    putimage(I + i, I, pimg2);    
                    Sleep(1);}

            }
            else if (msg.key == 'S') {
                for(int i=0;i<=120;i++){
                            cleardevice();
                            putimage(0, 0, pimg);
                            putimage(I , I + i, pimg2);    
                            Sleep(1);}
            }
            else if (msg.key == 'A') {
                for(int i=0;i<=120;i++){
                            cleardevice();
                            putimage(0, 0, pimg);
                            putimage(I - i , I, pimg2);    
                            Sleep(1);}
            }
            else if (msg.key == 'W') {
                for(int i=0;i<=120;i++){
                cleardevice();
                putimage(0, 0, pimg);
                putimage(I , I - i , pimg2);    
                Sleep(1);}
            }
        }
    }
    
}    
//    delimage(pimg);

getch();
closegraph();

return 0;

}

我现在的代码只能实现移动一次的效果,求指导,谢谢~
  • 这篇文章:2048源代码 也许能够解决你的问题,你可以看下