标签:style blog http color io ar for sp div
哇咔咔,这是我步入游戏制作界的第一步。
忙活了一个国庆,虽说bug略多,但是成就感满满的。
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #define H 20 #define L 20 void map(int hard); void maphard(void); void play(void); void food(void); int live(void); void direction(char button, struct snake*direction); void flashmap(struct snake*showsnake, int fx, int fy); char MAP[H][L]; char key; int life = 0; int h; int roof[4]; struct snake { int x; int y; struct snake *next; }*head, *body, *tail, *direct, *test; int main(void) { char choice; int speed; printf(" 《贪吃蛇》 作者:sp\n"); printf("请选择您的贪吃蛇水平(输入数字,按回车确认):\n"); printf("1.我是菜鸟 2.过关无压力\n"); printf("3.大神你懂吗 4.丧心病狂模式\n"); while (scanf("%c", &choice) == 1) { switch (choice) { case ‘1‘: map(0); play(800, 0); break; case ‘2‘: map(0); play(400, 0); break; case ‘3‘: map(0); play(100, 0); break; case ‘4‘: printf("丧心病狂模式介绍:\n"); printf("该模式下场地上会出现一片屋顶(用“+”表示)\n贪吃蛇可以穿过屋顶,但是在屋顶下玩家无法看到蛇的行踪\n"); printf("贪吃蛇的食物有几率出现在屋顶下,玩家需要通过其丧心病狂的直觉来吃到食物\n"); system("pause"); map(1); play(300, 1); break; default:printf("游戏结束!"); } } system("pause"); return 0; } void map(int hard) { int i, j, x, y; memset(MAP, ‘ ‘, sizeof(MAP)); system("cls"); head = (struct snake*)malloc(sizeof(struct snake)); srand(time(0));//随机种子 x = rand() % (H - 2) + 1;//初始化蛇头 y = rand() % (L - 2) + 1; MAP[x][y] = ‘*‘; head->x = x; head->y = y; head->next = NULL; direct = head; if (hard = 0) { for (i = 0; i < H; i++)//画地图 { for (j = 0; j < L; j++) { if (i == 0 || i == H - 1 || j == 0 || j == L - 1)//造围墙 MAP[i][j] = ‘#‘; printf("%c", MAP[i][j]); } printf("\n"); } } else { for (h = 0; h < 4; h++) { srand(time(0)); roof[h] = rand() % (H - 2) + 1; } for (i = 0; i < H; i++)//画地图 { for (j = 0; j < L; j++) { if (i == roof[0] || i == roof[1] || j == roof[2] || j == roof[3])//屋顶 MAP[i][j] = ‘+‘; if (i == 0 || i == H - 1 || j == 0 || j == L - 1)//造围墙 MAP[i][j] = ‘#‘; printf("%c", MAP[i][j]); } printf("\n"); } } } void play(int speed, int hard) { int fx, fy; int food = 1; while (1) { Sleep(speed); while (food)//生成食物 { srand(time(0)); fx = rand() % (H - 2) + 1; fy = rand() % (L - 2) + 1; food = 0; } if (_kbhit() != 0)//有键盘输入则变换key的值,否则保持原方向 key = _getch(); direction(key, direct); body = head; if ((direct->x != fx) || (direct->y != fy)) { while (body->next != NULL) { body->x = (body->next)->x; body->y = (body->next)->y; body = body->next; } } else{ tail = (struct snake*)malloc(sizeof(struct snake));//加长蛇身 direct->next = tail; tail->next = NULL; tail->x = fx; tail->y = fy; direct = tail; food = 1; } flashmap(head, fx, fy, hard); life = live(); if (life) break; } } int live(void) { if (body->x == 0 || body->y == 0 || body->x == H - 1 || body->y == L - 1) return 1; else return 0; } void flashmap(struct snake*showsnake, int fx, int fy, int hard)//刷新屏幕 { system("cls"); int i, j; memset(MAP, ‘ ‘, sizeof(MAP)); while (showsnake != NULL) { MAP[showsnake->x][showsnake->y] = ‘*‘; showsnake = showsnake->next; } MAP[fx][fy] = ‘@‘; if (hard == 0) { for (i = 0; i < H; i++)//画地图 { for (j = 0; j < L; j++) { if (i == 0 || i == H - 1 || j == 0 || j == L - 1)//造围墙 MAP[i][j] = ‘#‘; printf("%c", MAP[i][j]); } printf("\n"); } } else { for (i = 0; i < H; i++)//画地图 { for (j = 0; j < L; j++) { if (i == roof[0] || i == roof[1] || j == roof[2] || j == roof[3])//屋顶 MAP[i][j] = ‘+‘; if (i == 0 || i == H - 1 || j == 0 || j == L - 1)//造围墙 MAP[i][j] = ‘#‘; printf("%c", MAP[i][j]); } printf("\n"); } } } void direction(char button, struct snake*direction)//改变方向 { switch (button) { case ‘a‘:direction->y--; break; case ‘d‘:direction->y++; break; case ‘w‘:direction->x--; break; case ‘s‘:direction->x++; default:break; } }
标签:style blog http color io ar for sp div
原文地址:http://www.cnblogs.com/sunpeng1995/p/4020393.html