#include <SFML/Graphics.hpp>
#include <time.h>
#include <sstream>
using namespace sf;
int N = 30, M = 20;
int size = 16;
int w = size * N;
int h = size * M;
int speed = 20;
int dir, num = 4;
int fscore=0;
bool gameIsAlive = true;
struct Snake
{
int x, y;
} s[100];
struct Fruit
{
int x, y;
} f;
void Tick()
{
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}
if (dir == 0)
s[0].y += 1;
if (dir == 1)
s[0].x -= 1;
if (dir == 2)
s[0].x += 1;
if (dir == 3)
s[0].y -= 1;
if ((s[0].x == f.x) && (s[0].y == f.y))
{
fscore++;
num++;
if (speed < 60)
speed++;
f.x = rand() % N;
f.y = rand() % M;
}
if (s[0].x > N)
s[0].x = 0;
if (s[0].x < 0)
s[0].x = N;
if (s[0].y > M)
s[0].y = 0;
if (s[0].y < 0)
s[0].y = M;
//Checking for Snake Collision
for (int i = 1; i < num; i++)
if (s[0].x == s[i].x && s[0].y == s[i].y)
{
num = i;
gameIsAlive = false;
}
}
int main()
{
srand(time(0));
RenderWindow window(VideoMode(w, h), "Snake Game!");
sf::Font myFont;
if (!myFont.loadFromFile("images/font.ttf"))
{
}
Texture t1, t2, t3, t4;
t1.loadFromFile("images/white.png");
t2.loadFromFile("images/red.png");
t3.loadFromFile("images/green.png");
t4.loadFromFile("images/over.png");
Sprite sprite1(t1);
Sprite sprite2(t2);
Sprite sprite3(t3);
Sprite sover(t4);
Clock clock;
float timer = 0, delay = 0.1;
f.x = 10;
f.y = 10;
sf::Text score;
score.setFont(myFont);
score.setFillColor(sf::Color::Blue);
score.setStyle(sf::Text::Regular);
score.setString("Score");
score.setCharacterSize(25);
score.setPosition(25, 130); //25,130
sf::Text scoreCurrent;
scoreCurrent.setFont(myFont);
scoreCurrent.setFillColor(sf::Color::Green);
scoreCurrent.setStyle(sf::Text::Regular);
scoreCurrent.setCharacterSize(25);
scoreCurrent.setPosition(45, 175);
sf::RectangleShape scoreRectangle;
scoreRectangle.setSize(sf::Vector2f(100, 50));
scoreRectangle.setOutlineThickness(1);
scoreRectangle.setOutlineColor(sf::Color::White);
scoreRectangle.setFillColor(sf::Color::Black);
scoreRectangle.setPosition(25, 165);
while (window.isOpen())
{
window.setFramerateLimit(speed);
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
if (gameIsAlive)
{
if (Keyboard::isKeyPressed(Keyboard::Left) && dir != 2)
dir = 1;
if (Keyboard::isKeyPressed(Keyboard::Right) && dir != 1)
dir = 2;
if (Keyboard::isKeyPressed(Keyboard::Up) && dir != 0)
dir = 3;
if (Keyboard::isKeyPressed(Keyboard::Down) && dir != 3)
dir = 0;
if (timer > delay)
{
timer = 0;
Tick();
}
////// draw ///////
window.clear();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
sprite1.setPosition(i * size, j * size);
window.draw(sprite1);
}
for (int i = 0; i < num; i++)
{
sprite2.setPosition(s[i].x * size, s[i].y * size);
window.draw(sprite2);
}
sprite3.setPosition(f.x * size, f.y * size);
window.draw(sprite3);
window.display();
}
else
{
////// draw ///////
window.clear();
std::stringstream s;
s << fscore;
scoreCurrent.setString(s.str());
window.draw(sover);
window.draw(score);
window.draw(scoreRectangle);
window.draw(scoreCurrent);
window.display();
}
}
return 0;
}
0 Comments