#### Archived

This topic is now archived and is closed to further replies.

# Problems moving a bitmap sprite

This topic is 5857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, Okay I am basically working on a breakout clone, I have hit a brick wall with the paddle and gettting it to move. The paddle is loaded on the surface but the code I am using to move it aint working. I think I am not pointing it right or missing something off but after trying all day a whole host of methods I have to seek help - and learn at the same time Here''s the main bits of the code to implement the paddle,
  // defines for the actual bat or paddle #define PADDLE_START_X (SCREEN_WIDTH/2 - 16) #define PADDLE_START_Y (SCREEN_HEIGHT - 32) #define PADDLE_WIDTH 20 BITMAP_IMAGE paddle[1]; int paddle_x = 0, paddle_y = 0; // tracks the paddle pos void Paddle_Move(void) { if (KEY_DOWN(VK_RIGHT)) { // move the paddle to the right paddle_x+=8; // Make sure paddle doesn''t run off the screen if (paddle_x > (SCREEN_WIDTH - PADDLE_WIDTH)) paddle_x = SCREEN_WIDTH - PADDLE_WIDTH; } // end if else if (KEY_DOWN(VK_LEFT)) { // move paddle to right if (paddle_x < 0) paddle_x = 0; } // end if } // Paddle_Move() //load in the paddle Load_Bitmap_File(&bitmap8bit, "PADDLE.BMP"); for (index=0; index < 1; index++) { Create_Bitmap(&paddle[index],0,0,32,32); Load_Image_Bitmap(&paddle[index],&bitmap8bit,index,0,BITMAP_EXTRACT_MODE_CELL); } Unload_Bitmap_File(&bitmap8bit); for (index = 0; index<1; index++) { int paddle1 = rand()%1; paddle[paddle1].x = PADDLE_START_X; paddle[paddle1].y = PADDLE_START_Y; Draw_Bitmap(&paddle[paddle1],reactor.buffer, reactor.width,1); } // return success return(1); 
Then in Game_Main() I call the Paddle_Move function. I think I need to put the Bitmap_Image name into the Paddle_Move() function some how. Help really appreciated. Pk

##### Share on other sites
The first thing that hits me is that after you check to see if the left key is pressed you don''t alter the paddles coordinates, try adding
  if (KEY_DOWN(VK_LEFT)) { paddle_x-=8;

and at the end instead of having
  paddle[paddle1].x = PADDLE_START_X;paddle[paddle1].y = PADDLE_START_Y;

you want to use
  paddle[paddle1].x = paddle_x;paddle[paddle1].y = PADDLE_START_Y;

Thats all that jumps immediately to mind, give it a try and let us know how it goes. If it works you might want to think about chnging your code about so that you only call the Load_bitmap_file function at load time, the accesses to hard disc every frame will slow your game down a lot.

zipless

##### Share on other sites
Hey,

The first mistake was my bad, I had been altering the code all day and missed that off.

I tried the final change you suggested and the paddle is now at the far left hand side of the screen instead of the center, I don''t know why it''s done that just from changing that one thing.

If you spot anything else let me know, been at this all day LOL.

The only good thing is I have learned so much it''s untrue, looking through all the various articles on the web as well.

##### Share on other sites
It sounds like paddle_x is always zero or close to it, there are a couple of thing that *could* cause this but it''s probably outside the Move function.

Make sure that SCREEN_WIDTH is defined properly and check that the the increment part works by placing breakpoints inside the KEY_DOWN if statements. If your still having problems look at where you declare paddle_x and paddle_y if they''re not global that''ll be the problem, if not i''m out of ideas for the moment.

Yeah you can learn so much by reading through the articles, these boards are pretty damm good too.

zipless

##### Share on other sites
Hey,

Everything else is in order, I have not used the breakpoints before so I''ll have to have a play with it, cheers anyway.

Pk

##### Share on other sites
Ah! Then your in for a treat, after you get the hang of them VS is an absolute treat to use, you can see EXACTLY whats going on in most functions and it really helps to track down problems.

Believe me, pretty soon you''ll have red dots all down the side of your code (assuming your using Visual Studio). I think F5 and F11 are my most used keys now, hee hee.

zipless

##### Share on other sites
Hey,

Can anyone spot what I''m doing wrong because I''ve tried using the debugger and, it was my first go using it but I didn''t find anything.

Also has anyone got any good debug tricks for MSVC++6?

Thanks,

Pk

##### Share on other sites
Just a tip - don''t re-load the everytime you redraw it.

##### Share on other sites
Does the paddle jump all the way to the left when you press the left key or does it just start that way.
If it jumps, then, as hard as it is to believe considering how slow loading the bitmap each frame would be, your code may be running faster than your drawing to the screen.
Basically, it''s reading that the key is being pressed quite a few times before you actually let up.
It''s ugly but try inserting Sleep(100); after you redraw the bitmap just to see if this is the problem.
Of course I''m by no means an expert, or even good at this, so don''t blame me if it doesn''t work! =P
And for goodness sakes, try looking into loading the bitmap to an offscreen surface and blitting it to a backbuffer each frame if you''re using DirectX.

##### Share on other sites
Hey,

Basically the paddle does not move at all and as far as I can see everything looks okay.

Heeellllppp anyone ?

##### Share on other sites
Use the debug mode to break the program whenever the left key is pressed. Now once thats been done check to see what the value of paddle_x is BEFORE it''s altered, it should be zeor on the first run. Now ste through the rest of the if statement and make sure it''s working OK.

Now let the program run again and press left again, whats the value of paddle_x this time? If it''s zero again then it''s being reset outside the function and you need to find where it''s being set to zero.

If it''s not step the program down to the draw bitmap function and make sure that paddle[0].x is non zero.

zipless

/* Ignorance is bliss, then you go and spoil it by learning stuff */