本文最后更新于 1434 天前,其中的信息可能已经有所发展或是发生改变。
学习自知乎:https://zhuanlan.zhihu.com/p/211062266
WASD控制移动,绿色为食物,每吃一次食物蛇移动速度加快,红色为减速道具
加了个分数统计
本地双人玩法还在研究(远程多人有生之年会研究XD
下载地址(39.5KB):https://shennoter.top/wp-content/uploads/2020/12/贪吃蛇.exe
未安装Visual Studio的电脑需安装Visual C++ Redistributable for Visual Studio 2015运行库(可能)
演示截图如下:
//需要安装easyx库
#include<graphics.h>
#include<conio.h>
#include<iostream>
#define BLOCK_SIZE 20
#define HEIGHT 40
#define WIDTH 40
int blocks[HEIGHT][WIDTH] = { 0 };//背景数组
char moveDirection;
int food_i, food_j,slow_i,slow_j;
int isFailure = 0;
int maxTime = 10;//蛇的速度
void moveSnake() {
int i, j;//i为行,j为列
for (i = 0; i < HEIGHT; i++) {//遍历数组,将蛇所在数组元素的数值全部+1
for (j = 0; j < WIDTH; j++) {
if (blocks[i][j] > 0) {
blocks[i][j]++;
}
}
}
int oldTail_i, oldTail_j, oldHead_i, oldHead_j, max = 0;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (max < blocks[i][j]) {//获取旧尾巴位置,头部为1,数值从头到尾巴递增,尾巴数值最大
max = blocks[i][j];
oldTail_i = i;
oldTail_j = j;
}
if (blocks[i][j] == 2) {//全部元素+1后旧头部数值为2
oldHead_i = i;
oldHead_j = j;
}
}
}
int newHead_i = oldHead_i, newHead_j = oldHead_j;//得到新头部位置
if (moveDirection == 'w'|| moveDirection == 'W') {
newHead_i = oldHead_i - 1;
}
else if (moveDirection == 'a'|| moveDirection == 'A') {
newHead_j = oldHead_j - 1;
}
else if (moveDirection == 's'|| moveDirection == 'S') {
newHead_i = oldHead_i + 1;
}
else if (moveDirection == 'd'|| moveDirection == 'D') {
newHead_j = oldHead_j + 1;
}
if (newHead_i >= HEIGHT || newHead_j >= WIDTH || newHead_i < 0 || newHead_j < 0 || blocks[newHead_i][newHead_j]>0) {//边界判定
isFailure = 1;
return;
}
blocks[newHead_i][newHead_j] = 1;
if (newHead_i == food_i && newHead_j == food_j) {
food_i = rand() % (HEIGHT - 5) + 2;
food_j = rand() % (WIDTH - 5) + 2;//吃到食物后重新生成食物
if (maxTime > 1) {//游戏速度加快
maxTime--;
}
}
else {
blocks[oldTail_i][oldTail_j] = 0;
}
if (newHead_i == slow_i && newHead_j == slow_j) {
slow_i = rand() % (HEIGHT - 5) + 2;
slow_j = rand() % (WIDTH - 5) + 2;//重新生成减速道具
if (maxTime < 15) {//游戏速度减慢
maxTime++;
}
}
}
void initialize() {
int i;
blocks[HEIGHT / 2][WIDTH / 2] = 1;//初始化蛇头位置
for (i = 1; i <= 4; i++) {
blocks[HEIGHT / 2][WIDTH / 2 - i] = i + 1;//初始化蛇身位置
}
moveDirection = 'd';//设置初始方向为右
food_i = rand() % (HEIGHT - 5) + 2;
food_j = rand() % (WIDTH - 5) + 2;
while (blocks[food_j][food_i] > 1) {
food_i = rand() % (HEIGHT - 5) + 2;
food_j = rand() % (WIDTH - 5) + 2;
}
slow_i = rand() % (HEIGHT - 5) + 2;
slow_j = rand() % (WIDTH - 5) + 2;
while (blocks[slow_j][slow_i] > 1) {
slow_i = rand() % (HEIGHT - 5) + 2;
slow_j = rand() % (WIDTH - 5) + 2;
}
initgraph(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE, NULL);//绘制地图
setlinecolor(RGB(200, 200, 200));
BeginBatchDraw();//开始绘制
}
void draw() {
cleardevice();//清屏
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (blocks[i][j] > 0) {
setfillcolor(HSVtoRGB(30, 0.9, 1));//蛇身颜色
}
else {
setfillcolor(RGB(150, 150, 150));//背景格子颜色
}
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);
}
setfillcolor(RGB(0, 255, 0));//食物颜色,RGB 0 255 0为绿色
fillrectangle(food_j * BLOCK_SIZE, food_i * BLOCK_SIZE, (food_j + 1) * BLOCK_SIZE, (food_i + 1) * BLOCK_SIZE);
setfillcolor(RGB(255, 0, 0));//减速道具颜色
fillrectangle(slow_j * BLOCK_SIZE, slow_i * BLOCK_SIZE, (slow_j + 1) * BLOCK_SIZE, (slow_i + 1) * BLOCK_SIZE);
}
int score = 0;
if (isFailure) {//游戏结束提示
for (i = 0; i < HEIGHT; i++) {//计算蛇长度
for (j = 0; j < WIDTH; j++) {
if (blocks[i][j] > 0) {
score++;
}
}
}
TCHAR cscore[5];
swprintf_s(cscore, _T("%d"), score-5);
setbkmode(TRANSPARENT);
settextcolor(RGB(255, 0, 0));
settextstyle(80, 0, _T("微软雅黑"));
outtextxy(240, 240, _T("YOU DIED"));
outtextxy(240, 300, _T("SCORE:"));
outtextxy(480, 300, cscore);
}
FlushBatchDraw();//批量绘制,将缓冲区里绘制好的图形全部打印到屏幕
}
void updateWithoutInput() {//无键盘输入时的屏幕更新
if (isFailure)
return;
static int waitIndex = 1;
waitIndex++;
if (waitIndex == maxTime) {//设置游戏速度
moveSnake();
waitIndex = 1;
}
}
void updateWithInput() {//有键盘输入时的屏幕更新
if (_kbhit() && isFailure == 0) {
char input = _getch();//获取按键
if (input == 'a' || input == 's' || input == 'd' || input == 'w'|| input == 'W' || input == 'A' || input == 'S' || input == 'D') {
moveDirection = input;
moveSnake();
}
}
}
int main() {
initialize();
while (1) {//死循环,使游戏一直运行
draw();
updateWithoutInput();
updateWithInput();
}
return 0;
}