#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
void gotoxy(int x,int y) {COORD pos={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//游標定位
class Food {//食物類
private: int m_x; int m_y;
public:
void randfood() {//隨機產生一個食物
srand((int)time(NULL));//利用時間新增隨機數種子,需要ctime標頭檔案
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2) goto L1;//如果食物的x座標不是偶數則重新確定食物的座標
gotoxy(m_x,m_y);//在確認好的位置輸出食物
int getFoodm_x() {return m_x;}//返回食物的x座標
int getFoodm_y() {return m_y;}};//返回食物的y座標
class Snake {
private:
struct Snakecoor {int x; int y;};//定義一個蛇的座標機構
vector<Snakecoor> snakecoor;//將座標存入vector容器中
//判斷並改變前進方向的函式
void degdir(Snakecoor&nexthead) {//定義新的蛇頭變數
static char key="d";//靜態變數防止改變移動方向後重新改回來
if(_kbhit()) {
char temp=_getch();//定義一個臨時變數儲存鍵盤輸入的值
switch(temp) {//如果臨時變數的值為wasd中的一個,則賦值給key
default: break;//default是預設情況,只有任何條件都不匹配的情況下才會執行 必須寫在前面!不然蛇無法轉向
case"w": case"a": case"s": case"d":
//如果temp的方向和key的方向不相反則賦值 因為兩次移動方向不能相反 將蛇設定為初始向右走
if(key=="w" && temp!="s" || key=="s" && temp!="w" || key=="a" && temp!="d" || key=="d" && temp!="a") key=temp;}}
switch (key) {//根據key的值來確定蛇的移動方向
case"d": nexthead.x=snakecoor.front().x+2; nexthead.y=snakecoor.front().y; break;
//新的蛇頭的頭部等於容器內第一個資料(舊蛇頭)x座標+2 因為蛇頭佔兩個座標,移動一次加2
case"a": nexthead.x=snakecoor.front().x-2; nexthead.y=snakecoor.front().y; break;
case"w": nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y-1; break;
//因為控制檯的x長度是y的一半,所以用兩個x做蛇頭,需要的座標是二倍
case"s": nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y+1;}}
//遊戲結束時設計一個介面輸出“遊戲結束”以及分數
void finmatt(const int score) {
system("cls"); gotoxy(40, 14);//清屏然後輸出
cout << "遊戲結束"; gotoxy(40, 16);
cout << "得分:" << score; gotoxy(0, 26);
exit(0);}//exit為C++的退出函式 exit(0)表示程式正常退出,非0表示非正常退出
void finishgame(const int score) {//遊戲結束
if(snakecoor[0].x>=88 || snakecoor[0].x<0 || snakecoor[0].y>=28 || snakecoor[0].y<0) finmatt(score);//撞牆
for(int i=1;i<snakecoor.size();i++) if(snakecoor[0].x==snakecoor[i].x && snakecoor[0].y==snakecoor[i].y) finmatt(score
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
void gotoxy(int x,int y) {COORD pos={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//游標定位
class Food {//食物類
private: int m_x; int m_y;
public:
void randfood() {//隨機產生一個食物
srand((int)time(NULL));//利用時間新增隨機數種子,需要ctime標頭檔案
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2) goto L1;//如果食物的x座標不是偶數則重新確定食物的座標
gotoxy(m_x,m_y);//在確認好的位置輸出食物
int getFoodm_x() {return m_x;}//返回食物的x座標
int getFoodm_y() {return m_y;}};//返回食物的y座標
class Snake {
private:
struct Snakecoor {int x; int y;};//定義一個蛇的座標機構
vector<Snakecoor> snakecoor;//將座標存入vector容器中
//判斷並改變前進方向的函式
void degdir(Snakecoor&nexthead) {//定義新的蛇頭變數
static char key="d";//靜態變數防止改變移動方向後重新改回來
if(_kbhit()) {
char temp=_getch();//定義一個臨時變數儲存鍵盤輸入的值
switch(temp) {//如果臨時變數的值為wasd中的一個,則賦值給key
default: break;//default是預設情況,只有任何條件都不匹配的情況下才會執行 必須寫在前面!不然蛇無法轉向
case"w": case"a": case"s": case"d":
//如果temp的方向和key的方向不相反則賦值 因為兩次移動方向不能相反 將蛇設定為初始向右走
if(key=="w" && temp!="s" || key=="s" && temp!="w" || key=="a" && temp!="d" || key=="d" && temp!="a") key=temp;}}
switch (key) {//根據key的值來確定蛇的移動方向
case"d": nexthead.x=snakecoor.front().x+2; nexthead.y=snakecoor.front().y; break;
//新的蛇頭的頭部等於容器內第一個資料(舊蛇頭)x座標+2 因為蛇頭佔兩個座標,移動一次加2
case"a": nexthead.x=snakecoor.front().x-2; nexthead.y=snakecoor.front().y; break;
case"w": nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y-1; break;
//因為控制檯的x長度是y的一半,所以用兩個x做蛇頭,需要的座標是二倍
case"s": nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y+1;}}
//遊戲結束時設計一個介面輸出“遊戲結束”以及分數
void finmatt(const int score) {
system("cls"); gotoxy(40, 14);//清屏然後輸出
cout << "遊戲結束"; gotoxy(40, 16);
cout << "得分:" << score; gotoxy(0, 26);
exit(0);}//exit為C++的退出函式 exit(0)表示程式正常退出,非0表示非正常退出
void finishgame(const int score) {//遊戲結束
if(snakecoor[0].x>=88 || snakecoor[0].x<0 || snakecoor[0].y>=28 || snakecoor[0].y<0) finmatt(score);//撞牆
for(int i=1;i<snakecoor.size();i++) if(snakecoor[0].x==snakecoor[i].x && snakecoor[0].y==snakecoor[i].y) finmatt(score