diff --git a/as2/1717608_Liu_Yimian_2.pdf b/as2/1717608_Liu_Yimian_2.pdf new file mode 100644 index 0000000..61d996d Binary files /dev/null and b/as2/1717608_Liu_Yimian_2.pdf differ diff --git a/as2/1717608_Liu_Yimian_2.zip b/as2/1717608_Liu_Yimian_2.zip new file mode 100644 index 0000000..f5c4438 Binary files /dev/null and b/as2/1717608_Liu_Yimian_2.zip differ diff --git a/as3/code/GreedySnake/GreedySnake.cbp b/as3/code/GreedySnake/GreedySnake.cbp new file mode 100644 index 0000000..0b713c6 --- /dev/null +++ b/as3/code/GreedySnake/GreedySnake.cbp @@ -0,0 +1,42 @@ + + + + + + diff --git a/as3/code/GreedySnake/README.md b/as3/code/GreedySnake/README.md new file mode 100644 index 0000000..5de8a67 --- /dev/null +++ b/as3/code/GreedySnake/README.md @@ -0,0 +1,10 @@ +# GreedySnake +c++经典项目贪吃蛇游戏控制台版,详细注释。 +(点个star再走不急) +教程地址:http://blog.csdn.net/silence1772/article/details/55005008 + +## 游戏截图: +![](https://github.com/silence1772/GreedySnake/raw/master/shot01.jpg) +![](https://github.com/silence1772/GreedySnake/raw/master/shot02.jpg) +![](https://github.com/silence1772/GreedySnake/raw/master/shot03.gif) +![](https://github.com/silence1772/GreedySnake/raw/master/shot04.gif) diff --git a/as3/code/GreedySnake/controller.cpp b/as3/code/GreedySnake/controller.cpp new file mode 100644 index 0000000..954e503 --- /dev/null +++ b/as3/code/GreedySnake/controller.cpp @@ -0,0 +1,571 @@ +#include +#include +#include +#include +#include "controller.h" +#include "tools.h" +#include "startinterface.h" +#include "map.h" +#include "snake.h" +#include "food.h" + +void Controller::Start()//ʼ +{ + SetWindowSize(41, 32);//ôڴС + SetColor(2);//ÿʼɫ + StartInterface *start = new StartInterface();//̬һStartInterfacestart + start->Action();//ʼ + delete start;//ͷڴռ + + /*ùرλãʾȴ*/ + SetCursorPosition(13, 26); + std::cout << "Press any key to start... " ; + SetCursorPosition(13, 27); + system("pause"); +} + +void Controller::Select()//ѡ +{ + /*ʼѡ*/ + SetColor(3); + SetCursorPosition(13, 26); + std::cout << " " ; + SetCursorPosition(13, 27); + std::cout << " " ; + SetCursorPosition(6, 21); + std::cout << "ѡϷѶȣ" ; + SetCursorPosition(6, 22); + std::cout << "(¼ѡ,سȷ)" ; + SetCursorPosition(27, 22); + SetBackColor();//һѡñɫԱʾǰѡ + std::cout << "ģʽ" ; + SetCursorPosition(27, 24); + SetColor(3); + std::cout << "ͨģʽ" ; + SetCursorPosition(27, 26); + std::cout << "ģʽ" ; + SetCursorPosition(27, 28); + std::cout << "ģʽ" ; + SetCursorPosition(0, 31); + score = 0; + + /*·ѡģ*/ + int ch;//¼ֵ + key = 1;//¼ѡʼѡһ + bool flag = false;//¼ǷEnterǣʼΪ + while ((ch = getch())) + { + switch (ch)// + { + case 72://UPϷ + if (key > 1)//ʱѡΪһʱUPϷЧ + { + switch (key) + { + case 2: + SetCursorPosition(27, 22);//ѡñɫ + SetBackColor(); + std::cout << "ģʽ" ; + + SetCursorPosition(27, 24);//ѡȡұɫ + SetColor(3); + std::cout << "ͨģʽ" ; + + --key; + break; + case 3: + SetCursorPosition(27, 24); + SetBackColor(); + std::cout << "ͨģʽ" ; + + SetCursorPosition(27, 26); + SetColor(3); + std::cout << "ģʽ" ; + + --key; + break; + case 4: + SetCursorPosition(27, 26); + SetBackColor(); + std::cout << "ģʽ" ; + + SetCursorPosition(27, 28); + SetColor(3); + std::cout << "ģʽ" ; + + --key; + break; + } + } + break; + + case 80://DOWN· + if (key < 4) + { + switch (key) + { + case 1: + SetCursorPosition(27, 24); + SetBackColor(); + std::cout << "ͨģʽ" ; + SetCursorPosition(27, 22); + SetColor(3); + std::cout << "ģʽ" ; + + ++key; + break; + case 2: + SetCursorPosition(27, 26); + SetBackColor(); + std::cout << "ģʽ" ; + SetCursorPosition(27, 24); + SetColor(3); + std::cout << "ͨģʽ" ; + + ++key; + break; + case 3: + SetCursorPosition(27, 28); + SetBackColor(); + std::cout << "ģʽ" ; + SetCursorPosition(27, 26); + SetColor(3); + std::cout << "ģʽ" ; + + ++key; + break; + } + } + break; + + case 13://Enterس + flag = true; + break; + default://Ч + break; + } + if (flag) break;//Enterسȷϣ˳ѭ + + SetCursorPosition(0, 31);//½ǣر˸ӰϷ + } + + switch (key)//ѡѡߵƶٶȣspeedֵԽСٶԽ + { + case 1: + speed = 135; + break; + case 2: + speed = 100; + break; + case 3: + speed = 60; + break; + case 4: + speed = 30; + break; + default: + break; + } +} + +void Controller::DrawGame()//Ϸ +{ + system("cls");// + + /*Ƶͼ*/ + SetColor(3); + Map *init_map = new Map(); + init_map->PrintInitmap(); + delete init_map; + + /*Ʋ*/ + SetColor(3); + SetCursorPosition(33, 1); + std::cout << "Greedy Snake" ; + SetCursorPosition(34, 2); + std::cout << "̰" ; + SetCursorPosition(31, 4); + std::cout << "Ѷȣ" ; + SetCursorPosition(36, 5); + switch (key) + { + case 1: + std::cout << "ģʽ" ; + break; + case 2: + std::cout << "ͨģʽ" ; + break; + case 3: + std::cout << "ģʽ" ; + break; + case 4: + std::cout << "ģʽ" ; + break; + default: + break; + } + SetCursorPosition(31, 7); + std::cout << "÷֣" ; + SetCursorPosition(37, 8); + std::cout << " 0" ; + SetCursorPosition(33, 13); + std::cout << " ƶ" ; + SetCursorPosition(33, 15); + std::cout << " ESCͣ" ; +} + +int Controller::PlayGame()//Ϸѭ +{ + /*ʼߺʳ*/ + Snake *csnake = new Snake(); + Food *cfood = new Food(); + SetColor(6); + csnake->InitSnake(); + srand((unsigned)time(NULL));//ӣû ʳijλý̶ + cfood->DrawFood(*csnake); + + /*Ϸѭ*/ + while (csnake->OverEdge() && csnake->HitItself()) //жǷײǽײǷ + { + /*ѡ˵*/ + if (!csnake->ChangeDirection()) //Escʱ + { + int tmp = Menu();//Ʋ˵õֵ + switch (tmp) + { + case 1://Ϸ + break; + + case 2://¿ʼ + delete csnake; + delete cfood; + return 1;//1ΪPlayGameķֵصGameУʾ¿ʼ + + case 3://˳Ϸ + delete csnake; + delete cfood; + return 2;//2ΪPlayGameķֵصGameУʾ˳Ϸ + + default: + break; + } + } + + if (csnake->GetFood(*cfood)) //Եʳ + { + csnake->Move();// + UpdateScore(1);//·1ΪȨ + RewriteScore();//»Ʒ + cfood->DrawFood(*csnake);//ʳ + } + else + { + csnake->NormalMove();//ƶ + } + + if (csnake->GetBigFood(*cfood)) //Եʱʳ + { + csnake->Move(); + UpdateScore(cfood->GetProgressBar()/5);//ʱʳȷ + RewriteScore(); + } + + if (cfood->GetBigFlag()) //ʱʱʳ˸ + { + cfood->FlashBigFood(); + } + + Sleep(speed);//ߵƶЧ + } + + /**/ + delete csnake;//ͷŷڴռ + delete cfood; + int tmp = GameOver();//Ϸ棬ѡ + switch (tmp) + { + case 1: + return 1;//¿ʼ + case 2: + return 2;//˳Ϸ + default: + return 2; + } +} + +void Controller::UpdateScore(const int& tmp)//· +{ + score += key * 10 * tmp;//÷ϷѶȼ˵IJtmpȷ +} + +void Controller::RewriteScore()//ػ +{ + /*Ϊַβ룬Ϊ6λ㵱ǰλʣλÿոȫ*/ + SetCursorPosition(37, 8); + SetColor(11); + int bit = 0; + int tmp = score; + while (tmp != 0) + { + ++bit; + tmp /= 10; + } + for (int i = 0; i < (6 - bit); ++i) + { + std::cout << " " ; + } + std::cout << score ; +} + +int Controller::Menu()//ѡ˵ +{ + /*Ʋ˵*/ + SetColor(11); + SetCursorPosition(32, 19); + std::cout << "˵" ; + Sleep(100); + SetCursorPosition(34, 21); + SetBackColor(); + std::cout << "Ϸ" ; + Sleep(100); + SetCursorPosition(34, 23); + SetColor(11); + std::cout << "¿ʼ" ; + Sleep(100); + SetCursorPosition(34, 25); + std::cout << "˳Ϸ" ; + SetCursorPosition(0, 31); + + /*ѡ񲿷*/ + int ch; + int tmp_key = 1; + bool flag = false; + while ((ch = getch())) + { + switch (ch) + { + case 72://UP + if (tmp_key > 1) + { + switch (tmp_key) + { + case 2: + SetCursorPosition(34, 21); + SetBackColor(); + std::cout << "Ϸ" ; + SetCursorPosition(34, 23); + SetColor(11); + std::cout << "¿ʼ" ; + + --tmp_key; + break; + case 3: + SetCursorPosition(34, 23); + SetBackColor(); + std::cout << "¿ʼ" ; + SetCursorPosition(34, 25); + SetColor(11); + std::cout << "˳Ϸ" ; + + --tmp_key; + break; + } + } + break; + + case 80://DOWN + if (tmp_key < 3) + { + switch (tmp_key) + { + case 1: + SetCursorPosition(34, 23); + SetBackColor(); + std::cout << "¿ʼ" ; + SetCursorPosition(34, 21); + SetColor(11); + std::cout << "Ϸ" ; + + ++tmp_key; + break; + case 2: + SetCursorPosition(34, 25); + SetBackColor(); + std::cout << "˳Ϸ" ; + SetCursorPosition(34, 23); + SetColor(11); + std::cout << "¿ʼ" ; + + ++tmp_key; + break; + } + } + break; + + case 13://Enter + flag = true; + break; + + default: + break; + } + + if (flag) + { + break; + } + SetCursorPosition(0, 31); + } + + if (tmp_key == 1) //ѡϷ򽫲˵ + { + SetCursorPosition(32, 19); + std::cout << " " ; + SetCursorPosition(34, 21); + std::cout << " "; + SetCursorPosition(34, 23); + std::cout << " "; + SetCursorPosition(34, 25); + std::cout << " "; + } + return tmp_key; +} + +void Controller::Game()//Ϸһѭ +{ + Start();//ʼ + while (true)//ϷΪһѭֱ˳Ϸʱѭ + { + Select();//ѡ + DrawGame();//Ϸ + int tmp = PlayGame();//Ϸѭ¿ʼ˳Ϸʱѭֵtmp + if (tmp == 1) //ֵΪ1ʱ¿ʼϷ + { + system("cls"); + continue; + } + else if (tmp == 2) //ֵΪ2ʱ˳Ϸ + { + break; + } + else + { + break; + } + } +} + +int Controller::GameOver()//Ϸ +{ + /*Ϸ*/ + Sleep(500); + SetColor(11); + SetCursorPosition(10, 8); + std::cout << "" ; + Sleep(30); + SetCursorPosition(9, 9); + std::cout << " Game Over !!! " ; + Sleep(30); + SetCursorPosition(9, 10); + std::cout << " " ; + Sleep(30); + SetCursorPosition(9, 11); + std::cout << " ź " ; + Sleep(30); + SetCursorPosition(9, 12); + std::cout << " " ; + Sleep(30); + SetCursorPosition(9, 13); + std::cout << " ķΪ " ; + SetCursorPosition(24, 13); + std::cout << score ; + Sleep(30); + SetCursorPosition(9, 14); + std::cout << " " ; + Sleep(30); + SetCursorPosition(9, 15); + std::cout << " Ƿһ֣ " ; + Sleep(30); + SetCursorPosition(9, 16); + std::cout << " " ; + Sleep(30); + SetCursorPosition(9, 17); + std::cout << " " ; + Sleep(30); + SetCursorPosition(9, 18); + std::cout << " ţõ ˣѧϰ˼ " ; + Sleep(30); + SetCursorPosition(9, 19); + std::cout << " " ; + Sleep(30); + SetCursorPosition(9, 20); + std::cout << " " ; + Sleep(30); + SetCursorPosition(10, 21); + std::cout << "" ; + + Sleep(100); + SetCursorPosition(12, 18); + SetBackColor(); + std::cout << "ţõ" ; + SetCursorPosition(0, 31); + + /*ѡ񲿷*/ + int ch; + int tmp_key = 1; + bool flag = false; + while ((ch = getch())) + { + switch (ch) + { + case 75://LEFT + if (tmp_key > 1) + { + SetCursorPosition(12, 18); + SetBackColor(); + std::cout << "ţõ" ; + SetCursorPosition(20, 18); + SetColor(11); + std::cout << "ˣѧϰ˼" ; + --tmp_key; + } + break; + + case 77://RIGHT + if (tmp_key < 2) + { + SetCursorPosition(20, 18); + SetBackColor(); + std::cout << "ˣѧϰ˼" ; + SetCursorPosition(12, 18); + SetColor(11); + std::cout << "ţõ" ; + ++tmp_key; + } + break; + + case 13://Enter + flag = true; + break; + + default: + break; + } + + SetCursorPosition(0, 31); + if (flag) { + break; + } + } + + SetColor(11); + switch (tmp_key) + { + case 1: + return 1;//¿ʼ + case 2: + return 2;//˳Ϸ + default: + return 1; + } +} diff --git a/as3/code/GreedySnake/controller.h b/as3/code/GreedySnake/controller.h new file mode 100644 index 0000000..f90ce0d --- /dev/null +++ b/as3/code/GreedySnake/controller.h @@ -0,0 +1,23 @@ +#ifndef CONTROLLER_H +#define CONTROLLER_H + + +class Controller +{ +public: + Controller() : speed(200), key(1), score(0) {} + void Start(); + void Select(); + void DrawGame(); + int PlayGame(); + void UpdateScore(const int&); + void RewriteScore(); + int Menu(); + void Game(); + int GameOver(); +private: + int speed; + int key; + int score; +}; +#endif // CONTROLLER_H diff --git a/as3/code/GreedySnake/food.cpp b/as3/code/GreedySnake/food.cpp new file mode 100644 index 0000000..f08cf9f --- /dev/null +++ b/as3/code/GreedySnake/food.cpp @@ -0,0 +1,119 @@ +#include "food.h" +#include "tools.h" +#include +#include + + +void Food::DrawFood(Snake& csnake)//ʳ +{ + /*rand꣬䷶Χ2-29ڣڵͼڣõص»ȡ + ͬʱÿ5ʳͳһʱʳ*/ + while (true) + { + int tmp_x = rand() % 30; + int tmp_y = rand() % 30; + if(tmp_x < 2) tmp_x += 2; + if(tmp_y < 2) tmp_y += 2; + bool flag = false; + for (auto& point : csnake.snake) + { + if ((point.GetX() == tmp_x && point.GetY() == tmp_y) || (tmp_x == big_x && tmp_y == big_y)) { + flag = true; + break; + } + } + if (flag) + continue; + x = tmp_x; + y = tmp_y; + SetCursorPosition(x, y); + SetColor(13); + std::cout << "" ; + ++cnt; + cnt %= 5; + if (cnt == 0) + { + DrawBigFood(csnake); + } + break; + } +} + +void Food::DrawBigFood(Snake& csnake)//ʱʳ +{ + SetCursorPosition(5, 0); + SetColor(11); + std::cout << "------------------------------------------" ;// + progress_bar = 42; + while (true) + { + int tmp_x = rand() % 30; + int tmp_y = rand() % 30; + if(tmp_x < 2) tmp_x += 2; + if(tmp_y < 2) tmp_y += 2; + bool flag = false; + for (auto& point : csnake.snake) + { + if ((point.GetX() == tmp_x && point.GetY() == tmp_y) || (tmp_x == x && tmp_y == y)) + { + flag = true; + break; + } + } + if (flag) + continue; + + big_x = tmp_x; + big_y = tmp_y; + SetCursorPosition(big_x, big_y); + SetColor(18); + std::cout << "" ; + big_flag = true; + flash_flag = true; + break; + } +} + +int Food::GetCnt() +{ + return cnt; +} + +void Food::FlashBigFood()//˸ʱʳ +{ + SetCursorPosition(big_x, big_y); + SetColor(18); + if (flash_flag) + { + std::cout << " " ; + flash_flag = false; + } + else + { + std::cout << "" ; + flash_flag = true; + } + + SetCursorPosition(26, 0); + SetColor(11); + for (int i = 42; i >= progress_bar; --i)// + std::cout << "\b \b" ; + --progress_bar; + if (progress_bar == 0) { + SetCursorPosition(big_x, big_y); + std::cout << " " ; + big_flag = false; + big_x = 0; + big_y = 0; + } +} + +bool Food::GetBigFlag() +{ + return big_flag; +} + +int Food::GetProgressBar() +{ + return progress_bar; +} diff --git a/as3/code/GreedySnake/food.h b/as3/code/GreedySnake/food.h new file mode 100644 index 0000000..fcff71b --- /dev/null +++ b/as3/code/GreedySnake/food.h @@ -0,0 +1,25 @@ +#ifndef FOOD_H +#define FOOD_H + +#include "snake.h" +class Snake; +class Food +{ +public: + Food() : cnt(0), flash_flag(false), big_flag(false), x(0), y(0), big_x(0), big_y(0), progress_bar(0) {} + void DrawFood(Snake&); + void DrawBigFood(Snake&); + int GetCnt(); + void FlashBigFood(); + bool GetBigFlag(); + int GetProgressBar(); +private: + int cnt; + bool flash_flag;//˸ + bool big_flag;//Ƿʱʳ + int x, y; + int big_x, big_y; + int progress_bar;//ʱʳ + friend class Snake; +}; +#endif // FOOD_H diff --git a/as3/code/GreedySnake/main.cpp b/as3/code/GreedySnake/main.cpp new file mode 100644 index 0000000..9675a29 --- /dev/null +++ b/as3/code/GreedySnake/main.cpp @@ -0,0 +1,8 @@ +#include "controller.h" + +int main()// +{ + Controller c;//һController + c.Game();//Ϸѭ + return 0; +} diff --git a/as3/code/GreedySnake/map.cpp b/as3/code/GreedySnake/map.cpp new file mode 100644 index 0000000..717c761 --- /dev/null +++ b/as3/code/GreedySnake/map.cpp @@ -0,0 +1,11 @@ +#include "map.h" +#include + +void Map::PrintInitmap()//Ƴʼͼ +{ + for (auto& point : initmap) + { + point.Print(); + Sleep(10);//SleepӪ춯Ч + } +} diff --git a/as3/code/GreedySnake/map.h b/as3/code/GreedySnake/map.h new file mode 100644 index 0000000..bfe703d --- /dev/null +++ b/as3/code/GreedySnake/map.h @@ -0,0 +1,137 @@ +#ifndef MAP_H +#define MAP_H + +#include +#include "point.h" + +class Map +{ +public: + Map()//ĬϹ캯θѹinitmap + { + initmap.emplace_back(Point(1, 1)); + initmap.emplace_back(Point(2, 1)); + initmap.emplace_back(Point(3, 1)); + initmap.emplace_back(Point(4, 1)); + initmap.emplace_back(Point(5, 1)); + initmap.emplace_back(Point(6, 1)); + initmap.emplace_back(Point(7, 1)); + initmap.emplace_back(Point(8, 1)); + initmap.emplace_back(Point(9, 1)); + initmap.emplace_back(Point(10, 1)); + initmap.emplace_back(Point(11, 1)); + initmap.emplace_back(Point(12, 1)); + initmap.emplace_back(Point(13, 1)); + initmap.emplace_back(Point(14, 1)); + initmap.emplace_back(Point(15, 1)); + initmap.emplace_back(Point(16, 1)); + initmap.emplace_back(Point(17, 1)); + initmap.emplace_back(Point(18, 1)); + initmap.emplace_back(Point(19, 1)); + initmap.emplace_back(Point(20, 1)); + initmap.emplace_back(Point(21, 1)); + initmap.emplace_back(Point(22, 1)); + initmap.emplace_back(Point(23, 1)); + initmap.emplace_back(Point(24, 1)); + initmap.emplace_back(Point(25, 1)); + initmap.emplace_back(Point(26, 1)); + initmap.emplace_back(Point(27, 1)); + initmap.emplace_back(Point(28, 1)); + initmap.emplace_back(Point(29, 1)); + initmap.emplace_back(Point(30, 1)); + initmap.emplace_back(Point(1, 2)); + initmap.emplace_back(Point(30, 2)); + initmap.emplace_back(Point(1, 3)); + initmap.emplace_back(Point(30, 3)); + initmap.emplace_back(Point(1, 4)); + initmap.emplace_back(Point(30, 4)); + initmap.emplace_back(Point(1, 5)); + initmap.emplace_back(Point(30, 5)); + initmap.emplace_back(Point(1, 6)); + initmap.emplace_back(Point(30, 6)); + initmap.emplace_back(Point(1, 7)); + initmap.emplace_back(Point(30, 7)); + initmap.emplace_back(Point(1, 8)); + initmap.emplace_back(Point(30, 8)); + initmap.emplace_back(Point(1, 9)); + initmap.emplace_back(Point(30, 9)); + initmap.emplace_back(Point(1, 10)); + initmap.emplace_back(Point(30, 10)); + initmap.emplace_back(Point(1, 11)); + initmap.emplace_back(Point(30, 11)); + initmap.emplace_back(Point(1, 12)); + initmap.emplace_back(Point(30, 12)); + initmap.emplace_back(Point(1, 13)); + initmap.emplace_back(Point(30, 13)); + initmap.emplace_back(Point(1, 14)); + initmap.emplace_back(Point(30, 14)); + initmap.emplace_back(Point(1, 15)); + initmap.emplace_back(Point(30, 15)); + initmap.emplace_back(Point(1, 16)); + initmap.emplace_back(Point(30, 16)); + initmap.emplace_back(Point(1, 17)); + initmap.emplace_back(Point(30, 17)); + initmap.emplace_back(Point(1, 18)); + initmap.emplace_back(Point(30, 18)); + initmap.emplace_back(Point(1, 19)); + initmap.emplace_back(Point(30, 19)); + initmap.emplace_back(Point(1, 20)); + initmap.emplace_back(Point(30, 20)); + initmap.emplace_back(Point(1, 21)); + initmap.emplace_back(Point(30, 21)); + initmap.emplace_back(Point(1, 22)); + initmap.emplace_back(Point(30, 22)); + initmap.emplace_back(Point(1, 23)); + initmap.emplace_back(Point(30, 23)); + initmap.emplace_back(Point(1, 24)); + initmap.emplace_back(Point(30, 24)); + initmap.emplace_back(Point(1, 25)); + initmap.emplace_back(Point(30, 25)); + initmap.emplace_back(Point(1, 26)); + initmap.emplace_back(Point(30, 26)); + initmap.emplace_back(Point(1, 27)); + initmap.emplace_back(Point(30, 27)); + initmap.emplace_back(Point(1, 28)); + initmap.emplace_back(Point(30, 28)); + initmap.emplace_back(Point(1, 29)); + initmap.emplace_back(Point(30, 29)); + initmap.emplace_back(Point(1, 30)); + initmap.emplace_back(Point(2, 30)); + initmap.emplace_back(Point(3, 30)); + initmap.emplace_back(Point(4, 30)); + initmap.emplace_back(Point(5, 30)); + initmap.emplace_back(Point(6, 30)); + initmap.emplace_back(Point(7, 30)); + initmap.emplace_back(Point(8, 30)); + initmap.emplace_back(Point(9, 30)); + initmap.emplace_back(Point(10, 30)); + initmap.emplace_back(Point(11, 30)); + initmap.emplace_back(Point(12, 30)); + initmap.emplace_back(Point(13, 30)); + initmap.emplace_back(Point(14, 30)); + initmap.emplace_back(Point(15, 30)); + initmap.emplace_back(Point(16, 30)); + initmap.emplace_back(Point(17, 30)); + initmap.emplace_back(Point(18, 30)); + initmap.emplace_back(Point(19, 30)); + initmap.emplace_back(Point(20, 30)); + initmap.emplace_back(Point(21, 30)); + initmap.emplace_back(Point(22, 30)); + initmap.emplace_back(Point(23, 30)); + initmap.emplace_back(Point(24, 30)); + initmap.emplace_back(Point(25, 30)); + initmap.emplace_back(Point(26, 30)); + initmap.emplace_back(Point(27, 30)); + initmap.emplace_back(Point(28, 30)); + initmap.emplace_back(Point(29, 30)); + initmap.emplace_back(Point(30, 30)); + } + void PrintInitmap();//Ƴʼͼ +private: + std::vector initmap;//ʼͼ + /*MapԶֵͼֻ轫ʾͼĸ㱣ӦmapУSnakeӦжײǽ + std::vector map1; + std::vector map2; + */ +}; +#endif // MAP_H diff --git a/as3/code/GreedySnake/obj/Debug/main.o b/as3/code/GreedySnake/obj/Debug/main.o new file mode 100644 index 0000000..cc31e63 Binary files /dev/null and b/as3/code/GreedySnake/obj/Debug/main.o differ diff --git a/as3/code/GreedySnake/point.cpp b/as3/code/GreedySnake/point.cpp new file mode 100644 index 0000000..404a31f --- /dev/null +++ b/as3/code/GreedySnake/point.cpp @@ -0,0 +1,27 @@ +#include "point.h" +#include "tools.h" +#include + +void Point::Print()//输出方块 +{ + SetCursorPosition(x, y); + std::cout << "■" ; +} + +void Point::PrintCircular()//输出圆形 +{ + SetCursorPosition(x, y); + std::cout << "●" ; +} + +void Point::Clear()//清除输出 +{ + SetCursorPosition(x, y); + std::cout << " " ; +} + +void Point::ChangePosition(const int x, const int y)//改变坐标 +{ + this->x = x; + this->y = y; +} diff --git a/as3/code/GreedySnake/point.h b/as3/code/GreedySnake/point.h new file mode 100644 index 0000000..b2589a8 --- /dev/null +++ b/as3/code/GreedySnake/point.h @@ -0,0 +1,19 @@ +#ifndef POINT_H +#define POINT_H + +class Point +{ +public: + Point(){} + Point(const int x, const int y) : x(x), y(y) {} + void Print(); + void PrintCircular(); + void Clear(); + void ChangePosition(const int x, const int y); + bool operator== (const Point& point) { return (point.x == this->x) && (point.y == this->y); } + int GetX(){ return this->x; } + int GetY(){ return this->y; } +private: + int x, y; +}; +#endif // POINT_H diff --git a/as3/code/GreedySnake/shot01.jpg b/as3/code/GreedySnake/shot01.jpg new file mode 100644 index 0000000..7871d68 Binary files /dev/null and b/as3/code/GreedySnake/shot01.jpg differ diff --git a/as3/code/GreedySnake/shot02.jpg b/as3/code/GreedySnake/shot02.jpg new file mode 100644 index 0000000..6e510bd Binary files /dev/null and b/as3/code/GreedySnake/shot02.jpg differ diff --git a/as3/code/GreedySnake/shot03.gif b/as3/code/GreedySnake/shot03.gif new file mode 100644 index 0000000..11d3dc3 Binary files /dev/null and b/as3/code/GreedySnake/shot03.gif differ diff --git a/as3/code/GreedySnake/shot04.gif b/as3/code/GreedySnake/shot04.gif new file mode 100644 index 0000000..1b2a252 Binary files /dev/null and b/as3/code/GreedySnake/shot04.gif differ diff --git a/as3/code/GreedySnake/snake.cpp b/as3/code/GreedySnake/snake.cpp new file mode 100644 index 0000000..c68b10b --- /dev/null +++ b/as3/code/GreedySnake/snake.cpp @@ -0,0 +1,135 @@ +#include "snake.h" +#include +#include "tools.h" +#include + +void Snake::InitSnake()//ʼ +{ + for (auto& point : snake) + { + point.PrintCircular(); + } +} + +void Snake::Move()// +{ + switch (direction) + { + case Direction::UP: + snake.emplace_back(Point(snake.back().GetX(), snake.back().GetY() - 1 )); + break; + case Direction::DOWN: + snake.emplace_back(Point(snake.back().GetX(), snake.back().GetY() + 1 )); + break; + case Direction::LEFT: + snake.emplace_back(Point(snake.back().GetX() - 1, snake.back().GetY() )); + break; + case Direction::RIGHT: + snake.emplace_back(Point(snake.back().GetX() + 1, snake.back().GetY() )); + break; + default: + break; + } + SetColor(14); + snake.back().PrintCircular(); +} + +void Snake::NormalMove()//ƶͷβ +{ + Move(); + snake.front().Clear(); + snake.pop_front(); +} + +bool Snake::OverEdge()//߽ +{ + return snake.back().GetX() < 30 && + snake.back().GetY() < 30 && + snake.back().GetX() > 1 && + snake.back().GetY() > 1; +} + +bool Snake::HitItself()//ײ +{ + std::deque::size_type cnt = 1; + Point *head = new Point(snake.back().GetX(), snake.back().GetY());//ͷ + for (auto& point : snake) //ͷͬ겻߳ζͷײ + { + if ( !(point == *head) ) + ++cnt; + else + break; + } + delete head; + if(cnt == snake.size()) + return true; + else + return false; +} + +bool Snake::ChangeDirection()//ı䷽ +{ + char ch; + if (kbhit())//kbhitֵΪע + { + ch = getch(); + switch (ch) + { + case -32: + ch = getch(); + switch (ch) + { + case 72: + if (direction != Direction::DOWN)//뵱ǰ˶෴Ч + direction = Direction::UP; + break; + case 80: + if (direction != Direction::UP) + direction = Direction::DOWN; + break; + case 75: + if (direction != Direction::RIGHT) + direction = Direction::LEFT; + break; + case 77: + if (direction != Direction::LEFT) + direction = Direction::RIGHT; + break; + default: + break; + } + return true; + + case 27://ESC + return false; + + default: + return true; + + } + } + return true; +} + +bool Snake::GetFood(const Food& cfood) +{ + if (snake.back().GetX() == cfood.x && snake.back().GetY() == cfood.y) + return true; + else + return false; +} + +bool Snake::GetBigFood(Food& cfood) +{ + if (snake.back().GetX() == cfood.big_x && snake.back().GetY() == cfood.big_y) + { + cfood.big_flag = false; + cfood.big_x = 0; + cfood.big_y = 0; + SetCursorPosition(1, 0); + std::cout << " " ; + return true; + } + else + return false; +} diff --git a/as3/code/GreedySnake/snake.h b/as3/code/GreedySnake/snake.h new file mode 100644 index 0000000..459b497 --- /dev/null +++ b/as3/code/GreedySnake/snake.h @@ -0,0 +1,33 @@ +#ifndef SNAKE_H +#define SNAKE_H + +#include +#include "point.h" +#include "food.h" + +class Food; +class Snake +{ +public: + enum Direction {UP, DOWN, LEFT, RIGHT}; + + Snake() { + snake.emplace_back(14, 8); + snake.emplace_back(15, 8); + snake.emplace_back(16, 8); + direction = Direction::DOWN; + } + void InitSnake(); + void Move(); + void NormalMove(); + bool OverEdge(); + bool HitItself(); + bool ChangeDirection(); + bool GetFood(const Food&); + bool GetBigFood(Food&); +private: + std::deque snake; + Direction direction; + friend class Food;//FoodΪԪԱ˽Ԫ +}; +#endif // SNAKE_H diff --git a/as3/code/GreedySnake/startinterface.cpp b/as3/code/GreedySnake/startinterface.cpp new file mode 100644 index 0000000..4152026 --- /dev/null +++ b/as3/code/GreedySnake/startinterface.cpp @@ -0,0 +1,68 @@ +#include "startinterface.h" +#include + + +void StartInterface::PrintFirst()//ߴֵ߳ȫֵĹ +{ + for (auto& point : startsnake) + { + point.Print(); + Sleep(speed); + } +} + +void StartInterface::PrintSecond()//ߴƶĹ +{ + for (int i = 10; i != 40; ++i) //ͷҪ10ƶ40 + { + /*ͷһλãѹstartsnakeУƳβȥ*/ + int j = ( ((i-2)%8) < 4 )?( 15 + (i-2)%8 ) : ( 21 - (i-2)%8 ); + startsnake.emplace_back( Point(i, j) ); + startsnake.back().Print(); + startsnake.front().Clear(); + startsnake.pop_front(); + Sleep(speed); + } +} + +void StartInterface::PrintThird()//ߴӽӴұߵʧĹ +{ + while ( !startsnake.empty() || textsnake.back().GetX() < 33 ) //߻ûʧûƶָλ + { + if ( !startsnake.empty() ) //߻ûʧƶ + { + startsnake.front().Clear(); + startsnake.pop_front(); + } + ClearText();// + PrintText();//Ƹλú + Sleep(speed); + } +} + +void StartInterface::PrintText() +{ + for (auto& point : textsnake) + { + if (point.GetX() >= 0) + point.Print(); + } +} + +void StartInterface::ClearText() +{ + for (auto& point : textsnake) //ͬʱƶһ + { + if (point.GetX() >= 0) + point.Clear(); + point.ChangePosition(point.GetX() + 1, point.GetY()); + } +} + +void StartInterface::Action() +{ + PrintFirst(); + PrintSecond(); + PrintThird(); +} + diff --git a/as3/code/GreedySnake/startinterface.h b/as3/code/GreedySnake/startinterface.h new file mode 100644 index 0000000..0728ef8 --- /dev/null +++ b/as3/code/GreedySnake/startinterface.h @@ -0,0 +1,92 @@ +#ifndef STRATINTERFACE_H +#define STARTINTERFACE_H + +#include +#include +#include "point.h" + +class StartInterface +{ +public: + StartInterface() : speed(35) { + startsnake.emplace_back(Point(0,14));//Éß + startsnake.emplace_back(Point(1,14)); + startsnake.emplace_back(Point(2,15)); + startsnake.emplace_back(Point(3,16)); + startsnake.emplace_back(Point(4,17)); + startsnake.emplace_back(Point(5,18)); + startsnake.emplace_back(Point(6,17)); + startsnake.emplace_back(Point(7,16)); + startsnake.emplace_back(Point(8,15)); + startsnake.emplace_back(Point(9,14)); + + textsnake.emplace_back(Point(-26, 14));//S + textsnake.emplace_back(Point(-25, 14)); + textsnake.emplace_back(Point(-27, 15)); + textsnake.emplace_back(Point(-26, 16)); + textsnake.emplace_back(Point(-25, 17)); + textsnake.emplace_back(Point(-27, 18)); + textsnake.emplace_back(Point(-26, 18)); + + textsnake.emplace_back(Point(-23, 14));//N + textsnake.emplace_back(Point(-23, 15)); + textsnake.emplace_back(Point(-23, 16)); + textsnake.emplace_back(Point(-23, 17)); + textsnake.emplace_back(Point(-23, 18)); + textsnake.emplace_back(Point(-22, 15)); + textsnake.emplace_back(Point(-21, 16)); + textsnake.emplace_back(Point(-20, 17)); + textsnake.emplace_back(Point(-19, 14)); + textsnake.emplace_back(Point(-19, 15)); + textsnake.emplace_back(Point(-19, 16)); + textsnake.emplace_back(Point(-19, 17)); + textsnake.emplace_back(Point(-19, 18)); + + textsnake.emplace_back(Point(-17, 18));//A + textsnake.emplace_back(Point(-16, 17)); + textsnake.emplace_back(Point(-15, 16)); + textsnake.emplace_back(Point(-14, 15)); + textsnake.emplace_back(Point(-14, 16)); + textsnake.emplace_back(Point(-13, 14)); + textsnake.emplace_back(Point(-13, 16)); + textsnake.emplace_back(Point(-12, 15)); + textsnake.emplace_back(Point(-12, 16)); + textsnake.emplace_back(Point(-11, 16)); + textsnake.emplace_back(Point(-10, 17)); + textsnake.emplace_back(Point(-9, 18)); + + textsnake.emplace_back(Point(-7, 14));//K + textsnake.emplace_back(Point(-7, 15)); + textsnake.emplace_back(Point(-7, 16)); + textsnake.emplace_back(Point(-7, 17)); + textsnake.emplace_back(Point(-7, 18)); + textsnake.emplace_back(Point(-6, 16)); + textsnake.emplace_back(Point(-5, 15)); + textsnake.emplace_back(Point(-5, 17)); + textsnake.emplace_back(Point(-4, 14)); + textsnake.emplace_back(Point(-4, 18)); + + textsnake.emplace_back(Point(-2, 14));//E + textsnake.emplace_back(Point(-2, 15)); + textsnake.emplace_back(Point(-2, 16)); + textsnake.emplace_back(Point(-2, 17)); + textsnake.emplace_back(Point(-2, 18)); + textsnake.emplace_back(Point(-1, 14)); + textsnake.emplace_back(Point(-1, 16)); + textsnake.emplace_back(Point(-1, 18)); + textsnake.emplace_back(Point(0, 14)); + textsnake.emplace_back(Point(0, 16)); + textsnake.emplace_back(Point(0, 18)); + } + void PrintFirst(); + void PrintSecond(); + void PrintThird(); + void PrintText(); + void ClearText(); + void Action(); +private: + std::deque startsnake;//开始动画中的蛇 + std::vector textsnake;//开始动画中的文字 + int speed;//动画的速度 +}; +#endif // STRATINTERFACE_H diff --git a/as3/code/GreedySnake/tools.cpp b/as3/code/GreedySnake/tools.cpp new file mode 100644 index 0000000..c9c40d7 --- /dev/null +++ b/as3/code/GreedySnake/tools.cpp @@ -0,0 +1,33 @@ +#include "tools.h" +#include +#include + +void SetWindowSize(int cols, int lines)//ôڴС +{ + system("title ̰");//ôڱ + char cmd[30]; + sprintf(cmd, "mode con cols=%d lines=%d", cols * 2, lines);//һͼΡռַʿȳ2 + system(cmd);//system(mode con cols=88 lines=88)ôڿȺ͸߶ +} + +void SetCursorPosition(const int x, const int y)//ùλ +{ + COORD position; + position.X = x * 2; + position.Y = y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); +} + +void SetColor(int colorID)//ıɫ +{ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorID); +} + +void SetBackColor()//ıɫ +{ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), + FOREGROUND_BLUE | + BACKGROUND_BLUE | + BACKGROUND_GREEN | + BACKGROUND_RED ); +} diff --git a/as3/code/GreedySnake/tools.h b/as3/code/GreedySnake/tools.h new file mode 100644 index 0000000..eddbab2 --- /dev/null +++ b/as3/code/GreedySnake/tools.h @@ -0,0 +1,10 @@ +#ifndef TOOLS_H +#define TOOLS_H + + +void SetWindowSize(int cols, int lines); +void SetCursorPosition(const int x, const int y); +void SetColor(int colorID); +void SetBackColor(); + +#endif // TOOLS_H