Snake game C++ : how to make the snake bite
I'm trying to make a snake game in C++ with SFML. Here is my code below.
The problem is that I don't know how I could possibly make the snake bite
himself : each snake block takes the position of the last one, resulting
in a perfectly straight snake. So it's more of an algorithm problem.
Thanks for your advices :) BTW Didn't put hashtages and < in the include
because the editor takes them away :/ BTW2: Looked for similar responses
but none of them were with my approach or were in another language. Here
you go:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <deque>
void selfIncrement();
sf::Event event;
sf::Clock clockSnake;
sf::Time elapse;
enum Direction { Up, Down, Left, Right};
int dir = Up;
int n = 1;
class SnakeBlock
{
public:
SnakeBlock * next;
sf::Texture texture;
sf::Sprite snakeblock;
int lastX, lastY;
};
std::deque<SnakeBlock> Snake;
int main()
{
elapse = clockSnake.getElapsedTime();
sf::Music epicMusic;
epicMusic.openFromFile("epicmusic.wav");
epicMusic.play();
SnakeBlock snakeHead;
snakeHead.texture.loadFromFile("spritesheetsnake.png",
sf::IntRect(0,0,20,22));
snakeHead.snakeblock.setTexture(snakeHead.texture);
SnakeBlock snakeBody1;
snakeBody1.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));
SnakeBlock snakeBody2;
snakeBody2.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));
Snake.push_front(snakeHead);
Snake.push_front(snakeBody1);
Snake.push_front(snakeBody2);
Snake[2].snakeblock.setPosition(500,350);
Snake[1].snakeblock.setPosition(475, 338);
Snake[0].snakeblock.setPosition(450, 316);
sf::RenderWindow window(sf::VideoMode(1028,768), "SFML Snake");
window.setFramerateLimit(60);
while(window.isOpen())
{
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
epicMusic.stop();
window.close();
break;
default:
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
dir = Left;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
dir = Right;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
dir = Down;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
dir = Up;
}
if(dir == Up)
{
Snake[0].snakeblock.move(0,-2);
Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x,
Snake[n-1].snakeblock.getPosition().y+20);
}
if(dir == Down)
{
Snake[0].snakeblock.move(0,2);
Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x,
Snake[n-1].snakeblock.getPosition().y-20);
}
if(dir == Left)
{
Snake[0].snakeblock.move(-2,0);
Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x+20,
Snake[n-1].snakeblock.getPosition().y);
}
if(dir == Right)
{
Snake[0].snakeblock.move(2,0);
Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x-20,
Snake[n-1].snakeblock.getPosition().y);
}
window.clear(sf::Color::Red);
n++;
if( n > 2)
{
n = 1;
}
//selfIncrement();
for(unsigned int m = 0; m < Snake.size(); m++)
{
window.draw(Snake[m].snakeblock);
}
window.display();
}
return 0;
}
/*void selfIncrement()
{
elapse = clockSnake.getElapsedTime();
if(elapse.asSeconds() > 0.10)
{
n++;
clockSnake.restart();
}
if(n > Snake.size())
{
n = 1;
}
}
*/
No comments:
Post a Comment