您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“C語言如何實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“C語言如何實(shí)現(xiàn)簡(jiǎ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語言貪吃蛇 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ùn)n\n 1. 開始游戲\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); } } }
到此,相信大家對(duì)“C語言如何實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。