0

I just implemented my paddle movement function:

// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400

// Looks of the paddle
#define PADDLE_WIDTH  80
#define PADDLE_HEIGHT 10
#define PADDLE_COLOR "ORANGE"

// Initial position of the paddle
#define PADDLE_X0 ((WIDTH + PADDLE_WIDTH) / 2.0)
#define PADDLE_Y0 ((HEIGHT - PADDLE_HEIGHT) * 0.9)

// Constraints for paddle's movements
#define PADDLE_XMIN 0
#define PADDLE_XMAX (WIDTH - PADDLE_WIDTH)


void rectFollowCursor_X(GRect rect)
{
    GEvent event = getNextEvent(MOUSE_EVENT);

    if (event != NULL)
    {
        if (getEventType(event) == MOUSE_MOVED)
        {
            double x = getX(event) - (PADDLE_WIDTH) / 2;

            if ((x >= PADDLE_XMIN) && (x <= PADDLE_XMAX))
                setLocation(rect, x, PADDLE_Y0);
        }
    }

    return;
}

Of course, I call it from within the game loop.

After a few tries, I simplified it as much as I could, and converted most parameters to constant integers (well, macros too): that way there're as few as possible calculation at runtime (of course, I'd loose reusability of the function, since it will only be able to update the position of the paddle, and nothing else; but in this particular case, it's no big deal).

It actually runs smoothly, but when I compare it to the staff's implementation, I notice that "their's" performs better (no surprise there, really;) in that, when the mouse cursor moves out of the window, their paddle is (almost) always at the very edge of the screen. On the other hand, my paddle tends to stop moving quite a few pixels before getting to the edge when I move the cursor quickly out of the window.

I'm running both in the same appliance, so I'd say it's not a matter of resources.

Has anyone found a better way to perform the "check for boundaries"? (Am I being just too picky? =P )

  • What are PADDLE_XMIN and PADDLE_XMAX? – Kareem Dec 10 '14 at 22:52
  • I've updated my code to show their definitions – abelinux Dec 10 '14 at 22:56
  • I still think we dont have enough information; whats PADDLE_Y0? Honestly your function here looks fine to me, but you might be relying on something more complex to get PADDLE_Y0 or a dependent variable. – Josh Wieder Dec 11 '14 at 4:40
  • Updated again @JoshWieder . I think there's nothing more to it. No complexity here: all constants and/or macros. – abelinux Dec 11 '14 at 4:57
  • @abelinux, did you try commenting out the call to pause inside the while loop in main? – Kareem Dec 11 '14 at 10:32

You must log in to answer this question.

Browse other questions tagged .