使用C語言實現(xiàn)貪吃蛇小游戲
本文實例為大家分享了C語言實現(xiàn)貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
前言
控制臺的歡樂就是這么簡單;
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、貪吃蛇實現(xiàn)的結(jié)構(gòu)和方式
1.用枚舉定義蛇的移動方向
enum Dir { ?? ?UP, ?? ?DOWN, ?? ?LEFT, ?? ?RIGHT,//枚舉不能用分號; }; //創(chuàng)建結(jié)構(gòu)體,對蛇的參數(shù)進(jìn)行設(shè)置; struct Snake { ?? ?int size;//蛇的節(jié)數(shù); ?? ?int dir;//蛇的方向; ?? ?int speed;//蛇的移動速度; ?? ?//用數(shù)組來表示蛇的坐標(biāo); ?? ?POINT coor[SNAKE_SIZE];//蛇的最大節(jié)數(shù); }snake; //食物的結(jié)構(gòu)體; struct Food { ?? ?int x; ?? ?int y; ?? ?int r; ? //食物半徑; ?? ?bool flag;//用來判斷食物是否被吃; ?? ?DWORD color;//食物顏色; }food;
# 2、使用步驟
## 1.引入庫
代碼如下(示例):
```c import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ?ssl ssl._create_default_https_context = ssl._create_unverified_context
2.對窗口進(jìn)行設(shè)置;
代碼如下(示例):
void GameDraw() { ?? ?//雙緩沖繪圖; ?? ?BeginBatchDraw(); ?? ?//設(shè)置背景顏色; ?? ?setbkcolor(RGB(28, 115, 119)); ?? ?cleardevice(); ?? ?//繪制蛇; ?? ?setfillcolor(GREEN);//顏色的改變; ?? ?for (int i = 0; i < snake.size; i++) ?? ?{ ?? ??? ?solidcircle(snake.coor[i].x, snake.coor[i].y, 5);//solidcircle函數(shù)可以用來描繪無邊框填充函數(shù); ?? ?} ?? ?//繪制食物; ?? ?if (food.flag) ?? ?{ ?? ??? ?solidcircle(food.x, food.y, food.r);//solidcircle代表畫圓; ?? ?} ?? ?EndBatchDraw(); }
3.對蛇進(jìn)行初始化;
void GameInit() { ?? ?//播放背景音樂; ?? ?mciSendString("open./mp3.music alias BGM", 0, 0, 0); ?? ?mciSendString("play BGM repeat", 0, 0, 0); ?? ?initgraph(640, 480 /*SHOWCONSOLE*/); ?? ?//設(shè)計隨機(jī)數(shù)種子; ?? ?srand(GetTickCount());//GetTickCount()獲取開機(jī)到現(xiàn)在的毫秒數(shù); ?? ?snake.size = 3; ?? ?snake.speed = 10; ?? ?snake.dir = RIGHT; ?? ?for (int i = 0; i <= snake.size - 1; i++) ?? ?{ ?? ??? ?snake.coor[i].x = 10 * (2 - i) + 20;//向右偏移; ?? ??? ?snake.coor[i].y = 10;//確保蛇在同一水品線; ?? ?} ?? ?//食物的初始化;隨機(jī)產(chǎn)生一個整數(shù);設(shè)置隨機(jī)種子,頭文件是stdlib.h; ?? ?food.x = rand() % 640; ?? ?food.y = rand() % 480; ?? ?food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//顏色值為0到256; ?? ?food.r = rand() % 10 + 5; ?? ?food.flag = true; }
二、源代碼
#include<stdio.h> #include<conio.h> #include<graphics.h>//不是庫函數(shù); #include<stdlib.h>//隨機(jī)數(shù)的產(chǎn)生; #include<mmsystem.h>//導(dǎo)入背景音樂; #pragma comment(lib,"winmm.lib")//導(dǎo)入背景音樂; //結(jié)構(gòu)體對snake初始化. #define SNAKE_SIZE 500 /*typedef struct tagpoint { ?? ?LONG x; ?? ?LONG y; }POINT;*/ //用枚舉表示蛇的方向; enum Dir { ?? ?UP, ?? ?DOWN, ?? ?LEFT, ?? ?RIGHT,//枚舉不能用分號; }; //創(chuàng)建結(jié)構(gòu)體,對蛇的參數(shù)進(jìn)行設(shè)置; struct Snake { ?? ?int size;//蛇的節(jié)數(shù); ?? ?int dir;//蛇的方向; ?? ?int speed;//蛇的移動速度; ?? ?//用數(shù)組來表示蛇的坐標(biāo); ?? ?POINT coor[SNAKE_SIZE];//蛇的最大節(jié)數(shù); }snake; //食物的結(jié)構(gòu)體; struct Food { ?? ?int x; ?? ?int y; ?? ?int r; ? //食物半徑; ?? ?bool flag;//用來判斷食物是否被吃; ?? ?DWORD color;//食物顏色; }food; //數(shù)據(jù)的初始化; /*void GameInit() { ?? ?//初始化graph圖形窗口,SHOWCONSOLE控制臺; ?? ?initgraph(640, 480, SHOWCONSOLE); ?? ?snake.size = 0; ?? ?snake.speed = 10; ?? ?snake.dir; ?? ?snake.coor[0].x =10; ?? ?snake.coor[0].y = 10; }*/ //數(shù)據(jù)的初始化; void GameInit() { ?? ?//播放背景音樂; ?? ?mciSendString("open./mp3.music alias BGM", 0, 0, 0); ?? ?mciSendString("play BGM repeat", 0, 0, 0); ?? ?initgraph(640, 480 /*SHOWCONSOLE*/); ?? ?//設(shè)計隨機(jī)數(shù)種子; ?? ?srand(GetTickCount());//GetTickCount()獲取開機(jī)到現(xiàn)在的毫秒數(shù); ?? ?snake.size = 3; ?? ?snake.speed = 10; ?? ?snake.dir = RIGHT; ?? ?for (int i = 0; i <= snake.size - 1; i++) ?? ?{ ?? ??? ?snake.coor[i].x = 10 * (2 - i) + 20;//向右偏移; ?? ??? ?snake.coor[i].y = 10;//確保蛇在同一水品線; ?? ?} ?? ?//食物的初始化;隨機(jī)產(chǎn)生一個整數(shù);設(shè)置隨機(jī)種子,頭文件是stdlib.h; ?? ?food.x = rand() % 640; ?? ?food.y = rand() % 480; ?? ?food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//顏色值為0到256; ?? ?food.r = rand() % 10 + 5; ?? ?food.flag = true; } //繪制; void GameDraw() { ?? ?//雙緩沖繪圖; ?? ?BeginBatchDraw(); ?? ?//設(shè)置背景顏色; ?? ?setbkcolor(RGB(28, 115, 119)); ?? ?cleardevice(); ?? ?//繪制蛇; ?? ?setfillcolor(GREEN);//顏色的改變; ?? ?for (int i = 0; i < snake.size; i++) ?? ?{ ?? ??? ?solidcircle(snake.coor[i].x, snake.coor[i].y, 5);//solidcircle函數(shù)可以用來描繪無邊框填充函數(shù); ?? ?} ?? ?//繪制食物; ?? ?if (food.flag) ?? ?{ ?? ??? ?solidcircle(food.x, food.y, food.r);//solidcircle代表畫圓; ?? ?} ?? ?EndBatchDraw(); } //蛇的移動; //坐標(biāo)的改變; void snakemove() { ?? ?//讓頭部后面的跟著頭走; ?? ?for (int i = snake.size - 1; i > 0; i--) ?? ?{ ?? ??? ?snake.coor[i] = snake.coor[i - 1]; ?? ?} ?? ?switch (snake.dir) ?? ?{ ?? ?case RIGHT: ?? ??? ?snake.coor[0].x += snake.speed; ?? ??? ?if (snake.coor[0].x - 10 >= 640) ?? ??? ?{ ?? ??? ??? ?snake.coor[0].x = 0; ?? ??? ?} ?? ??? ?break; ?? ?case LEFT: ?? ??? ?snake.coor[0].x -= snake.speed; ?? ??? ?if (snake.coor[0].x + 10 <= 0) ?? ??? ?{ ?? ??? ??? ?snake.coor[0].x = 640; ?? ??? ?} ?? ??? ?break; ?? ?case UP: ?? ??? ?snake.coor[0].y -= snake.speed; ?? ??? ?if (snake.coor[0].y + 10 <= 0) ?? ??? ?{ ?? ??? ??? ?snake.coor[0].y = 480; ?? ??? ?} ?? ??? ?break; ?? ?case DOWN: ?? ??? ?snake.coor[0].y += snake.speed; ?? ??? ?if (snake.coor[0].y - 10 >= 480) ?? ??? ?{ ?? ??? ??? ?snake.coor[0].y = 0; ?? ??? ?} ?? ??? ?break; ?? ?default: ?? ??? ?break; ?? ?} } //通過按鍵改變蛇的方向; void KeyControl() { ?? ?//判斷有沒有按鍵; ?? ?if (_kbhit())//檢測有沒有按鍵,如果有就返回真,否則返回假; ?? ?{ ?? ??? ?//72 80 75 77上下左右鍵值; ?? ??? ?switch (_getch()) ?? ??? ?{ ?? ??? ?case 'w': ?? ??? ?case 'W': ?? ??? ?case 72: ?? ??? ??? ?if (snake.dir != DOWN) ?? ??? ??? ?{ ?? ??? ??? ??? ?snake.dir = UP; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case 's': ?? ??? ?case 'S': ?? ??? ?case 80: ?? ??? ??? ?if (snake.dir != UP) ?? ??? ??? ?{ ?? ??? ??? ??? ?snake.dir = DOWN; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case 'a': ?? ??? ?case 'A': ?? ??? ?case 75: ?? ??? ??? ?if (snake.dir != RIGHT) ?? ??? ??? ?{ ?? ??? ??? ??? ?snake.dir = LEFT; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case 'd': ?? ??? ?case 'D': ?? ??? ?case 77: ?? ??? ??? ?if (snake.dir != LEFT) ?? ??? ??? ?{ ?? ??? ??? ??? ?snake.dir = RIGHT; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case ' ': ?? ??? ??? ?while (1) ?? ??? ??? ?{ ?? ??? ??? ??? ?if (_getch() == ' '); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?} ?? ?} } //吃食物; void EatFood() { ?? ?if (food.flag && snake.coor[0].x >= food.x - food.r && snake.coor[0].x <= food.x + food.r && snake.coor[0].y <= food.y + food.r && snake.coor[0].y >= food.y - food.r) ?? ?{ ?? ??? ?snake.size++; ?? ??? ?food.flag == false; ?? ?} ?? ?if (!food.flag) ?? ?{ ?? ??? ?food.x = rand() % 640; ?? ??? ?food.y = rand() % 480; ?? ??? ?food.color = RGB(rand() % 256, rand() % 256, rand() % 256); ?? ??? ?food.r = rand() % 10 + 5; ?? ??? ?food.flag = true; ?? ?} } int main() { ?? ?//init初始化graph ; ?? ?initgraph(640, 480); ?? ?//設(shè)置背景顏色; ?? ?setbkcolor(RGB(28, 115, 119));//設(shè)置背景繪圖顏色; ?? ?cleardevice(); ?? ?GameInit(); ?? ?while (1) ?? ?{ ?? ??? ?snakemove(); ?? ??? ?GameDraw(); ?? ??? ?KeyControl(); ?? ??? ?EatFood(); ?? ??? ?Sleep(80);//時間延遲; ?? ?} ?? ?return 0; }
利用學(xué)會的知識做點小游戲
提示:這里對文章進(jìn)行總結(jié):
例如:以上就是今天要講的內(nèi)容,本文僅僅簡單介紹了pandas的使用,而pandas提供了大量能使我們快速便捷地處理數(shù)據(jù)的函數(shù)和方法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++?實現(xiàn)單鏈表創(chuàng)建、插入和刪除
這篇文章主要介紹了C++?實現(xiàn)單鏈表創(chuàng)建、插入和刪除方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07關(guān)于vs strcpy_s()和strcat_s()用法探究
這篇文章主要介紹了關(guān)于vs strcpy_s()strcat_s()用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)字符串分割的實例
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)字符串分割的實例的相關(guān)資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10C++數(shù)據(jù)結(jié)構(gòu)二叉搜索樹的實現(xiàn)應(yīng)用與分析
從這篇博客開始,我就要和大家介紹有關(guān)二叉搜索樹的知識,它還衍生出了兩棵樹——AVL樹和紅黑樹,在后面兩篇博客我都會介紹。今天先從二叉搜索樹開始引入2022-02-02C++關(guān)鍵字volatile學(xué)習(xí)筆記
這篇文章主要為大家介紹了C++關(guān)鍵字volatile學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10