C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲的示例代碼
一個(gè)簡(jiǎn)單的貪吃蛇游戲本來(lái)代碼就不多,在保證可讀性的情況下,很容易就控制在100以內(nèi)了。
運(yùn)行效果

代碼
#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語(yǔ)言貪吃蛇 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語(yǔ)言貪吃蛇\(yùn)n\n 1. 開(kāi)始游戲\n 2. 關(guān)于\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);
}
}
}到此這篇關(guān)于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲的示例代碼的文章就介紹到這了,更多相關(guān)C語(yǔ)言貪吃蛇游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++函數(shù)參數(shù)聲明解析int?fun()?與?int?fun(void)?的區(qū)別講解
C++中int fun()和int fun(void)的區(qū)別在于函數(shù)參數(shù)的聲明方式,前者默認(rèn)允許任意參數(shù),而后者表示沒(méi)有參數(shù),通過(guò)清晰的實(shí)例源代碼,詳細(xì)解釋了它們?cè)诤瘮?shù)聲明和調(diào)用中的不同之處,這篇文章介紹了C/C++函數(shù)參數(shù)聲明int?fun()與int?fun(void)的差異,需要的朋友可以參考下2024-01-01
C++ 中的虛函數(shù)表及虛函數(shù)執(zhí)行原理詳解
這篇文章主要介紹了C++ 中的虛函數(shù)表及虛函數(shù)執(zhí)行原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Microsoft Visual C++ 6.0開(kāi)發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了Microsoft Visual C++ 6.0開(kāi)發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Mac OS X 10.8 中編譯APUE(Unix環(huán)境高級(jí)編程)的源代碼過(guò)程
這篇文章主要介紹了Mac OS X 10.8 中編譯APUE(Unix環(huán)境高級(jí)編程)的源代碼過(guò)程,對(duì)于用MAC學(xué)習(xí)Unix環(huán)境高級(jí)編程的同學(xué)會(huì)有些作用,需要的朋友可以參考下2014-09-09
C語(yǔ)言SQLite3事務(wù)和鎖的操作實(shí)例
這篇文章主要介紹了C語(yǔ)言SQLite3事務(wù)和鎖的操作,結(jié)合完整實(shí)例形式分析了C語(yǔ)言針對(duì)SQLite3數(shù)據(jù)庫(kù)的事務(wù)與鎖相關(guān)操作技巧,需要的朋友可以參考下2017-07-07

