snakeplusplus/src/main.cpp

45 lines
1.4 KiB
C++
Raw Normal View History

#include <iostream>
#include <stdlib.h>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "GameState.h"
#include "Snake.h"
int main()
{
GameState newGame;
2022-07-29 16:27:18 -05:00
newGame.gameWindow.create(newGame.gameVideoMode, "SnakePlusPlus");
sf::RenderWindow *window = &newGame.gameWindow;
sf::Time delay = sf::milliseconds(25);
int snakeDirection = 0;
Snake Player(sf::Vector2f(25,25));
2022-05-06 01:26:17 -05:00
sf::RectangleShape snakeHead(sf::Vector2f(25,25));
sf::RectangleShape snakeFood(sf::Vector2f(25,25));
snakeFood.setFillColor(sf::Color::Red);
snakeFood.setPosition(25,25);
2022-05-06 01:26:17 -05:00
2022-07-29 16:27:18 -05:00
while (window->isOpen())
{
2022-05-06 01:26:17 -05:00
sf::Event event;
2022-07-29 16:27:18 -05:00
while (window->pollEvent(event))
{
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
2022-07-29 16:27:18 -05:00
window->close();
}
2022-05-06 01:26:17 -05:00
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeDirection = 1;
2022-05-06 01:26:17 -05:00
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
snakeDirection = 2;
2022-05-06 01:26:17 -05:00
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
snakeDirection = 3;
2022-05-06 01:26:17 -05:00
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snakeDirection = 4;
Player.MoveSnake(snakeFood);
2022-07-29 16:27:18 -05:00
window->clear();
window->draw(snakeFood);
Player.DisplaySnake(*window);
window->display();
sf::sleep(delay);
}
}