贪吃蛇C语言

喜你入骨 提交于 2021-01-14 16:05:26

http://groups.tianya.cn/post-233945-a26c77a21b324ae5952434f366b8504c-1.shtml
http://groups.tianya.cn/post-233945-affb7a12483a4ae3a5d5fa0a979cf256-1.shtml
http://groups.tianya.cn/post-233945-69147112bfc84a4ab7fe4e24245e71fc-1.shtml
http://groups.tianya.cn/post-233945-b7f4bc30917a48b9a1ce6ad37e095a4c-1.shtml
http://groups.tianya.cn/post-233945-f89157e886864b77997b83469e2236f7-1.shtml
http://groups.tianya.cn/post-233945-ebf45fbb7f104197bc00a7f2b0156e57-1.shtml
http://groups.tianya.cn/post-233945-75deb3d90b35440da4f0bf53b68577a2-1.shtml
http://groups.tianya.cn/post-233945-c6a633dfd181414281776aaa22b0ce77-1.shtml
http://groups.tianya.cn/post-233945-58fd3abbba2744ac961842fe3c7d0bb0-1.shtml
http://groups.tianya.cn/post-233945-f70a25ad01524b08b4267a860dc74264-1.shtml
http://groups.tianya.cn/post-233945-7a331681cad44c869b6419cc6c003f3e-1.shtml
http://groups.tianya.cn/post-233945-9441de1a15234d5994d7390d69232834-1.shtml
http://groups.tianya.cn/post-233945-9a820676e89b43fbb3958675a0eebae8-1.shtml
http://groups.tianya.cn/post-233945-50c8cc8b4bac46fca530b56167fd8eaf-1.shtml
http://groups.tianya.cn/post-233945-2bfa5658dbd34e19925386c4a3660612-1.shtml
http://groups.tianya.cn/post-233945-5cd559245e5146558d9dedd68a9e85ec-1.shtml
http://groups.tianya.cn/post-233945-918d0df3097c4788897de904ed1fe887-1.shtml
http://groups.tianya.cn/post-233945-f4fa9ebc568f4efe833a0a0930289aa3-1.shtml
http://groups.tianya.cn/post-233945-65a3b5f22c5044a59883701a87f96634-1.shtml
http://groups.tianya.cn/post-233945-1dd725b41a4a4d568352ed9e39ee23cc-1.shtml
http://groups.tianya.cn/post-233945-d55fb3816be745c7844d74c8669a38cd-1.shtml
http://groups.tianya.cn/post-233945-e99fb29edce74e27ba7ffa5ffd251da3-1.shtml
http://groups.tianya.cn/post-233945-cf719a5cd2b244daa09c07946a462ddd-1.shtml
http://groups.tianya.cn/post-233945-d23eadfd37364803acbd4bc0ec16bdad-1.shtml
http://groups.tianya.cn/post-233945-196dea6ae7a44a61a106712ac9fae76b-1.shtml
http://groups.tianya.cn/post-233945-34c05ce4b3fb416abcea2e297662c4a9-1.shtml
http://groups.tianya.cn/post-233945-20008bc1220e4e58adc4a000b3caad05-1.shtml
http://groups.tianya.cn/post-233945-e6ee8da6590d4a8695f7897ad99b5fe8-1.shtml
http://groups.tianya.cn/post-233945-d9cf60adb0e84350babb400ac6a18437-1.shtml
http://groups.tianya.cn/post-233945-dab9b08f6faf443f89321b6fd0968dba-1.shtml
http://groups.tianya.cn/post-233945-8d3f5532eee745fa879c6e46224b3eb8-1.shtml
http://groups.tianya.cn/post-233945-64a1ca1e2d384e9888c52b2b85f8e8a8-1.shtml
http://groups.tianya.cn/post-233945-f2446e05b7e24b5db74f371bc833aad0-1.shtml
http://groups.tianya.cn/post-233945-198714fc7a0b4d099eb680f0be78ca9e-1.shtml
http://groups.tianya.cn/post-233945-9e02a45d488e47dfba236e8e99868e56-1.shtml
http://groups.tianya.cn/post-233945-a07e7a8b778c477d924215876ac6e5f5-1.shtml
http://groups.tianya.cn/post-233945-b0367c3aadcb469e997af145fb9270a0-1.shtml
http://groups.tianya.cn/post-233945-66c586a6013f43e98680cd89088ae0ca-1.shtml
http://groups.tianya.cn/post-233945-72c3b6ffa7a649ae85c5c1e9fc87e95d-1.shtml
http://groups.tianya.cn/post-233945-f8dfaeb7b32f40c38e7f81ea66e817c6-1.shtml
http://groups.tianya.cn/post-233945-4212a905f16f40e2a9f1af93918a27a5-1.shtml
http://groups.tianya.cn/post-233945-c1285ce3d4154d9d88aef178a76ee856-1.shtml
http://groups.tianya.cn/post-233945-9afbc095a909444683a7f51b37058a7d-1.shtml
http://groups.tianya.cn/post-233945-fe34bc97430f410fb8548d88ac9fd90a-1.shtml
http://groups.tianya.cn/post-233945-22774573c0404a4fa738f1dd9a050a7f-1.shtml
http://groups.tianya.cn/post-233945-0fec326c51644e5ab307aee197628102-1.shtml
http://groups.tianya.cn/post-233945-3df24e0c2a53426c94f8ab4e9478da74-1.shtml
http://groups.tianya.cn/post-233945-b03f16ee57c548409bb24f07e9282177-1.shtml
http://groups.tianya.cn/post-233945-7e62c171081e4945b0a59a4521b081d5-1.shtml
http://groups.tianya.cn/post-233945-ef31f486e44c4f888bacae35664b8de9-1.shtml
http://groups.tianya.cn/post-233945-3433883ec77f44ecb4153a03be089085-1.shtml
http://groups.tianya.cn/post-233945-47fa779bf1c6485787adf01ae79aa836-1.shtml
http://groups.tianya.cn/post-233945-9bda29dc9e394e5da3f430411cb5a337-1.shtml
http://groups.tianya.cn/post-233945-765bc1a6fd244018bb203860555011cd-1.shtml
http://groups.tianya.cn/post-233945-1abcbfcd85504e12ad7896451e505bd2-1.shtml
http://groups.tianya.cn/post-233945-4414a512215f4788afecd23efd9319f5-1.shtml
http://groups.tianya.cn/post-233945-fa1495f110ef4fbcaa3c14448f203c0a-1.shtml
http://groups.tianya.cn/post-233945-a71f768adba54ec6ba72bef2631098d1-1.shtml
http://groups.tianya.cn/post-233945-ab94afb8f07c453793261ab174034be0-1.shtml
http://groups.tianya.cn/post-233945-7b2985f5fd604d96ad33ebcab7dc4274-1.shtml
http://groups.tianya.cn/post-233945-b7d76620001b4b8293f27c96038ed273-1.shtml
http://groups.tianya.cn/post-233945-af888fef61824bcca880b31170eef18d-1.shtml
http://groups.tianya.cn/post-233945-428fbd0d680a445eadc8925336c537d5-1.shtml
http://groups.tianya.cn/post-233945-bb61169d484d49819a5761683b4eebc0-1.shtml
http://groups.tianya.cn/post-233945-ce76580375a3487dafc39153f36895d7-1.shtml
http://groups.tianya.cn/post-233945-b022aefd4038435e80769e4d923bdad8-1.shtml
http://groups.tianya.cn/post-233945-134d2555e473460faba767e4dc0def29-1.shtml
http://groups.tianya.cn/post-233945-d23409c8adf742328834585fba93321d-1.shtml
http://groups.tianya.cn/post-233945-f6ad6f7d25ec4366aaa8a8d2ea772224-1.shtml
http://groups.tianya.cn/post-233945-b106cb65826d43d7885f4a88d482e3e5-1.shtml
http://groups.tianya.cn/post-233945-0b256df8b2c94235806a6849405174e7-1.shtml
http://groups.tianya.cn/post-233945-839aac1f1b04469fb23d3af4c689494d-1.shtml
http://groups.tianya.cn/post-233945-3d1966d67db445fc9ab61b4a4a7ac278-1.shtml
http://groups.tianya.cn/post-233945-123fb25a363a43a28a59a3bfd1fd2a2c-1.shtml
http://groups.tianya.cn/post-233945-7ef2f65c2c174097ade2ea7c1e53c2fc-1.shtml
http://groups.tianya.cn/post-233945-42cb0cc62ecc4e36969a47164be276cc-1.shtml
http://groups.tianya.cn/post-233945-0f587df088904ae6a4b0036812683e07-1.shtml
http://groups.tianya.cn/post-233945-69e7079b6c904a4eabbaee0ea38ae76e-1.shtml
http://groups.tianya.cn/post-233945-02a467079a4e4843b88e8c40be7cfc22-1.shtml
http://groups.tianya.cn/post-233945-dc723b894e7a4f38ae29dfbb76fb94fb-1.shtml
http://groups.tianya.cn/post-233945-88de8608e87e490ea0ddeea9fb31a4e2-1.shtml
http://groups.tianya.cn/post-233945-73560cd0350443239fe57b02a891877a-1.shtml
http://groups.tianya.cn/post-233945-6a3ae2f0a28d445a8e35bfe2ccc23833-1.shtml
http://groups.tianya.cn/post-233945-6a3ae2f0a28d445a8e35bfe2ccc23833-1.shtml
http://groups.tianya.cn/post-233945-4a0a735b3db44f18857538cf244653be-1.shtml
http://groups.tianya.cn/post-233945-fdf61773f3e641689d5c21f6e6bdacf7-1.shtml
http://groups.tianya.cn/post-233945-038361e196c240de93a0230b77adfa02-1.shtml
http://groups.tianya.cn/post-233945-48e6c9dd9dff42989f7168afd21d0431-1.shtml
http://groups.tianya.cn/post-233945-7efbecc175be4d738560b1a1f17adf63-1.shtml
http://groups.tianya.cn/post-233945-cd22c4a2b7e64a349c20b729bcb3dd6f-1.shtml
http://groups.tianya.cn/post-233945-89d531d103e0440e82bd8b7763079c66-1.shtml
http://groups.tianya.cn/post-233988-0e7fa78aba104e0aae4302f7883d7ff5-1.shtml
http://groups.tianya.cn/post-233988-8ac24d620bb44957b49771988595c4b5-1.shtml
http://groups.tianya.cn/post-233988-919fa51e60e0438580146b3a5d898f9f-1.shtml
http://groups.tianya.cn/post-233988-b75d2df73540424383d210a1c9581eaa-1.shtml
http://groups.tianya.cn/post-233988-8cc9195c2f9043f2bc804de7f2c3669b-1.shtml
http://groups.tianya.cn/post-233988-f35cf5ec50484e449ad807791e10b7eb-1.shtml
http://groups.tianya.cn/post-233988-43adb6bb993c4b77b4923f0820076f68-1.shtml
http://groups.tianya.cn/post-233988-f849fcad1cbe4a46bfe8e55f0dc660d5-1.shtml
http://groups.tianya.cn/post-233988-6ec5a3ea636a485a80e1d7e11aa3aa24-1.shtml
http://groups.tianya.cn/post-233988-bd7f73cc51004dcc9bf6b42eb1abf1d5-1.shtml
http://groups.tianya.cn/post-233988-0e9000a3605247f181c54ade7eca4e1f-1.shtml
http://groups.tianya.cn/post-233988-ed50ea134aec4d6ca353895eb50d16e9-1.shtml
http://groups.tianya.cn/post-233988-c0e67d131d4a47bfb0957945f01705f7-1.shtml
http://groups.tianya.cn/post-233988-0ecc88a67e5442ba80e1e7594bda48f3-1.shtml
http://groups.tianya.cn/post-233988-b13ed570a8674d37b0abde5444628f4b-1.shtml
http://groups.tianya.cn/post-233988-2df1652ceeb44f2c83863d8afeed88be-1.shtml
http://groups.tianya.cn/post-233988-91d082a5d2874510af17d94f3b255b08-1.shtml
http://groups.tianya.cn/post-233988-8db3b06f455148419a410b9c82b6bec5-1.shtml
http://groups.tianya.cn/post-233988-38b65508b71a428c96167652d0b20128-1.shtml
http://groups.tianya.cn/post-233988-22a0d629defa4ac2b0fbe6967a9408e0-1.shtml
http://groups.tianya.cn/post-233988-1e4b1b82709d4858826d1b6d862f43e9-1.shtml
http://groups.tianya.cn/post-233988-9be45a5b65174480b6ab6b4fadc201df-1.shtml
http://groups.tianya.cn/post-233988-c5aba24584a54ca2830128135b9f7d65-1.shtml
http://groups.tianya.cn/post-233988-fab0fbd5ff8e4437a1c5e9676a567178-1.shtml
http://groups.tianya.cn/post-233988-5f0c4538129d49758a3482c147aa71ac-1.shtml
http://groups.tianya.cn/post-233988-90d9f80555d344b3adf0b0bd9089f2b4-1.shtml
http://groups.tianya.cn/post-233988-33e06b0f3aba416da08570fa008aed9a-1.shtml
http://groups.tianya.cn/post-233988-f556f0c4c6634bee98e02265bb5b6105-1.shtml
http://groups.tianya.cn/post-233988-07ca55d6c0544d04b8bf60ec0589420a-1.shtml
http://groups.tianya.cn/post-233988-df207c4793774a3bb49e582b550ef848-1.shtml
http://groups.tianya.cn/post-233988-921bc900f8ac4896aeb5a2d5ac6470f9-1.shtml
http://groups.tianya.cn/post-233988-e20059f12e234354a86e1d372ea32d18-1.shtml
http://groups.tianya.cn/post-233988-11ee1c907a60488786f0d0b5d6814468-1.shtml
http://groups.tianya.cn/post-233988-33b3b21ba03d43248797c15e70a05c96-1.shtml
http://groups.tianya.cn/post-233988-1772fe3bacde4cf09b2d4b2a94bdb975-1.shtml
http://groups.tianya.cn/post-233988-4cf042715eae4d0ba39e631e588f5755-1.shtml
http://groups.tianya.cn/post-233988-a7a8a87267fb4a11a303343598230b4f-1.shtml
http://groups.tianya.cn/post-233988-343c79c7595040aead839413c88e27c1-1.shtml
http://groups.tianya.cn/post-233988-40c5b19cc0664201bff7fb6e42f29166-1.shtml
http://groups.tianya.cn/post-233988-1d190a74b78b426aa508710f2ed9a6f0-1.shtml
http://groups.tianya.cn/post-233988-0fbb874619904d11afba683aaceba4ab-1.shtml
http://groups.tianya.cn/post-233988-f58abe6fcdf64ebab85738d659eeae12-1.shtml
http://groups.tianya.cn/post-233988-56e6f18a68ad40ca902c90727865303a-1.shtml
http://groups.tianya.cn/post-233988-dbf65f131a9a472695f6fe2ffc4e92ea-1.shtml
http://groups.tianya.cn/post-233988-1e88bfdea1e94e0bb53bb38a73178760-1.shtml
http://groups.tianya.cn/post-233988-b356ecd524ba416f9b48eb067a07d922-1.shtml
http://groups.tianya.cn/post-233988-64666b36643948e88463b25d106ee429-1.shtml
http://groups.tianya.cn/post-233988-1efb4574ef4345449fa6adf2200aff50-1.shtml
http://groups.tianya.cn/post-233988-5b39546880b7424ba5ab90fd473ccefd-1.shtml
http://groups.tianya.cn/post-233988-88616bca5ad04f32b066c6e8e4137487-1.shtml
http://groups.tianya.cn/post-233988-b47723974ffa4f6fb6606e9912a9fe90-1.shtml
http://groups.tianya.cn/post-233988-27a72deca2db43be858442f048ee94e2-1.shtml
http://groups.tianya.cn/post-233988-4e1dfb0024884007b5a7175678393c9f-1.shtml
http://groups.tianya.cn/post-233988-7b07e56da32244cc97c94184af709ea6-1.shtml
http://groups.tianya.cn/post-233988-5ecdac30c7f24e88b75c477208fe1e3c-1.shtml
http://groups.tianya.cn/post-233988-b734896d91a449ccab34eed14bc26554-1.shtml
http://groups.tianya.cn/post-233988-42dfb42d94be4112841f3eec640d7da9-1.shtml
http://groups.tianya.cn/post-233988-aa500e6c3d9f4831b91532f99e0bf10b-1.shtml
http://groups.tianya.cn/post-233988-e24b9a65f06141a68df40f3ad2c0d86d-1.shtml
http://groups.tianya.cn/post-233988-04f665679022456bac077b3a5f9f881a-1.shtml
http://groups.tianya.cn/post-233988-3a95d67f3a26480284fdf760cb7d1bf8-1.shtml
http://groups.tianya.cn/post-233988-e89e11c5b2bc47979a6bc2631ba5a8ee-1.shtml
http://groups.tianya.cn/post-233988-bcb96017cfc64eb7a834b5a036e77303-1.shtml
http://groups.tianya.cn/post-233988-9cdfea4a3f674b7fb11fca44667827b4-1.shtml
http://groups.tianya.cn/post-233988-d49f5709eece4867b2fac2fdc5559e8d-1.shtml
http://groups.tianya.cn/post-233988-50425a8346364b55bb0ff6f29c812d57-1.shtml
http://groups.tianya.cn/post-233988-df7ba639bb5a4203882e065d3e5b7e9a-1.shtml
http://groups.tianya.cn/post-233988-9dc0a4134fd943c9a0597291ce26f4d3-1.shtml
http://groups.tianya.cn/post-233988-c27989c9732b45fe80462db9d31f8e68-1.shtml
http://groups.tianya.cn/post-233988-846ddd246d454696a75508bf962fba07-1.shtml
http://groups.tianya.cn/post-233988-c993762c05d840e7a70e343ab1a742c1-1.shtml
http://groups.tianya.cn/post-233988-6abe7099514f4da28a25c4c5186fc116-1.shtml
http://groups.tianya.cn/post-233988-9b8c99aa9a324a0496b1891781b5a16b-1.shtml
http://groups.tianya.cn/post-233988-49c5bbb20b3b4a2ea37b4dd9fda8f2ae-1.shtml
http://groups.tianya.cn/post-233988-8703a45b93194772b9e59806763d209b-1.shtml
http://groups.tianya.cn/post-233988-b86341f5377f4df6b3a5c21c5959baeb-1.shtml
http://groups.tianya.cn/post-233988-b727ddc1a4094e75ac074a1a81d9d4a4-1.shtml
http://groups.tianya.cn/post-233988-4b973e3bdb3742868d34428ba78ed3b6-1.shtml
http://groups.tianya.cn/post-233988-5984d364ebee499680c7ca1b4b63f3d1-1.shtml
http://groups.tianya.cn/post-233988-3cfc6aa4d57c47d4af7881793e8ec7c1-1.shtml
http://groups.tianya.cn/post-233988-bd8af1e9bcc14088bd2f90736a047e7f-1.shtml
http://groups.tianya.cn/post-233988-6a7ae413746e435a942bd21193af6091-1.shtml
http://groups.tianya.cn/post-233988-5f3002770849426ba82b5c0b95fdfa19-1.shtml
http://groups.tianya.cn/post-233988-8aed2c9fa02c40ccbdd3d207dfbd215f-1.shtml
http://groups.tianya.cn/post-233988-7dae522e002b440fb9ee2dadf3d32404-1.shtml
http://groups.tianya.cn/post-233988-d4477c97e0b0407ba1ad5f1f14d8ef84-1.shtml
http://groups.tianya.cn/post-233988-49ed28be0d714be2b94eebed655835b4-1.shtml
http://groups.tianya.cn/post-233988-b2ee1564fd54482aa4877b6b8623152d-1.shtml
http://groups.tianya.cn/post-233988-9aaf8ac476d24d0fbfd74483efc7a8e0-1.shtml
http://groups.tianya.cn/post-233988-06deebcda9974804b36a5988879ee192-1.shtml
http://groups.tianya.cn/post-233988-d706ea36faa44cb39c5a2d8fa55e5627-1.shtml
http://groups.tianya.cn/post-233988-c9b4d74c2e224e439f3abe748617ae29-1.shtml
http://groups.tianya.cn/post-233988-beda0feeade544eea6098a60bb5f5d4d-1.shtml
http://groups.tianya.cn/post-233988-062ddf24dd784d0cba9336837f51d28d-1.shtml

贪吃蛇C语言

不知不觉大一已过去了一半, 今天要考试了, 我也是感慨良多, 那是我失去的青春, 呜呜呜~~ 这次代码是继上次的优化和完善, 若有好的建议或意见随时欢迎交流

运行效果图
运行效果图
代码奉上

# include <stdio.h>
# include <stdlib.h> 					//malloc()、calloc()、realloc()、free()、system()、atoi()、atol()、rand()、srand()、exit()
# include <windows.h>					//Sleep()、gotoxy()、HideCursor()
# include <conio.h>						//_getch()、kbhit()
# include <time.h>						//time()

void gotoxy(int x, int y) {				//移动光标; 网上复制
	COORD pos;
	HANDLE hOutput;
	pos.X = (SHORT)x;
	pos.Y = (SHORT)y;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, pos);
}

void HideCursor() {						//隐藏光标; 网上复制
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
}

typedef struct Node {                 	//结构体坐标
	int x, y;
	struct Node *next;
} Node, *List;

List head; 								//全局变量
//num蛇身长度; x, y蛇身坐标; fx, fy食物坐标; garde玩家分数
int num = 5, x = 5, y = 5, fx = 5, fy = 5, garde = 0;
DWORD speed = 200;                       //speed移动速度



List Create() { 						//创建结点
	List p = (List)malloc(sizeof(Node));
	if (!p) {
		puts("空间不足");
		exit(0);
	}
	p->next = NULL;
	return p;
}

void Insert() { 						//录入结点
	List p = Create();
	p->x = x;
	p->y = y;
	p->next = head->next;
	head->next = p;
}

void Wall() {
	int i, j;
	gotoxy(0, 0);
	for (i = 0; i < 20; ++i) {
		for (j = 0; j < 40; ++j) {
			if (0 == i || 19 == i || 0 == j || 39 == j) {
				printf("*");
			} else {
				printf(" ");
			}
		}
		printf("\n");
	}
}

void GameOver() {                       //游戏结束
	gotoxy(0, 0);                       //覆盖蛇身
	Wall();
	gotoxy(15, 9);
	puts("GAME OVER");
	system("pause");
	exit(0);
}

void EatOver() {                    	//吃到自己结束;
	int i, j;
	List pa, pb;
	//比较是否存在坐标相同结点, 如果存在即撞到自身, 游戏结束
	for (i = 0, pa = head->next; i < num - 1 && pa->next; ++i, pa = pa->next) {
		for (j = 0, pb = pa->next; j < num && pb; ++j, pb = pb->next) {
			if (pa->x == pb->x && pa->y == pb->y) {
				GameOver();
			}
		}
	}
}

List Food() {                     		//创建食物
	List p = Create();
	srand((unsigned)time(NULL));        //完全随机
	p->x = rand() % 17 + 1;
	p->y = rand() % 17 + 1;
	return p;
}

List FoodCreate() {                     //打印食物
	List p = head->next, pf = Create();
	int i, judge = 0;
	while (1) {
		judge = 0;
		pf->x = Food()->x;
		pf->y = Food()->y;
		//食物坐标与蛇身坐标比较, 如果相同, 则重新得到食物
		for (i = 0, p = head->next; i < num && p->next; ++i, p = p->next) {
			if (pf->x == p->x && pf->y == p->y) {
				judge = 1;
				break;
			}
		}
		if (0 == judge) {
			return pf;
		}
	}
}

void Print() { 							//打印蛇身
	int i;
	EatOver();                          //是否吃到自己
	List p = head->next;
	for (i = 0; i < num && p; ++i, p = p->next) {
		gotoxy(p->x, p->y);
		if (0 == i) {                   //蛇头
			printf("@");
		} else if (1 == i) {            //蛇身
			printf("+");
		} else if (i + 1 == num) {      //蛇尾覆盖
			printf(" ");
		}
	}
}

void WallDeath() {                      //撞墙结束
	if (0 == x || 39 == x || 0 == y || 19 == y) {
		GameOver();
	}
}

void WallPass() {                      //穿墙(未插入, 可替换WallDeath();)
	if (0 == x) {
		x = 38;
	}
	if (39 == x) {
		x = 1;
	}
	if (0 == y) {
		y = 18;
	}
	if (19 == y) {
		y = 1;
	}
}

void FoodEat() {						//吃到食物
	if (x == fx && y == fy) {
		if (0 == num % 5 && speed > 50) {               //速度变快
			speed -= 5;
		}
		gotoxy(41, 1);
		printf("速度: %.1f%%", 10000 / (float)speed);   //速度上限是200%
		garde += 10;                    //分数增加
		gotoxy(41, 3);
		printf("分数: %d", garde);
		++num;                          //蛇身加长
		gotoxy(41, 5);
		printf("长度: %d", num);
		fx = FoodCreate()->x;
		fy = FoodCreate()->y;
		gotoxy(fx, fy);                 //打印食物
		printf("$");
		gotoxy(41, 7);
		printf("食物: (%d, %d)  ", fx, fy);//注意细节, 加空格是为了覆盖
	}
}

void Menu() {                           //开始界面
	gotoxy(0, 0);
	puts("****************************************");
	puts("*              ________                *");
	puts("*              --------                *");
	puts("*            //        \\\\              *");
	puts("*          //      @     \\\\            *");
	puts("*        //     ______     \\\\          *");
	puts("*               ------                 *");
	puts("*                    //                *");
	puts("*                   //                 *");
	puts("*                  //                  *");
	puts("*           ________________           *");
	puts("*           ----------------           *");
	puts("*          ||              ||          *");
	puts("*          ||      ||      ||          *");
	puts("*          ||      ||      ||          *");
	puts("*                //  \\\\                *");
	puts("*              //      \\\\              *");
	puts("*            //          \\\\            *");
	puts("*                                      *");
	puts("****************************************");
	Sleep(2000);                        //显示2秒
}

int main() {
	HideCursor();                       //隐藏光标
	Menu();                             //打印开始界面
	Wall();                             //打印墙体
	head = Create();
	gotoxy(fx, fy);                     //打印第一个食物
	printf("$");
	char ch;
	while (1) {                          //有按键改变方向
		ch = (char)getch();
		while(!kbhit()) {            	//无按键向指定方向移动
			switch(ch) {
				case 'a': {             //向左
					--x;
					break;
				}
				case 'd': {             //向右
					++x;
					break;
				}
				case 'w': {             //向上
					--y;
					break;
				}
				case 's': {             //向下
					++y;
					break;
				}
				default: {
					break;
				}
			}
			FoodEat();                  //是否吃到食物
			WallDeath();            	//是否撞墙; 可替换为wallPass(x, y);
			Insert();                   //录入数据
			Print();                    //打印蛇身
			Sleep(speed);               //延迟; 与速度有关
		}
	}

	return 0;
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244

希望各位看望之后给个免费的红心加关注, 之后我还会继续分享的>-<

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!