C++ Question

amol

Lifer
Jul 8, 2001
11,680
3
81
I'm in a high school C++ class right now for summer and my group's final project is to make a game using C++. We decided on Pong.

We created 2 versions, the first uses a two dimensional character array to represent the playing area while the second uses 966 individual characters to represent a 14x69 playing area. Both of these versions work fine, it's just that the first version is A LOT shorter.

The main problem we're having is that every time the player (user) wants to move the paddle up or down, he has to push the key and then enter everytime. So to move it up 3 times, you have to push "q, enter, q, enter, q, enter" or "q, q, q, enter".

If you guys could help, that'd be awesome. Even our teacher (who holds a PhD in Computer Science) is not sure how to have it work in realtime on our school's system. The thing is due Friday (tomorrow), so if you guys could help us by then, it'd be cool. It has to work on our school's system, on which some header files don't work, like <conio.h>

Here are the source codes - main version (2D char array) ||| longer 966 individual char
 

itachi

Senior member
Aug 17, 2004
390
0
0
hahahah wtf.. you actually sat there and wrote 966 variables? that's insane. i would've rather just failed the class.

getchar() is probably what you're looking for. it'll read any single character from stdin and return it.

#include <stdio.h>
....
int d = getchar ();
if (d == 'q')
move_up();
...
 

imported_FishTaco

Golden Member
Apr 28, 2004
1,120
0
0
Haha, I love the 966 individual char version. Who's idea was it to write the program that way?

Anyway, you didn't state what system you needed this to work on, but you did mention that conio.h was available so I'm assuming some kind of linux or unix. In which case you'll need to use a library like ncurses.

Here's a guide that has examples of stuff you'll need for your pong game.

http://www.apmaths.uwo.ca/~xli/ncurses.html

Good luck, if you work hard at it you should be able to grind out an ncurses version by your due date.

My final suggestion is that you should figure out how to make your program sleep for a few milliseconds at the end of each loop instead of using for(w = 0; w <=1000000; w++) // Wait. Look in unistd.h.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
He said the school computers *didn't* have conio.h. So they are windows?

The long version is a gem! Funny stuff.

p.s. it's int main(), not void main().
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
You need non-blocking IO. It looks like the only easy way to get that is with conio.h, or ncurses. I thought I've used a "kbhit" function before on windows, but I can find almost no documentation on msdn, and it looks like it does require conio.h. This message implies that you can set stdin to be nonblocking using fnctl or ioctl, and then just use getch(). What compiler and OS are you using?
 

amol

Lifer
Jul 8, 2001
11,680
3
81
The system is "Red Hat Linux release 7.3 (Valhalla)".

As for the 966 indiv. char, I have 2 other group members. One did that and then about 2 days later, the other did the array.


Thanks for the help so far; I'll check this up at school in an hour or so.

EDIT:

We are accessing the server from MAC OS 9 using NCSA Telnet. The server, like I said, runs Red Hat Linux release 7.3 (Valhalla)