Example #1
0
File: snake.c Project: g-d-l/snaker
// Draw the fruit somewhere randomly on the board
void snake_draw_fruit(void) {
    attrset(COLOR_PAIR(3));
    int idx;
    do {
        idx = rand() % (g_width * g_height);
        fruit = snake_index_to_coordinate(idx);
    }
    while(spaces[idx] || !snake_in_bounds(fruit));    
    snake_write_text(fruit.y, fruit.x, "F");
}
Example #2
0
File: snake.c Project: g-d-l/snaker
int main(int argc, char *argv[]) {
    int move = KEY_RIGHT;
    if ((g_mainwin = initscr()) == NULL) {
        perror("could not initialize ncurses\n");
        exit(EXIT_FAILURE);
    }
    
    srand(time(NULL));
    noecho();
    curs_set(2);
    halfdelay(1);
    keypad(g_mainwin, TRUE);
    g_oldcur = curs_set(0);
    start_color();
    init_pair(1, COLOR_RED,     COLOR_BLACK);
    init_pair(2, COLOR_GREEN,   COLOR_BLACK);
    init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
    init_pair(4, COLOR_BLUE,    COLOR_BLACK);
    init_pair(5, COLOR_CYAN,    COLOR_BLACK);
    init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
    init_pair(7, COLOR_WHITE,   COLOR_BLACK);
    getmaxyx(g_mainwin, g_height, g_width);
    
    g_width = g_width  < DESIRED_WIDTH ? g_width : DESIRED_WIDTH;
    g_height = g_height < DESIRED_HEIGHT ? g_height : DESIRED_HEIGHT; 
    
    spaces = malloc(sizeof(bool) * g_height * g_width);

    snake_draw_board();
    snake_draw_fruit();
    pos head = {5, 5};
    enqueue(head);
    
    while(true) {
        int key_stroke = getch();
        if (key_stroke != ERR)
            move = key_stroke;
        switch(move) {
            case KEY_UP:
            case 'w':
            case 'W':
                head.y--;
                break;
            case KEY_LEFT:
            case 'a':
            case 'A':
                head.x--;
                break;
            case KEY_DOWN:
            case 's':
            case 'S':
                head.y++;
                break;
            case KEY_RIGHT:
            case 'd':
            case 'D':
                head.x++;
                break;
        }
        if(!snake_in_bounds(head))    
            snake_game_over();
        else
            snake_move_player(head);
    }
    snake_game_over();
}
Example #3
0
File: snake.c Project: mnisjk/snake
int main( int argc, char *argv[] )
{
    int key = KEY_RIGHT;
    if( ( g_mainwin = initscr() ) == NULL ) {
        perror( "error initialising ncurses" );
        exit( EXIT_FAILURE );
    }
    
    // Set up
    srand( time( NULL ) );
    noecho( );
    curs_set( 2 );
    halfdelay( 1 );
    keypad( g_mainwin, TRUE );
    g_oldcur = curs_set( 0 );
    start_color( );
    init_pair( 1, COLOR_RED,     COLOR_BLACK );
    init_pair( 2, COLOR_GREEN,   COLOR_BLACK );
    init_pair( 3, COLOR_YELLOW,  COLOR_BLACK );
    init_pair( 4, COLOR_BLUE,    COLOR_BLACK );
    init_pair( 5, COLOR_CYAN,    COLOR_BLACK );
    init_pair( 6, COLOR_MAGENTA, COLOR_BLACK );
    init_pair( 7, COLOR_WHITE,   COLOR_BLACK );
    getmaxyx( g_mainwin, g_height, g_width );
    
    g_width  = g_width  < DESIRED_WIDTH  ? g_width  : DESIRED_WIDTH;
    g_height = g_height < DESIRED_HEIGHT ? g_height : DESIRED_HEIGHT; 
    
    // Set up the 2D array of all spaces
    spaces = (bool*) malloc( sizeof( bool ) * g_height * g_width );

    snake_draw_board( );
    snake_draw_fruit( );
    pos head = { 5,5 };
    enqueue( head );
    
    // Event loop
    while( 1 )
    {
        int in = getch( );
        if( in != ERR )
            key = in;
        switch( key )
        {
            case KEY_DOWN:
            case 'k':
            case 'K':
                head.y++;
                break;
            case KEY_RIGHT:
            case 'l':
            case 'L':
                head.x++;
                break;
            case KEY_UP:
            case 'j':
            case 'J':
                head.y--;
                break;
            case KEY_LEFT:
            case 'h':
            case 'H':
                head.x--;
                break;

        }
        if( !snake_in_bounds( head ) )    
            snake_game_over( );
        else
            snake_move_player( head );
    }
    snake_game_over( );
}