dumn如下
wasd移动;
v加速;
b减速
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<deque>
#include<ctime>
#include<stdexcept>
using namespace std;
struct Snake { //é?àà?á11ì?
char image;
short x, y; //×?±ê
};
class snakeGame {
public:
snakeGame();
void printMap();
void gotoxy(short x, short y) {
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //??è???±ú
pos = {x, y};
SetConsoleCursorPosition(hOut, pos); //ò??ˉ1a±ê
}
void HideCursor() //òt2?1a±ê
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//??è?????ì¨1a±êD??¢
CursorInfo.bVisible = false; //òt2?????ì¨1a±ê
SetConsoleCursorInfo(handle, &CursorInfo);//éè??????ì¨1a±ê×′ì?
}
void initSnake() { //3?ê??ˉé?£??éò?×?DD??±?
snake.push_front({'@', width / 2, height / 2});
for (int i=0; i<2;++i)
snake.push_back({'+', width/2,static_cast<short>(height/2+i+1)});
}
int WrongLocation() { //?D??ê?·?ê3??2úéú????ó?é?éí3?í?
for (Snake body : snake)
if(body.x == food_x && body.y == food_y) return 0;
return 1;
}
void createFood() {
do {
food_x = rand() % (width - 4) + 2;
food_y = rand() % (height - 2) + 1;
} while (!WrongLocation());//′|àí3?í?
gotoxy(food_x,food_y); cout << '*' << endl; //′òó?ê3??
}
void printSnake();
inline void clearSnake(Snake &tail) {
gotoxy(tail.x, tail.y); cout << ' '; //?2??é??2£?2?ê1ó????áoˉêy£?±ü?aá?éá??
}
void judgeCrash();
void foodEaten();
void userInput() {
switch(char ch; ch=getch()) {
case 'w':if (dir != 's') dir = ch;break;
case 'a':if (dir != 'd') dir = ch;break;
case 's':if (dir != 'w') dir = ch;break;
case 'd':if (dir != 'a') dir = ch;break;
case 'v':speed*=0.8;break; case 'b':speed*=1.5;break;
case ' ':gotoxy(width / 2, height); cout << "ó??·ò??Yí££?è?òa?ü?ìD?"; getch();
gotoxy(width / 2, height); cout << " "; break;
default:break;
}
}
private:
enum MapSize {height = 40,width = 120}; //μ?í?3?′?
HANDLE hOut; COORD pos;
char dir; //direction
bool beg,eatFood=false;
double speed=200;
deque<Snake> snake;
int food_x,food_y;
int score=0;
};
void snakeGame::foodEaten() { //?D??ê?·?3?μ?ê3??
createFood();
eatFood=true;
speed*=.8;
++score;
}
void snakeGame::judgeCrash() { //?D??ê?·?×2???ò3?μ?×??o
int flag=0;
if (snake.size()>=5) {
deque<Snake>::iterator iter = snake.begin() + 1;
int x = (iter-1)->x, y = (iter-1)->y;
for (; iter != snake.end(); ++iter) {
if (iter->x == x && iter->y == y) flag=1;
}}
if (flag || snake.front().x == 1 || snake.front().x == width - 2 || snake.front().y == 0 || snake.front().y == height - 1)//?ì2aê?·?×2???ò??ê?·?3?μ?×?éí
{
gotoxy(width / 2 - 10, height /2);
cout << "ó??·?áê?£??úμ?·?êyê?: " << score << "·?£¨??3μ?ìD?£?"<<endl;
while(1) {
dir = getch();
if (dir == '\r') break;}
runtime_error quit("ó??·?áê?£??y3£í?3?"); throw quit;
}
}
void snakeGame::printSnake() { //′òó?é?éí
deque<Snake>::const_iterator iter = snake.begin();
for (; iter <= snake.begin() + 1 && iter < snake.end(); ++iter) {
gotoxy(iter->x, iter->y); cout << iter->image;
}
}
void snakeGame::printMap() { //′òó?μ?í?
int i;
for (i = 0; i != width; i += 2) cout << "??"; //?a??í?°??í?è??2£????è??1
gotoxy(0, 1);
for (i = 1; i != height; ++i) cout << "??" << endl;
for (i = 1; i != height; ++i) {
gotoxy(width - 2, i); cout << "??";}
gotoxy(0, height - 1);
for (i = 0; i != width; i += 2) cout << "??";
cout << "ì°3?é?£o1.·??ò?ü?aê?ó??· 2.*′ú±íê3?? 3.?????ü?Yí£ó??·\n 4.?üè?'v'?ó?ù 5.?üè?'b'???ù";
}
snakeGame::snakeGame() { //?aê?ó??·
HideCursor(); srand(static_cast<unsigned int>(time(NULL)));
beg=true;
Snake tmp1,tmp2;
while (1) {
if(beg) {
printMap();
dir = getch();
initSnake();
createFood();
beg = eatFood=false;}
tmp2=snake.back(); tmp1=snake.front();
snake.pop_back();
if (eatFood) {tmp2.image='+'; snake.push_back(tmp2);
eatFood=false;}
else clearSnake(tmp2);
if (dir == 's') ++tmp1.y;
else if (dir == 'a') --tmp1.x;
else if (dir == 'd') ++tmp1.x;
else --tmp1.y;
try{judgeCrash();}
catch(runtime_error &quitSignal) {
throw quitSignal;
}
snake.front().image='+'; snake.push_front(tmp1);
printSnake();
Sleep(speed+30);
if (tmp1.x == food_x && tmp1.y == food_y)
foodEaten();
if(kbhit()) userInput();
}
}
int main() {
system("mode con cols=120 lines=42");
try{
snakeGame game;
}
catch(runtime_error &gameEnd) { //?áê?
system("cls");
cout<<gameEnd.what();
getch();
}
}