版本比较
C++小游戏
正文
增加 37 行删除 37 行
12 行未改动13141516171718192021222324252627272829303136 行未改动6869707172727374757677787979808182838384858687888989909192933 行未改动9798991001011011021031041051061061071081091102 行未改动11311411511611711711811912012112212212312412512621 行未改动14814915015115215215315415415515615615715815915916016116216216316416516516616716716816917017125 行未改动19719819920020120120220320420511 行未改动21721821922022122122222322422518 行未改动24424524624724824824925025125225325425525525625725825910 行未改动2702712722732742742752762772782 行未改动28128228328428528528628728828929029029129229329426 行未改动3213223233243253253263273273283293293303313323333343353363373383393393403413423436 行未改动35035135235335435435535635735812 行未改动37137237337437537537637737837918 行未改动3983994004014024024034044054064 行未改动4114124134144154154164174184194204214224234244254254264274284294304304314324334344 行未改动43944044144244344344444544644744844945045145245345345445545645716 行未改动47447547647747847847948012 行未改动public: int x, y; Node(int x1, int y1);};
class UserData {public: string name; long long score; int gt; int gr; UserData(string s, long long sc,int gametime,int grade); friend bool operator < (UserData a, UserData b);};
#define RIGHT 0x4d#define LEFT 0x4b#define UP 0x48#define DOWN 0x5036 行未改动bool GameMenu(); //游戏菜单void ShowRanking(); //排行榜void ShowAbout(); //相关信息void InputData(); //输入玩家名字
int main() { while (true) { if (!GameMenu()) return 0; } return 0;}
Node::Node(int x1, int y1) { //构造Node对象 x = x1; y = y1;}
int SuiJi(){srand((unsigned)time(NULL));return (rand()*rand()+rand()*rand())%14;}
bool operator < (UserData a, UserData b) { //重载运算符,按分数由大到小排列 if(a.score != b.score) return a.score > b.score; if(a.gt !=b.gt)3 行未改动}UserData::UserData(string s, long long sc,int gametime_,int _grade) { //构造UserData对象 name = s; score = sc; gt=gametime_; gr=_grade;}
void color(WORD A){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), A);}
void Color(int a){ switch (a%4) {2 行未改动 case 2:color(YELLOW);break; case 3:color(PURPLE);break; }}
void GoTo(short x, short y) { //定位光标 COORD coord = { x, y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}
void ShowInformation() { //输出游戏信息 color(YELLOW); GoTo(78, 5); printf("贪吃蛇游戏");21 行未改动 GoTo(78,25); printf("剩余时间:%d秒",20+(snakeLen-3)*5-gametime); sysj=20+(snakeLen-3)*5-gametime;}
void DrawBoard() { //绘制墙体
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获得输出句柄
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; //光标信息 SetConsoleCursorInfo(hOut, &cursor_info); //隐藏光标
COORD size = { 120, 30 }; SetConsoleScreenBufferSize(hOut, size); //重设缓冲区大小
SMALL_RECT rc = { 0 , 0, 120, 30 }; SetConsoleWindowInfo(hOut, true, &rc); //重设窗口大小
SetConsoleTextAttribute(hOut, CYAN);
for (int i = STARTX - 2; i <= ENDX + 2; i += 2) { //横向墙体 GoTo(i, STARTY - 1); printf("■"); GoTo(i, ENDY + 1);25 行未改动 mj=clock(); point=20; sysj=20;}
void RunSnake(int x, int y) { //绘制蛇身 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW); score += snakeLen + 1; if (x == foodx && y == foody) {11 行未改动 isSnake[back.y][back.x] = false; GoTo(back.x, back.y); printf(" ");}
void Try(int& x, int& y) { //试走 int key, cnt = 100; while (cnt--) { //多次检测键盘状态 if (_kbhit()) {18 行未改动 else if (curDiraction == 2) ++y; else if (curDiraction == 3) x -= 2; else x += 2;}
bool IsGoodCoord(int x, int y) { //判断光标是否合法 if (x <= ENDX && y <= ENDY && x >= STARTX && y >= STARTY) return true; else return false;}
void AddFood() { //增加食物 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), PURPLE); srand((unsigned)time(NULL)); while (true) {10 行未改动 printf("#"); else printf("&");}
void EartFood() { //吃东西 point+=4; int sb=gametime=(clock()-mj)/1000; sysj=point-sb;2 行未改动 snake.push_front(Node(foodx, foody)); isSnake[foody][foodx] = true; ++snakeLen; if (speed >= 55) speed -= 5;
GoTo(foodx, foody); draw(); AddFood();}
void InitSnake() { //初始化蛇身 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW); snakeLen = 3, gox = 18, goy = 14; snake.clear();26 行未改动 } else key = getch(); }}
bool StartGame() { //启动游戏
Init();
while (sysj>0) { //开挂 RunSnake(gox, goy); ShowInformation(); Try(gox, goy); Sleep(speed); } InputData(); return true;}
bool GameMenu() { //游戏菜单 system("cls"); DrawBoard(); GoTo(STARTX + 22, STARTY + 4);6 行未改动 GoTo(STARTX + 24, STARTY + 14); printf("3: 关于游戏"); GoTo(STARTX + 24, STARTY + 16); printf("4: 退出游戏");
while (true) { if (_kbhit()) { char key = getch(); switch (key) {12 行未改动 } } }}
void ShowRanking() { //展示排行榜 vector<UserData> vu; FILE *fp = fopen("Gamedata2.txt", "r"); if (fp == NULL) fp = fopen("Gamedata2.txt", "w+");18 行未改动 printf("生存时间"); GoTo(STARTX + 44, STARTY + 2); printf("排行"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);
for (int i = 0; i < len && i < 10; ++i) { //打印前十名用户数据 char const *p = vu[i].name.c_str(); Color(score); GoTo(STARTX + 8, STARTY + 4 + i);4 行未改动 printf("%d秒", vu[i].gt); GoTo(STARTX + 44, STARTY + 4 + i); printf(" %d", i + 1); }
GoTo(STARTX + 4, ENDY - 2); printf("----------------- 按'1'返回游戏菜单 ---------------"); while (true) { if (_kbhit()) { char key = getch(); if (key == '1') break; } }}
void ShowAbout() { //展示游戏相关 system("cls"); DrawBoard(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);
GoTo(STARTX + 4, STARTY + 2); printf("------------------- 贪吃蛇游戏 -------------------"); GoTo(STARTX + 10,STARTY + 8); printf("贪吃蛇游戏");4 行未改动 GoTo(STARTX + 10,STARTY + 14); printf("吃的越多,蛇就越长,您的等级也将越高"); GoTo(STARTX + 10,STARTY + 16); printf("当蛇吃到自己或撞上墙时,游戏结束。");
GoTo(STARTX + 4, ENDY - 2); printf("----------------- 按'1'返回游戏菜单 ---------------"); while (true) { if (_kbhit()) { char key = getch(); if (key == '1') break; } }}
void InputData() { //用户输入名字 char name[20]; if(score>=1000) {16 行未改动 cout<<"哟!这分数也能上榜??"<<endl; Sleep(1000); } EndGame();
}```