Advertisement
Felanpro

Snake Graphics Version

Jul 2nd, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4.  
  5. using namespace std;
  6.  
  7. unsigned int width = 500;
  8. unsigned int height = 500;
  9.  
  10.  
  11. int main()
  12. {
  13.     sf::RenderWindow window(sf::VideoMode(width, height), "In progress");
  14.     window.setFramerateLimit(5);
  15.  
  16.     srand(time(NULL));
  17.  
  18.     sf::RectangleShape player(sf::Vector2f(50, 50));
  19.     player.setFillColor(sf::Color::Green);
  20.  
  21.     sf::RectangleShape fruit(sf::Vector2f(50, 50));
  22.     fruit.setFillColor(sf::Color::Red);
  23.  
  24.     sf::RectangleShape body[100];
  25.     for (int x = 0; x < 100; x++)
  26.     {
  27.         body[x].setSize(sf::Vector2f(50, 50));
  28.         body[x].setFillColor(sf::Color::Magenta);
  29.     }
  30.     int tailX[100];
  31.     int tailY[100];
  32.  
  33.     int direction = 2;
  34.     int up = 1;
  35.     int right = 2;
  36.     int down = 3;
  37.     int left = 4;
  38.     int playerX = 250;
  39.     int playerY = 250;
  40.     int tailN = -1;
  41.     bool defeat = false;
  42.     int fruitX = 300;
  43.     int fruitY = 400;
  44.     int randomX = 0;
  45.     int randomY = 0;
  46.  
  47.     sf::Event event;
  48.     while (window.isOpen() && defeat == false)
  49.     {
  50.  
  51.         while (window.pollEvent(event))
  52.         {
  53.             if (event.type == sf::Event::Closed)
  54.             {
  55.                 window.close();
  56.             }
  57.  
  58.             if (event.type == sf::Event::KeyPressed)
  59.             {
  60.                 if (event.key.code == sf::Keyboard::Right)
  61.                 {
  62.                     direction = right;
  63.                 }
  64.                 else if (event.key.code == sf::Keyboard::Left)
  65.                 {
  66.                     direction = left;
  67.                 }
  68.                 else if (event.key.code == sf::Keyboard::Up)
  69.                 {
  70.                     direction = up;
  71.                 }
  72.                 else if (event.key.code == sf::Keyboard::Down)
  73.                 {
  74.                     direction = down;
  75.                 }
  76.                 else if (event.key.code == sf::Keyboard::Space)
  77.                 {
  78.                     cout << "The snake grew" << endl;
  79.                     tailN++;
  80.                 }
  81.             }
  82.         }  
  83.  
  84.         if (tailN >= 0)
  85.         {
  86.  
  87.             for (int a = tailN; a > 0; a--)
  88.             {
  89.                 tailX[a] = tailX[a - 1];
  90.                 tailY[a] = tailY[a - 1];
  91.             }
  92.  
  93.             tailX[0] = playerX;
  94.             tailY[0] = playerY;
  95.         }
  96.  
  97.         if (direction == right)
  98.         {
  99.             playerX += 50;
  100.         }
  101.         else if (direction == left)
  102.         {
  103.             playerX -= 50;
  104.         }
  105.         else if (direction == up)
  106.         {
  107.             playerY -= 50;
  108.         }
  109.         else if (direction == down)
  110.         {
  111.             playerY += 50;
  112.         }
  113.  
  114.         window.clear(sf::Color::White); //Clear
  115.  
  116.         //Check collision with fruit
  117.         if (playerX == fruitX && playerY == fruitY)
  118.         {
  119.             cout << "The snake just ate a fruit" << endl;
  120.             tailN++;
  121.             bool collision_with_body = true;
  122.             bool collision_with_head = true;
  123.  
  124.             while (collision_with_body == true && collision_with_head == true)
  125.             {
  126.                 randomX = rand() % 10;
  127.                 randomY = rand() % 10;
  128.                 randomX = randomX * 50;
  129.                 randomY = randomY * 50;
  130.                 if (playerX == randomX && playerY == randomY)
  131.                     collision_with_head = true;
  132.                 else
  133.                     collision_with_head = false;
  134.  
  135.                 for (int u = tailN; u >= 0; u--)
  136.                 {
  137.                     if (tailX[u] == randomX && tailY[u] == randomY)
  138.                     {
  139.                         collision_with_body = true;
  140.                         break;
  141.                     }
  142.                     else
  143.                         collision_with_body = false;
  144.                 }
  145.             }
  146.             fruitX = randomX;
  147.             fruitY = randomY;
  148.         }
  149.  
  150.         for (int x = 0; x < 500; x += 50)
  151.         {
  152.             for (int y = 0; y < 500; y += 50)
  153.             {
  154.                 if (x == playerX && y == playerY)
  155.                 {
  156.                     player.setPosition(x, y);
  157.                     window.draw(player);
  158.                 }
  159.                 else if (x == fruitX && y == fruitY)
  160.                 {
  161.                     fruit.setPosition(x, y);
  162.                     window.draw(fruit);
  163.                 }
  164.                 else
  165.                 {
  166.                     for (int m = tailN; m >= 0; m--)
  167.                     {
  168.                         if (x == tailX[m] && y == tailY[m])
  169.                         {
  170.                             body[m].setPosition(x, y);
  171.                             window.draw(body[m]);
  172.                         }
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.  
  178.         //Check defeat
  179.         for (int q = tailN; q >= 0; q--)
  180.         {
  181.             if (playerX == tailX[q] && playerY == tailY[q])
  182.             {
  183.                 defeat = true;
  184.             }
  185.         }
  186.  
  187.         cout << tailN << endl;
  188.         window.display(); //Display
  189.     }
  190.  
  191.     window.close();
  192.  
  193.     int pause; cin >> pause;
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement