diff --git a/snake.h b/snake.h index ebe1192..7f48ded 100644 --- a/snake.h +++ b/snake.h @@ -11,10 +11,10 @@ using namespace std; using std::chrono::system_clock; using namespace std::this_thread; -char direction='r'; +char direction = 'r'; -void input_handler(){ +void input_handler() { // change terminal settings struct termios oldt, newt; tcgetattr(STDIN_FILENO, &oldt); @@ -22,80 +22,85 @@ void input_handler(){ // turn off canonical mode and echo newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); + map keymap = {{'d', 'r'}, {'a', 'l'}, {'w', 'u'}, {'s', 'd'}, {'q', 'q'}}; while (true) { char input = getchar(); if (keymap.find(input) != keymap.end()) { // This now correctly modifies the single, shared 'direction' variable direction = keymap[input]; - }else if (input == 'q'){ + } else if (input == 'q') { exit(0); } - // You could add an exit condition here, e.g., if (input == 'q') break; } tcsetattr(STDIN_FILENO, TCSANOW, &oldt); } - -void render_game(int size, deque> &snake, pair food){ - for(size_t i=0;i> &snake, pair food) { + for (size_t i = 0; i < size; i++) { + for (size_t j = 0; j < size; j++) { + if (i == food.first && j == food.second) { + cout << "🍒"; // changed from apple 🍎 to cherry 🍒 + } else if (find(snake.begin(), snake.end(), make_pair(int(i), int(j))) != snake.end()) { cout << "🐍"; - }else{ + } else { cout << "⬜"; } + } + cout << endl; } - cout << endl; -} } -pair get_next_head(pair current, char direction){ - pair next; - if(direction =='r'){ - next = make_pair(current.first,(current.second+1) % 10); - }else if (direction=='l') - { - next = make_pair(current.first, current.second==0?9:current.second-1); - }else if(direction =='d'){ - next = make_pair((current.first+1)%10,current.second); - }else if (direction=='u'){ - next = make_pair(current.first==0?9:current.first-1, current.second); - } +pair get_next_head(pair current, char direction) { + pair next; + if (direction == 'r') { + next = make_pair(current.first, (current.second + 1) % 10); + } else if (direction == 'l') { + next = make_pair(current.first, current.second == 0 ? 9 : current.second - 1); + } else if (direction == 'd') { + next = make_pair((current.first + 1) % 10, current.second); + } else if (direction == 'u') { + next = make_pair(current.first == 0 ? 9 : current.first - 1, current.second); + } return next; - } - - -void game_play(){ +void game_play() { + // Welcome screen + cout << "==============================" << endl; + cout << " Welcome to Snake Game 🐍 " << endl; + cout << " Use W/A/S/D to move, Q to quit" << endl; + cout << "==============================" << endl; + sleep_for(1s); // pause so player can read system("clear"); + deque> snake; - snake.push_back(make_pair(0,0)); + snake.push_back(make_pair(0, 0)); pair food = make_pair(rand() % 10, rand() % 10); - for(pair head=make_pair(0,1);; head = get_next_head(head, direction)){ + for (pair head = make_pair(0, 1);; head = get_next_head(head, direction)) { // send the cursor to the top cout << "\033[H"; + // check self collision if (find(snake.begin(), snake.end(), head) != snake.end()) { system("clear"); - cout << "Game Over" << endl; + cout << "Game Over 😢" << endl; + cout << "Final length of snake: " << snake.size() << endl; exit(0); - }else if (head.first == food.first && head.second == food.second) { + } else if (head.first == food.first && head.second == food.second) { // grow snake food = make_pair(rand() % 10, rand() % 10); - snake.push_back(head); - }else{ + snake.push_back(head); + } else { // move snake snake.push_back(head); snake.pop_front(); } + render_game(10, snake, food); - cout << "length of snake: " << snake.size() << endl; - + cout << "Length of snake: " << snake.size() << endl; + sleep_for(500ms); } }