Viết mã C++ rắn săn mồi chạy trên cmd windows
- User
Viết mã C++ rắn săn mồi chạy trên cmd windows
- Assistant
Đây là một đoạn mã C++ đơn giản để tạo ra trò chơi rắn săn mồi chạy trên cmd windows:
#include <iostream> #include <conio.h> #include <windows.h> #include <vector> using namespace std; const int width = 50; const int height = 20; const int size = width * height; int headPosition = size / 2 + width / 2; bool gameOver = false; int score = 0; vector<int> tail; enum direction {UP, DOWN, LEFT, RIGHT}; direction dir; void Setup() { dir = LEFT; } void Draw() { system("cls"); // clear console for (int i = 0; i < size; i++) { if (i % width == 0) cout << endl; if (i == headPosition) cout << "0"; else if (i == rand() % size) cout << "F"; else if (find(tail.begin(), tail.end(), i) != tail.end()) cout << "o"; else cout << " "; } cout << endl << "Score: " << score << endl; } void Input() { if (_kbhit()) { switch(_getch()) { case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'x': gameOver = true; break; } } } void Logic() { int prevPosition = tail.empty() ? headPosition : tail.back(); int x = prevPosition % width; int y = prevPosition / width; switch(dir) { case UP: y--; break; case DOWN: y++; break; case LEFT: x--; break; case RIGHT: x++; break; } int newPosition = y * width + x; if (find(tail.begin(), tail.end(), newPosition) != tail.end()) { gameOver = true; return; } tail.insert(tail.begin(), headPosition); while (tail.size() > score + 1) { tail.pop_back(); } headPosition = newPosition; if (headPosition == tail.back()) { tail.pop_back(); } if (headPosition == rand() % size) { score++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(100); } return 0; }Để chạy chương trình, bạn chỉ cần sao chép đoạn mã trên vào trình biên dịch C++ của bạn và chạy. Sau đó, game sẽ hiện ra trên màn hình console của bạn và bạn có thể bắt đầu chơi trò chơi rắn săn mồi của mình!
- User
fatal error: windows.h: No such file or directory 3 | #include <windows.h> | ^~~~~~~~~~~ compilation terminated.
- Assistant
Lỗi này xảy ra khi trình biên dịch không tìm thấy tệp tin
windows.htrong hệ thống của bạn. Điều này thường xảy ra khi bạn đang sử dụng trình biên dịch trên macOS hoặc Linux.Để giải quyết vấn đề này, bạn có thể sử dụng các của thư viện khác để thay thế cho
windows.h. Ví dụ, bạn có thể sử dụng thư việnncursesđể thay thế chowindows.h.Bạn có thể cài đặt
ncursesbằng cách sử dụng trình quản lý gói của hệ thống của bạn. Ví dụ, trên Ubuntu, bạn có thể cài đặtncursesbằng lệnh sau:sudo apt-get install libncurses5-dev libncursesw5-devSau khi cài đặt
ncurses, bạn có thể sửa đổi mã C++ của mình như sau:#include <iostream> #include <ncurses.h> #include <vector> using namespace std; const int width = 50; const int height = 20; const int size = width * height; int headPosition = size / 2 + width / 2; bool gameOver = false; int score = 0; vector<int> tail; enum direction {UP, DOWN, LEFT, RIGHT}; direction dir; void Setup() { initscr(); // initialize ncurses clear(); // clear screen noecho(); // disable echoing of characters cbreak(); // disable line buffering curs_set(0); // hide cursor keypad(stdscr, TRUE); // enable function keys (e.g. arrow keys) nodelay(stdscr, TRUE); // set getch() to non-blocking mode dir = LEFT; } void Draw() { clear(); // clear screen for (int i = 0; i < size; i++) { if (i % width == 0) { move(i / width, 0); // move cursor to new line addch('\n'); // print newline character } if (i == headPosition) addch('O'); else if (i == rand() % size) addch('F'); else if (find(tail.begin(), tail.end(), i) != tail.end()) addch('o'); else addch(' '); } mvprintw(height + 1, 0, "Score: %d\n", score); // print score refresh(); // update screen } void Input() { int key = getch(); switch(key) { case KEY_UP: if (dir != DOWN) dir = UP; break; case KEY_DOWN: if (dir != UP) dir = DOWN; break; case KEY_LEFT: if (dir != RIGHT) dir = LEFT; break; case KEY_RIGHT: if (dir != LEFT) dir = RIGHT; break; case 'x': gameOver = true; break; } } void Logic() { int prevPosition = tail.empty() ? headPosition : tail.back(); int x = prevPosition % width; int y = prevPosition / width; switch(dir) { case UP: y--; break; case DOWN: y++; break; case LEFT: x--; break; case RIGHT: x++; break; } int newPosition = y * width + x; if (find(tail.begin(), tail.end(), newPosition) != tail.end()) { gameOver = true; return; } tail.insert(tail.begin(), headPosition); while (tail.size() > score + 1) { tail.pop_back(); } headPosition = newPosition; if (headPosition == tail.back()) { tail.pop_back(); } if (headPosition == rand() % size) { score++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); usleep(100000); // sleep for 100 milliseconds } endwin(); // end ncurses return 0; }Ngoài ra, mã trên sử dụng
usleep()để tạm dừng chương trình trong 100 milliseconds. Bạn cũng có thể sử dụng hàmstd::this_thread::sleep_for()nếu đang sử dụng C++11 trở lên.Chúc bạn thành công!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.