c语言 贪吃蛇 程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言 贪吃蛇 程序相关的知识,希望对你有一定的参考价值。
参考技术A基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define BEG_X2
#define BEG_Y1
#define WID20
#define HEI20
HANDLE hout;
typedef enum UP, DOWN, LEFT, RIGHT DIR;
typedef struct Snake_body
COORD pos;//蛇身的位置
struct Snake_body *next;//下一个蛇身
struct Snake_body *prev;//前一个蛇身
SNAKE, *PSNAKE;
PSNAKE head = NULL;//蛇头
PSNAKE tail = NULL;//蛇尾
//画游戏边框的函数
void DrawBorder()
int i, j;
COORD pos = BEG_X, BEG_Y;
for(i = 0; i < HEI; ++i)
SetConsoleCursorPosition(hout, pos);
for(j = 0; j < WID; ++j)
if(i == 0)//第一行
if(j == 0)
printf("┏");
else if(j == WID - 1)
printf("┓");
else
printf("━");
else if(i == HEI - 1)//最后一行
if(j == 0)
printf("┗");
else if(j == WID - 1)
printf("┛");
else
printf("━");
else if(j == 0 || j == WID - 1)//第一列或最后一列
printf("┃");
else
printf(" ");
++pos.Y;
//添加蛇身的函数
void AddBody(COORD pos)
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew->pos = pos;
if(!head)
head = tail = pnew;
else
pnew->next = head;//新创建蛇身的next指向原先的蛇头
head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身
head = pnew;//把新创建的蛇身作为新的蛇头
SetConsoleCursorPosition(hout, head->pos);
printf("◎");
//蛇身移动的函数
void MoveBody(DIR dir)
PSNAKE ptmp;
COORD pos = head->pos;
switch(dir)
case UP:
if(head->pos.Y > BEG_Y + 1)
--pos.Y;
else
return;
break;
case DOWN:
if(head->pos.Y < BEG_Y + HEI - 2)
++pos.Y;
else
return;
break;
case LEFT:
if(head->pos.X > BEG_X + 2)
pos.X -= 2;
else
return;
break;
case RIGHT:
if(head->pos.X < BEG_X + (WID - 2) * 2)
pos.X += 2;
else
return;
break;
AddBody(pos);//添加了一个新的蛇头
ptmp = tail;//保存当前的蛇尾
tail = tail->prev;
if(tail)
tail->next = NULL;
SetConsoleCursorPosition(hout, ptmp->pos);
printf(" ");
free(ptmp);
int main()
int ctrl;
DIR dir = RIGHT;//初始蛇的方向是向右的
COORD pos = BEG_X + 2, BEG_Y + HEI / 2;
system("color 0E");
system("mode con cols=90 lines=30");
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(" ------------贪吃蛇的移动------------");
DrawBorder();
//自定义几个蛇的身体
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
//控制蛇的移动
while(ctrl = getch())
switch(ctrl)
case 'w':
if(dir == DOWN)
continue;
dir = UP;
break;
case 's':
if(dir == UP)
continue;
dir = DOWN;
break;
case 'a':
if(dir == RIGHT)
continue;
dir = LEFT;
break;
case 'd':
if(dir == LEFT)
continue;
dir = RIGHT;
break;
case 'q':
return 0;
MoveBody(dir);
return 0;
扩展资料:
实现逻辑
1,可以设置光标,就能实现制定位置打印制定符号。
2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。
3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。
4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。
5,食物产生的位置判定,不能越界,也不能与蛇身体重合。
6,蛇的转向判定,一条规则,不允许倒退。
7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)
8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。
9,加速减速,设置刷新休眠时间实现。
参考资料来源:百度百科-C语言
C语言贪吃蛇
基于C语言编写的贪吃蛇
开发环境:VS2019
这是一个基于C语言链表开发的贪吃蛇游戏
其实贪吃蛇游戏要解决的主要问题就是
1、这个游戏的基本组成元素和架构
2、如何初始化贪吃蛇并正常行走
3、如何判断事件发生
代码中运用到了键盘虚拟键判断、终端窗口大小的改变、光标的定位以及输出字体的颜色
转载本文章,请标注原创!
若有不懂的地方,可以在评论区给我留言
代码如下:
#include <stdio.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();
C语言课程设计,贪吃蛇应该怎么做?