贪吃蛇成品
目前贪吃蛇的代码也是弄完了,只是其中的原理大部分还不懂,还需要再继续看一下,理解
代码
#include <stdio.h> #include <time.h> #include <windows.h> #include <stdlib.h> #include <conio.h>
#define U 1 #define D 2 #define L 3 #define R 4 typedef struct snake { int x; int y; struct snake* next; } snake; // 自定义 gotoxy,控制控制台光标位置 int score = 0, add = 10, level = 0; snake* head = NULL, * food = NULL;//舌头指针 snake* q = NULL;//遍历蛇身时的中间指针 int endgamestatus = 0; int sleeptime = 200; int status = R; void createfood(); void initsnake(); void welcomegame(); void keyboardcontrol(); void createmap(); void explation(); void levelChange(); void scoreandtips(); void lostDraw(); void choose(int n); void endgame(); void gotoxy(int x, int y) { COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); }
// 自定义 color,设置控制台文字颜色 int color(int c) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, c); return 0; } void createmap() { int i, j; for (i = 0; i < 58; i += 2) { gotoxy(i, 0); color(5); printf("□ "); gotoxy(i, 26); printf("□ "); } for (i = 1; i < 26; i++) { gotoxy(0, i); printf("□ "); gotoxy(56, i); printf("□ "); } for (i = 2; i < 56; i += 2) { for (j = 1; j < 26; j++) { gotoxy(i, j); color(3); printf("□ "); } } } // welcomegame 函数:游戏欢迎界面与选择逻辑 void explation() { system("cls"); int i, j = 1; color(13); gotoxy(44, 3); printf("游戏说明"); color(2); for (i = 6; i < 25; i++) { for (j = 20; j <= 75; j++) { gotoxy(j, i); if (i == 6 || i == 25)printf("="); else if (j == 20 || j == 75)printf(""); } } color(3); gotoxy(30, 6); printf("1.不能穿墙,不能咬到自己"); color(10); gotoxy(30, 9); printf("2.分别控制上下左右控制蛇的移动"); color(14); gotoxy(30, 12); printf("3.小惊喜 F1可以加速\tf2可以减速 "); color(11); gotoxy(30, 15); printf("4.按空格键暂停,再按继续"); color(15); gotoxy(30, 18); printf("5.由任涵提供技术支持"); color(4); gotoxy(30, 21); printf("6.按任意键返回开始界面"); _getch(); system("cls"); welcomegame();
} void welcomegame() { int a; gotoxy(43, 10); color(11); printf("贪 吃 蛇 大 作 战"); color(12); gotoxy(25, 22); printf("1.开始游戏"); gotoxy(45, 22); printf("2.游戏说明"); gotoxy(65, 22); printf("3.退出游戏"); gotoxy(43, 27); color(3); printf("请选择 1 2 3:");
// 输入选择
scanf("%d", &a);
switch (a) {
case 1:
system("cls"); // 清屏
createmap();
initsnake();
createfood();
keyboardcontrol();
break;
case 2:
explation();
break;
case 3:
exit(0); // 退出程序
break;
default:
gotoxy(40, 28);
color(12);
printf("请输入1到3的数字");
_getch(); // 输入任意键
system("cls");
welcomegame(); // 重新显示菜单
break;
}
}
// createmap 函数:绘制游戏地图 void levelChange() { if (score >= 0 && score < 50)level = 1; else if (score >= 50 && score < 200)level = 2; else if (score >= 200 && score < 500)level = 3; else if (score >= 500 && score < 1000)level = 4; else level = 5; switch (level) { case 1: printf("等级:坚韧黑铁"); break; case 2: printf("等级:英勇黄铜"); break; case 3: printf("等级:不屈白银"); break; case 4: printf("等级:荣耀黄金"); break; case 5: printf("等级:华贵铂金"); break; default: break; } } void scoreandtips() { gotoxy(74, 2); color(12); printf("得分:%d", score); gotoxy(74, 4); color(12); levelChange(); gotoxy(74, 8); color(14); printf("每个食物得分:%d分!", add); gotoxy(74, 10); printf("不能碰墙,不能咬到自己!"); gotoxy(74, 12); printf("用上下左右来控制蛇的移动!"); gotoxy(74, 14); printf("F1为加速,F2为减速!"); gotoxy(74, 16); printf("space:暂停游戏!"); gotoxy(74, 18); printf("ESC:退出游戏!"); time_t timep; struct tm* p; time(&timep); p = gmtime(&timep); gotoxy(74, 22); color(15); printf("Date:%d/%d/%d\n", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday); gotoxy(74, 24); printf("Time:%d:%d:%d\n", 8 + p->tm_hour, p->tm_min, p->tm_sec); gotoxy(74, 26); printf("由任涵提供技术支持"); }
void lostDraw() { system("cls"); int i; gotoxy(46, 2); color(6); printf("\\|///"); gotoxy(43, 3); printf("\\\"); gotoxy(47, 3); color(15); printf(".-.-"); gotoxy(54, 3); color(6); printf("//"); gotoxy(44, 4); color(14); printf("("); gotoxy(47, 4); color(14); printf("<<.@.>>@"); gotoxy(54, 4); color(14); printf(")"); gotoxy(17, 5); color(11); printf("+--------------"); gotoxy(35, 5); color(14); printf("o00o"); gotoxy(39, 5); color(11); printf("--------"); gotoxy(61, 5); color(14); printf("o00o"); gotoxy(65, 5); color(11); printf("---------+"); for (i = 6; i <= 19; i++) { gotoxy(17, i); printf("|"); gotoxy(82, i); printf("|"); } gotoxy(17, 22); printf("+----------------"); gotoxy(47, 22); color(14); printf("★★★★★"); gotoxy(56, 22); color(11); printf("-----------------------"); } void initsnake() { snake* tail = (snake*)malloc(sizeof(snake)); tail->x = 24; tail->y = 5; tail->next = NULL; int i; for (i = 1; i <= 2; i++) { head = (snake*)malloc(sizeof(snake)); head->next = tail;//蛇头的下一个位置 head->x = 24 + 2 * i; head->y = 10; tail = head; } snake* temp = head; while (temp != NULL) { gotoxy(temp->x, temp->y); color(14); printf("★"); temp = temp->next; }
} void createfood() { snake* food_1 = (snake*)malloc(sizeof(snake)); srand((unsigned)time(NULL)); food_1->x = 36; food_1->y = 9; while ((food_1->x % 2) != 0) { food_1->x = rand() % 52 + 2; } food_1->y = rand() % 24 + 1; q = head; int flag = 0; while (q != NULL) { if (q->x == food_1->x && q->y == food_1->y) { free(food_1); createfood(); flag = 1; break; } q = q->next; } if (flag) return; gotoxy(food_1->x, food_1->y); food = food_1; color(12); printf("");//输出食物; } int biteself() { snake self = head->next; while (self != NULL) { if (self->x == head->x && self->y == head->y) { return 1; } self = self->next;
}
return 0;
} void cantcrosswall() { if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) { endgamestatus = 1; endgame(); } } void speedup() { if (sleeptime >= 50) { sleeptime = sleeptime - 10; add = add + 2; } } void speeddown() { if (sleeptime < 350) { sleeptime = sleeptime + 30; add = add - 2; } } void snakemove() { snake* nexthead = (snake*)malloc(sizeof(snake)); cantcrosswall(); if (status == U) { nexthead->x = head->x; nexthead->y = head->y - 1; nexthead->next = head; head = nexthead; q = head; } else if (status == D) { nexthead->x = head->x; nexthead->y = head->y + 1; nexthead->next = head; head = nexthead; q = head; } else if (status == L) { nexthead->x = head->x - 2; nexthead->y = head->y; nexthead->next = head; head = nexthead; q = head; } else if (status == R) { nexthead->x = head->x + 2; nexthead->y = head->y; nexthead->next = head; head = nexthead; q = head; } if (nexthead->x == food->x && nexthead->y == food->y) { snake* temp = head; while (temp != NULL) { gotoxy(temp->x, temp->y); color(14); printf("★"); temp = temp->next; } score = score + add; speedup(); createfood(); } else { snake* temp = head; while (temp->next->next != NULL) { gotoxy(temp->x, temp->y); color(14); printf("★"); temp = temp->next; } gotoxy(temp->next->x, temp->next->y); color(3); printf("■"); free(temp->next); temp->next = NULL; } if (biteself() == 1) { endgamestatus = 2; endgame(); } } //控制键盘; void keyboardcontrol() { while (1) { scoreandtips(); if (GetAsyncKeyState(VK_UP) && status != D) { status = U; } else if (GetAsyncKeyState(VK_DOWN) && status != U) { status = D; } else if (GetAsyncKeyState(VK_LEFT) && status != R) { status = L; } else if (GetAsyncKeyState(VK_RIGHT) && status != L) { status = R; } if (GetAsyncKeyState(' ')) { while (1) { Sleep(300); if (GetAsyncKeyState(' ')) { break; } } } else if (GetAsyncKeyState(VK_ESCAPE)) { endgamestatus = 3; break;
}
else if (GetAsyncKeyState(VK_F1)) {
speedup();
}
else if (GetAsyncKeyState(VK_F2))
{
speeddown();
}
Sleep(sleeptime);
snakemove();
}
} void endgame() { system("cls"); if (endgamestatus == 1) { lostDraw(); gotoxy(45, 10); color(12); printf("你撞墙了哥们"); } else if (endgamestatus == 2) { lostDraw(); gotoxy(45, 10); color(12); printf("Game Over!"); } gotoxy(44, 13); color(11); printf("您的得分是%d", score); gotoxy(44, 15); color(14); levelChange(); int n; choose(n); } void choose(int n) { gotoxy(30, 24); color(12); printf("继续游戏-----------1"); gotoxy(57, 24); printf("退出游戏-----------2"); gotoxy(47, 28); color(11); printf("请选择(1或2):"); scanf("%d", &n); switch (n) { case 1: system("cls"); score = 0; sleeptime = 200; add = 10; welcomegame(); break; case 2: exit(0); break; default: gotoxy(35,27); color(12); printf("输入有误!请重新输入1或2!"); system("nause>null"); endgame(); choose(0); break;} } int main(){ system("mode con cols=110 line=40"); welcomegame(); keyboardcontrol; endgame();
return 0;
}
1.宏定义 2.typedef数据结构?