使用C語言實現貪吃蛇小遊戲
本文實例為大傢分享瞭C語言實現貪吃蛇小遊戲的具體代碼,供大傢參考,具體內容如下
前言
控制臺的歡樂就是這麼簡單;
提示:以下是本篇文章正文內容,下面案例可供參考
一、貪吃蛇實現的結構和方式
1.用枚舉定義蛇的移動方向
enum Dir { UP, DOWN, LEFT, RIGHT,//枚舉不能用分號; }; //創建結構體,對蛇的參數進行設置; struct Snake { int size;//蛇的節數; int dir;//蛇的方向; int speed;//蛇的移動速度; //用數組來表示蛇的坐標; POINT coor[SNAKE_SIZE];//蛇的最大節數; }snake; //食物的結構體; 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.對窗口進行設置;
代碼如下(示例):
void GameDraw() { //雙緩沖繪圖; BeginBatchDraw(); //設置背景顏色; 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函數可以用來描繪無邊框填充函數; } //繪制食物; if (food.flag) { solidcircle(food.x, food.y, food.r);//solidcircle代表畫圓; } EndBatchDraw(); }
3.對蛇進行初始化;
void GameInit() { //播放背景音樂; mciSendString("open./mp3.music alias BGM", 0, 0, 0); mciSendString("play BGM repeat", 0, 0, 0); initgraph(640, 480 /*SHOWCONSOLE*/); //設計隨機數種子; srand(GetTickCount());//GetTickCount()獲取開機到現在的毫秒數; 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;//確保蛇在同一水品線; } //食物的初始化;隨機產生一個整數;設置隨機種子,頭文件是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>//不是庫函數; #include<stdlib.h>//隨機數的產生; #include<mmsystem.h>//導入背景音樂; #pragma comment(lib,"winmm.lib")//導入背景音樂; //結構體對snake初始化. #define SNAKE_SIZE 500 /*typedef struct tagpoint { LONG x; LONG y; }POINT;*/ //用枚舉表示蛇的方向; enum Dir { UP, DOWN, LEFT, RIGHT,//枚舉不能用分號; }; //創建結構體,對蛇的參數進行設置; struct Snake { int size;//蛇的節數; int dir;//蛇的方向; int speed;//蛇的移動速度; //用數組來表示蛇的坐標; POINT coor[SNAKE_SIZE];//蛇的最大節數; }snake; //食物的結構體; struct Food { int x; int y; int r; //食物半徑; bool flag;//用來判斷食物是否被吃; DWORD color;//食物顏色; }food; //數據的初始化; /*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; }*/ //數據的初始化; void GameInit() { //播放背景音樂; mciSendString("open./mp3.music alias BGM", 0, 0, 0); mciSendString("play BGM repeat", 0, 0, 0); initgraph(640, 480 /*SHOWCONSOLE*/); //設計隨機數種子; srand(GetTickCount());//GetTickCount()獲取開機到現在的毫秒數; 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;//確保蛇在同一水品線; } //食物的初始化;隨機產生一個整數;設置隨機種子,頭文件是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(); //設置背景顏色; 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函數可以用來描繪無邊框填充函數; } //繪制食物; if (food.flag) { solidcircle(food.x, food.y, food.r);//solidcircle代表畫圓; } EndBatchDraw(); } //蛇的移動; //坐標的改變; 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); //設置背景顏色; setbkcolor(RGB(28, 115, 119));//設置背景繪圖顏色; cleardevice(); GameInit(); while (1) { snakemove(); GameDraw(); KeyControl(); EatFood(); Sleep(80);//時間延遲; } return 0; }
利用學會的知識做點小遊戲
提示:這裡對文章進行總結:
例如:以上就是今天要講的內容,本文僅僅簡單介紹瞭pandas的使用,而pandas提供瞭大量能使我們快速便捷地處理數據的函數和方法。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。