// 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 ); }
void snake_run(){ _delay_ms(200); snake_init(snake); snake_print(snake); uint16_t delay = 15000 / (s_difficulty * 1.0); while (1){ if(timer_read(TIMER_3) > delay){ timer_reset(TIMER_3); if(joystick_x_value() > THRESHOLD && (direction != left)){ direction = right; } else if(joystick_x_value() < -THRESHOLD && (direction != right)){ direction = left; } else if(joystick_y_value() > THRESHOLD && (direction != down)){ direction = up; } else if(joystick_y_value() < -THRESHOLD && (direction != up)){ direction = down; } if(button_right_read()){ snake_add_size(snake); } snake_move(direction, snake); //if we hit border if((snake[0][pos_x] > 15) || (snake[0][pos_x] < 0)){ snake_game_over(); } if((snake[0][pos_y] > 7) || (snake[0][pos_y] < 0)){ snake_game_over(); } //if snake head position == apple position if((snake[0][pos_x] == apple[0]) && (snake[0][pos_y] == apple[1]) && (apple[2])){ //apple bool value = 0, add snake size apple[2] = 0; snake_points++; snake_add_size(snake); } if(!(apple[2])){ snake_make_apple(); } if(snake_self_collide(snake)){ snake_game_over(); } snake_print(snake); oled_write_screen(); } //leave game if(button_left_read()){ return; } } }
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( ); }