C語(yǔ)言鏈表實(shí)現(xiàn)貪吃蛇小游戲
本文實(shí)例為大家分享了C語(yǔ)言鏈表實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目名稱(chēng):
貪吃蛇小游戲
運(yùn)行環(huán)境:
Linux
編程語(yǔ)言:
C語(yǔ)言
主要語(yǔ)法:
鏈表,指針,函數(shù)
備注:
游戲中可選不同難度模式,
1.簡(jiǎn)易——Easy——速度慢,可穿墻,可觸碰自己
2.困難——Hard——速度快,不可穿墻,不可觸碰自己
3.自動(dòng)——Auto——外掛模式,自動(dòng)吃食,直到勝利
代碼
貪吃蛇小游戲代碼:
#include <curses.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <stdio.h> #define UP 1 #define DOWN -1 #define LEFT 2 #define RIGHT -2 struct Snake //define the snake { int hang; int lie; struct Snake *next; }; struct Snake food; //define the food struct Snake *head= NULL; struct Snake *tail = NULL; int key; int dir; int set = 49; int nub = 4; void initFood() //init food { int x = rand()%20; int y = (rand()%19)+1; food.hang = x; food.lie = y; } void initNcurse() //init ncurse { initscr(); keypad(stdscr,1); noecho(); } int hasFood(int i,int j) //judge whether shoule show the food { if(food.hang == i && food.lie == j) { return 1; } return 0; } int hasSnakeNode(int i, int j) //judge whether shoule show the snake { struct Snake *p; p = head; while(p != NULL) { if(p->hang == i && p->lie == j) { return 1; } p = p->next; } return 0; } void gamePic() //show the whole picture { int hang; int lie; move(0,0); for(hang=0;hang<20;hang++) //20*20 size map { if(hang == 0) { for(lie=0;lie<20;lie++) { printw("--"); } printw("\n"); } if(hang >= 0 && hang <= 19) { for(lie=0;lie<=20;lie++) { if(lie == 0 || lie == 20) printw("|"); else if(hasSnakeNode(hang,lie)) { printw("[]"); } else if(hasFood(hang,lie)) { printw("##"); } else printw(" "); } printw("\n"); } if(hang == 19) { for(lie=0;lie<20;lie++) { printw("--"); } printw("\n"); } } printw("by HaoQing, Happy Game!\n"); switch(dir) { case UP: printw("Snake UP! \n"); break; case DOWN: printw("Snake DOWN! \n"); break; case LEFT: printw("Snake LEFT! \n"); break; case RIGHT: printw("Snake RIGHT!\n"); break; } if(nub >= 380) { printw("------------------------------------\n"); printw("==============YOU WIN!==============\n"); printw("------------------------------------\n"); } } void addNode() //add the node of snake { struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake)); new->next = NULL; switch(dir) { case UP: new->hang = tail->hang-1; new->lie = tail->lie; break; case DOWN: new->hang = tail->hang+1; new->lie = tail->lie; break; case LEFT: new->hang = tail->hang; new->lie = tail->lie-1; break; case RIGHT: new->hang = tail->hang; new->lie = tail->lie+1; break; } tail->next = new; tail = new; } void initSnake() //init snake { struct Snake *p; dir = RIGHT; while(head != NULL) { p = head; head = head->next; free(p); } initFood(); head = (struct Snake *)malloc(sizeof(struct Snake)); head->hang = 1; head->lie = 1; head->next = NULL; tail = head; addNode(); addNode(); addNode(); } void deleNode() //delete the node of snake { struct Snake *p; p = head; head = head->next; free(p); } int ifSnakeDie() //judge whether the snake is die { struct Snake *p; p = head; if(tail->hang < 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20) { return 1; } while(p->next != NULL) { if(p->hang == tail->hang && p->lie == tail->lie) return 1; p=p->next; } return 0; } void moveSnake() //move the snake { addNode(); if(hasFood(tail->hang,tail->lie)) { initFood(); nub++; //judge whether win } else deleNode(); if(set != 49 && set != 50 && set != 53) { if(ifSnakeDie()) initSnake(); } } void refreshJieMian() //refresh the picture { while(1) { moveSnake(); gamePic(); refresh(); switch(set) { case 49: usleep(200000); break; case 50: usleep(100000); break; case 51: usleep(100000); break; case 52: usleep(80000); break; case 53: usleep(70000); break; default: usleep(150000); } } } void turn(int direction) //prevent reverse { if(set != 49 && set != 50 && set != 53) { if(abs(dir) != abs(direction)) { dir = direction; } } else dir = direction; } void changeDir() //change direction { while(1) { if(set != 53) { key = getch(); switch(key) { case KEY_DOWN: turn(DOWN); //-1 break; case KEY_UP: turn(UP); //1 break; case KEY_LEFT: turn(LEFT); //2 break; case KEY_RIGHT: turn(RIGHT); //-2 break; } } else { if(tail->hang > food.hang) turn(UP); else if(tail->hang < food.hang) turn(DOWN); if(tail->lie > food.lie) turn(LEFT); else if(tail->lie < food.lie) turn(RIGHT); } } } int main() { initNcurse(); printw("======Welcome To Snake Eating!======\n"); printw("------------------------------------\n"); printw("Please select difficulty level:\n"); printw("\n"); printw(" Fool,please enter \"1\"\n"); printw(" Simple,please enter \"2\"\n"); printw(" Hard,please enter \"3\"\n"); printw(" Hell,please enter \"4\"\n"); printw(" Auto,please enter \"5\"\n"); printw("------------------------------------\n"); printw("You want:"); set = getch(); pthread_t t1; pthread_t t2; initSnake(); gamePic(); pthread_create(&t1,NULL,(void *)refreshJieMian,NULL); pthread_create(&t2,NULL,(void *)changeDir,NULL); while(1); endwin(); return 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
從string類(lèi)的實(shí)現(xiàn)看C++類(lèi)的四大函數(shù)(面試常見(jiàn))
C++類(lèi)一般包括構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)四大函數(shù),非常常見(jiàn),本文給大家介紹從string類(lèi)的實(shí)現(xiàn)看C++類(lèi)的四大函數(shù),一起看看吧2016-06-06C++實(shí)踐Time類(lèi)中的運(yùn)算符重載參考方法
今天小編就為大家分享一篇關(guān)于C++實(shí)踐Time類(lèi)中的運(yùn)算符重載參考方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02C++基于EasyX實(shí)現(xiàn)簡(jiǎn)單掃雷游戲
這篇文章主要為大家詳細(xì)介紹了C++基于EasyX實(shí)現(xiàn)簡(jiǎn)單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02從匯編看c++的默認(rèn)析構(gòu)函數(shù)的使用詳解
本篇文章是對(duì)c++中默認(rèn)析構(gòu)函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05C++分步實(shí)現(xiàn)職工管理系統(tǒng)詳解
這篇文章主要為大家詳細(xì)介紹了基于C++實(shí)現(xiàn)職工管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-10-10C++ 虛函數(shù)的詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了C++ 虛函數(shù)的詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06C++實(shí)戰(zhàn)之二進(jìn)制數(shù)據(jù)處理與封裝
在電腦上一切數(shù)據(jù)都是通過(guò)二進(jìn)制(0或1)進(jìn)行存儲(chǔ)的,通過(guò)多位二進(jìn)制數(shù)據(jù)可以進(jìn)而表示整形、浮點(diǎn)型、字符、字符串等各種基礎(chǔ)類(lèi)型數(shù)據(jù)或者一些更復(fù)雜的數(shù)據(jù)格式。本文將為大家詳細(xì)講講二進(jìn)制數(shù)據(jù)處理與封裝,需要的可以參考一下2022-08-08利用C/C++二進(jìn)制讀寫(xiě)png文件的方法示例
最近在做項(xiàng)目的時(shí)候遇到了這個(gè)問(wèn)題,所以想著總結(jié)下,方法自己和有需要的朋友,下面這篇文章主要介紹了利用C/C++二進(jìn)制讀寫(xiě)png文件的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2016-12-12