Hi

This question gives me a weird feeling, actually I'm not really sure if this is relevant.
Anyway, here we go:

I'm (still) busy coding my first game in C. The goal is simple: the player is a fisherman and needs to catch all the fish in the water. If you like an example of how it looks I can draw it here but I don’t think it’s necessary.

Everything works fine, I have a function to move the fisherman, a function to move the fishes, but I can’t make the fishes move while playing. Unless I make the fishes move at the same time the player moves the fisherman. So the problem is that I can’t make the fishes move independent from user input without still being able to move the fisherman around.

This is the code I have now, it makes the fishes move when the player moves the fisherman. The for-loop in first case statement is experimental, too move more than one fish. (I translated function some names to make it easier to understand the code.)

do {
	if(!stop){
	  do { // input from the keyboard 
		 key =	getch();
		 switch (key){
			case UP:
				for(i=0;i<fish_left;i++) {
				  rand_fish = rand() %fish_left;
				  move_fish(rand_fish);
				}
				hook_up();
				break;
			case DOWN:
				random_fish = rand() %fish_left;
				move_fish(rand_fish);
				hook_down();
				break;
			case LEFT:
				random_fish = rand() %fish_left;
				move_fish(rand_fish);
				fisherman_toleft();
				break;
			case RIGHT:
				random_fish = rand() %fish_left;
				move_fish(rand_fish);
				fisherman_toright();
				break;
			default:
				NULL;  // ignore other input
		 }
	  }
	  while ((vissenresterend > aantalbommen) && (bomgevangen != 1));

What I hope to achieve is something like this:
Execute this:

while(timer<5){                                                             
             timer=clock()/CLK_TCK;  
             sw_timer=timer*10;

		switch(timer){
			case 0:   move_fish(1);  clrscr(); break;
			case 10: move_fish (2); clrscr(); break;
			case 20: move_fish (3); clrscr(); break;
			case 30: move_fish (1); clrscr(); break;
			case 40: move_fish (2); clrscr(); break;
			case 50: move_fish (3); clrscr(); break;
		}
}

And at the same time run this so that the user can move the fisherman around to try to catch a fish.

switch (key){
	case UP:
	         hook_up();
	         break;
	case DOWN:
	         hook_down();
	         break;
	case LEFT:
	         fisherman_toleft();
	         break;
	case RIGHT:
	         fisherman_toright();
	         break;
	default:
	         NULL;  // ignore other input

Is it possible to do something like that? How should I program something like this? I’m fine with the explanation or some useful link/code. I’m not asking people to program this specific code for me.

Greetz
Abberline

You can do that with the help of multi threading in C++ and Java. In C, no such feature exists. Although, some libraries for C might be supporting multi threading. I'm not sure.

And one more thing, I've read somewhere over here that C++ is the de facto language for game programming. So, if you are interested in game development only, then I think you should focus on learning C++.

Well, C supports in multi threading. But before i can say anything about light weight process. I should ask you which OS and compiler are you using.

If you are on Linux, you should looking at something called pthread library.

ssharish

You don't need threads, just the ability to read user input without blocking.

do {
  if ( keypressed() ) {
    key = readKey();
    handleKey(key);
  }
  moveFishes();
  refreshScreen();
} while ( numFishes > 0 );

Salem’s idee is closer to what I’m looking for guys. Thanks Salem.

Jishnu, your right when you say that C++ is the de facto language for game programming but, this is a project I have to make for school and they don’t teach us C++. This game (project) is an assignment for the C lessons. Which means that I have to use good old C. I’m not sure if they will be teaching C++ but we will have Java classes after Christmas.

Multi threading… never even crossed my mind. Its way to advanced stuff for me (I guess). Even if you could help me with multi threading I wouldn’t be able to explain my code on presentation day. But thanks for very much for you advice, Jishnu and ssharish2005.

If you know some good help on the Borland C++ 4.5 compiler for windows I would be very pleased. Oh yeah don’t mind the contradiction… I know that the complier we are to use is designed for C++ but we use it for C. Why? Well guess… that’s what the teacher wants us to use.

I’m going to try Salem’s idee first.

When coding graphic game, there are two things you should know: the non-graphic part and graphic part. Non-graphic part is the bunch of variables that work in the program; Graphic part is to translate from those variables into visable graphic. Anothing thing you should know if FPS(frames per second). For example: If you want your fish to move every 5miliseconds then you will render one frame per 5 miliseconds. You need to process everything that is needed for the frame, then render it out. In this case, you render 12 frames per second. I hope you understand I mean.

invisal, I'm not sure I understand what you are saying correctly. Your last sentence where you say that in my case I render 12 frames per second is the hardest one to get.

If i'm not mistaken you say that all code for one frame must be done before it can be displayed? Soo I need to do "calculate" or "memorise" everything on the screen, re-calculate and put everything back? To be honest I don't see what this could help me. (Sorry)

I tryed Salem's idee and came up with this:

// spel spelen
do {
	if(!stop){
		do { 
		  if(getch()){
			switch (getch()){
			  case UP:
					hook_up();
					break;
			  case DOWN:
					hook_down();
					break;
			  case LEFT:
					fisher_toleft();
					break;
			  case RIGHT:
					fisher_toright();
					break;
			  default:
					NULL;  // NULL om andere key's te negeren
			}
		 }
	 	while(timer<5){
		timer=clock()/CLK_TCK;
		sw_timer=timer*10;
			switch(timer){
				case 0:  move_fish(1); clrscr(); break;
				case 10: move_fish(2); clrscr(); break;
				case 20: move_fish(3); clrscr(); break;
				case 30: move_fish(1); clrscr(); break;
				case 40: move_fish(2); clrscr(); break;
				case 50: move_fish(3); clrscr(); break;
			}
	 	}
		}
		while ((fish_left > boms_left) && (boms_catched != 1));
	  }
	
	end();
	key=getch();
}
while(key != ESC);
_exit(0);

return 0;
}

but it dosn't do thins the way i would like them to be done.
Again I translated some variable or function names.

What happens now is that I need to press a movement key twice to get it happen.
Also the 'game' freeses for 5 seconds at the start of it.

(This is just a concept, it is not a real code)

For example: I make fishing game. There are 20 fishes and there is only one hook.

struct POS {
   int x;
   int y;
};

POS hook;
POS fish[20];

Each fish contains the x, y varaibles, it is to dedicate the fish location. Hook also contains the x, y varaibles to dedicate it position.

// Where you handle the key input
switch(key) {
   case DOWN:
        hook.y += 5;
        break;

   case UP:
        hook.y -= 5;
        break;

   case LEFT:
        hook.x -= 5;
        break;

   case RIGHT:
        hook.x += 5;
        break;

Move the position of your hook whenever any key was pressing.

// The fishes are moving every 5miliseconds
for(int i=0; i<20; i++) {
   fish[i].x += (rand() % 10) - 5;
   fish[i].y += (rand() % 10) - 5;
}

This part should call every 5 miliseconds.

bool Render() {
   // Render all the 20 fishes
   for(int i=0; i<20; i++)
        DrawTheFish(fish[i].x, fish[i].y);
    // Render the hook
    DrawTheHook(hook.x, hook.y);
}

This is the function where you render to the screen. This fuction should be call in every 10 miliseconds or 5 miliseconds (up to you)

invisal,

You code makes me realise something very important. That might be the solution to more then just the problem I stared this thread with. You see I re drawed everything everytime the user moves that hook. which is a bad thing to do. But i didn't realised it until I saw your example.

void move_left(){
clrscr();
if(posvisser > 0){ // test to keep player on the field
	posvisser -= 1;
	render();
}
else
	render();
}

damned that code ....

I thank you very very much for this one invisal!

do { 
		  if(getch()){
			switch (getch()){
			  case UP:
					hook_up();
					break;

but it dosn't do thins the way i would like them to be done.
Again I translated some variable or function names.

What happens now is that I need to press a movement key twice to get it happen.
Also the 'game' freeses for 5 seconds at the start of it.

Yep. getch() gets a character from the keyboard. The first one in the IF statement waits for a key, when pressed it returns the character pressed, which is interpreted as TRUE. Then you enter the SWITCH and have to press another key.

What you are looking for is kbhit() . This function returns TRUE if a key is ready to be read and FALSE if not key has been pressed. That's the function you want in the IF statement.

thanks WaltP it does the job like you said it would. But it didn't actulaly solve my 'bigger' problem. :( But still its intressenting to know about bkhit().

kbhit() is an old DOS function which detects when a key is pressed in a DOS environment.

You can write something which works the same in a win32 console, using the win32 console API

Thanks again from the info on kbhit(). I found it usefull. But its related to the problem (anymore).

But its related to the problem (anymore).

Does that mean anything?

damned I forgot the word "not" ...
But its not related to the problem (anymore).

I managed to code the program as I want it. Thank you all for helping me out, every repley was helpfull in some way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.