使用C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
前言
控制臺(tái)的歡樂(lè)就是這么簡(jiǎn)單;
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、貪吃蛇實(shí)現(xiàn)的結(jié)構(gòu)和方式
1.用枚舉定義蛇的移動(dòng)方向
enum Dir { ?? ?UP, ?? ?DOWN, ?? ?LEFT, ?? ?RIGHT,//枚舉不能用分號(hào); }; //創(chuàng)建結(jié)構(gòu)體,對(duì)蛇的參數(shù)進(jìn)行設(shè)置; struct Snake { ?? ?int size;//蛇的節(jié)數(shù); ?? ?int dir;//蛇的方向; ?? ?int speed;//蛇的移動(dòng)速度; ?? ?//用數(shù)組來(lái)表示蛇的坐標(biāo); ?? ?POINT coor[SNAKE_SIZE];//蛇的最大節(jié)數(shù); }snake; //食物的結(jié)構(gòu)體; struct Food { ?? ?int x; ?? ?int y; ?? ?int r; ? //食物半徑; ?? ?bool flag;//用來(lái)判斷食物是否被吃; ?? ?DWORD color;//食物顏色; }food;
# 2、使用步驟
## 1.引入庫(kù)
代碼如下(示例):
```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.對(duì)窗口進(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ù)可以用來(lái)描繪無(wú)邊框填充函數(shù); ?? ?} ?? ?//繪制食物; ?? ?if (food.flag) ?? ?{ ?? ??? ?solidcircle(food.x, food.y, food.r);//solidcircle代表畫(huà)圓; ?? ?} ?? ?EndBatchDraw(); }
3.對(duì)蛇進(jìn)行初始化;
void GameInit() { ?? ?//播放背景音樂(lè); ?? ?mciSendString("open./mp3.music alias BGM", 0, 0, 0); ?? ?mciSendString("play BGM repeat", 0, 0, 0); ?? ?initgraph(640, 480 /*SHOWCONSOLE*/); ?? ?//設(shè)計(jì)隨機(jī)數(shù)種子; ?? ?srand(GetTickCount());//GetTickCount()獲取開(kāi)機(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)生一個(gè)整數(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>//不是庫(kù)函數(shù); #include<stdlib.h>//隨機(jī)數(shù)的產(chǎn)生; #include<mmsystem.h>//導(dǎo)入背景音樂(lè); #pragma comment(lib,"winmm.lib")//導(dǎo)入背景音樂(lè); //結(jié)構(gòu)體對(duì)snake初始化. #define SNAKE_SIZE 500 /*typedef struct tagpoint { ?? ?LONG x; ?? ?LONG y; }POINT;*/ //用枚舉表示蛇的方向; enum Dir { ?? ?UP, ?? ?DOWN, ?? ?LEFT, ?? ?RIGHT,//枚舉不能用分號(hào); }; //創(chuàng)建結(jié)構(gòu)體,對(duì)蛇的參數(shù)進(jìn)行設(shè)置; struct Snake { ?? ?int size;//蛇的節(jié)數(shù); ?? ?int dir;//蛇的方向; ?? ?int speed;//蛇的移動(dòng)速度; ?? ?//用數(shù)組來(lái)表示蛇的坐標(biāo); ?? ?POINT coor[SNAKE_SIZE];//蛇的最大節(jié)數(shù); }snake; //食物的結(jié)構(gòu)體; struct Food { ?? ?int x; ?? ?int y; ?? ?int r; ? //食物半徑; ?? ?bool flag;//用來(lái)判斷食物是否被吃; ?? ?DWORD color;//食物顏色; }food; //數(shù)據(jù)的初始化; /*void GameInit() { ?? ?//初始化graph圖形窗口,SHOWCONSOLE控制臺(tái); ?? ?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() { ?? ?//播放背景音樂(lè); ?? ?mciSendString("open./mp3.music alias BGM", 0, 0, 0); ?? ?mciSendString("play BGM repeat", 0, 0, 0); ?? ?initgraph(640, 480 /*SHOWCONSOLE*/); ?? ?//設(shè)計(jì)隨機(jī)數(shù)種子; ?? ?srand(GetTickCount());//GetTickCount()獲取開(kāi)機(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)生一個(gè)整數(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ù)可以用來(lái)描繪無(wú)邊框填充函數(shù); ?? ?} ?? ?//繪制食物; ?? ?if (food.flag) ?? ?{ ?? ??? ?solidcircle(food.x, food.y, food.r);//solidcircle代表畫(huà)圓; ?? ?} ?? ?EndBatchDraw(); } //蛇的移動(dòng); //坐標(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; ?? ?} } //通過(guò)按鍵改變蛇的方向; void KeyControl() { ?? ?//判斷有沒(méi)有按鍵; ?? ?if (_kbhit())//檢測(cè)有沒(méi)有按鍵,如果有就返回真,否則返回假; ?? ?{ ?? ??? ?//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);//時(shí)間延遲; ?? ?} ?? ?return 0; }
利用學(xué)會(huì)的知識(shí)做點(diǎn)小游戲
提示:這里對(duì)文章進(jìn)行總結(jié):
例如:以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了pandas的使用,而pandas提供了大量能使我們快速便捷地處理數(shù)據(jù)的函數(shù)和方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++?實(shí)現(xiàn)單鏈表創(chuàng)建、插入和刪除
這篇文章主要介紹了C++?實(shí)現(xiàn)單鏈表創(chuàng)建、插入和刪除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07關(guān)于vs strcpy_s()和strcat_s()用法探究
這篇文章主要介紹了關(guān)于vs strcpy_s()strcat_s()用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05C語(yǔ)言中常用的幾個(gè)頭文件及庫(kù)函數(shù)
這篇文章主要介紹了C語(yǔ)言中常用的幾個(gè)頭文件及庫(kù)函數(shù)的相關(guān)資料,需要的朋友可以參考下2017-09-09C語(yǔ)言實(shí)現(xiàn)運(yùn)籌學(xué)中的馬氏決策算法實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)運(yùn)籌學(xué)中的馬氏決策算法,簡(jiǎn)單介紹了馬氏決策的概念,并結(jié)合實(shí)例形式分析了C語(yǔ)言實(shí)現(xiàn)馬氏決策算法的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10C++中的vector容器對(duì)象學(xué)習(xí)筆記
這篇文章主要介紹了C++中的vector容器對(duì)象學(xué)習(xí)筆記,其中文章最后標(biāo)紅的resize與reserve方法的差別特別需要注意,需要的朋友可以參考下2016-05-05C++數(shù)據(jù)結(jié)構(gòu)二叉搜索樹(shù)的實(shí)現(xiàn)應(yīng)用與分析
從這篇博客開(kāi)始,我就要和大家介紹有關(guān)二叉搜索樹(shù)的知識(shí),它還衍生出了兩棵樹(shù)——AVL樹(shù)和紅黑樹(shù),在后面兩篇博客我都會(huì)介紹。今天先從二叉搜索樹(shù)開(kāi)始引入2022-02-02C++關(guān)鍵字volatile學(xué)習(xí)筆記
這篇文章主要為大家介紹了C++關(guān)鍵字volatile學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10