-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApple.cpp
More file actions
37 lines (31 loc) · 1005 Bytes
/
Apple.cpp
File metadata and controls
37 lines (31 loc) · 1005 Bytes
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
#include "Apple.h"
#include "Game.h"
#include <cstdlib>
#include <ctime>
namespace SnakeGame
{
void InitApple(Apple& apple, const sf::Texture& texture)
{
apple.sprite.setTexture(texture);
SetSpriteSize(apple.sprite, SIZE_ITEM, SIZE_ITEM);
SetSpriteRelativeOrigin(apple.sprite, 0.5f, 0.5f);
}
void PositionApple(Apple& apple)
{
apple.gridX = rand() % GRID_WIDTH;
apple.gridY = rand() % GRID_HEIGHT;
float pixelX = apple.gridX * SIZE_ITEM + SIZE_ITEM / 2;
float pixelY = apple.gridY * SIZE_ITEM + SIZE_ITEM / 2;
apple.sprite.setPosition(pixelX, pixelY);
}
void DrawApple(Apple& apple, sf::RenderWindow& window)
{
window.draw(apple.sprite);
}
bool CheckAppleCollision(const Apple& apple, const Snake& snake)
{
if (snake.segments.empty()) return false;
const auto& head = snake.segments[0];
return (head.gridX == apple.gridX && head.gridY == apple.gridY);
}
}