Exemple #1
0
void game_resize(GameData *data, Evas_Coord w, Evas_Coord h)
{
//	food::width = w/FOOD_BOXES;
//	food::height = h/FOOD_BOXES;
	snake_food_width_set(w/FOOD_BOXES);
	snake_food_height_set(h/FOOD_BOXES);
//	snake_food_x_start_pos();
//	snake_food_y_start_pos();
//	LOG_DBG("%d :::::::::::::::::::::: %d \n", width, height);
	snake_food_update(data->food);
//	data->food
}
Exemple #2
0
int
main(int argc, char *argv[])
{
    (void) argc; (void) argv;


    // -- Initialize the graphics system. -- //
    
    SDL_Window *window = NULL;
    SDL_Renderer *renderer = NULL;
    snake_gfx_initialize(&window, &renderer);

    // -- Initialize library functions. -- //

    // Seed the random number generator.
    srand((unsigned int) time(NULL));

    // -- Initialize game entities. -- //

    // Put the snake in the center of the screen.
    SnakePlayer player = snake_player_new(SNAKE_WIDTH / 2, SNAKE_HEIGHT / 2);

    // Put the food in a random location.
    SnakeFood food = snake_food_new();

    // -- Game loop. -- //

    // Loop until the player quits or dies.
    bool enabled = true;

    // Track the elapsed time of each frame.
    unsigned int frame_time_start = SDL_GetTicks();

    do {
        // -- User input. -- //

        // Get user input.
        const int cmd = snake_gfx_get_input();

        // Handle user input.
        switch (cmd) {

            // Quit command.
            case SNAKE_GFX_KEY_QUIT:
                enabled = false;
                break;

            // Movement commands.
            case SNAKE_GFX_KEY_U: snake_player_move_u(&player); break;
            case SNAKE_GFX_KEY_L: snake_player_move_l(&player); break;
            case SNAKE_GFX_KEY_D: snake_player_move_d(&player); break;
            case SNAKE_GFX_KEY_R: snake_player_move_r(&player); break;
        }

        // -- Update game entities. -- //

        // Update all snake segment positions.
        snake_player_update(&player);

        // Check if snake has collided with itself.
        if (snake_player_collision(player)) {

            // Shrink snake.
            snake_player_shrink(&player);
        }

        // Check if food has been eaten.
        if (snake_food_eaten(food, player)) {

            // Grow snake.
            snake_player_grow(&player);

            // Randomize food position.
            food = snake_food_new();
            snake_food_update(&food, player);
        }

        // -- Update the screen. -- //

        // Convert all game entities to gfx drawable entities.
        SnakeGfxEntity entities[SNAKE_CELLS];
        const size_t entites_len = snake_entities_load(entities, SNAKE_CELLS, player, food);

        // Draw the collection of game entities to the screen.
        snake_gfx_update_screen(entities, entites_len, &renderer);

        // -- Delay until the next frame. -- //

        snake_gfx_frame_delay(&frame_time_start);

    } while (enabled);

    // -- Terminate the graphics system. -- //

    snake_gfx_terminate(&window);

    return EXIT_SUCCESS;
}