0 Star 0 Fork 0

米糖/贪吃蛇

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Snake.cpp 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
米糖 提交于 2023-01-05 07:03 . 重命名 snake.cpp 为 Snake.cpp
#include "pch.h"
#include "Snake.h"
#include <list>
#include <random>
#define MAXx 34
#define MAXy 34
#define MINx 0
#define MINy 0
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define EXIST_ITEM 1
#define NON_EXIST_ITEM 0
Snake::Snake()
{
body.push_front({ 12,15 });
body.push_front({ 12,14 });
body.push_front({ 12,13 });
body.push_front({ 12,12 });
direction = RIGHT;
speed = 1;
length = 3;
score = 0;
lose = false;
itemOldTime = clock();
speedOldTime = clock();
memset(map, 0, sizeof map);
}
void Snake::move()
{
auto head = body.back();
//判断蛇是否会死亡
switch (direction)
{
case UP:
if (head.first - 1 < MINy || !checkIsOnSnake({ head.first - 1, head.second })) {
lose = true;
return;
}
break;
case DOWN:
if (head.first + 1 > MAXy || !checkIsOnSnake({ head.first + 1, head.second })) {
lose = true;
return;
}
break;
case LEFT:
if (head.second - 1 < MINx || !checkIsOnSnake({ head.first, head.second - 1 })) {
lose = true;
return;
}
break;
case RIGHT:
if (head.second + 1 > MAXx || !checkIsOnSnake({ head.first, head.second + 1 })) {
lose = true;
return;
}
break;
}
auto Tempit = body.begin(); //保存前一格蛇身的指针
auto tail = *Tempit; //保存蛇尾
for (auto it = ++body.begin(); it != body.end(); ++it) {
*Tempit = *it;
Tempit = it;
//判断是否为蛇头
if (next(it) == body.end()) {
switch (direction) //进行移动
{
case UP:
it->first--;
break;
case DOWN:
it->first++;
break;
case LEFT:
it->second--;
break;
case RIGHT:
it->second++;
break;
}
//判断是否吃到食物
if (map[it->second][it->first] == EXIST_ITEM) {
map[it->second][it->first] = NON_EXIST_ITEM;
//查找并删除食物
std::pair<int, int> point = { it->second, it->first };
auto tempit = find(item.begin(), item.end(), point);
item.erase(tempit);
length++;
body.push_front(tail);
score += speed;
}
}
}
}
bool Snake::gameOver()
{
if (lose == true) {
return true;
}
return false;
}
void Snake::addItem()
{
//添加食物
if (item.size() == 1) return;
//获取随机数
std::default_random_engine engine;
std::uniform_int_distribution<int> random(0, 50);
engine.seed(time(0));
//定时产生食物
if (clock() > itemOldTime + 1000) {
itemOldTime = clock();
int x, y;
do{
srand(time(NULL));
x = random(engine) % MAXy;
y = random(engine) % MAXx;
} while (map[x][y] == EXIST_ITEM && checkIsOnSnake({x, y}));
map[x][y] = EXIST_ITEM;
item.push_back({ x, y });
}
}
bool Snake::checkIsOnSnake(std::pair<int, int> temp)
{
//检测坐标是否不在蛇身上
for (auto it = body.begin(); it != body.end(); ++it) {
if (*it == temp) return false;
}
return true;
}
bool Snake::speedUp()
{
//定时加速
switch (speed)
{
case 1:
if (clock() > speedOldTime + 30000) speed += 2;
return true;
case 3:
if (clock() > speedOldTime + 60000) speed += 2;
return true;
case 5:
if (clock() > speedOldTime + 90000) speed += 2;
return true;
case 7:
if (clock() > speedOldTime + 120000) speed += 2;
return true;
}
return false;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ricesugar/gluttonous-snake.git
git@gitee.com:ricesugar/gluttonous-snake.git
ricesugar
gluttonous-snake
贪吃蛇
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385