2022-05-06 00:40:36 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <SFML\Graphics.hpp>
|
|
|
|
#include <SFML\System.hpp>
|
2022-07-25 15:40:45 -05:00
|
|
|
#include "Snake.h"
|
2022-05-06 00:40:36 -05:00
|
|
|
using namespace std;
|
|
|
|
|
2022-07-25 15:40:45 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
TODO: Work on snake body
|
|
|
|
Add ability for body to extend when eating food
|
|
|
|
|
|
|
|
Turn snake body into queue
|
|
|
|
Each piece of queue has coordinates
|
|
|
|
If head touches food, just add to queue, don't pop
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-06-10 14:40:12 -05:00
|
|
|
|
2022-05-06 00:40:36 -05:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int videoSizeHorizontal, videoSizeVertical;
|
2022-05-06 01:26:17 -05:00
|
|
|
videoSizeHorizontal = 1024;
|
|
|
|
videoSizeVertical = 725;
|
2022-05-06 00:40:36 -05:00
|
|
|
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
|
2022-05-06 01:26:17 -05:00
|
|
|
sf::Time delay = sf::milliseconds(100);
|
|
|
|
|
|
|
|
sf::RectangleShape snakeHead(sf::Vector2f(25,25));
|
2022-06-10 14:40:12 -05:00
|
|
|
sf::RectangleShape snakeFood(sf::Vector2f(25,25));
|
2022-05-06 01:26:17 -05:00
|
|
|
snakeHead.setFillColor(sf::Color::Green);
|
2022-06-10 14:40:12 -05:00
|
|
|
snakeFood.setFillColor(sf::Color::Red);
|
|
|
|
|
|
|
|
snakeFood.setPosition(25,25);
|
2022-05-06 01:26:17 -05:00
|
|
|
|
2022-05-06 00:40:36 -05:00
|
|
|
while (window.isOpen())
|
|
|
|
{
|
2022-05-06 01:26:17 -05:00
|
|
|
sf::Event event;
|
2022-05-06 00:40:36 -05:00
|
|
|
while (window.pollEvent(event))
|
|
|
|
{
|
|
|
|
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
|
|
window.close();
|
|
|
|
}
|
2022-05-06 01:26:17 -05:00
|
|
|
sf::Vector2f snakeHeadPosition = snakeHead.getPosition();
|
2022-06-10 14:40:12 -05:00
|
|
|
sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
|
2022-07-25 15:40:45 -05:00
|
|
|
// TODO: Split Movement into separate function
|
|
|
|
// Add boundaries
|
|
|
|
// Add movement until boundaries
|
2022-05-06 01:26:17 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
|
|
|
snakeHeadPosition.x -= 25;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
|
|
|
snakeHeadPosition.y -= 25;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
|
|
|
snakeHeadPosition.y += 25;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
|
|
|
snakeHeadPosition.x += 25;
|
|
|
|
snakeHead.setPosition(snakeHeadPosition.x, snakeHeadPosition.y);
|
2022-06-10 14:40:12 -05:00
|
|
|
if (SnakeCollision(snakeHead, snakeFood))
|
|
|
|
{
|
|
|
|
snakeFoodPosition.x += 25;
|
|
|
|
snakeFoodPosition.y += 25;
|
|
|
|
snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
|
|
|
|
}
|
2022-05-06 01:26:17 -05:00
|
|
|
window.clear();
|
2022-06-10 14:40:12 -05:00
|
|
|
window.draw(snakeFood);
|
2022-05-06 01:26:17 -05:00
|
|
|
window.draw(snakeHead);
|
|
|
|
window.display();
|
|
|
|
sf::sleep(delay);
|
2022-05-06 00:40:36 -05:00
|
|
|
}
|
|
|
|
}
|