added fruit and edge collision
This commit is contained in:
parent
a3d6742d98
commit
75cf6f524d
|
|
@ -0,0 +1,16 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <Color.h>
|
||||||
|
#include <Player.h>
|
||||||
|
|
||||||
|
struct Fruit
|
||||||
|
{
|
||||||
|
Fruit() = default;
|
||||||
|
Fruit(sf::Vector2i gridPos_in, const sf::Color& color_in = sf::Color::White);
|
||||||
|
sf::Vector2u gridPos{};
|
||||||
|
Color color{};
|
||||||
|
|
||||||
|
sf::Vector2f windowPos(float gridSize_in);
|
||||||
|
void respawn(const std::vector<SnakeNode>& body_in, sf::Vector2u gridCount_in);
|
||||||
|
};
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <Util.h>
|
#include <Util.h>
|
||||||
#include <Player.h>
|
#include <Player.h>
|
||||||
|
#include <Fruit.h>
|
||||||
#include <GameConfig.h>
|
#include <GameConfig.h>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <SFML/Audio.hpp>
|
#include <SFML/Audio.hpp>
|
||||||
|
|
@ -39,6 +40,8 @@ private:
|
||||||
|
|
||||||
void failState();
|
void failState();
|
||||||
|
|
||||||
|
void winState();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GameConfig config;
|
GameConfig config;
|
||||||
|
|
@ -54,9 +57,13 @@ private:
|
||||||
//sf::Sound failSound;
|
//sf::Sound failSound;
|
||||||
|
|
||||||
Player player{};
|
Player player{};
|
||||||
|
Fruit fruit{};
|
||||||
|
|
||||||
|
sf::Vector2u gridCount;
|
||||||
|
|
||||||
size_t frameCount{};
|
size_t frameCount{};
|
||||||
bool useImgui;
|
bool useImgui;
|
||||||
bool fail;
|
bool fail{};
|
||||||
|
bool win{};
|
||||||
bool running{true};
|
bool running{true};
|
||||||
};
|
};
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <Util.h>
|
#include <Util.h>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
|
@ -8,6 +10,6 @@ struct GameConfig
|
||||||
int framerate{};
|
int framerate{};
|
||||||
Color playerHeadColor;
|
Color playerHeadColor;
|
||||||
Color playerBodyColor;
|
Color playerBodyColor;
|
||||||
sf::Vector2i headGridStartPos;
|
sf::Vector2u headGridStartPos;
|
||||||
float gridSize;
|
float gridSize;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
|
#pragma once
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include <Util.h>
|
#include <Util.h>
|
||||||
|
|
@ -13,14 +13,16 @@ enum class Direction
|
||||||
|
|
||||||
struct SnakeNode
|
struct SnakeNode
|
||||||
{
|
{
|
||||||
SnakeNode(sf::Vector2i gridPos_in, sf::Color color_in = sf::Color::White);
|
SnakeNode(sf::Vector2u gridPos_in, const sf::Color& color_in = sf::Color::White);
|
||||||
sf::Vector2f windowPos{};
|
// sf::Vector2f windowPos{};
|
||||||
sf::Vector2i gridPos{};
|
sf::Vector2u gridPos{};
|
||||||
sf::Vector2i previousGridPos{};
|
sf::Vector2u previousGridPos{};
|
||||||
Color color{};
|
Color color{};
|
||||||
|
|
||||||
|
sf::Vector2f windowPos(float gridSize_in);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Player
|
struct Player
|
||||||
{
|
{
|
||||||
Player() = default;
|
Player() = default;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#ifdef LOG_ENABLE
|
||||||
|
#define LOG_CONDITON(x) if (x)
|
||||||
|
#define LOG(x) std::cout << x << "\n"
|
||||||
|
#else
|
||||||
|
#define LOG(x)
|
||||||
|
#define LOG_CONDITON(x)
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
#include <Player.h>
|
||||||
|
#include <Fruit.h>
|
||||||
|
#include <Random.h>
|
||||||
|
|
||||||
|
Fruit::Fruit(sf::Vector2i gridPos_in, const sf::Color& color_in)
|
||||||
|
: gridPos(gridPos_in)
|
||||||
|
, color(color_in)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
sf::Vector2f Fruit::windowPos(float gridSize_in)
|
||||||
|
{
|
||||||
|
return {gridPos.x * gridSize_in, gridPos.y * gridSize_in};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Fruit::respawn(const std::vector<SnakeNode>& body_in, sf::Vector2u gridCount_in)
|
||||||
|
{
|
||||||
|
unsigned int tempX;
|
||||||
|
unsigned int tempY;
|
||||||
|
bool valid = false;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
tempX = Random::get(0, gridCount_in.x - 1);
|
||||||
|
tempY = Random::get(0, gridCount_in.y - 1);
|
||||||
|
for (const auto& node : body_in)
|
||||||
|
{
|
||||||
|
if (node.gridPos.x != tempX and node.gridPos.y != tempY)
|
||||||
|
{
|
||||||
|
valid = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (!valid);
|
||||||
|
|
||||||
|
this->gridPos = {tempX, tempY};
|
||||||
|
|
||||||
|
}
|
||||||
107
src/Game.cpp
107
src/Game.cpp
|
|
@ -31,13 +31,26 @@ Game::Game(bool useImgui_in)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gridCount.x = window.getSize().x / config.gridSize;
|
||||||
|
gridCount.y = window.getSize().y / config.gridSize;
|
||||||
|
|
||||||
player.body.reserve(100);
|
player.body.reserve(100);
|
||||||
player.body.emplace_back(config.headGridStartPos);
|
player.body.emplace_back(config.headGridStartPos);
|
||||||
player.head().windowPos.x = player.head().gridPos.x * config.gridSize;
|
|
||||||
player.head().windowPos.y = player.head().gridPos.y * config.gridSize;
|
|
||||||
|
|
||||||
|
fruit.gridPos = {5,5};
|
||||||
|
fruit.color = sf::Color::Red;
|
||||||
|
|
||||||
|
//uncomment to quickly test player movement
|
||||||
|
/*
|
||||||
|
player.body.emplace_back(sf::Vector2i{config.headGridStartPos.x - 1, config.headGridStartPos.y});
|
||||||
|
player.body.emplace_back(sf::Vector2i{config.headGridStartPos.x - 2, config.headGridStartPos.y});
|
||||||
|
player.body.emplace_back(sf::Vector2i{config.headGridStartPos.x - 3, config.headGridStartPos.y});
|
||||||
|
player.body.emplace_back(sf::Vector2i{config.headGridStartPos.x - 4, config.headGridStartPos.y});
|
||||||
|
*/
|
||||||
|
|
||||||
tempRect.setSize({config.gridSize, config.gridSize});
|
tempRect.setSize({config.gridSize, config.gridSize});
|
||||||
tempRect.setOutlineThickness(5);
|
tempRect.setOutlineThickness(-5);
|
||||||
|
tempRect.setOutlineColor(sf::Color::Black);
|
||||||
|
|
||||||
score.setPosition({10, static_cast<float>(window.getSize().y - 40)});
|
score.setPosition({10, static_cast<float>(window.getSize().y - 40)});
|
||||||
|
|
||||||
|
|
@ -179,10 +192,23 @@ void Game::collision()
|
||||||
if (player.head().gridPos == player.body.at(i).gridPos)
|
if (player.head().gridPos == player.body.at(i).gridPos)
|
||||||
{
|
{
|
||||||
fail = true;
|
fail = true;
|
||||||
//failSound.play();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.head().gridPos == fruit.gridPos)
|
||||||
|
{
|
||||||
|
player.body.emplace_back(player.body.back().previousGridPos);
|
||||||
|
if (player.body.size() != (gridCount.x * gridCount.y))
|
||||||
|
{
|
||||||
|
//play sound
|
||||||
|
fruit.respawn(player.body, gridCount);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//play win sound
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -211,39 +237,68 @@ void Game::movement()
|
||||||
|
|
||||||
if (frameCount % config.tickRate != 0) return;
|
if (frameCount % config.tickRate != 0) return;
|
||||||
|
|
||||||
|
SnakeNode& head = player.head();
|
||||||
switch (player.inputBuffer)
|
switch (player.inputBuffer)
|
||||||
{
|
{
|
||||||
case Direction::up:
|
case Direction::up:
|
||||||
player.head().previousGridPos = player.head().gridPos;
|
if (head.gridPos.y == 0)
|
||||||
player.head().gridPos += sf::Vector2i{ 0, -1 };
|
{
|
||||||
|
fail = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
head.previousGridPos = head.gridPos;
|
||||||
|
head.gridPos.y -= 1;
|
||||||
player.facing = Direction::up;
|
player.facing = Direction::up;
|
||||||
// std::cout << "up\n";
|
// std::cout << "up\n";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Direction::down:
|
case Direction::down:
|
||||||
player.head().previousGridPos = player.head().gridPos;
|
if (head.gridPos.y == gridCount.y - 1)
|
||||||
player.head().gridPos += sf::Vector2i{ 0, 1 };
|
{
|
||||||
|
fail = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
head.previousGridPos = head.gridPos;
|
||||||
|
head.gridPos.y += 1;
|
||||||
player.facing = Direction::down;
|
player.facing = Direction::down;
|
||||||
// std::cout << "down\n";
|
// std::cout << "down\n";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Direction::left:
|
case Direction::left:
|
||||||
player.head().previousGridPos = player.head().gridPos;
|
if (head.gridPos.x == 0)
|
||||||
player.head().gridPos += sf::Vector2i{ -1, 0 };
|
{
|
||||||
|
fail = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
head.previousGridPos = head.gridPos;
|
||||||
|
head.gridPos.x -= 1;
|
||||||
player.facing = Direction::left;
|
player.facing = Direction::left;
|
||||||
// std::cout << "left\n";
|
// std::cout << "left\n";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Direction::right:
|
case Direction::right:
|
||||||
player.head().previousGridPos = player.head().gridPos;
|
if (head.gridPos.x == gridCount.x -1)
|
||||||
player.head().gridPos += sf::Vector2i{ 1, 0 };
|
{
|
||||||
|
fail = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
head.previousGridPos = head.gridPos;
|
||||||
|
head.gridPos.x += 1;
|
||||||
player.facing = Direction::right;
|
player.facing = Direction::right;
|
||||||
// std::cout << "right\n";
|
// std::cout << "right\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
player.head().windowPos.x = player.head().gridPos.x * config.gridSize;
|
const size_t limit = player.body.size();
|
||||||
player.head().windowPos.y = player.head().gridPos.y * config.gridSize;
|
for (size_t i = 1; i < limit; i++)
|
||||||
|
{
|
||||||
|
player.body[i].previousGridPos = player.body[i].gridPos;
|
||||||
|
player.body[i].gridPos = player.body[i-1].previousGridPos;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -279,10 +334,14 @@ void Game::render()
|
||||||
for (auto& node : player.body)
|
for (auto& node : player.body)
|
||||||
{
|
{
|
||||||
tempRect.setFillColor(node.color.sfml());
|
tempRect.setFillColor(node.color.sfml());
|
||||||
tempRect.setPosition(node.windowPos);
|
tempRect.setPosition(node.windowPos(config.gridSize));
|
||||||
window.draw(tempRect);
|
window.draw(tempRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tempRect.setFillColor(fruit.color.sfml());
|
||||||
|
tempRect.setPosition(fruit.windowPos(config.gridSize));
|
||||||
|
window.draw(tempRect);
|
||||||
|
|
||||||
score.setString("Score: " + std::to_string(player.score));
|
score.setString("Score: " + std::to_string(player.score));
|
||||||
|
|
||||||
window.draw(score);
|
window.draw(score);
|
||||||
|
|
@ -304,7 +363,10 @@ void Game::scoreSystem()
|
||||||
|
|
||||||
void Game::resetGame()
|
void Game::resetGame()
|
||||||
{
|
{
|
||||||
|
player.body.clear();
|
||||||
|
player.body.emplace_back(config.headGridStartPos);
|
||||||
|
fail = false;
|
||||||
|
player.facing = Direction::up;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::failState()
|
void Game::failState()
|
||||||
|
|
@ -316,6 +378,11 @@ void Game::failState()
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Game::winState()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void Game::run()
|
void Game::run()
|
||||||
{
|
{
|
||||||
while(window.isOpen())
|
while(window.isOpen())
|
||||||
|
|
@ -326,7 +393,7 @@ void Game::run()
|
||||||
|
|
||||||
input();
|
input();
|
||||||
|
|
||||||
if (!fail)
|
if (!fail and !win)
|
||||||
{
|
{
|
||||||
movement();
|
movement();
|
||||||
|
|
||||||
|
|
@ -336,10 +403,14 @@ void Game::run()
|
||||||
|
|
||||||
scoreSystem();
|
scoreSystem();
|
||||||
}
|
}
|
||||||
else
|
else if (fail)
|
||||||
{
|
{
|
||||||
failState();
|
failState();
|
||||||
}
|
}
|
||||||
|
else if (win)
|
||||||
|
{
|
||||||
|
winState();
|
||||||
|
}
|
||||||
|
|
||||||
imgui();
|
imgui();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
#include <Player.h>
|
#include <Player.h>
|
||||||
|
|
||||||
SnakeNode::SnakeNode(sf::Vector2i gridPos_in, sf::Color color_in)
|
SnakeNode::SnakeNode(sf::Vector2u gridPos_in, const sf::Color& color_in)
|
||||||
: gridPos(gridPos_in)
|
: gridPos(gridPos_in)
|
||||||
, color(color_in)
|
, color(color_in)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
sf::Vector2f SnakeNode::windowPos(float gridSize_in)
|
||||||
|
{
|
||||||
|
return {gridPos.x * gridSize_in, gridPos.y * gridSize_in};
|
||||||
|
}
|
||||||
|
|
||||||
SnakeNode& Player::head()
|
SnakeNode& Player::head()
|
||||||
{
|
{
|
||||||
return body[0];
|
return body[0];
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <log.h>
|
||||||
#include <Game.h>
|
#include <Game.h>
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
|
|
@ -11,6 +12,14 @@ int main(int argc, char* argv[])
|
||||||
useImgui = false;
|
useImgui = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG("\n\n\033[32mTODO: ");
|
||||||
|
LOG("-Implement fruit eating");
|
||||||
|
LOG("-Implement sound effects and background music");
|
||||||
|
LOG("-Handle game over state");
|
||||||
|
LOG("-Implement game config via imgui");
|
||||||
|
LOG("\033[0m");
|
||||||
|
|
||||||
Game game(useImgui);
|
Game game(useImgui);
|
||||||
|
|
||||||
game.run();
|
game.run();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue