基于C語言撰寫的貪吃蛇
開發環境:VS2019
這是一個基于C語言鏈表開發的貪吃蛇游戲
其實貪吃蛇游戲要解決的主要問題就是
1、這個游戲的基本組成元素和架構
2、如何初始化貪吃蛇并正常行走
3、如何判斷事件發生
代碼中運用到了鍵盤虛擬鍵判斷、終端視窗大小的改變、游標的定位以及輸出字體的顏色
轉載本文章,請標注原創!
若有不懂的地方,可以在評論區給我留言
代碼如下:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#define R 'R'
#define L 'L'
#define U 'U'
#define D 'D'
typedef struct Node
{
int x;
int y;
struct Node* next;
}snake;
char FORWARD_DIRECTION; //定義當前前進的方向
snake* head, * p, * nexthead;//蛇頭指標、創建貪吃蛇的間接指標、下一步生成的頭指標、尾指標
snake* food;//隨機生成的食物
int grade = 0; //當前得分
int addGrade = 5; //加的分數隨速度而增加
int sleepTime = 250; //貪吃蛇停頓的時間,可通過修改停頓時間改變貪吃蛇前進的速度
void hideCursor(); //定義隱藏游標函式
void gotoxy(int x, int y); //移動游標函式
void color(int c); //設定接下來輸出字體的顏色
void createMap(); //創建地圖函式
void initSnake(); //初始化貪吃蛇函式
void speedUp(); //加速函式
void speedDown(); //減速函式
void createFood(); //隨機生成食物函式
void collideWall(); //檢測撞墻函式
void collideSelf(); //檢測是否撞到自己
void moveSnake(); //貪吃蛇移動函式
void gradeBoard(); //顯示得分板函式
void endGame(); //結束游戲函式
void keyboardControl(); //檢測鍵盤按鍵函式
void welcometoGame(); //初始化函式
void hideCursor() //定義隱藏游標函式
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
void gotoxy(int x, int y)
{ //移動游標到相應位置
COORD c = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void color(int c)
{ //設定接下來輸出字符的顏色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void createMap()
{ //創建地圖
system("mode con cols=150 lines=30");
system("cls");
color(3 | FOREGROUND_INTENSITY);
for (int i = 1; i <= 40; i++)
{ //上邊框
printf("■");
}
for (int i = 1; i <= 28; i++)
{ //左右邊框
gotoxy(0, i);
printf("■");
gotoxy(78, i);
printf("■");
}
gotoxy(0, 29);
for (int i = 1; i <= 40; i++)
{ //上邊框
printf("■");
}
}
void initSnake()
{
snake* tail;
color(15);
head = (snake*)malloc(sizeof(snake));
head->x = 36;
head->y = 12;
p = head;
for (int i = 1; i <= 4; i++) {
tail = (snake*)malloc(sizeof(snake));
tail->x = head->x + 2 * i;
tail->y = head->y;
p->next = tail;
p = tail;
}
p->next = NULL;
p = head; //記錄下頭指標的位置
gotoxy(head->x, head->y);
while (head != NULL)
{
printf("■");
head = head->next;
}
gotoxy(149, 29);
}
void speedUp() //通過減小sleepTime加快速度,但sleepTime不能小于100
{
if (sleepTime > 50)
{
sleepTime = sleepTime - 50; addGrade++;
}
}
void speedDown() //通過增大sleepTime減慢速度,但sleepTime不能大于500
{
if (sleepTime < 450)
{
sleepTime = sleepTime + 50; addGrade--;
}
}
void createFood()
{ //隨機生成食物
byte i;
head = p;
food = (snake*)malloc(sizeof(snake));
food->next = NULL;
while (1)
{
i = 0; //每次回圈時重置i為0,若此條陳述句放在外部while陳述句外部,當隨機生成的食物與蛇身重疊時,i被置1
//此時i就一直為1,將陷入死回圈
food->x = rand() % 75 + 2; //注意在范圍內生成的數要為偶數,要與蛇頭對齊
food->y = rand() % 25 + 2;
while (head != NULL)
{
if (food->x == head->x && food->y == head->y)
i = 1;
head = head->next;
} //重置head的值,當隨機生成的食物與蛇身重疊時,又從外部while的第一條陳述句開始執行
head = p; //,此時若沒有重置head的值,內嵌的while將直接跳過,沒有二次判斷
if (food->x % 2 == 0 && food->y % 2 == 0 && i == 0)
break;
}
gotoxy(food->x, food->y);
color(12 | FOREGROUND_INTENSITY);
printf("■");
}
void collideWall() {
if (head->x == 0)
endGame();
if (head->x == 78)
endGame();
if (head->y == 0)
endGame();
if (head->y == 29)
endGame();
}
void collideSelf() {
head = head->next;
while (head != NULL)
{
if (head->x == p->x && head->y == p->y)
endGame();
head = head->next;
}
head = p;
}
void moveSnake()
{
color(15);
head = p; //初始化頭指標的位置
if (FORWARD_DIRECTION == L)
{
//gotoxy(100, 3);
nexthead = (snake*)malloc(sizeof(snake));
nexthead->x = head->x - 2;
nexthead->y = head->y;
nexthead->next = head;
head = nexthead; //頭指標指向新開創的空間
p = head; //記錄下頭指標的值
collideWall();
collideSelf();
gotoxy(head->x, head->y);
printf("■");
if (head->x == food->x && head->y == food->y) //若吃到了食物,則尾部不清除
{
free(food);
grade = grade + addGrade;
createFood();
}
else { //若沒有吃到食物,則清除尾部,保持原有長度
while (head->next->next != NULL)
{ //此處注意
head = head->next;
}
gotoxy(head->next->x, head->next->y); //通過next釋放尾部是因為直接釋放尾部會導致next指向混亂
printf(" ");
free(head->next);
head->next = NULL;
}
head = p;
}
if (FORWARD_DIRECTION == U)
{
//gotoxy(100, 3);
nexthead = (snake*)malloc(sizeof(snake));
nexthead->x = head->x;
nexthead->y = head->y - 1;
nexthead->next = head;
head = nexthead;
p = head;
gotoxy(head->x, head->y);
printf("■");
collideWall();
collideSelf();
if (head->x == food->x && head->y == food->y)
{
free(food);
grade = grade + addGrade;
createFood();
}
else {
while (head->next->next != NULL)
{ //此處注意
head = head->next;
}
gotoxy(head->next->x, head->next->y); //通過next釋放尾部是因為直接釋放尾部會導致next指向混亂
printf(" ");
free(head->next);
head->next = NULL;
}
head = p;
}
if (FORWARD_DIRECTION == R)
{
//gotoxy(100, 3);
nexthead = (snake*)malloc(sizeof(snake));
nexthead->x = head->x + 2;
nexthead->y = head->y;
nexthead->next = head;
head = nexthead;
p = head;
gotoxy(head->x, head->y);
printf("■");
collideWall();
collideSelf();
if (head->x == food->x && head->y == food->y)
{
free(food);
grade = grade + addGrade;
createFood();
}
else {
while (head->next->next != NULL)
{ //此處注意
head = head->next;
}
gotoxy(head->next->x, head->next->y); //通過next釋放尾部是因為直接釋放尾部會導致next指向混亂
printf(" ");
free(head->next);
head->next = NULL;
}
head = p;
}
if (FORWARD_DIRECTION == D)
{
//gotoxy(100, 3);
nexthead = (snake*)malloc(sizeof(snake));
nexthead->x = head->x;
nexthead->y = head->y + 1;
nexthead->next = head;
head = nexthead;
p = head;
gotoxy(head->x, head->y);
printf("■");
collideWall();
collideSelf();
if (head->x == food->x && head->y == food->y)
{
free(food);
grade = grade + addGrade;
createFood();
}
else {
while (head->next->next != NULL)
{ //此處注意
head = head->next;
}
gotoxy(head->next->x, head->next->y); //通過next釋放尾部是因為直接釋放尾部會導致next指向混亂
printf(" ");
free(head->next);
head->next = NULL;
}
head = p;
}
}
void gradeBoard()
{
gotoxy(100, 12);
color(4);
printf("得分:%d ", grade);
gotoxy(100, 14);
printf("每個食物得分: %d分", addGrade);
gotoxy(100, 16);
printf("碰墻或碰到自己即Game Over.");
gotoxy(100, 18);
printf("↑ ↓ ← →控制蛇的移動");
gotoxy(100, 20);
printf("F1 加速,F2 減速");
gotoxy(100, 22);
printf("空格space:暫停游戲");
gotoxy(100, 24);
printf("ESC :退出游戲");
}
void endGame() {
system("cls");
gotoxy(66, 10);
color(12 | FOREGROUND_INTENSITY);
printf("游戲結束");
gotoxy(64, 12);
printf("您的得分為:%d", grade);
gotoxy(61, 14);
printf("再按一次ESC退出游戲");
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_ESCAPE))
exit(0);
}
}
void keyboardControl()
{
FORWARD_DIRECTION = L;
while (1)
{
gradeBoard();
if (GetAsyncKeyState(VK_UP) && FORWARD_DIRECTION != D)
{
FORWARD_DIRECTION = U;
}
else if (GetAsyncKeyState(VK_DOWN) && FORWARD_DIRECTION != U)
{
FORWARD_DIRECTION = D;
}
else if (GetAsyncKeyState(VK_LEFT) && FORWARD_DIRECTION != R)
{
FORWARD_DIRECTION = L;
}
else if (GetAsyncKeyState(VK_RIGHT) && FORWARD_DIRECTION != L)
{
FORWARD_DIRECTION = R;
}
else if (GetAsyncKeyState(VK_F1)) //按F1加快速度
{
speedUp();
}
else if (GetAsyncKeyState(VK_F2)) //按F2減慢速度
{
speedDown();
}
else if (GetAsyncKeyState(VK_SPACE)) //空格鍵暫停
{
while (1)
{
if (GetAsyncKeyState(VK_ESCAPE)) //在暫停時也能回應ESC鍵退出游戲
{
endGame();
}
gotoxy(100, 8);
color(4);
printf("Pause.");
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
gotoxy(100, 8);
printf("Start.");
break;
}
}
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
endGame();
}
moveSnake();
Sleep(sleepTime); //通過改變sleepTime大小更改貪吃蛇速度
}
}
void welcometoGame()
{
char n;
color(15);
gotoxy(42, 10);
printf("Wecome to snake.");
color(12);
gotoxy(20, 27);
printf("按1開始游戲");
gotoxy(65, 27);
printf("按任意除1按鍵退出游戲");
color(3 | FOREGROUND_INTENSITY);
gotoxy(40, 14);
printf("每個食物得分: %d分", addGrade);
gotoxy(38, 16);
printf("碰墻或咬到自己即Game Over.");
gotoxy(38, 18);
printf("↑ ↓ ← →控制蛇的移動");
gotoxy(41, 20);
printf("F1 加速,F2 減速");
gotoxy(40, 22);
printf("空格space:暫停游戲");
gotoxy(43, 24);
printf("ESC :退出游戲");
gotoxy(43, 28);
printf("please select:");
n = getch();
switch (n)
{
case '1':
{
createMap();
initSnake();
createFood();
keyboardControl();
break;
}
default:exit(0);
}
}
int main()
{
system("mode con cols=100 lines=30");
hideCursor();
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
//printf("Hello, World");
//system("cls");
welcometoGame();
}
運行截圖:


若有疑問或者更好的創意,可以在評論區留言!
若有轉載參考本作者的代碼,還請通知本作者并標注原創!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394424.html
標籤:其他
上一篇:如何使用MySQL的MAKETIME函式構造小于1小時的負時間?
下一篇:自動化測驗框架總結
