Sign in to follow this  

game making

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
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.
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this