各位朋友们好啊!今天我为大家带来一个贪吃蛇游戏进阶版!
如果点赞超过15个,1小时内立马更新哦!
em…音乐我就不说了,没有看过的小伙伴们可以在我的专栏里面看一看之前的飞机大战和自制金山打字通
OK!上代码喽
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<string.h>
#include<time.h>
#include<string>
#define H 24
#define W 78
#define false 0
#define true 1
int n=0;
int zpalyer=0;
int fs;
int key=72;
int changeFlag = 0;
int i;
char name[20];
char name1[20];
struct palyerinfo {char name[20];int palyefs;
} player[100];
struct {int x;int y;
} food;
struct {int x[100];int y[100];int len;int zfs;int speed;
} snake1;
DWORD WINAPI MusicFun(LPVOID lpParamte) {DWORD OBJ;sndPlaySound(TEXT("Hungry_Snake_BGM.wav"), SND_FILENAME|SND_ASYNC);return 0;
}
void xj();
void dq();
void save();
void ck();
void xuanz();
void jm();
void phb();
void fs1();
int begin();
void creatfood();
void keyDown();
bool sfjs();
void getoxy(int x,int y);
void ex();
int yy();
void menu1() {int n;HANDLE MFUN;MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);system("color 3e");printf("====================================================\n");printf("# #\n");printf("# #\n");printf("# 1. 建立新玩家 #\n");printf("# 2. 读取存档 #\n");printf("# #\n");printf("# #\n");printf("====================================================\n");printf(" 选项:");scanf("%d", &n);if (n == 1) {xj();} else if (n == 2) {dq();} else {printf("输入错误,请重新选择:\n");menu1();}}void menu2() {int n;HANDLE MFUN;MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);printf("====================================================\n");printf("# #\n");printf("# 贪吃蛇 Version goddemon #\n");printf("# #\n");printf("# 1. 开始游戏 #\n");printf("# 2. 查看排行榜 #\n");printf("# 3. 查看我的分数 #\n");printf("# 4. 退出游戏 #\n");printf("# #\n");printf("# #\n");printf("====================================================\n");printf(" 选项:\n");while (1) {scanf("%d", &n); switch (n) {case 1:begin();break;case 2:phb();break;case 3:fs1();break;case 4:ex();break;default:printf("输出错误,请重新输入:\n");break;}}}
void xj() { //新建用户的名字ck();++zpalyer;printf("\t\t输入用户名:\n");scanf("%s", name);strcpy(player[zpalyer].name,name);//注意赋值的先后menu2();
}
void dq() {ck();printf("请输入读取的姓名:\n");scanf("%s", name1);for (i = 0; i <= zpalyer; i++) {if ((strcmp(player[i].name, name1) == 0)) {menu2();} else if(i==zpalyer) {printf("未找到该用户,请在次选择读取与创建功能\n");menu1();}}
}
int begin() {system("cls");jm();while (1) {keyDown();if (!sfjs())break;creatfood();Sleep(snake1.speed);}player[zpalyer].palyefs = snake1.zfs;getoxy(W / 2, H / 2);printf("总共得分:%d\n",snake1.zfs);Sleep(3000);system("cls");menu2();Sleep(5000);return 0;}
void phb() {int i;printf("====================== 排行榜 ====================\n");for (i = 0; i <zpalyer; i++) {printf("%s\t%d\n", player[i].name, player[i].palyefs);}printf("====================================================\n");}
void fs1() {printf("你的分数为:\n");printf("%d", player[zpalyer].palyefs);}
void ex() {save();exit(0);
}
void save() {FILE *fp;fp = fopen("tcs.txt", "w+");fwrite(&zpalyer, 4, 1, fp);for (i = 0; i <= zpalyer; i++) {fwrite(&player[i], sizeof(struct palyerinfo), 1, fp);}fclose(fp);}
void ck() {FILE *fp;int i;fp = fopen("tcs.txt", "r+");fread(&zpalyer, 4, 1, fp);for (i = 0; i <=zpalyer; i++) {fread(&player[i], sizeof(struct palyerinfo), 1, fp);}fclose(fp);
}
void xuanz() {int j = 0, x = 1;menu2();}
void getoxy(int x,int y) {COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void jm() { system("cls");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);system("color 3e");for (i = 0; i <= W; i+=2) {getoxy(i, 0);printf("++");getoxy(i,H);printf("++");}for (i = 0; i < H; i++) {getoxy(0, i);printf("++");getoxy(W, i);printf("++");}while (1) {srand((unsigned int)time(NULL));food.x = rand() % (W - 4) + 2;food.y = rand() % (H - 2) + 1;if (food.x % 2 == 0) {break;}}getoxy(food.x, food.y);printf("○");snake1.len = 3;snake1.speed = 200;snake1.x[0] = W / 2 + 1;snake1.y[0] = H / 2;getoxy(snake1.x[0], snake1.y[0]);printf("★");for (i = 1; i < snake1.len; i++) {snake1.x[i] = snake1.x[i - 1] + 2;snake1.y[i] = snake1.y[i - 1];getoxy(snake1.x[i], snake1.y[i]);printf("★");}getoxy(W - 2,0);return;
}
void creatfood() {if (snake1.x[0] == food.x&&snake1.y[0] == food.y) {while (1) {int flag = 1;srand((unsigned int)time(NULL));food.x = rand() % (W - 4) + 2;food.y = rand() % (H - 2) + 1;for (i = 0; i < snake1.len; i++) {if (snake1.x[i] == food.x&&snake1.y[i] == food.y) {flag = 0;break;}}if (flag&&food.x % 2 == 0) { //判断是否该结束坐标赋值break;}}getoxy(food.x, food.y);printf("○");snake1.len++;snake1.speed -= 5;snake1.zfs += 10;changeFlag = 1;}return;
}
void keyDown() {int pre_key = key;if (_kbhit()) {fflush(stdin);key = _getch();key = _getch();}if (changeFlag == 0) {getoxy(snake1.x[snake1.len - 1], snake1.y[snake1.len - 1]);printf(" ");}for (i = snake1.len - 1; i > 0; i--) {snake1.x[i] = snake1.x[i - 1];snake1.y[i] = snake1.y[i - 1];}if (pre_key == 72 && key == 80)key = 72;if (pre_key == 80 && key == 72)key = 80;if (pre_key == 75 && key == 77)key = 75;if (pre_key == 77 && key == 75)key = 77;switch (key) {case 75:snake1.x[0] -= 2;//往左break;case 77:snake1.x[0] += 2;//往右break;case 72:snake1.y[0]--;//往上break;case 80:snake1.y[0]++;//往下break;}getoxy(snake1.x[0], snake1.y[0]);printf("★");getoxy(W - 2, 0);changeFlag = 0;return;
}
bool sfjs() {if (snake1.y[0] == 0 || snake1.y[0] == H) {return false;}if (snake1.x[0] == 0 || snake1.x[0] == W) {return false;}for (i = 1; i < snake1.len; i++) {if (snake1.x[i] == snake1.x[0] && snake1.y[i] == snake1.y[0]) {return false;}}return true;
}
int main() {menu1();system("pause");return 0;
}
这个音乐大家可以自己去找一找,如果想要我的原版的话,私信我就好了!
大家一起上赞!欢迎大佬指导哦!ヾ( ̄▽ ̄)Bye~Bye~