這篇文章主要介紹“C語言怎么實(shí)現(xiàn)貪吃蛇小游戲程序”,在日常操作中,相信很多人在C語言怎么實(shí)現(xiàn)貪吃蛇小游戲程序問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C語言怎么實(shí)現(xiàn)貪吃蛇小游戲程序”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
10多年的臨洮網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整臨洮建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“臨洮網(wǎng)站設(shè)計(jì)”,“臨洮網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
代碼如下:
#include#include #include #include #include #define WALL_LENGTH 22 #define LEFT 0x4b #define RIGHT 0x4d #define DOWN 0x50 #define UP 0x48 struct Snakes{ int x; int y; struct Snakes *prev; struct Snakes *next;}; struct Food{ int x; int y;}; struct Snakes *header; struct Snakes *tailer; struct Food *food; int wall[WALL_LENGTH][WALL_LENGTH];int direction = RIGHT; /**/void init();void draw();void move();void doMove(int x1, int y1);void eat(); void keydown(); void foods();int isOver();int isDrawSnake(int x, int y);int isDrawFood(int x, int y); int main(){ init(); while(1){ if(isOver()){ break; } move(); eat(); draw(); _sleep( 100 ); keydown(); } printf("GAME OVER!"); system("pause");} void init(){ int y, x; for(y=0; y < WALL_LENGTH; y++){ for(x=0; x < WALL_LENGTH; x++){ if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1){ wall[y][x] = 1; } } } header=(struct Snakes *)malloc(sizeof(struct Snakes)); header->x=10; header->y=10; header->prev=NULL; tailer=(struct Snakes *)malloc(sizeof(struct Snakes)); tailer->x=9; tailer->y=10; tailer->next=NULL; tailer->prev=header; header->next=tailer; foods(); } void draw(){ int y, x; system("cls"); for(y=0; y < WALL_LENGTH; y++){ for(x=0; x < WALL_LENGTH; x++){ if(wall[y][x] == 1){ printf("[]"); }else if(isDrawSnake(x, y)){ printf("[]"); }else if(isDrawFood(x, y)){ printf("()"); }else{ printf(" "); } } printf("\n"); } } void move(){ switch(direction){ case LEFT : doMove(-1, 0); break; case RIGHT : doMove(1, 0); break; case UP : doMove(0, -1); break; case DOWN : doMove(0, 1); break; } } void doMove(int x1, int y1){ struct Snakes *temp_tailer = tailer->prev; tailer->x=header->x + x1; tailer->y=header->y + y1; tailer->next=header; tailer->prev->next = NULL; tailer->prev = NULL; header->prev=tailer; header = tailer; tailer = temp_tailer; } void eat(){ if(header->x == food->x && header->y == food->y){ int x1=0, y1 =0; struct Snakes *temp_tailer = tailer; tailer=(struct Snakes *)malloc(sizeof(struct Snakes)); switch(direction){ case LEFT : x1 = -1; y1 = 0; break; case RIGHT : x1 = 1; y1 = 0; break; case UP : x1 = 0; y1 = -1; break; case DOWN : x1 = 0; y1 = 1; break; } tailer->x=temp_tailer->x + x1; tailer->y=temp_tailer->y + y1; tailer->next=NULL; tailer->prev=temp_tailer; temp_tailer->next = tailer; foods(); } } void foods(){ int y,x; struct Snakes *temp = header; _sleep(20); srand((unsigned)time(NULL)); y=rand()%WALL_LENGTH; x=rand()%WALL_LENGTH; if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1 ){ return foods(); } do{ if(temp->x == x && temp->y == y){ return foods(); } temp = temp->next; }while(temp != NULL); if(food == NULL){ food=(struct Food *)malloc(sizeof(struct Food)); } food->x = x; food->y = y; } void keydown(){ char keycode; if(_kbhit()&&(keycode =_getch())) { switch(keycode) { case LEFT: if(RIGHT!=direction) { direction=LEFT; // move(); // draw(); } break; case RIGHT: if(LEFT!=direction) { direction=RIGHT; // move(); // draw(); } break; case UP: if(DOWN!=direction) { direction=UP; // move(); // draw(); } break; case DOWN: if(UP!=direction){ direction=DOWN; // move(); // draw(); } break; } } } int isDrawSnake(int x, int y){ struct Snakes *temp = header; do{ if(temp->x == x && temp->y == y){ return 1; } temp = temp->next; }while(temp != NULL); return 0;} int isDrawFood(int x, int y){ if(food->x == x && food->y == y){ return 1; } return 0;} int isOver(){ int x1=0, y1 =0; switch(direction){ case LEFT : x1 = -1; y1 = 0; break; case RIGHT : x1 = 1; y1 = 0; break; case UP : x1 = 0; y1 = -1; break; case DOWN : x1 = 0; y1 = 1; break; } if(header->x + x1 <= 0 || header->x + x1 >= WALL_LENGTH - 1 || header->y + y1 <= 0 || header->y + y1 >= WALL_LENGTH - 1){ return 1; } return 0; }
到此,關(guān)于“C語言怎么實(shí)現(xiàn)貪吃蛇小游戲程序”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!