贪吃蛇c语言代码vc,纯C语言实现贪吃蛇游戏(VC6.0)

今天笔者为大家展示C语言写的贪吃蛇游戏,让大家玩一玩自己写的游戏~ 是纯C语言哦~VC6.0开发 无问题

首先,开始界面:

c039a7693a5d623d3433db74c946b4fa.png

游戏界面如下:

c3633bd1c6f05bd691df671fef545152.png

代码如下:

笔者VC6.0下编写,测试无问题,可复制代码直接到VC6源文件下,后缀为.c文件 可以编译通过运行~

#include

#include

#include

#include

#define U 1

#define D 2

#define L 3

#define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右

typedef struct SNAKE //蛇身的一个节点

{

int x;

int y;

struct SNAKE *next;

}snake;

//全局变量//

int score=0,add=10;//总得分与每次吃食物得分。

int status,sleeptime=200;//每次运行的时间间隔

snake *head, *food;//蛇头指针,食物指针

snake *q;//遍历蛇的时候用到的指针

int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。

//声明全部函数//

void Pos();

void creatMap();

void initsnake();

int biteself();

void createfood();

void cantcrosswall();

void snakemove();

void pause();

void gamecircle();

void welcometogame();

void endgame();

void gamestart();

void Pos(int x,int y)//设置光标位置

{

COORD pos;

HANDLE hOutput;

pos.X=x;

pos.Y=y;

hOutput=GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(hOutput,pos);

}

void creatMap()//创建地图

{

int i;

for(i=0;i<58;i+=2)//打印上下边框

{

Pos(i,0);

printf("■");

Pos(i,26);

printf("■");

}

for(i=1;i<26;i++)//打印左右边框

{

Pos(0,i);

printf("■");

Pos(56,i);

printf("■");

}

}

void initsnake()//初始化蛇身

{

snake *tail;

int i;

tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//

tail->x=24;</

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的C语言贪吃蛇游戏程序代码,希望能够帮到你: ``` #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 30 #define HEIGHT 20 int score = 0; // 记录得分 int speed = 200; // 控制速度 // 定义蛇的结构体 struct Snake { int x; int y; struct Snake *next; }; // 定义食物的结构体 struct Food { int x; int y; }; // 初始化蛇 struct Snake *initSnake() { struct Snake *head = (struct Snake*)malloc(sizeof(struct Snake)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = NULL; return head; } // 初始化食物 struct Food initFood() { struct Food food; food.x = rand() % WIDTH; food.y = rand() % HEIGHT; return food; } // 画出地图 void drawMap(struct Snake *snake, struct Food food) { system("cls"); // 清屏 for (int i = 0; i < WIDTH + 2; i++) { printf("#"); } printf("\n"); for (int i = 0; i < HEIGHT; i++) { printf("#"); for (int j = 0; j < WIDTH; j++) { if (i == food.y && j == food.x) { printf("F"); } else if (i == snake->y && j == snake->x) { printf("S"); } else { struct Snake *p = snake->next; int flag = 0; while (p != NULL) { if (i == p->y && j == p->x) { printf("s"); flag = 1; break; } p = p->next; } if (!flag) { printf(" "); } } } printf("#\n"); } for (int i = 0; i < WIDTH + 2; i++) { printf("#"); } printf("\n"); printf("Score: %d\n", score); } // 蛇移动 void move(struct Snake *snake, int direction) { int x = snake->x; int y = snake->y; switch (direction) { case 1: // 上 y--; break; case 2: // 下 y++; break; case 3: // 左 x--; break; case 4: // 右 x++; break; } struct Snake *p = snake; while (p->next != NULL) { p = p->next; } free(p); struct Snake *newHead = (struct Snake*)malloc(sizeof(struct Snake)); newHead->x = x; newHead->y = y; newHead->next = snake; snake = newHead; } // 判断是否吃到食物 int eatFood(struct Snake *snake, struct Food food) { if (snake->x == food.x && snake->y == food.y) { struct Snake *newHead = (struct Snake*)malloc(sizeof(struct Snake)); newHead->x = food.x; newHead->y = food.y; newHead->next = snake; snake = newHead; score++; return 1; } return 0; } // 判断是否撞墙或者撞到自己 int isOver(struct Snake *snake) { if (snake->x < 0 || snake->x >= WIDTH || snake->y < 0 || snake->y >= HEIGHT) { return 1; } struct Snake *p = snake->next; while (p != NULL) { if (p->x == snake->x && p->y == snake->y) { return 1; } p = p->next; } return 0; } int main() { struct Snake *snake = initSnake(); struct Food food = initFood(); int direction = 4; // 初始方向向右 while (1) { drawMap(snake, food); if (eatFood(snake, food)) { food = initFood(); } if (isOver(snake)) { printf("Game Over!\n"); break; } if (_kbhit()) { // 判断是否有键盘输入 char ch = _getch(); switch (ch) { case 'w': if (direction != 2) { direction = 1; } break; case 's': if (direction != 1) { direction = 2; } break; case 'a': if (direction != 4) { direction = 3; } break; case 'd': if (direction != 3) { direction = 4; } break; } } move(snake, direction); Sleep(speed); // 控制速度 } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值