post  making a wait in milliseconds function?

ICANSEEYOU7687 (6)   Link to this post
For my class I have to make the game snake. And I have the snake drawing with linked lists correctly and it all works pretty well except it draws everything way too fast.... By the way this is in C, not C++

So I need some sort of wait function... I found
void wait ( int seconds ){
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}


on the site, but could not get it to work.And I included stdlib and time.h.
But im not that great of a programmer, so if someone could help me with this function I think I got the rest of the homework down.

thanks!
demosthenes2k8 (128)   Link to this post
If it's for windows you could try Sleep(); inside windows.h

but if you're sane, then you'll want to make sure that CLOCKS_PER_SEC is valid (should be 1000 IIRC)

Post your errors?
ICANSEEYOU7687 (6)   Link to this post
I tried including windows.h

#include <windows.h>
and it says it cant find it....

maybe its cause we are using eclipse and not visual studios?
Last edited on
helios (4790)   Link to this post
then you'll want to make sure that CLOCKS_PER_SEC is valid
What? How could it not be valid? I guess if it was zero or negative, but the function should work for any positive value of CLOCKS_PER_SEC. And what the hell kind of implementation would have CLOCKS_PER_SEC<=0?

ICANSEEYOU7687: You are using Windows, right? Not Linux? Because that header, as its name suggests, only exists for Windows. Other than that, I don't know what could be failing. Every compiler for Windows except Cygwin comes with windows.h. Maybe you need to set your compiler search paths manually with Eclipse?
Last edited on
demosthenes2k8 (128)   Link to this post
A screwed up implementation

Again, error messages would be helpful.
ICANSEEYOU7687 (6)   Link to this post
yes im using windows, haha windows 7 actually.

But if I cannot get this to work, I may just make a count function, and make an if statement that says something like...

If(count % 3 == 0){
drawsnake()
}

etc....

so only every 3 cycles it would draw the snake.
Mythios (753)   Link to this post
Make a new program - see if something like this works. It could possibly be you're calling something in the wrong spot.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
	clock_t endwait;
	endwait = clock () + seconds * CLOCKS_PER_SEC ;
	while (clock() < endwait) {}
}

int main()
{
	for( int i = 0; i < 10; i++ )
	{
		static int foo = 0;
		printf("Waiting ... %d\n", foo);
		foo++;

		wait(1);
	}

	return 0;
}
Last edited on
Duoas (2964)   Link to this post
The problem with that delay function is that it spends clock cycles.

Unless you have good reason not to, use the OS forms to delay:
http://www.cplusplus.com/forum/unices/10491/page1.html#msg49054

And, FWIW, "wait" is a really bad name for a function, because it might already be used elsewhere.

Hope this helps.
demosthenes2k8 (128)   Link to this post
Erm...couldn't you just get rid of foo and use the value i from the for loop?

Registered users can post in this forum.