【无标题】

160 篇文章 11 订阅
152 篇文章 20 订阅

一个简单的贪吃蛇游戏本来代码就不多,在保证可读性的情况下,很容易就控制在100以内了。

运行效果:

 代码:

#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define PANIC(err) (fprintf(stderr,"PANIC Line %d : %s",__LINE__,err),exit(-1),1)
#define PANICIFNULL(EXP) ((EXP)==NULL && PANIC("NULL"))
typedef enum { EMPTY=0, WALL, BODY, FOOD } MAP;
typedef int POSITION;
struct { int color; const char* shape; } UI[] = {
	{2,"■"},{4,"□"},{6,"★"},{4,"●"}
};
struct {
	int WIDTH, HEIGHT, direction, delay;
	MAP* map;
	POSITION* body, head, tail, len;
} C;
void initConsole(int width, int height) {
	char cmd[100];
	sprintf_s(cmd,100, "mode con cols=%d lines=%d && title C语言贪吃蛇 By dreamer2q %s", width, height,__DATE__);
	system(cmd);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cur_info;
	GetConsoleCursorInfo(handle, &cur_info);
	cur_info.bVisible = FALSE;
	SetConsoleCursorInfo(handle, &cur_info);
}
void updatePosition(POSITION pos) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord = { (pos % (C.WIDTH)) * 2 ,pos / (C.WIDTH) };
	SetConsoleCursorPosition(handle, coord);
	SetConsoleTextAttribute(handle, UI[C.map[pos]].color);
	printf("%s", UI[C.map[pos]].shape);
}
MAP food(int t) {
	POSITION pos = (rand() % ((C.WIDTH - 2) * (C.HEIGHT - 2))) + C.WIDTH + 1;
	if (C.map[pos]) return food(t);
	else return  (C.map[pos] = FOOD) ? updatePosition(pos), BODY : BODY;
}
int init() {
	C.WIDTH = C.HEIGHT = 30;
	initConsole(C.WIDTH * 2, C.HEIGHT);
	PANICIFNULL(C.map = (MAP*)malloc((C.WIDTH) * (C.HEIGHT) * sizeof(MAP)));
	PANICIFNULL(C.body = (POSITION*)malloc(C.WIDTH * C.HEIGHT * sizeof(POSITION)));
	C.head = (C.len = 3) - 1;
	C.direction = (C.tail = 0) + 1;
	C.delay = -150;
	memset(C.map, EMPTY, (C.WIDTH) * (C.HEIGHT) * sizeof(MAP));
	for (int i = 0; i < (C.WIDTH) * (C.HEIGHT); i++) {
		i < C.WIDTH && (C.map[i] = C.map[C.WIDTH * (C.HEIGHT - 1) + i] = WALL);
		i < C.HEIGHT && (C.map[C.WIDTH * i] = C.map[C.WIDTH * i + C.WIDTH - 1] = WALL);
		i < C.len && (C.map[C.body[i] = C.WIDTH * C.HEIGHT / 2 + C.WIDTH / 2 - 1 + i] = BODY);
		updatePosition(i);
	}
	srand(time(NULL));
	return food(0);
}
int Run(int shit) {
	int prv = 77;
	while (1) {
		if (_kbhit()) {
			int t = _getch();
			if ((prv + t) == 152)continue;
			switch (t) {
			case 72:C.direction = -C.WIDTH; break;
			case 80:C.direction = C.WIDTH; break;
			case 75:C.direction = -1; break;
			case 77:C.direction = 1; break;
			case ' ':C.delay = -C.delay; break;
			default:continue;
			}
			prv = t;
		}
#define INC(p) (((p)+1)%(C.WIDTH*C.HEIGHT))
		if (C.delay > 0) Sleep(C.delay);
		else continue;
		switch (C.map[C.body[INC(C.head)] = C.body[C.head] + C.direction]) {
		case FOOD:food(C.len = -C.len - 1);
		case EMPTY:
			C.map[C.body[C.head = INC(C.head)]] = BODY;
			updatePosition(C.body[C.head]);
			if (C.len > 0) updatePosition((C.map[C.body[C.tail]] = EMPTY) ? BODY : C.body[C.tail]), C.tail = INC(C.tail);
			else C.len = -C.len;
			break;
		case WALL:case BODY:
			return -1;//dead
		}
	}
}
int main() {
	while (1) {
		initConsole(25, 10);
		printf("\n\tC语言贪吃蛇\n\n    1. 开始游戏\n    2. 关于\n    q. 退出\n%");
		switch (_getch()) {
		case 'q':return 0;
		case '2':MessageBoxA(GetConsoleWindow(), MB_OK|MB_ICONASTERISK); continue;
		case '1':Run(init());
			MessageBoxA(GetConsoleWindow(),  "SHIT", MB_OK | MB_ICONERROR);
		}
	}
}

小鱼快来啊的个人空间-小鱼快来啊个人主页-哔哩哔哩视频哔哩哔哩小鱼快来啊的个人空间,提供小鱼快来啊分享的视频、音频、文章、动态、收藏等内容,关注小鱼快来啊账号,第一时间了解UP注动态。编程学习群:725022484 每天分享一个编程小游戏~C/C++游戏源码素材及各种安装包, 私信不常看!https://space.bilibili.com/1827181878?spm_id_from=333.788.0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值