最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

snake.cpp

2023-06-28 19:44 作者:Euler_Formula  | 我要投稿

''' #include #include #include #include #include #include using namespace std; bool gameover; const int width = 20; const int height = 20; int x, y, fruitx, fruity, score; int tailx[100], taily[100], ntail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; struct termios stored_settings; void Setup() { ??gameover = false; ??dir = STOP; ??x = width / 2; ??y = height / 2; ??fruitx = rand() % width; ??fruity = rand() % height; ??score = 0; } void Draw() { ??system("clear"); // 清空屏幕 ??for (int i = 0; i < width + 2; i++) // 畫出上邊緣 ????cout << "#"; ??cout << endl; ??for (int i = 0; i < height; i++) // 畫出每一行的內容 ??{ ????for (int j = 0; j < width; j++) ????{ ??????if (j == 0) ????????cout << "#"; // 左邊緣 ??????if (i == y && j == x) ????????cout << "O"; // 蛇頭位置 ??????else if (i == fruity && j == fruitx) ????????cout << "F"; // 果實位置 ??????else ??????{ ????????bool print = false; ????????for (int k = 0; k < ntail; k++) ????????{ ??????????if (tailx[k] == j && taily[k] == i) ??????????{ ????????????cout << "o"; ????????????print = true; ??????????} ????????} ????????if (!print) ??????????cout << " "; ??????} ??????if (j == width - 1) ????????cout << "#"; // 右邊緣 ????} ????cout << endl; ??} ??for (int i = 0; i < width + 2; i++) // 畫出下邊緣 ????cout << "#"; ??cout << endl; ??cout << "Score:" << score << endl; } void Input() { ??char c; ??if (read(STDIN_FILENO, &c, 1) == 1) ??{ ????switch (c) ????{ ????case 'a': ??????dir = LEFT; ??????break; ????case 'd': ??????dir = RIGHT; ??????break; ????case 'w': ??????dir = UP; ??????break; ????case 's': ??????dir = DOWN; ??????break; ????case 'x': ??????gameover = true; ??????break; ????} ??} } void Logic() { ??int prevx = tailx[0]; ??int prevy = taily[0]; ??int prev2x, prev2y; ??tailx[0] = x; ??taily[0] = y; ??for (int i = 1; i < ntail; i++) ??{ ????prev2x = tailx[i]; ????prev2y = taily[i]; ????tailx[i] = prevx; ????taily[i] = prevy; ????prevx = prev2x; ????prevy = prev2y; ??} ??switch (dir) ??{ ??case LEFT: ????x--; ????break; ??case RIGHT: ????x++; ????break; ??case UP: ????y--; ????break; ??case DOWN: ????y++; ????break; ??default: ????break; ??} ??// 撞墻 ??if (x >= width) ????x = 0; ??else if (x < 0) ????x = width - 1; ??if (y >= height) ????y = 0; ??else if (y < 0) ????y = height - 1; ??// 撞到自己 ??for (int i = 0; i < ntail; i++) ??{ ????if (tailx[i] == x && taily[i] == y) ??????gameover = true; ??} ??if (x == fruitx && y == fruity) ??{ ????score += 10; ????fruitx = rand() % width; ????fruity = rand() % height; ????ntail++; ??} } int main() { ??Setup(); ??struct termios new_settings; ??tcgetattr(STDIN_FILENO, &stored_settings); ??new_settings = stored_settings; ??new_settings.c_lflag &= (~ICANON & ~ECHO); ??tcsetattr(STDIN_FILENO, TCSANOW, &new_settings); ??while (!gameover) ??{ ????Draw(); ????Input(); ????Logic(); ????usleep(50000); // 蛇的速度限制 ??} ??tcsetattr(STDIN_FILENO, TCSANOW, &stored_settings); ??return 0; } '''

snake.cpp的評論 (共 條)

分享到微博請遵守國家法律
抚州市| 建德市| 保德县| 河曲县| 香格里拉县| 根河市| 通化市| 宁阳县| 中西区| 博野县| 鄂伦春自治旗| 南城县| 乐至县| 通山县| 怀远县| 托克托县| 九江市| 林口县| 墨脱县| 淅川县| 巴林右旗| 延吉市| 贵溪市| 邯郸县| 广灵县| 灌阳县| 类乌齐县| 晋中市| 佳木斯市| 琼结县| 临夏县| 博乐市| 海原县| 深圳市| 怀集县| 台湾省| 隆德县| 广东省| 安仁县| 沙雅县| 恩平市|