Example #1
0
File: main.c Project: elekmad/snake
/**
* Use first to init the world and place the apple
*/
void draw_world(SDL_Surface *s, char world[SCREEN_X_SIZE][SCREEN_Y_SIZE][4])
{
    int x, y;
    for(x = 0; x < SCREEN_X_SIZE; x++)
    {
	for(y = 0; y < SCREEN_Y_SIZE; y++)
	    draw_frame(s, x, y, background);
    }
    place_apple(s); 
}
Example #2
0
int _update_nodes(int x_inc, int y_inc)
{
    struct NODE *n = NULL;
    int i = 0;
    int od_index = 0;
    int x_new = f_state.head->x + x_inc;
    int y_new = f_state.head->y + y_inc;

    if (has_node(x_new, y_new) || x_new < ARENA_START + 1 || y_new < ARENA_START + 1
        || x_new >= ARENA_START + ARENA_SIZE || y_new >= ARENA_START + ARENA_SIZE)
    {
        return 1;
    }

    if (f_state.node_count >= f_state.size)
    {
        _new_head(f_state.tail, x_new, y_new);
        f_state.tail = f_state.tail->next;
        h3m_object_move(f_state.h3m, f_state.head->od_index, f_state.head->x, f_state.head->y, 0);
    }
    else
    {
        n = calloc(1, sizeof(*n));
        _new_head(n, x_new, y_new);

        ++f_state.node_count;

        h3m_object_add(f_state.h3m, "Serpent Fly", n->x, n->y, 0, &n->od_index);
    }

    if (has_apple(x_new, y_new))
    {
        place_apple();
        place_caged();
        ++f_state.score;
        ++f_state.size;
    }

    return 0;
}
Example #3
0
int h3msnake_init(const char *filename)
{
    struct NODE *nodes[START_SIZE];
    int od_index = 0;

    memset(&f_state, 0, sizeof(f_state));
    h3m_init_min(&f_state.h3m, H3M_FORMAT_ROE, 36);
    h3m_write(f_state.h3m, filename);

    srand((unsigned int)time(NULL));

    f_state.filename = _strdup(filename);
    g_map_filename = f_state.filename;
    f_state.direction = DIR_RIGHT;

    for (int i = 0; i < START_SIZE; ++i)
    {
        h3m_object_add(f_state.h3m, "Serpent Fly", START_POS + i, START_POS, 0, &od_index);
        nodes[i] = calloc(1, sizeof(*(nodes[i])));
        nodes[i]->od_index = od_index;
        nodes[i]->x = START_POS + i;
        nodes[i]->y = START_POS;
    }
    f_state.head = nodes[START_SIZE - 1];
    f_state.tail = nodes[0];

    for (int i = 0; i < START_SIZE - 1; ++i)
    {
        nodes[i]->next = nodes[i + 1];
        nodes[i]->prev = (0 != -i) ? nodes[i - 1] : NULL;
    }
    nodes[0]->prev = f_state.head;
    nodes[START_SIZE - 1]->next = f_state.tail;
    f_state.size = START_SIZE;
    f_state.node_count = START_SIZE;
    //f_state.tail->prev = f_state.head;

    uint8_t terrain_types[36 * 36];
    uint8_t river_types[36 * 36];
    uint8_t road_types[36 * 36];
    uint8_t impassable[36][36];

    f_state.terrain = (rand() + 1) % 8;
    memset(terrain_types, f_state.terrain, sizeof(terrain_types));
    memset(river_types, 0, sizeof(river_types));
    memset(road_types, 0, sizeof(road_types));
    memset(impassable, 1, sizeof(impassable));

#if 1
    h3m_generate_tiles(f_state.h3m, 36, 0, (uint8_t *)terrain_types, (uint8_t *)road_types, (uint8_t *)river_types);

    const char *defs_apple[] = { "Skeleton", "Gold Golem", "Pikeman", "Gremlin", "Gnoll", "Goblin", "Troglodyte", "Imp" };
    const char *defs_wall[] = { "Skeleton Warrior", "Diamond Golem", "Halberdier", "Master Gremlin", "Gnoll Marauder", "Hobgoblin", "Infernal Troglodyte", "Familiar" };

    for (int i = ARENA_START; i < ARENA_START+ARENA_SIZE; ++i)
    {
        h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], i, ARENA_START + ARENA_SIZE, 0, NULL);
        h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], ARENA_START + ARENA_SIZE, i, 0, NULL);
        h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], i, ARENA_START, 0, NULL);
        h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], ARENA_START, i, 0, NULL);
        for (int j = ARENA_START; j < ARENA_START + ARENA_SIZE + 1; ++j)
            impassable[i][j] = 0;

        for (int j = CAGE_START; j < CAGE_START + CAGE_SIZE; ++j)
            impassable[i][j] = 0;
    }

    h3m_impassable_fill(f_state.h3m, (uint8_t *)impassable, 36);
    h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], ARENA_START + ARENA_SIZE, ARENA_START, 0, NULL);
    h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], ARENA_START, ARENA_START + ARENA_SIZE, 0, NULL);
    h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], ARENA_START, ARENA_START, 0, NULL);
    h3m_object_add(f_state.h3m, defs_wall[f_state.terrain], ARENA_START + ARENA_SIZE, ARENA_START + ARENA_SIZE, 0, NULL);

#endif

    f_state.def_apple = defs_apple[f_state.terrain];

    h3m_object_add(f_state.h3m, f_state.def_apple, 15, 0, 0, &f_state.apple.od_index);

    place_apple();

    return 0;
}
Example #4
0
File: main.c Project: elekmad/snake
int move_snake(SDL_Surface *s, int xspeed, int yspeed)
{
    static timer = 0;
    if(timer < TIMER)
    {
	timer++;
	return 0;
    }
    timer = 0;
/*
[0] => droite/gauche => 0 : xspeed > 0, 1 : xspeed < 0
[1] => bas/haut => 0 : yspeed > 0, 1 : yspeed < 0
[2] => ver/pas de ver => 0 : pas de ver, 1 : ver
*/
    if(actual_size == snake_size)
    {
        int new_old_x = old_x, new_old_y = old_y;
	if(world[old_x][old_y][0] > 0)
	{
	    new_old_x = old_x + 1;
	    if(new_old_x >= SCREEN_X_SIZE)
		new_old_x = 0;
	}
	else if(world[old_x][old_y][0] < 0)
	{
	    new_old_x = old_x - 1;
	    if(new_old_x < 0)
		new_old_x = SCREEN_X_SIZE - 1;
	}
	if(world[old_x][old_y][1] > 0)
	{
	    new_old_y = old_y + 1;
	    if(new_old_y >= SCREEN_Y_SIZE)
		new_old_y = 0;
	}
	else if(world[old_x][old_y][1] < 0)
	{
	    new_old_y = old_y - 1;
	    if(new_old_y < 0)
		new_old_y = SCREEN_Y_SIZE - 1;
	}
	printf("move ass : [%d, %d] => [%d, %d]\n", old_x, old_y, new_old_x, new_old_y);
        draw_frame(s, old_x, old_y, background);
        world[old_x][old_y][0] = 0;
        world[old_x][old_y][1] = 0;
        world[old_x][old_y][2] = 0;
        old_x = new_old_x;
        old_y = new_old_y;
    }
    else
    {
	actual_size++;
	printf("draw ass : [%d, %d]\n", old_x, old_y);
    }
    world[x][y][0] = xspeed;
    world[x][y][1] = yspeed;
    world[x][y][2] = 2;
    x += xspeed;
    if(x >= SCREEN_X_SIZE)
	x = 0;
    if(x < 0)
	x = SCREEN_X_SIZE - 1;
    y += yspeed;
    if(y >= SCREEN_Y_SIZE)
	y = 0;
    if(y < 0)
	y = SCREEN_Y_SIZE - 1;

    switch((enum world_content)world[x][y][2])
    {
        case wall :
        case snake :
	    return -1;
        case apple :
            snake_size += 10;
            place_apple(s);
            //FALLTROUGH
        case background :
            draw_frame(s, x, y, snake);
            return 0;
    }
    return 0;
}