From 3baad1c90d3459039878a86e39fc18256200762a Mon Sep 17 00:00:00 2001 From: pw6mfljci <2537107798@qq.com> Date: Mon, 17 Jun 2024 19:12:10 +0800 Subject: [PATCH] ADD file via upload --- food.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 food.cpp diff --git a/food.cpp b/food.cpp new file mode 100644 index 0000000..b5bfdd7 --- /dev/null +++ b/food.cpp @@ -0,0 +1,66 @@ +#include "Food.h" +#include +#include +#include +#include + +Food::Food() +{ + //初始化食物的数据 + this->m_state = true; + this->m_pos = Point(310, 230); +} + +bool Food::getState() +{ + return this->m_state; +} + +void Food::setState(bool state) +{ + this->m_state = state; +} + +Point Food::getPos() +{ + return this->m_pos; +} + +void Food::Generate(Snake* snake) +{ + //产生食物要求获取蛇的身体节点来检查是否生成的食物出现在了蛇的身上 + int x = 0; + int y = 0; + bool isOk = false; + + while (true) + { + //使用随机函数产生食物 + srand(time(0)); + x = (rand() % 24) * 20 + 10; + y = (rand() % 24) * 20 + 10; + + //检查是否在蛇的身上 + isOk = 1; + + std::list pos = snake->GetSnakeAllNode(); + std::list::iterator it = find(pos.begin(), pos.end(), Point(x, y)); + //find()需要用到Point的重载 == 操作符 + + + if (it == pos.end())//不在 + { + this->m_pos = Point(x, y);//修改坐标 + this->m_state = true; + return; + } + } +} + +void Food::DrawFood() +{ + //红色,全填充,无边框的圆 + setfillcolor(RED); + setfillstyle(BS_SOLID); + solidcircle(this->m_pos.x, this->m_pos.y, FOOD_RADIU); +} \ No newline at end of file