void SnakeGame::play_game() { SDL_Event window_event; program_running = true; game_over = false; while(program_running) { handle_events(&window_event); //Clear Screen glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); snake->move(); snake_eats(); sidebar->draw(renderer); board->draw(renderer); border->draw(renderer); snake->draw(renderer); apple->draw(renderer); SDL_GL_SwapWindow(window); if(check_death()) { reset(); } } }
void creature_t::regenerate_hp(int amount) { hp.cur += amount; if (hp.cur > hp.max) hp.cur = hp.max; // If negative regeneration. check_death(nullptr, this); }
/* Callback function called for each tick. This is the complete machinery of everything that moves. */ void draw_pacman(ModeInfo * mi) { unsigned int g; pacmangamestruct *pp; if (pacmangames == NULL) return; pp = &pacmangames[MI_SCREEN(mi)]; if (pp->ghosts == NULL) return; pp->pacman.err.x = (pp->pacman.err.x + 1) % pp->pacman.speed; pp->pacman.err.y = (pp->pacman.err.y + 1) % pp->pacman.speed; pp->pacman.delta.x += pp->pacman.err.x != 0 ? pp->incx : 0; pp->pacman.delta.y += pp->pacman.err.y != 0 ? pp->incy : 0; if (pp->pacman.delta.x >= pp->xs && pp->pacman.delta.y >= pp->ys) { pac_update(mi, pp, &(pp->pacman)); check_death(mi, pp); pp->pacman.delta.x = pp->incx; pp->pacman.delta.y = pp->incy; } if (pp->pacman.delta.x > pp->xs + pp->incx) pp->pacman.delta.x = pp->xs + pp->incx; if (pp->pacman.delta.y > pp->ys + pp->incy) pp->pacman.delta.y = pp->ys + pp->incy; for (g = 0; g < pp->nghosts; g++) { if (pp->ghosts[g].dead == True) continue; pp->ghosts[g].err.x = (pp->ghosts[g].err.x + 1) % pp->ghosts[g].speed; pp->ghosts[g].err.y = (pp->ghosts[g].err.y + 1) % pp->ghosts[g].speed; pp->ghosts[g].delta.x += pp->ghosts[g].err.x != 0 ? pp->incx : 0; pp->ghosts[g].delta.y += pp->ghosts[g].err.y != 0 ? pp->incy : 0; if (pp->ghosts[g].delta.x >= pp->xs && pp->ghosts[g].delta.y >= pp->ys) { ghost_update(pp, &(pp->ghosts[g])); pp->ghosts[g].delta.x = pp->incx; pp->ghosts[g].delta.y = pp->incy; } if (pp->ghosts[g].delta.x > pp->xs + pp->incx) pp->ghosts[g].delta.x = pp->xs + pp->incx; if (pp->ghosts[g].delta.y > pp->ys + pp->incy) pp->ghosts[g].delta.y = pp->ys + pp->incy; } pacman_tick(mi); }
/* Resets state of ghosts + pacman. Creates a new level, draws that level. */ static void repopulate(ModeInfo * mi) { pacmangamestruct *pp = &pacmangames[MI_SCREEN(mi)]; unsigned int ghost; int i = createnewlevel(mi); MI_CLEARWINDOW(mi); drawlevel(mi); pp->gamestate = GHOST_DANGER; pp->pacman.row = (LEVHEIGHT + JAILHEIGHT)/2 - i; pp->pacman.col = (LEVWIDTH/2); pp->pacman.nextrow = NOWHERE; pp->pacman.nextcol = NOWHERE; pp->pacman.cf = NOWHERE; pp->pacman.rf = NOWHERE; pp->pacman.oldcf = NOWHERE; pp->pacman.oldrf = NOWHERE; pp->pacman.oldlx = NOWHERE; pp->pacman.oldly = NOWHERE; pp->pacman.aistate = ps_eating; pp->pacman.cur_trace = 0; pp->pacman.roundscore = 0; pp->pacman.speed = 4; pp->pacman.lastturn = 0; pp->pacman.delta.x = 0; pp->pacman.delta.y = 0; pac_clear_trace(&(pp->pacman)); for (ghost = 0; ghost < pp->nghosts; ghost++) { pp->ghosts[ghost].col = (LEVWIDTH/2); pp->ghosts[ghost].row = (LEVHEIGHT/2); pp->ghosts[ghost].nextcol = NOWHERE; pp->ghosts[ghost].nextrow = NOWHERE; pp->ghosts[ghost].dead = 0; pp->ghosts[ghost].lastbox = START; pp->ghosts[ghost].cf = NOWHERE; pp->ghosts[ghost].rf = NOWHERE; pp->ghosts[ghost].oldcf = NOWHERE; pp->ghosts[ghost].oldrf = NOWHERE; pp->ghosts[ghost].aistate = inbox; pp->ghosts[ghost].timeleft = ghost * 50; pp->ghosts[ghost].speed = 3; pp->ghosts[ghost].delta.x = 0; pp->ghosts[ghost].delta.y = 0; ghost_update(pp, &(pp->ghosts[ghost])); } check_death(mi, pp); pac_update(mi, pp, &(pp->pacman)); }
static int player_loop(t_lemipc *lemipc) { while (g_check) { printf("still Alive !\n"); lock_sem(lemipc->game->sem_id); if (check_death(lemipc) == 1) return (0); move_random(lemipc); usleep(600000); unlock_sem(lemipc->game->sem_id); } return (1); }
int update_fork_incant(t_serv *serv, double elapsed, t_client *tmp) { if (tmp->incanting) { tmp->incant_time -= elapsed / 1000000; if (tmp->incant_time <= 0) if (cmd_end_incantation(serv, tmp) == EXIT_FAILURE) return (EXIT_FAILURE); } if (tmp->forking) { tmp->fork_time -= elapsed / 1000000; if (tmp->fork_time <= 0) if (cmd_fork(serv, tmp, NULL) == EXIT_FAILURE) return (EXIT_FAILURE); } if (check_death(serv, tmp) == EXIT_FAILURE) return (EXIT_FAILURE); return (EXIT_SUCCESS); }