Advertisement
Guest User

snake game with sfml

a guest
Sep 4th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.40 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <ctime>
  5. #include <vector>
  6. #include <sstream>
  7. #include <math.h>
  8. /*
  9. head following direction,*/
  10. enum Direction {UP, DOWN ,PLACE_HOLDER, LEFT , RIGHT};
  11. Direction dir=LEFT;
  12. Direction prevDir=DOWN;
  13. bool gameOver = false;
  14. float tileSize = 40.0;
  15. sf::Vector2i screenSize(20,20);
  16. std::vector <std::vector <float>> tailPos (20,std::vector<float>(20));
  17. sf::Vector2i headPosition((screenSize.x/2),(screenSize.y/2));
  18. sf::Vector2i fruitPosition;
  19. int tailLenght = 0;
  20.  
  21. bool AreSame(double a, double b)
  22. {
  23.     return fabs(a - b) < 0.001;
  24. }
  25.  
  26. void setup(float &gameSpeed)
  27. {
  28.     gameSpeed=0.35;
  29.     tailLenght=0;
  30.     headPosition.x=screenSize.x/2;
  31.     headPosition.y=screenSize.y/2;
  32.         for(int i = 0 ; i< screenSize.x; i++)
  33.     {
  34.  
  35.         for(int k = 0 ; k< screenSize.y; k++)
  36.         {
  37.  
  38.  
  39.  
  40.                 tailPos[i][k]=0;
  41.  
  42.  
  43.         }
  44.     }
  45.     fruitPosition.x= rand()%screenSize.x;
  46.     fruitPosition.y= rand()%screenSize.y;
  47.     gameOver = false;
  48.  
  49.  
  50. }
  51.  
  52. void logic(float &gameSpeed)
  53. {
  54.  
  55.  
  56.  
  57.  
  58.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
  59.     {
  60.  
  61.         dir=UP;
  62.  
  63.     }
  64.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
  65.     {
  66.         dir=DOWN;
  67.  
  68.     }
  69.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
  70.     {
  71.         dir= RIGHT;
  72.  
  73.     }
  74.  
  75.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  76.     {
  77.  
  78.         dir=LEFT;
  79.  
  80.     }
  81.  
  82.  
  83.     if(headPosition.x == fruitPosition.x && headPosition.y == fruitPosition.y)
  84.     {
  85.         tailLenght++;
  86.         gameSpeed-=(0.04*gameSpeed);
  87.         fruitPosition.x= rand()%screenSize.x;
  88.         fruitPosition.y= rand()%screenSize.y;
  89.  
  90.         while((tailPos[fruitPosition.x][fruitPosition.y]!=0))
  91.         {
  92.             fruitPosition.x= rand()%screenSize.x;
  93.             fruitPosition.y= rand()%screenSize.y;
  94.  
  95.         }
  96.     }
  97.     if(headPosition.x<0||headPosition.x>19||headPosition.y<0||headPosition.y>19||(tailPos[headPosition.x][headPosition.y]>=1))
  98.     {
  99.  
  100.         gameOver=true;
  101.  
  102.  
  103.     }
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110. void update(sf::Sprite &head)
  111. {
  112.  
  113.  
  114.  
  115.     for(int i = 0 ; i< screenSize.x; i++)
  116.     {
  117.  
  118.         for(int k = 0 ; k< screenSize.y; k++)
  119.         {
  120.  
  121.             if(tailPos[i][k]>=1)
  122.             {
  123.  
  124.                 tailPos[i][k]--;
  125.  
  126.             }
  127.         }
  128.     }
  129.  
  130.     switch (dir){
  131.         case UP:
  132.         case DOWN:
  133.     tailPos[headPosition.x][headPosition.y]=tailLenght+0.1;
  134.         break;
  135.         case LEFT :
  136.         case RIGHT:
  137.      tailPos[headPosition.x][headPosition.y]=tailLenght+0.2;
  138.  
  139.     }
  140.     if(std::abs(prevDir-dir)==1){
  141.  
  142.  
  143.         dir = prevDir;
  144.  
  145.     }
  146.     switch (dir) {
  147.     case UP:
  148.          head.setTextureRect(sf::IntRect(0,0,40,40));
  149.         headPosition.y--;
  150.         break;
  151.     case DOWN:
  152.          head.setTextureRect(sf::IntRect(40,0,40,40));
  153.         headPosition.y++;
  154.         break;
  155.     case LEFT:
  156.          head.setTextureRect(sf::IntRect(80,0,40,40));
  157.         headPosition.x--;
  158.         break;
  159.     case RIGHT:
  160.         head.setTextureRect(sf::IntRect(120,0,40,40));
  161.         headPosition.x++;
  162.         break;
  163.  
  164.  
  165.  
  166.  
  167.     }
  168. prevDir=dir;
  169.  
  170.  
  171. //std::cout<<headPosition.x<<" "<<headPosition.y<<std::endl;
  172.  
  173. }
  174.  
  175.  
  176.  
  177. void draw (sf::RenderWindow &window,sf::Sprite &head,sf::Sprite &tail,sf::Sprite &fruit,sf::Text text)
  178. {
  179.  
  180.  
  181.     for(int i = 0 ; i< screenSize.x; i++)
  182.     {
  183.  
  184.         for(int k = 0 ; k< screenSize.y; k++)
  185.         {
  186.  
  187.  
  188.  
  189.             if(i == headPosition.x && k == headPosition.y)
  190.             {
  191.  
  192.  
  193.                 head.setPosition(headPosition.x*tileSize,headPosition.y*tileSize);
  194.  
  195.                 window.draw(head);
  196.  
  197.             }
  198.             if(i == fruitPosition.x && k == fruitPosition.y)
  199.             {
  200.                 fruit.setPosition(fruitPosition.x*tileSize,fruitPosition.y*tileSize);
  201.                 window.draw(fruit);
  202.  
  203.             }
  204.  
  205.             if(tailPos[i][k]>=1)
  206.             {
  207.                 float shit = tailPos[i][k]-floor(tailPos[i][k]);
  208.  
  209.                 if(AreSame(shit,0.1)){
  210.                     tail.setTextureRect(sf::IntRect(0,0,40,40));
  211.  
  212.                 }else {
  213.  
  214.                  tail.setTextureRect(sf::IntRect(40,0,40,40));
  215.                 }
  216.                 tail.setPosition(i*tileSize,k*tileSize);
  217.                 window.draw(tail);
  218.  
  219.             }
  220.  
  221.  
  222.  
  223.  
  224.         }
  225.  
  226.     }
  227.  
  228. //the prompt that appears
  229.     if(gameOver==true)
  230.     {
  231.         std::string yel;
  232.         std::stringstream sstream;
  233.         sstream<<tailLenght;
  234.         yel=sstream.str();
  235.         text.setColor(sf::Color::Black);
  236.         text.setPosition(200,300);
  237.         text.setString("game over your score is :" +yel+"\n press Escape to exit \n press R to restart");
  238.         window.draw(text);
  239.  
  240.  
  241.     }
  242.  
  243.  
  244. }
  245.  
  246.  
  247.  
  248.  
  249. int main()
  250. {
  251.     srand(time(0));
  252.     sf::Clock clock;
  253.     float elapsedTime;
  254.     float gameSpeed=0.35;
  255.     sf::RenderWindow Window;
  256.     Window.create(sf::VideoMode(800,800),"nigra",sf::Style::None);
  257.     sf::Font font;
  258.     font.loadFromFile("DejaVuSansMono.ttf");
  259.     sf::Text text;
  260.     text.setFont(font);
  261.  
  262.  
  263.     sf::Texture textureGrass;
  264.     textureGrass.loadFromFile("Grass.png");
  265.     sf::Sprite Grass(textureGrass);
  266.  
  267.     sf::Texture textureHead;
  268.     textureHead.loadFromFile("snakeHead.png");
  269.     sf::Sprite Head(textureHead);
  270.  
  271.     sf::Texture textureTail;
  272.     textureTail.loadFromFile("tail.png");
  273.     sf::Sprite Tail(textureTail);
  274.  
  275.     sf::Texture textureFruit;
  276.     textureFruit.loadFromFile("fruit.png");
  277.     sf::Sprite Fruit(textureFruit);
  278.     setup(gameSpeed);
  279.     sf::Event event;
  280.     while(Window.isOpen())
  281.     {
  282.  
  283.  
  284.  
  285.         Window.draw(Grass);
  286. //the code that makes the prompt appear
  287.   if(gameOver){
  288.  
  289.         while(Window.waitEvent(event)){
  290.  
  291.                 if(event.key.code==sf::Keyboard::Escape){
  292.                     Window.close();
  293.  
  294.                 }
  295.                 if(event.key.code==sf::Keyboard::R){
  296.                     setup(gameSpeed);
  297.  
  298.                     break;
  299.                 }
  300.  
  301.             }
  302.         }
  303. //-----------------------
  304.         std::cout<<gameSpeed<<std::endl;
  305.  
  306.         logic(gameSpeed);
  307.         elapsedTime= clock.getElapsedTime().asSeconds();
  308.  
  309.         if(elapsedTime>=gameSpeed)
  310.         {
  311.             update(Head);
  312.  
  313.             clock.restart();
  314.             elapsedTime=0;
  315.  
  316.         }
  317.  
  318.  
  319.  
  320.         draw(Window,Head,Tail,Fruit,text);
  321.  
  322.         Window.display();
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.         Window.clear();
  331.  
  332.  
  333.  
  334.  
  335.  
  336.     }
  337.  
  338.  
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement