Public Group

game making

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

Recommended Posts

I am working on trying to make a simple pong game in C++, using just characters ( such as "|" for the paddle, and "o" for the ball. I have figured out how to do everything except one thing: I need to know how to make it so that you can just press the arrow keys and the paddle moves. Please someone tell me how I can do this.

Share on other sites
I'm no expert here, but I'm pretty sure what you want is impossible without an event queue or something similar. Event queues are capable of storing events that occur (such as the player pressing an arrow key). All you need to do in your program is poll the event queue and then act if a certain key is pressed.

Unfortunately, the only way I know of to use an event queue is through APIs such as Windows or SDL. In fact, I just finished my first Pong clone a few days ago using SDL. I would recommend looking up a few tutorials on SDL. It's really very easy. After two days of somewhat light reading I was able to create a fully functional Pong game. To do the same in Windows would have taken much longer, which is the reason that I suggest SDL. Another benefit would be that you could use actual graphics instead of just characters. A simple Paint program is all you need to make a Pong game in SDL.

Share on other sites
SDL, IMHO, is your best bet. Either that or Allegro, but I've never used that. Either option would be a lot simpler than doing it purely ASCII in a DOS window, trust me. You'd have endless calls to system("cls") and all sorts just to give the impression of ball movement, and it would flicker horribly. No, go for SDL, it really isn't hard, you'll get up and running pretty damn quickly.

I once made a terrible, terrible Pong clone in SDL, it could have been so much better, but that was purely my coding and not a problem with SDL.

You could also use the GDI functions in the Win32 API, but that's probably too advanced for a beginner programmer.

Share on other sites
I agree with ukdeveloper. Let SDL be your friend.

Share on other sites
I know this isn't exactly what your looking for but give it a look and you could figure out how to do what you want with this. Ben Ryve's Programming Notes

Share on other sites
try something like this.
You'll need to #include <conio.h>

in your gameloop add some code like this to check if a key has been pressed and continue even if one wasnt.
if(!kbhit()){    char c = getchar(); // maybe getch(), its been awhile...        if(c = 115) // thats 's'    {        // do something for s key (down maybe :D )    }    if(c = 119) // thats 'w'    {        // do something for w key (up? )    }}

thats it...

you can look up the 'scan codes' by googling ascii table. or just look at the second page of this pdf...
ascii table

hope that helps.

edit:
you could use switch and that would probably be cleaner. i.e.
switch(c){    case 115: //s key    //do something    break;      //...}

Share on other sites
Thanks all for your help, ill look into it and tell you when im succesful :)

Share on other sites
Use GetAsyncKeyState(38) to detect the UP arrow, and GetAsyncKeyState(40) to detect the DOWN arrow. Windows only. Remember to include <windows.h> in your program.

Share on other sites
Quote:
 Original post by ukdeveloperSDL, IMHO, is your best bet. Either that or Allegro, but I've never used that. Either option would be a lot simpler than doing it purely ASCII in a DOS window, trust me. You'd have endless calls to system("cls") and all sorts just to give the impression of ball movement, and it would flicker horribly. No, go for SDL, it really isn't hard, you'll get up and running pretty damn quickly.I once made a terrible, terrible Pong clone in SDL, it could have been so much better, but that was purely my coding and not a problem with SDL.You could also use the GDI functions in the Win32 API, but that's probably too advanced for a beginner programmer.
I made a text-based pong clone. I used gotoxy to print and delete from specific places on screen in an earlier version and the flicker wasn't that bad at all. The only annoying thing was that the blinking cursor followed the ball.

The flicker is only bad if you use system("cls") at the end of the loop. If you use a screen buffer (an array of chars, for example) and delete the characters at their old positions(ie change them to spaces) and write the new ones in, then draw the whole thing all at once, there is no flicker.

Share on other sites

Great site that walks you through building a few different games with dsl.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×