I can't for the love of god find out why this isn't working. I implemented the way The Hatchet did it (kinda). Going to make it into for loops and make it able to obtain more peices that you pick up. That part shouldn't be so hard I guess. However in the first place, the code that I have at the moment is not working as intended.
The snake is not being printed on the screen at all. I removed some of the methods that wasn't related to the problem:
Header file:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <vector>
class Game {
private:
sf::RenderWindow mainWindow;
sf::Texture snakeTexture;
sf::Texture tailTexture;
sf::Sprite snakeSprite;
sf::Sprite tail;
std::vector<sf::Sprite> snakeSegments;
int oldX;
int oldY;
enum direction {Up, Down, Right, Left} Direction;
void handleEvents();
void moveSnake();
void setupSegments();
//void checkForCollisionWithWall();
//bool collisionWithTail();
void createMainWindow();
public:
void run();
Game();
};
Implementation file:
#include "Game.h"
void Game::createMainWindow() {
mainWindow.create(sf::VideoMode(800,600), "Snake", sf::Style::Titlebar | sf::Style::Close);
mainWindow.setMouseCursorVisible(false);
}
void Game::handleEvents() {
sf::Event event;
while (mainWindow.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
mainWindow.close();
break;
}
switch (event.key.code)
{
case sf::Keyboard::Up:
if (Direction != Down) {
Direction = Up;
} break;
case sf::Keyboard::Down:
if (Direction != Up) {
Direction = Down;
} break;
case sf::Keyboard::Right:
if (Direction != Left) {
Direction = Right;
} break;
case sf::Keyboard::Left:
if (Direction != Right) {
Direction = Left;
} break;
}
}
}
void Game::moveSnake() {
oldX = snakeSegments.at(0).getPosition().x;
oldY = snakeSegments.at(0).getPosition().y;
switch(Direction)
{
case Up:
snakeSegments.at(0).setPosition(oldX, oldY-20);
snakeSegments.at(2).setPosition(snakeSegments.at(1).getPosition().x, snakeSegments.at(1).getPosition().y);
snakeSegments.at(1).setPosition(oldX, oldY);
break;
case Down:
snakeSegments.at(0).setPosition(oldX, oldY+20);
snakeSegments.at(2).setPosition(snakeSegments.at(1).getPosition().x, snakeSegments.at(1).getPosition().y);
snakeSegments.at(1).setPosition(oldX, oldY);
break;
case Right:
snakeSegments.at(0).setPosition(oldX+20, oldY);
snakeSegments.at(2).setPosition(snakeSegments.at(1).getPosition().x, snakeSegments.at(1).getPosition().y);
snakeSegments.at(1).setPosition(oldX, oldY);
break;
case Left:
snakeSegments.at(0).setPosition(oldX-20, oldY);
snakeSegments.at(2).setPosition(snakeSegments.at(1).getPosition().x, snakeSegments.at(1).getPosition().y);
snakeSegments.at(1).setPosition(oldX, oldY);
break;
}
}
void Game::setupSegments() {
for (int i = 0; i < 3; i++) {
snakeSegments.push_back(snakeSprite);
snakeSegments.at(i).setPosition(100 + i*20, 100);
}
}
void Game::run() {
createMainWindow();
while(mainWindow.isOpen())
{
handleEvents();
moveSnake();
mainWindow.clear();
for(std::vector<int>::size_type i = 0; i != snakeSegments.size(); i++) {
mainWindow.draw(snakeSegments.at(i));
}
mainWindow.display();
}
}
Game::Game() {
setupSegments();
Direction = Down;
snakeTexture.loadFromFile("pTexture.png");
tailTexture.loadFromFile("tailTexture.png");
snakeSprite.setTexture(snakeTexture);
tail.setTexture(tailTexture);
}
Thanks in advance!