-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoods.cpp
59 lines (54 loc) · 1.5 KB
/
foods.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "foods.h"
// 坐标系原点在中间
Foods::Foods(QObject* parent, Map* m) : h_size(m->h_size), v_size(m->v_size), map(m)
{
setParent(parent);
for(int i = 0; i < 8; ++i)
randomGenerate();
}
void Foods::eaten(POINT p, QList<FOOD> &foodsEaten)
{
for(auto it = locations.begin(); it != locations.end(); ++it)
{
if(p == (*it).location)
{
foodsEaten.push_back(*it);
return;
}
}
}
void Foods::randomGenerate()
{
srand((unsigned)time(NULL));
bool succeed = false;
while(!succeed)
{
//随机生成一个点
POINT random((rand()%(map->h_size - 1)) + 1, (rand()%(map->v_size - 1)) + 1);
//这个点是墙
if(map->blocks.contains(random))
continue;
//这个点已经有food了
succeed = true;
for(auto it = locations.begin(); it != locations.end(); ++it)
{
if((*it).location.x == random.x && (*it).location.y == random.y)
{
succeed = false;
break;
}
}
if(succeed)
{
//生成正常食物的几率为67%,特殊食物为33%
if(rand() % 3 == 0)
locations.push_back(FOOD(random, FOODTYPE(rand() % 4 + 1)));
else
locations.push_back(FOOD(random, ADDLENGTH));
}
}
}
POINT Foods::getFoodLocation()
{
return locations.back().location;
}