Jump to content

  • Log In with Google      Sign In   
  • Create Account

What's wrong with my game?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 ChocaPaluza123   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 03:46 PM

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++

Sponsor:

#2 El Duderino   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 05:16 PM

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




#3 gmcbay   Members   -  Reputation: 130

Like
Likes
Like

Posted 28 June 2001 - 05:27 PM

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;



#4 ChocaPaluza123   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 June 2001 - 03:44 PM

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++

#5 ChocaPaluza123   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 June 2001 - 04:17 PM

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++

#6 shaft   Members   -  Reputation: 126

Like
Likes
Like

Posted 30 June 2001 - 11:30 AM

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

#7 ChocaPaluza123   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 June 2001 - 03:36 PM

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++

#8 origil   Members   -  Reputation: 134

Like
Likes
Like

Posted 30 June 2001 - 04:21 PM

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?

#9 Minion   Members   -  Reputation: 118

Like
Likes
Like

Posted 01 July 2001 - 10:03 AM

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

#10 Cyberdrek   Members   -  Reputation: 100

Like
Likes
Like

Posted 01 July 2001 - 10:25 AM

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

#11 adpeace   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2001 - 10:52 AM

You can''t call DeleteObject etc outside of your functions!! I would recommend you get a basic book on C++ and learn that before learning games-specific techniques, especially graphics. Start out with basic console in/out stuff and then move forwards.

> Andrew.

#12 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 01 July 2001 - 10:54 AM

quote:
Original post by shaft
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.



I think this only works in MFC? In Win32 or DX I think you have to use OutputDebugString().

> Andrew.



#13 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 01 July 2001 - 11:12 AM

adpeace is correct: TRACE("") only works within MFC.

ChocaPaluza123: why is your paddle RECT static? And where is it declared? If you declare it outside of any function (at the top, if you like, just after the header #include''s) then all functions within your source file will be able to access it because it has global scope . If you declare it as static at global scope, however, then you wont be able to modify it after intiilization. It seems you really want a DrawPaddle() function which maintains the static RECT. So your main loop/function will handle the keyboard input ( if(KEY_DOWN(...)) ) and then call DrawPaddle(), which will call InvalidateRect() to send a WM_PAINT message to Windows.

Keep posting. You''re trying first, which is why you''re getting quality replies (and don''t need to be so apologetic).

#14 Dean Harding   Members   -  Reputation: 546

Like
Likes
Like

Posted 01 July 2001 - 12:34 PM

quote:
Original post by Oluseyi
If you declare it outside of any function (at the top, if you like, just after the header #include''s) then all functions within your source file will be able to access it because it has global scope . If you declare it as static at global scope, however, then you wont be able to modify it after intiilization.



No, static at the file scope means it can''t be accessed outside that file. const at any scope means you can''t change it after initialization.

As for the original question, you''re still right. He wants to declare left_paddle outside of any functions, so any functions in that file can access it. Whether or not the static is there doesn''t really matter (since this is a first project, I doubt he''s got more than one .cpp file anyway.)

Anyway, good luck!

#15 ChocaPaluza123   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2001 - 12:55 PM

You guys got me thinking. Thanks for all those messages, and I think I should read a book other than C++ For Dummies and Windows Game Programming for Dummies.

C++

#16 ChocaPaluza123   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2001 - 01:02 PM

Since you guys are getting really involved with this (a lot more than I thought), I''ll show you how my code is going now. I''ll try to pick up a copy of C++: The Complete Reference or something like that.

int MakeRect (int nLeft, int nTop, int nRight, int nBottom)
{
static RECT paddle;
paddle.left = nLeft;
paddle.top = nTop;
paddle.right = nRight;
paddle.bottom = nBottom;
}

if (KEY_DOWN(''A''))
{
}

if (KEY_DOWN(''Z''))
{
}

// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
return(0);
} break;

case WM_PAINT:
{
// start painting
hdc = BeginPaint(hwnd,&ps);

// 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

HBRUSH brush = CreateSolidBrush(RGB(0,0,0));

MakeRect(10,200,300,15);

// draw it
FillRect(hdc,&paddle,brush);

DeleteObject(&brush);

// end painting
EndPaint(hwnd,&ps);
return(0);
} break;

C++




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS