Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <stdlib.h>
- #include <ctime>
- #include <vector>
- #include <sstream>
- #include <math.h>
- /*
- head following direction,*/
- enum Direction {UP, DOWN ,PLACE_HOLDER, LEFT , RIGHT};
- Direction dir=LEFT;
- Direction prevDir=DOWN;
- bool gameOver = false;
- float tileSize = 40.0;
- sf::Vector2i screenSize(20,20);
- std::vector <std::vector <float>> tailPos (20,std::vector<float>(20));
- sf::Vector2i headPosition((screenSize.x/2),(screenSize.y/2));
- sf::Vector2i fruitPosition;
- int tailLenght = 0;
- bool AreSame(double a, double b)
- {
- return fabs(a - b) < 0.001;
- }
- void setup(float &gameSpeed)
- {
- gameSpeed=0.35;
- tailLenght=0;
- headPosition.x=screenSize.x/2;
- headPosition.y=screenSize.y/2;
- for(int i = 0 ; i< screenSize.x; i++)
- {
- for(int k = 0 ; k< screenSize.y; k++)
- {
- tailPos[i][k]=0;
- }
- }
- fruitPosition.x= rand()%screenSize.x;
- fruitPosition.y= rand()%screenSize.y;
- gameOver = false;
- }
- void logic(float &gameSpeed)
- {
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
- {
- dir=UP;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
- {
- dir=DOWN;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
- {
- dir= RIGHT;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- {
- dir=LEFT;
- }
- if(headPosition.x == fruitPosition.x && headPosition.y == fruitPosition.y)
- {
- tailLenght++;
- gameSpeed-=(0.04*gameSpeed);
- fruitPosition.x= rand()%screenSize.x;
- fruitPosition.y= rand()%screenSize.y;
- while((tailPos[fruitPosition.x][fruitPosition.y]!=0))
- {
- fruitPosition.x= rand()%screenSize.x;
- fruitPosition.y= rand()%screenSize.y;
- }
- }
- if(headPosition.x<0||headPosition.x>19||headPosition.y<0||headPosition.y>19||(tailPos[headPosition.x][headPosition.y]>=1))
- {
- gameOver=true;
- }
- }
- void update(sf::Sprite &head)
- {
- for(int i = 0 ; i< screenSize.x; i++)
- {
- for(int k = 0 ; k< screenSize.y; k++)
- {
- if(tailPos[i][k]>=1)
- {
- tailPos[i][k]--;
- }
- }
- }
- switch (dir){
- case UP:
- case DOWN:
- tailPos[headPosition.x][headPosition.y]=tailLenght+0.1;
- break;
- case LEFT :
- case RIGHT:
- tailPos[headPosition.x][headPosition.y]=tailLenght+0.2;
- }
- if(std::abs(prevDir-dir)==1){
- dir = prevDir;
- }
- switch (dir) {
- case UP:
- head.setTextureRect(sf::IntRect(0,0,40,40));
- headPosition.y--;
- break;
- case DOWN:
- head.setTextureRect(sf::IntRect(40,0,40,40));
- headPosition.y++;
- break;
- case LEFT:
- head.setTextureRect(sf::IntRect(80,0,40,40));
- headPosition.x--;
- break;
- case RIGHT:
- head.setTextureRect(sf::IntRect(120,0,40,40));
- headPosition.x++;
- break;
- }
- prevDir=dir;
- //std::cout<<headPosition.x<<" "<<headPosition.y<<std::endl;
- }
- void draw (sf::RenderWindow &window,sf::Sprite &head,sf::Sprite &tail,sf::Sprite &fruit,sf::Text text)
- {
- for(int i = 0 ; i< screenSize.x; i++)
- {
- for(int k = 0 ; k< screenSize.y; k++)
- {
- if(i == headPosition.x && k == headPosition.y)
- {
- head.setPosition(headPosition.x*tileSize,headPosition.y*tileSize);
- window.draw(head);
- }
- if(i == fruitPosition.x && k == fruitPosition.y)
- {
- fruit.setPosition(fruitPosition.x*tileSize,fruitPosition.y*tileSize);
- window.draw(fruit);
- }
- if(tailPos[i][k]>=1)
- {
- float shit = tailPos[i][k]-floor(tailPos[i][k]);
- if(AreSame(shit,0.1)){
- tail.setTextureRect(sf::IntRect(0,0,40,40));
- }else {
- tail.setTextureRect(sf::IntRect(40,0,40,40));
- }
- tail.setPosition(i*tileSize,k*tileSize);
- window.draw(tail);
- }
- }
- }
- //the prompt that appears
- if(gameOver==true)
- {
- std::string yel;
- std::stringstream sstream;
- sstream<<tailLenght;
- yel=sstream.str();
- text.setColor(sf::Color::Black);
- text.setPosition(200,300);
- text.setString("game over your score is :" +yel+"\n press Escape to exit \n press R to restart");
- window.draw(text);
- }
- }
- int main()
- {
- srand(time(0));
- sf::Clock clock;
- float elapsedTime;
- float gameSpeed=0.35;
- sf::RenderWindow Window;
- Window.create(sf::VideoMode(800,800),"nigra",sf::Style::None);
- sf::Font font;
- font.loadFromFile("DejaVuSansMono.ttf");
- sf::Text text;
- text.setFont(font);
- sf::Texture textureGrass;
- textureGrass.loadFromFile("Grass.png");
- sf::Sprite Grass(textureGrass);
- sf::Texture textureHead;
- textureHead.loadFromFile("snakeHead.png");
- sf::Sprite Head(textureHead);
- sf::Texture textureTail;
- textureTail.loadFromFile("tail.png");
- sf::Sprite Tail(textureTail);
- sf::Texture textureFruit;
- textureFruit.loadFromFile("fruit.png");
- sf::Sprite Fruit(textureFruit);
- setup(gameSpeed);
- sf::Event event;
- while(Window.isOpen())
- {
- Window.draw(Grass);
- //the code that makes the prompt appear
- if(gameOver){
- while(Window.waitEvent(event)){
- if(event.key.code==sf::Keyboard::Escape){
- Window.close();
- }
- if(event.key.code==sf::Keyboard::R){
- setup(gameSpeed);
- break;
- }
- }
- }
- //-----------------------
- std::cout<<gameSpeed<<std::endl;
- logic(gameSpeed);
- elapsedTime= clock.getElapsedTime().asSeconds();
- if(elapsedTime>=gameSpeed)
- {
- update(Head);
- clock.restart();
- elapsedTime=0;
- }
- draw(Window,Head,Tail,Fruit,text);
- Window.display();
- Window.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement