#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
class GameField;
class Snake
{
private:
sf::Vector2i snakeVector;
std::string pathToSnakeImage;
sf::Image snakeImage;
sf::Texture snakeTexture;
sf::Sprite snakeSprite;
public:
Snake(sf::Vector2i snakeVector, std::string pathToSnakeImage)
{
this->snakeVector = snakeVector;
this->pathToSnakeImage = pathToSnakeImage;
snakeImage.loadFromFile(this->pathToSnakeImage);
snakeTexture.loadFromImage(snakeImage);
snakeSprite.setTexture(snakeTexture);
}
sf::Sprite getSnakeSprite(void)
{
return snakeSprite;
}
void setSnakePosition(sf::Vector2f snakeVector)
{
snakeSprite.setPosition(snakeVector);
}
void snakeUpdate(GameField &gameField);
};
class GameField
{
private:
int** gameField;
int heightGameField;
int widthGameField;
std::string pathToGameField;
sf::Image gameFieldImage;
sf::Texture gameFieldTexture;
sf::Sprite gameFieldSprite;
int sizeGameFieldSprite = 32;
public:
GameField(int heightGameField, int widthGameField, std::string pathToGameFieldBorder)
{
this->heightGameField = heightGameField;
this->widthGameField = widthGameField;
this->pathToGameField = pathToGameFieldBorder;
gameFieldImage.loadFromFile(this->pathToGameField);
gameFieldTexture.loadFromImage(gameFieldImage);
gameFieldSprite.setTexture(gameFieldTexture);
gameField = new int*[heightGameField];
for (int i = 0; i < heightGameField; i++)
gameField[i] = new int[widthGameField];
}
void clear()
{
for (int i = 0; i < heightGameField; i++)
for (int j = 0; j < widthGameField; j++)
gameField[i][j] = 0;
}
int** getGameField(void)
{
return gameField;
}
void drawGameField(sf::RenderWindow &window, Snake &snake)
{
for (int i = 0; i < heightGameField; i++)
{
for (int j = 0; j < widthGameField; j++)
{
switch (gameField[i][j])
{
case 0:
window.draw(gameFieldSprite);
gameFieldSprite.setPosition(sf::Vector2f(sizeGameFieldSprite * i, sizeGameFieldSprite * j));
break;
case 1:
window.draw(snake.getSnakeSprite());
snake.setSnakePosition(sf::Vector2f(sizeGameFieldSprite * i, sizeGameFieldSprite * j));
break;
}
}
}
}
~GameField()
{
for (int i = 0; i < heightGameField; i++)
delete[] gameField[i];
delete[] gameField;
}
};
void Snake::snakeUpdate(GameField& gameField)
{
gameField.getGameField()[5][5] = 1;
}
int main()
{
GameField gameField(35, 25, "C:/Users/White/Documents/VSProgects/SFML/x64/Release/Image/GameField.png");
gameField.clear();
Snake snake(sf::Vector2i(17, 12),"C:/Users/White/Documents/VSProgects/SFML/x64/Release/Image/Snake.png");
sf::RenderWindow window(sf::VideoMode(35*32, 25*32), "Snake", sf::Style::Close);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
snake.snakeUpdate(gameField);
gameField.drawGameField(window, snake);
window.display();
}
return 0;
}
for (int i = 0; i < heightGameField; i++)
gameField[i] = new int[widthGameField];
for (int i = 0; i < heightGameField; i++)
{
for (int j = 0; j < widthGameField; j++)
{
switch (gameField[i][j])
int** getGameField(void)
{
return gameField;
}