Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-11-2003, 03:05 AM   #1 (permalink)
Gorbash9k
Registered User
 
Join Date: Feb 2003
Location: Virginia
Posts: 2
Gorbash9k is on a distinguished road
Send a message via AIM to Gorbash9k
C++ help (Real Time)

Hey this is my first post here, so i dont know if you guys have the haze the newbie policy or anything like that, but i need some quick help.

I'm in my second year of Computer Science (C++) and we have to do a dossier, where basically its a decent size program that follow several requirements. My is a basic game, divider down the middle, one person on each side. Object is to avoid the shot from the oppent but to shoot them. Simple.

The only problem i have is im trying to make it in a real time format, an accquintence kept on yelling about key buffering and sent me an example that made no sense, so i was wondering if one of you guys could help me with this. I can send you my program in the works if you want to see what im talking about.
Gorbash9k is offline   Reply With Quote
Old 02-12-2003, 02:51 AM   #2 (permalink)
Pussnuts
Registered User
 
Join Date: Feb 2003
Location: Orlando, Florida
Posts: 9
Pussnuts is on a distinguished road
Send a message via AIM to Pussnuts
Re: C++ help (Real Time)

Quote:
Originally posted by Gorbash9k
Hey this is my first post here, so i dont know if you guys have the haze the newbie policy or anything like that, but i need some quick help.

I'm in my second year of Computer Science (C++) and we have to do a dossier, where basically its a decent size program that follow several requirements. My is a basic game, divider down the middle, one person on each side. Object is to avoid the shot from the oppent but to shoot them. Simple.

The only problem i have is im trying to make it in a real time format, an accquintence kept on yelling about key buffering and sent me an example that made no sense, so i was wondering if one of you guys could help me with this. I can send you my program in the works if you want to see what im talking about.
firstly, welcome to the forums. I'm new myself and I would say I am fairly certain a website called "codenewbies" would not be about hazing newbies :-)

On to your dilema. Bascially what you want is to setup an infinite loop. You will process very tiny descrete units of time in each loop. For a general algorithm:

Start game;
while(Game!=done){
get keyboard input
update game to next 'cycle'
if (winner) Game = done;
}

You are concerned with the keyboard input. I believe there is an easy way to do this in C but can't think of it right now. Your friends example of keyboard buffering sounds like it might do the trick. If you can, post the example on the forums or a webpage. I will try to explain it the best I can.
Pussnuts is offline   Reply With Quote
Old 02-12-2003, 11:50 AM   #3 (permalink)
Gorbash9k
Registered User
 
Join Date: Feb 2003
Location: Virginia
Posts: 2
Gorbash9k is on a distinguished road
Send a message via AIM to Gorbash9k
I should have told my problem more indepth, I needed a way for the game to keep running while the user is inputting his command. I finally though of a way though, I'm going to set a timer of some small amount, where if the user doesnt input when the timer runs out, the computer will just put in a NULL response and continue on. My problem now is how to set a timer.

Anyways this is the program that my friend sent me.

/////////////////////////////////////////////////////////////////////////////////

#include <conio.h>

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char tmp;

system("cls");

cout << "Hit a key, please ('q' to quit)." << endl;

do
{
while(!_kbhit());
tmp = _getch();
cout << tmp;
} while (tmp != 'q');

cout << endl << endl;

system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
Gorbash9k is offline   Reply With Quote
Old 02-26-2003, 05:18 PM   #4 (permalink)
ShadowSoft
Registered User
 
ShadowSoft's Avatar
 
Join Date: Feb 2003
Location: NJ
Posts: 7
ShadowSoft is on a distinguished road
Send a message via AIM to ShadowSoft
i've noticed that conio.h sometimes makes weird faults..it's supposed to give the program the ability to clear screen , but sometimes it makes errors that even the debugger doesn't pick up on....maybe that's why things aren't working right?
ShadowSoft is offline   Reply With Quote
Old 02-27-2003, 01:36 PM   #5 (permalink)
ShadowSoft
Registered User
 
ShadowSoft's Avatar
 
Join Date: Feb 2003
Location: NJ
Posts: 7
ShadowSoft is on a distinguished road
Send a message via AIM to ShadowSoft
Thumbs down

Quote:
Originally posted by ShadowSoft
i've noticed that conio.h sometimes makes weird faults..it's supposed to give the program the ability to clear screen , but sometimes it makes errors that even the debugger doesn't pick up on....maybe that's why things aren't working right?
forget i mentioned that..sorry i'm a fool that has nothing to do with your problem.
ShadowSoft is offline   Reply With Quote
Old 03-23-2003, 10:08 AM   #6 (permalink)
Mr.Anderson
Registered User
 
Mr.Anderson's Avatar
 
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
Mr.Anderson is on a distinguished road
Send a message via AIM to Mr.Anderson Send a message via Yahoo to Mr.Anderson
Oohh... that's going to hurt.

Quote:
Originally posted by Gorbash9k
I should have told my problem more indepth, I needed a way for the game to keep running while the user is inputting his command. I finally though of a way though, I'm going to set a timer of some small amount, where if the user doesnt input when the timer runs out, the computer will just put in a NULL response and continue on. My problem now is how to set a timer.

Anyways this is the program that my friend sent me.

/////////////////////////////////////////////////////////////////////////////////

#include <conio.h>

#include <iostream>
using std::cout;
using std::endl;

int main()
{
char tmp;

system("cls");

cout << "Hit a key, please ('q' to quit)." << endl;

do
{
while(!_kbhit());
tmp = _getch();
cout << tmp;
} while (tmp != 'q');

cout << endl << endl;

system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
I might just be a pompus, rotund, and arrogant pile of unnecessary carbon, but it sounds like you might need to do some multi-threading. Haveing the timer count down and getting commands at the EXACT same time? I would think so.
Then again, I'm a n00b.

Multi threading ... quite frankly, I hate the stuff. (Probably because I've never been able to do it.)

I wish I could have been more help. Sorry if I've said anything stupid.

THen again, if you just want to have the user enter something AND THEN update the timer, Pussnuts's idea is a pretty good one.

Have a nice day.

-- Jo
Mr.Anderson is offline   Reply With Quote
Old 03-27-2003, 11:07 AM   #7 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Another twist...

Hello,

I am thinking about this all 20 minutes before a calc II test, so I will make it brief. Another method you could employ to solve your problem is do the reverse. Wait for input in the main() loop, and multi-thread to update the game. For instance...(this is using C signal processing and setting alarms to do the trick...posix stuff I believe, so windows should have this)

Code:
#include <iostream>
#include <signal.h>
#include <unistd.h>
using namespace std;

// Handle the alarm signal and reset the alarm to go off again
// Alarm goes for 6 second intervals
void updateGame (int nothing) {
	cout << "We are updating the game now\n";
	alarm (6);
}

int main (int argc, char * argv[]) {
	struct sigaction timaction, old_timaction;
	timaction.sa_handler = updateGame;
	sigemptyset (&timaction.sa_mask);
	timaction.sa_flags = 0;


	// Set signal for alarm to go to Update Game
	sigaction(SIGALRM, &timaction, &old_timaction);
	alarm (6); // Set the alarm

	// Process keyboard input
	while (1) {
		// Handle keyboard input here
	}

	// Clean up, and quit
	return 0;
}
Using this method, the keyboard does not have to wait to get input, but the game still updates (without threads, I might add), so you have the same memory to mess with, but still have two seperate processes going on (sort of). I hope this helps.

Yet another way is to use fork() and threads, but make sure it does it in memory-shared mode...then you can while inside the other thread, and get keyboard input in the main thread, so it just does it as fast as possible (an eats the CPU time like no other) Anyway, just some suggestions...hope the code helps...

Ted Morse

PS: I didn't test this code, or even try to compile it, so don't think its perfect or will even work/make sense/anything.

** EDITED: I know the code works now on a linux-based system using g++ **
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 03-27-2003, 07:26 PM   #8 (permalink)
CountZero
Registered User
 
CountZero's Avatar
 
Join Date: Mar 2003
Location: Arlington, TX
Posts: 1
CountZero is on a distinguished road
Send a message via AIM to CountZero Send a message via Yahoo to CountZero
I made a game in my high school AP CS class. It was my rendition of Duck Hunt using CMU Graphics. I currently have the source up @ my website.

http://interrupt.ath.cx/code/Duck_Hunt.zip

It's there if you want some idea how to start. Good luck.
CountZero is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple C program Spooky Standard C, C++ 1 10-22-2004 08:26 AM
Implementing C++ file structure in C# IAmWeasel MS Technologies ( ASP, VB, C#, .NET ) 1 07-08-2004 08:38 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 03:40 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting