// Moves the snake for each iteration void snake_move_player(pos head) { attrset(COLOR_PAIR(1)) ; int idx = snake_cooridinate_to_index(head); if (spaces[idx]) snake_game_over(); spaces[idx] = true; enqueue(head); g_score += 10; if (head.x == fruit.x && head.y == fruit.y) { snake_draw_fruit(); g_score += 1000; } else { pos* tail = dequeue(); spaces[snake_cooridinate_to_index(*tail)] = false; snake_write_text(tail->y, tail->x, " "); } snake_write_text(head.y, head.x, "S"); char buffer[25]; sprintf(buffer, "%d", g_score); attrset(COLOR_PAIR(2)); snake_write_text(g_height + 1, 9, buffer); }
// Handles moving the snake for each iteration bool snake_move_player( pos head ) { attrset( COLOR_PAIR( 1 ) ) ; // Check if we ran into ourself int idx = snake_cooridinate_to_index( head ); if( spaces[idx] ) snake_game_over( ); spaces[idx] = true; // Mark the space as occupied enqueue( head ); g_score += 10; // Check if we're eating the fruit if( head.x == fruit.x && head.y == fruit.y ) { snake_draw_fruit( ); g_score += 1000; } else { // Handle the tail pos *tail = dequeue( ); spaces[snake_cooridinate_to_index( *tail )] = false; snake_write_text( tail->y, tail->x, " " ); } // Draw the new head snake_write_text( head.y, head.x, "S" ); // Update scoreboard char buffer[25]; sprintf( buffer, "%d", g_score ); attrset( COLOR_PAIR( 2 ) ); snake_write_text( g_height+1, 9, buffer ); }
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(); }
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( ); }