-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStone.cpp
More file actions
84 lines (75 loc) · 2.39 KB
/
Stone.cpp
File metadata and controls
84 lines (75 loc) · 2.39 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Stone.h"
#include "Game.h"
#include <cstdlib>
#include <ctime>
namespace SnakeGame
{
void InitStones(std::vector<Stone>& stones, const sf::Texture& texture, int count)
{
stones.clear();
stones.resize(count);
for (auto& stone : stones)
{
stone.sprite.setTexture(texture);
SetSpriteSize(stone.sprite, SIZE_ITEM, SIZE_ITEM);
SetSpriteRelativeOrigin(stone.sprite, 0.5f, 0.5f);
}
}
void PositionStones(std::vector<Stone>& stones, const Snake& snake)
{
for (auto& stone : stones)
{
bool validPosition = false;
while (!validPosition)
{
stone.gridX = rand() % GRID_WIDTH;
stone.gridY = rand() % GRID_HEIGHT;
validPosition = true;
for (const auto& segment : snake.segments)
{
if (segment.gridX == stone.gridX && segment.gridY == stone.gridY)
{
validPosition = false;
break;
}
}
if (validPosition)
{
for (const auto& otherStone : stones)
{
if (&stone != &otherStone &&
stone.gridX == otherStone.gridX &&
stone.gridY == otherStone.gridY)
{
validPosition = false;
break;
}
}
}
}
float pixelX = stone.gridX * SIZE_ITEM + SIZE_ITEM / 2;
float pixelY = stone.gridY * SIZE_ITEM + SIZE_ITEM / 2;
stone.sprite.setPosition(pixelX, pixelY);
}
}
void DrawStones(const std::vector<Stone>& stones, sf::RenderWindow& window)
{
for (const auto& stone : stones)
{
window.draw(stone.sprite);
}
}
bool CheckStoneCollision(const std::vector<Stone>& stones, const Snake& snake)
{
if (snake.segments.empty()) return false;
const auto& head = snake.segments[0];
for (const auto& stone : stones)
{
if (head.gridX == stone.gridX && head.gridY == stone.gridY)
{
return true;
}
}
return false;
}
}