我的2D游戏的动画问题

时间:2013-03-24 19:03:22

标签: visual-c++

void runGame(){
    readimagefile("PIX\\Landscape.jpg", 0, 0, 800, 800);
    while(1 == 1){
        moveCharacter();
        //moveMonster();
    }
}
void moveCharacter(){

    int move = 0;
    int size = imagesize(0, 0, 800, 800);
    void *background = malloc(size);
    getimage(0, 0, 800, 800, background);

    readimagefile("PIX\\Character\\down_still.gif",x9,y9,x9+50,y9+50);
    thunderClaw();
    //specialAttack();
    //thunderball();
    if(kbhit()){

    if(getch()== 'd'){
        move = 1;
        }
    if(getch()== 'a'){
        move =  3;
        }
    if(getch()== 'w'){
        move = 2;
        }
    if(getch()== 's'){
        move = 4;
        }
    }

    switch (move){

    case 1://left

        readimagefile("PIX\\Character\\left_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 + 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\left_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 + 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\left_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 + 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\left_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 + 5;
        putimage(0, 0, background, COPY_PUT);
        break;

    case 2://up

        readimagefile("PIX\\Character\\up_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 - 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\up_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 - 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\up_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 - 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\up_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 - 5;
        putimage(0, 0, background, COPY_PUT);
        break;

    case 3://right

        readimagefile("PIX\\Character\\right_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 - 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\right_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 - 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\right_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 - 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\right_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        x9 = x9 - 5;
        putimage(0, 0, background, COPY_PUT);
        break;


    case 4://down

        readimagefile("PIX\\Character\\down_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 + 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\down_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 + 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\down_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 + 5;
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Character\\down_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
        y9 = y9 + 5;
        putimage(0, 0, background, COPY_PUT);
        break;

    default:
        readimagefile("PIX\\Character\\down_still.gif", x9, y9, x9+50, y9+50);
        putimage(0, 0, background, COPY_PUT);
    }
    }//end function

void thunderClaw(){
    int mx = mousex();
    int my = mousey();

    int size = imagesize(0, 0, 800, 800);
    void *background = malloc(size);
    getimage(0, 0, 800, 800, background);

    getmouseclick(WM_LBUTTONDOWN, mx, my);
    if(WM_LBUTTONDOWN){
        printf("Left mouse button was clicked!\n");
        #ifdef SOUNDfx
        PlaySound("SOUNDS\\Electricity_Shock_Sound_Effect.wav", NULL, SND_ASYNC);
        #endif
        readimagefile("PIX\\Thunderclaw\\thunderclaw00.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw01.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw0.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw2.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw3.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw4.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
        readimagefile("PIX\\Thunderclaw\\thunderclaw5.gif", mx-25, my-25, mx+25, my+25);Sleep(80);
        putimage(0, 0, background, COPY_PUT);
    }
}

所以这是我目前的代码。我的问题是它一直在想着不断点击鼠标左键。因此,它会不断地将动画放在左上角并减慢角色的移动速度。我尝试了一些令人厌恶的事情来阻止这种情况,但却无法弄明白。谷歌也没有帮助。我只是不知道它是怎么想我不断点击鼠标左键。我相信这是因为if(WM_LBUTTONDOWN)语句。但即便如此,我也尝试过改变它,添加不同的规格,移动它,摆脱它,没有任何解决方法,我需要它。它只是不断地认为左键被点击(即使我摆脱了if()语句它也这样做了!)。

有人可以帮我解决这个问题吗?

谢谢!

P.S。我在Visual Studio 2012和graphics.h库中。

void getmouseclick( int kind, int& x, int& y )
{
    WindowData *pWndData = BGI__GetWindowDataPtr( );
    POINTS where; // POINT (short) to tell where mouse event happened.

    // Check if mouse event is in range
    if ( !MouseKindInRange( kind ) )
        return;

    // Set position variables to mouse location, or to NO_CLICK if no event occured
    if ( MouseKindInRange( kind ) && pWndData->clicks[kind - WM_MOUSEFIRST].size( ) )
    {
        where = pWndData->clicks[kind - WM_MOUSEFIRST].front( );
        pWndData->clicks[kind - WM_MOUSEFIRST].pop( );
        x = where.x;
        y = where.y;
    }
    else
    {
        x = y = NO_CLICK;
    }
}

这是您在getmouseclick()的代码中所指的内容吗?这不是我写的东西,只是我正在使用的东西。

2 个答案:

答案 0 :(得分:1)

WM_LBUTTONDOWN是一个Windows常量,定义为

#define WM_LBUTTONDOWN                  0x0201

所以

 if(WM_LBUTTONDOWN){

将始终评估为真。

你想要的可能就像

if (getmouseclick(WM_LBUTTONDOWN, mx, my) == WM_LBUTTONWDOWN) {

答案 1 :(得分:0)

DWORD result = 0;
result = getmouseclick(WM_LBUTTONDOWN, mx, my);
    if(result == WM_LBUTTONDOWN){ // then left mouse button was clicked and do some actions
}