1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| #include <bits/stdc++.h> #include <conio.h> #include <windows.h> using namespace std;
#define Map_Width 50 #define Map_Height 30 #define Left_Edge 30
class Snake{ private: int x; int y; int player; bool eat = false; class Body{ private: int x; int y; public: int xpos(){ return x; } int ypos(){ return y; } void setxy(int a, int b){ x = a; y = b; } }; deque<Body> body; public: Body GetBody(int i){ return body[i]; } void gotoxy(int xpos, int ypos) { COORD scrn; HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE); scrn.X = xpos; scrn.Y = ypos; SetConsoleCursorPosition(hOuput,scrn); } void Init(){ player = 4; Body head; head.setxy(1, 1); body.clear(); body.push_front(head); } void move(char key){ Body head = body.front(); if(!eat){ Body tail = body.back(); gotoxy(tail.xpos() + Left_Edge, tail.ypos()); cout << " "; body.pop_back(); } if(key == 'a'){ head.setxy(head.xpos()-1, head.ypos()); } else if(key == 'd'){ head.setxy(head.xpos()+1, head.ypos()); } else if(key == 'w'){ head.setxy(head.xpos(), head.ypos()-1); } else if(key == 's'){ head.setxy(head.xpos(), head.ypos()+1); } gotoxy(head.xpos() + Left_Edge, head.ypos()); cout << "S"; body.push_front(head); eat = false; } int length(){ return body.size(); } void grow(){ eat = true; } bool die(){ Body head = body.front(); if(player <= 0) return true; if(head.xpos() < 1 || head.xpos() > Map_Width-1 || head.ypos() < 1 || head.ypos() > Map_Height-1) return true; for(int i = 1; i < body.size(); ++i){ if(head.xpos() == body[i].xpos() && head.ypos() == body[i].ypos()){ return true; } } return false; } }snake;
class Food{ private: bool flag; int x; int y; public: int xpos(){ return x; } int ypos(){ return y; } void gotoxy(int xpos, int ypos) { COORD scrn; HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE); scrn.X = xpos; scrn.Y = ypos; SetConsoleCursorPosition(hOuput,scrn); } void InitFood(){ flag = true; } void CreateFood(){ if(flag){ x = rand() % (Map_Width-2) + 1; y = rand() % (Map_Height-2) + 1; for(int i = 0; i < snake.length(); ++i){ if(x == snake.GetBody(i).xpos() && y == snake.GetBody(i).ypos()){ x = rand() % (Map_Width-2) + 1; y = rand() % (Map_Height-2) + 1; } } bool CreateError = false; for(int i = 0; i < snake.length(); ++i){ if(x == snake.GetBody(i).xpos() && y == snake.GetBody(i).ypos()){ CreateError = true; break; } } if(CreateError) flag = true; else { gotoxy(x + Left_Edge, y); cout << "f"; flag = false; } } } void EatFood(){ if(x == snake.GetBody(0).xpos() && y == snake.GetBody(0).ypos()){ snake.grow(); flag = true; } } }food;
void HideCursor(){ CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }
void gotoxy(int xpos, int ypos) { COORD scrn; HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE); scrn.X = xpos; scrn.Y = ypos; SetConsoleCursorPosition(hOuput,scrn); }
void InitScene(); void die_UI(); void update();
int main(){ char Key = 'd'; HideCursor(); InitScene(); snake.Init(); food.InitFood(); while(true){ if(_kbhit()){ Key = _getch(); } food.CreateFood(); snake.move(Key); food.EatFood(); update(); if(snake.die()){ system("CLS"); die_UI(); break; } Sleep(100); } return 0; } void InitScene(){ for(int i = 0; i < Map_Width + 1; ++i){ gotoxy(Left_Edge + i, 0); cout << "-"; gotoxy(Left_Edge + i, Map_Height); cout << "-"; } for(int i = 1; i < Map_Height; ++i){ gotoxy(Left_Edge - 1, i); cout << "|"; gotoxy(Left_Edge + Map_Width, i); cout << "|"; } }
void die_UI(){ gotoxy(Left_Edge + Map_Width/2, 5); cout << "你已經死了!!"; gotoxy(Left_Edge + Map_Width/2, 7); cout << "得點: " << snake.length(); }
void update(){ gotoxy(Left_Edge + Map_Width + 1, 2); cout << "得點: " << snake.length() << " "; }
|