C語言實現貪吃蛇遊戲(命令列)
阿新 • • 發佈:2020-07-01
這是一個純C語言寫的貪吃蛇遊戲,供大家參考,具體內容如下
#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<time.h> #include<conio.h> #define SNAKE_LENGTH 100//定義蛇的最大長度 #define SCREEN_WIDETH 80 #define SCREEN_HEIGHT 30 //定義每一節蛇的座標 struct coor{ int x; int y; }; //列舉方向 enum CH { right = VK_RIGHT,left = VK_LEFT,up = VK_UP,down = VK_DOWN }; //定義蛇的屬性 struct snake{ int len;//當前蛇的長度 struct coor coord[SNAKE_LENGTH];//每一節蛇的座標 enum CH CH;//定義蛇的方向 int SPEED; int flag;//定義蛇的狀態 1表示存活 0表示死亡 }snake; //游標移動函式 void gotoxy(int x,int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); } //初始化遊戲介面 void init_sence() { //初始化上下牆 for (int i = 0; i < SCREEN_WIDETH; i += 2) { gotoxy(i,0); printf("■"); gotoxy(i,SCREEN_HEIGHT); printf("■"); } //初始化左右牆 for (int i = 0; i <=SCREEN_HEIGHT; i++) { gotoxy(0,i); printf("■"); gotoxy(SCREEN_WIDETH,i); printf("■"); } //列印提示資訊 gotoxy(SCREEN_WIDETH + 5,2); printf("\t\t貪吃蛇"); gotoxy(SCREEN_WIDETH + 5,6); printf("2018//12//1"); gotoxy(SCREEN_WIDETH + 5,8); printf("作者:小豆芽"); gotoxy(SCREEN_WIDETH + 5,10); printf("F1:加速\tF2:減速"); gotoxy(SCREEN_WIDETH + 5,12); printf("CTRL:繼續\t空格:暫停"); gotoxy(SCREEN_WIDETH + 5,14); printf("ESC:退出遊戲"); gotoxy(SCREEN_WIDETH + 5,28); printf("建議:QQ:2862841130:::"); } struct foodcoord { int x; int y; int flag;//定義食物的狀態 }food; //**這是c程式** #include"snake.h" //蛇的移動 void move_snake(); //畫出蛇 void draw_snake(); //產生食物 void creatfood(); //判斷蛇是否吃到食物 void eatfood(); //判斷蛇是否死掉 void SnakeState(); int main() { //設定視窗大小 system("mode con cols=110 lines=31"); //設定標題 SetConsoleTitleA("貪吃蛇"); //初始化蛇 begin: snake.CH = VK_RIGHT;//初始化方向 snake.len = 5; //初始化長度 snake.SPEED = 300;//初始化蛇的移動速度 snake.coord[1].x = SCREEN_WIDETH / 2;//初始化蛇頭的座標 snake.coord[1].y = SCREEN_HEIGHT / 2; snake.coord[2].x = SCREEN_WIDETH / 2-2;//初始化蛇頭的座標 snake.coord[2].y = SCREEN_HEIGHT / 2; snake.coord[3].x = SCREEN_WIDETH / 2-4;//初始化蛇頭的座標 snake.coord[3].y = SCREEN_HEIGHT / 2; //初始化食物狀態 food.flag = 1;//1表示吃到食物 0表示沒有吃到食物 //初始化食物狀態 snake.flag = 1;//1活 0死 init_sence();//初始化遊戲介面 while (1) { draw_snake();//畫蛇 Sleep(snake.SPEED);//蛇的移動速度 move_snake();//移動蛇 if(food.flag) creatfood();//產生食物 eatfood();//判斷是否吃到食物 SnakeState();//判斷蛇是否死亡 if (!snake.flag)break; } system("cls"); gotoxy(SCREEN_WIDETH/2,SCREEN_HEIGHT/2-4); printf(" GAME OVER!!!"); gotoxy(SCREEN_WIDETH / 2-6,SCREEN_HEIGHT / 2+2); printf("你的得分是:\t\t\t%d ",snake.len-1); gotoxy(SCREEN_WIDETH / 2-6,SCREEN_HEIGHT / 2+4); printf("我不服再來:\t\t\tCTRL "); gotoxy(SCREEN_WIDETH / 2-6,SCREEN_HEIGHT / 2+6); printf("算了垃圾遊戲毀我青春:\t\tESC"); while (1) { if (GetAsyncKeyState(VK_CONTROL)) { system("cls"); goto begin; } else if (GetAsyncKeyState(VK_ESCAPE)) return 0; } } //蛇的移動 void move_snake() { //判斷是否有按鍵操作 if (GetAsyncKeyState(up)) { if(snake.CH!=down)snake.CH = up; } else if (GetAsyncKeyState(down)) { if (snake.CH != up)snake.CH = down; } else if (GetAsyncKeyState(right)) { if (snake.CH != left)snake.CH = right; } else if (GetAsyncKeyState(left)) { if (snake.CH != right)snake.CH = left; } else if (GetAsyncKeyState(VK_F1)) { if(snake.SPEED>=100)snake.SPEED -= 50; } else if (GetAsyncKeyState(VK_F2)) { if (snake.SPEED <= 3000)snake.SPEED += 100; } //根據檢測到的方向改變蛇頭的位置 switch (snake.CH) { case right:snake.coord[1].x += 2; break; case left:snake.coord[1].x -= 2; break; case up:snake.coord[1].y -= 1; break; case down:snake.coord[1].y += 1; break; } } //畫出蛇 void draw_snake() { //畫出蛇頭 gotoxy(snake.coord[1].x,snake.coord[1].y); printf("□"); //畫出蛇身,直接一個for迴圈實現 for (int i = 2; i <= snake.len; i++) { gotoxy(snake.coord[i].x,snake.coord[i].y); printf("□"); } //擦掉尾巴 gotoxy(snake.coord[snake.len].x,snake.coord[snake.len].y); printf(" "); //遍歷每一節蛇 for (int i = snake.len; i >1; i--) { snake.coord[i].x = snake.coord[i - 1].x; snake.coord[i].y = snake.coord[i - 1].y; } gotoxy(0,0); printf("■"); gotoxy(85,25); printf("得分:%d ",snake.len-1); } //產生食物 void creatfood() { //隨機種子生成 srand((unsigned)time(NULL)); if(food.flag) while (1) { food.x = rand() % 80; food.y = rand() % 30; if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30) { int flag = 0; //判斷產生的食物可不可能在蛇的身體上 for (int i = 1; i <= snake.len; i++) { if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y) { flag = 1; break; } } if (flag)continue; //繪製食物 else { gotoxy(food.x,food.y); printf("⊙"); food.flag = 0; break; } } } food.flag = 0; } //判斷蛇是否吃到食物 void eatfood() { //只需要判斷蛇頭是否與食物重合 if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y) { snake.len+=1; food.flag = 1; } } //判斷蛇是否死掉 void SnakeState() { if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29) snake.flag = 0; for (int i = 2; i <= snake.len; i++) { if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y) snake.flag = 0; } }
更多有趣的經典小遊戲實現專題,分享給大家:
C++經典小遊戲彙總
python經典小遊戲彙總
python俄羅斯方塊遊戲集合
JavaScript經典遊戲 玩不停
java經典小遊戲彙總
javascript經典小遊戲彙總
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。