Advertisement

What's wrong with my game?

Started by June 28, 2001 09:46 PM
14 comments, last by ChocaPaluza123 23 years, 5 months ago
Hey, I don''t want to annoy you, but I am making a simple Pong game. So far, I have been able to draw the paddle, but when I try to check for keyboard input to make the paddle go up and down, the code doesn''t work. I''ll post my code here, so please tell me whats wrong. (I am just putting some parts of the code in here). // MACROS ///////////////////////////////////////////////// // these read the keyboard asynchronously #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) int x; int nBottom; int nTop; // GRAPHICS // Step 1: Create the two paddles. Make their height about 40 pixels high and about 5 pixels wide, // making their starting position 220 (top), 260 (bottom), 10(facing the edge of the screen), // and 15 (facing the center of the screen). Position them 10 pixels away from the edge // of the screen and center them // create the left paddle static RECT left_paddle = {10,nTop,15,nBottom}; // cool trick // create a white brush HBRUSH white_brush = CreateSolidBrush(RGB(255,255,255)); // draw it FillRect(hdc,&left_paddle,white_brush); // Delete the brush DeleteObject(white_brush); int Game_Main(void *parms) { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! if (KEY_DOWN(''A'')) { nTop = nTop + 25; nBottom = nBottom + 25; } if (KEY_DOWN(''Z'')) { nTop = nTop - 25; nBottom = nBottom - 25; } // return success return(1); } // end Game_Main C++
C++
Maybe you want VK_A and VK_Z ?

I''ve never used this function, but perhaps

if (KEY_DOWN(VK_A))

is along the right track.

El Duderino


Advertisement
The key routines look fine. The problem is your ''cool trick'' is not as cool as you think it is.

When you create the left paddle, you are giving it the values of nBottom and nTop (which, by the way, havent even been initalized at that point, which is a seperate problem). The rect takes these values and stores them in a different area of memory. Changing nBottom and nTop does NOT change the corresponding value within the left_paddle!

Instead of changing nBottom, nTop when the keys are pressed, change the values in left_paddle, eg. left_paddle.top = left_paddle.top + 25;

Thanks you guys! I didn''t know that you could actually change the RECT structure by itself (rect.top += 25). I never would have thought of that.

C++
C++
Hi, its me again. I hope you don''t get to annoyed by me, because we don''t want that rodneyldixon vs. tryforfulluse thing again. I took your advice, but the code still doesn''t work. I looked it up in Windows Game Programming For Dummies, but Andre tells you the first time that the code is if(KEY_DOWN(VK_A)) and the second time if(KEY_DOWN(''A'')), I even looked in the help file, but it still doesn''t work. I think it might be because I am putting the key code in the WM_PAINT message, but the compiler freaks out (like it always does) if I put it somewheres else. I''m probably getting you kindof annoyed by now, but I''ll put in the paint code here.

case WM_PAINT:
{
// All the graphics stuff here about drawing the paddle

if (KEY_DOWN(''A''))
{
left_paddle.top = left_paddle.top + 25;
left_paddle.bottom = left_paddle.bottom + 25;
}

if (KEY_DOWN(''Z''))
{
left_paddle.top = left_paddle.top - 25;
left_paddle.bottom = left_paddle.bottom - 25;
}



C++
C++
The code inside your WM_PAINT area should ONLY paint the screen. I.e. Draw your bitmaps.

Inside your main loop do the checks for keys being pressed, and modifying the paddles location.

Also make sure you "dirty" the screen so that WM_PAINT message is actually sent, thereby updating the visuals.


If you are using Visual C++, I recommend you use the TRACE("") statements to make sure the code you think is executing is actually getting executing.

-Good Luck
He''s a bad motha - Shut yo mouth.
Advertisement
I guess Pong is harder than I thought! I took your advice, shaft, but when I try to put the if(KEYDOWN(... code in my game loop I can''t access the left_paddle struct from in there. Man, if I''m having this much trouble from Pong, I feel bad for the makers of Half Life.

C++
C++
ChocaPaluza123 you don''t need to worry about getting people annoyed because you''re asking questions about something you''re having a problem with.
If people will be willing to help they will, and believe me there are plenty of people who will.
Why can''t the rect be accessed from within the game loop?
The Department of Next Life - Get your Next-Life Insurance here!
I have something that might work. You could try to create a function DrawRect(int x1, int y1, int x2, int y2, LPCOLORREF color). In the function you could create the paddle rect so maybe...

int DrawRect (int x1, int y1, int x2, int y2, LPCOLORREF color){    RECT paddle;    paddle.top = y1;    paddle.left = x1;    paddle.bottom = y2;    paddle.right = x2;    // drawing code here} 


then you could use it like this:

DrawRect (paddle_x, paddle_y, paddle_width, paddle_height, &paddle_color);

I''m not sure about the inclusiveness of RECT structs but that might work with a little fixup.

this way you could modify the positions of the paddle outside of the call.

hope it helps
I don't want to sound mad or anything but I think that you still have alot to learn about C/C++ before going any further in your game programming. Modifying structs members are a fundamental part of C/C++ expecially for games. Also, knowing how to set your structs so that they can be accessed from other files in the project is also a really big part of C/C++ programming. I recommend that you write a couple of little programs to really get the feal for structs & maybe even get into classes which could get you off to a completely different approach to your project. Anyhow, these are my views and comments.



"And that's the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP's Invasive Production Activation Technology!

Edited by - Cyberdrek on July 1, 2001 5:26:59 PM
[Cyberdrek | ]

This topic is closed to new replies.

Advertisement