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

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

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的評論 (共 條)

分享到微博請遵守國家法律
二连浩特市| 长寿区| 绍兴县| 稻城县| 靖安县| 商洛市| 江门市| 丰原市| 育儿| 扬州市| 温州市| 海伦市| 长治市| 兴和县| 城口县| 博客| 阿城市| 桦甸市| 东至县| 辛集市| 鞍山市| 桐庐县| 灌云县| 乌恰县| 五家渠市| 五峰| 裕民县| 永平县| 天长市| 拜城县| 沽源县| 吉木乃县| 大名县| 宁远县| 武汉市| 石首市| 蓝田县| 高碑店市| 喀喇| 孟州市| 固镇县|