Beispiel #1
0
void clean_body(struct body *b)
{
    struct body *rest = NULL;
    if (b) {
        rest = b->rest;
        free(b->part);
        clean_body(rest);
    }
}
/* Reset Player Data */
void player_data(void)
{
    config.snake_status = LIVE;
    snake_pos[HEAD][POS_X] = DEFAULT_X;
    snake_pos[HEAD][POS_Y] = DEFAULT_Y;
    create_apple();
    clean_body();
    config.count = 0;
    config.points_user = 0;
    config.snake_direction = WE;
    config.game_level = N3;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
        fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
        SDL_Quit();
        return -1;
    }
    SDL_Window *win = SDL_CreateWindow("snake", 50, 50, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
    if (!win) {
        fprintf(stderr, "Unable to initialize window: %s\n", SDL_GetError());
        SDL_Quit();
        return -1;
    }
    SDL_GLContext gl_con = SDL_GL_CreateContext(win);
    struct square *head = make_square(0, 0, TILE, brown);
    struct square *fruit = make_square(0, 0, TILE, red);
    if (!head || !fruit) {
        fprintf(stderr, "Unable to allocate memory for square struct.");
        if (head) {
            free(head);
        }
    }
    struct body *snake_body = NULL;
    reposition_fruit(fruit);
    enum direction snake_d = LEFT;
    int previousx;
    int previousy;
    if (gl_con == NULL) {
        fprintf(stderr, "Unable to initialize gl context: %s\n", SDL_GetError());
        SDL_DestroyWindow(win);
        SDL_Quit();
        return -1;
    }
    SDL_Event event;
    while (1) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                goto end;
            }
            else if (event.type == SDL_KEYDOWN) {
                switch (event.key.keysym.sym) {
                case SDLK_ESCAPE:
                    goto end;
                    break;
                case SDLK_UP:
                    snake_d = UP;
                    break;
                case SDLK_DOWN:
                    snake_d = DOWN;
                    break;
                case SDLK_LEFT:
                    snake_d = LEFT;
                    break;
                case SDLK_RIGHT:
                    snake_d = RIGHT;
                    break;
                }
            }
        }
        previousx = head->x;
        previousy = head->y;
        switch (snake_d) {
        case UP:
            head->y += TILE;
            break;
        case DOWN:
            head->y -= TILE;
            break;
        case LEFT:
            head->x -= TILE;
            break;
        case RIGHT:
            head->x += TILE;
            break;
        }
        if (is_intersect_body(head, snake_body)) {
            goto end;
        }
        step_body(&snake_body, previousx, previousy);
        if (head->y > SCREEN_HEIGHT - TILE || head->y < -1 * SCREEN_HEIGHT || head->x > SCREEN_WIDTH - TILE || head->x < -1 * SCREEN_WIDTH) {
            goto end;
        }
        else if (is_intersect(head, fruit)) {
            add_body(&snake_body, previousx, previousy);
            do {
                reposition_fruit(fruit);
            } while (is_intersect(head, fruit) || is_intersect_body(fruit, snake_body));
        }
	cap_game_fps(display, win, head, snake_body, fruit);
    }
end:
    free(head);
    clean_body(snake_body);
    free(fruit);
    SDL_GL_DeleteContext(gl_con);
    SDL_Quit();
    return 0;
}