I'm learning C language and very new to this. I'm trying to write a simple game that spawns an enemy after 3 secs from the start of the level. I tried clock()
function and had the problem that while it spawns after given time it also freezes the game so it is unplayable.
void delay (clock_t n) {
clock_t start = clock();
while(clock() - start < n);
}
Also tried the get_current_time()
method but the game freezes if I got to this level so my code must be wrong. Can anyone give me some solution on how to approach this?
void draw_hero( void ) {
char * hero_image =
/**/ "H H"
/**/ "H H"
/**/ "HHHHH"
/**/ "H H"
/**/ "H H";
int hero_x = (screen_width() - HERO_WIDTH) / 2;
int hero_y = (screen_height() - HERO_HEIGHT) / 2;
hero = sprite_create(x, y, HERO_WIDTH, HERO_HEIGHT, hero_image);
double lastTime = get_current_time();
while (true){
double current = get_current_time();
double elapsed = current - lastTime;
//lastTime = current;
while (elapsed < lastTime + 500) ???
sprite_draw(hero);
show_screen();
}