Results 1 to 4 of 4

Thread: Riddle me this programmers

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default Riddle me this programmers

    This is a part of my pong game (the function that controls the movement of the paddle).
    Initially I had it operating using the vkup & vk_down arrows. Everything worked correctly. Now I would like to add an additional feature that allows the user to also use the mouse POS to control the paddle.

    Here is my function:
    PHP Code:
    void movePaddle()
    {
        
    POINT myMouse;

        
    //NOTE 0x8000 - Determines that the key is pressed instantly (realtime)
        //So there is no delay

        ///////////////////////////////////        Use Keyboard to control paddle

        
    if(GetAsyncKeyState(VK_UP) & 0x8000)
        {
            if(
    player_t.position.top 0)
            {
                
    player_t.position.top += ((player_t.myPos.1) - paddleSpeed);
                
    player_t.position.bottom += ((player_t.myPos.1) - paddleSpeed);
            }
        }

        
    //Move paddle down
        
    else if(GetAsyncKeyState(VK_DOWN)& 0x8000)
        {
            if(
    player_t.position.bottom screenHeight)
            {
                
    player_t.position.top -= ((player_t.myPos.1) - paddleSpeed);
                
    player_t.position.bottom -= ((player_t.myPos.1) - paddleSpeed);
            }
        }

        
    ///////////////////////////////////        Use mouse to control paddle
        ////Move paddle up
        
    if(GetAsyncKeyState(VK_LBUTTON))
        {
            if(
    player_t.position.top && GetCursorPos(&myMouse) < player_t.position.top)
            {
                
    player_t.position.top += ((player_t.myPos.1));
                
    player_t.position.bottom += ((player_t.myPos.1));
            }
        }
        
    //Move paddle down
        
    else if(GetAsyncKeyState(VK_LBUTTON))
        {
            if(
    player_t.position.bottom screenHeight && GetCursorPos(&myMouse) < player_t.position.bottom)
            {
                
    player_t.position.top -= ((-player_t.myPos.1));
                
    player_t.position.bottom -= ((-player_t.myPos.1));
            }
        }

    Currently the keyboard still works fine, however the mouse ONLY controls the paddle upwards. if I comment out the first if() statement for the mouse, then the paddle moves downward fine so I know my code is correct, however if I uncomment and I have both statements available (if() & else if()) it breaks it and it only moves upwards. I tried combining the 2 into 1 and it still doesn't work.

    PHP Code:
    if(GetAsyncKeyState(VK_LBUTTON))
    {
            if(
    player_t.position.top && GetCursorPos(&myMouse) < player_t.position.top)
            {
                
    player_t.position.top += ((player_t.myPos.1));
                
    player_t.position.bottom += ((player_t.myPos.1));
            }

            else if(
    player_t.position.bottom screenHeight && GetCursorPos(&myMouse) < player_t.position.bottom)
            {
                
    player_t.position.top -= ((-player_t.myPos.1));
                
    player_t.position.bottom -= ((-player_t.myPos.1));
            }

    Any ideas?? I'm pretty sure my code is correct.

  2. #2

    Default

    MSDN tells us that GetCursorPos returns a BOOL and not actual coordinates. The coordinates are placed in an out parameter of type POINT.

    So that means that this statement:
    Code:
    if(player_t.position.top > 0 && GetCursorPos(&myMouse) < player_t.position.top)
    Is comparing the result of GetCursorPos (which will most likely be TRUE) to player_t.position.top (which is part of a RECT struct and is of type LONG). This comparison will not work as expected:

    A BOOL set to true may technically be anything BUT zero. Most likely it is going to be 0xFFFFFFFF which is quite a big positive(if interpreted as unsigned int) or negative (if interpreted as a signed int) number.

    I suggest using the coordinates it places into the struct you give it (myMouse in this case) in the check instead of the returned variable.

  3. #3
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Thanks for the info. You're right, I was under the impression that GetCursorPos() returned the value for x,y coords, but it is just a bool ha. Anywho, here is what I currently have.

    PHP Code:
    if(GetAsyncKeyState(VK_LBUTTON))
        {
            
            if(
    player_t.position.top && myMouse.player_t.position.top)
            {
                
    player_t.position.top += ((player_t.myPos.1));
                
    player_t.position.bottom += ((player_t.myPos.1));
            }
            
            else if(
    player_t.position.bottom screenHeight && myMouse.player_t.position.bottom)
            {
                
    player_t.position.top -= ((-player_t.myPos.1));
                
    player_t.position.bottom -= ((-player_t.myPos.1));
            }
            
        } 
    When I compile, the paddle only moves upward & not downward, however if I comment out this part :
    PHP Code:
    if(player_t.position.top && myMouse.player_t.position.top)
            {
                
    player_t.position.top += ((player_t.myPos.1));
                
    player_t.position.bottom += ((player_t.myPos.1));
            } 
    and change the else if() to if() then it moves the paddle down fine.

    Bah..... brain fart!!!

    Edit:
    If the full source is required, it can be grabbed here: http://x-null.net/myPong/myPong4.zip (latest version)

  4. #4
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Got it sorted. Here is my end result:

    PHP Code:
    if(GetAsyncKeyState(VK_LBUTTON))
    {
            
    GetCursorPos( &myMouse );
            
    ScreenToClienthWnd, &myMouse);

            if(
    player_t.position.top && (myMouse.player_t.position.top))
            {
                
    player_t.position.top += (player_t.myPos.-(10));
                
    player_t.position.bottom += (player_t.myPos.-(10));
            }
            
            else if(
    player_t.position.bottom screenHeight && (myMouse.player_t.position.bottom))
            {
                
    player_t.position.top -= (player_t.myPos.-(10));
                
    player_t.position.bottom -= (player_t.myPos.-(10));
            }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •