Advertisement
Felanpro

Avoid rectangle game

Mar 23rd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Window.hpp>
  4. #include <SFML/Graphics.hpp>
  5. #include <conio.h>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. int width = 500;
  11. int height = 500;
  12.  
  13. int direction;
  14. int x = width / 2;
  15. int y = (height / 2);
  16.  
  17. int x_of_enemy = 400;
  18. int y_of_enemy = 0;
  19.  
  20. int random_value_x;
  21.  
  22. bool touched = false;
  23. int fails = 0;
  24.  
  25. int main()
  26. {
  27.     srand(time(NULL));
  28.     string fails_converted_to_a_string = to_string(fails);
  29.     sf::Font arial_font;
  30.     arial_font.loadFromFile("arial.ttf"); //This file has to be in project folder on the computer!
  31.     sf::Text some_text;
  32.     some_text.setFont(arial_font);
  33.     some_text.setString("Fails: ");
  34.     some_text.setFillColor(sf::Color::Black);
  35.     some_text.setCharacterSize(16);
  36.  
  37.     //Points display
  38.     sf::Text some_other_text;
  39.     some_other_text.setFont(arial_font);
  40.     some_other_text.setFillColor(some_text.getFillColor());
  41.     some_other_text.setString("Hello");
  42.     some_other_text.setCharacterSize(16);
  43.     some_other_text.setPosition(40, 0);
  44.  
  45.     sf::RenderWindow window(sf::VideoMode(width, height), "Generic name for a window");
  46.     window.setFramerateLimit(70);
  47.     window.setVerticalSyncEnabled(true);
  48.  
  49.     sf::RectangleShape player(sf::Vector2f(40, 40));
  50.     player.setFillColor(sf::Color::Cyan);
  51.  
  52.     sf::RectangleShape enemy(sf::Vector2f(40, 40));
  53.     enemy.setFillColor(sf::Color::Red);
  54.     enemy.setPosition(x_of_enemy, y_of_enemy);
  55.  
  56.     sf::Event event;
  57.     while (window.isOpen())
  58.     {
  59.         window.clear(sf::Color::White);
  60.         while (window.pollEvent(event))
  61.         {
  62.             if (event.type == sf::Event::Closed)
  63.                 window.close();
  64.  
  65.             if (event.type == sf::Event::KeyPressed)
  66.             {
  67.                 if (event.key.code == sf::Keyboard::A)
  68.                 {
  69.                     direction = 1;
  70.                 }
  71.                 else if (event.key.code == sf::Keyboard::D)
  72.                 {
  73.                     direction = 2;
  74.                 }
  75.             }
  76.         }
  77.  
  78.         for (int k = 0; k < 41; k++)
  79.         {
  80.             for (int p = 0; p < 41; p++)
  81.             {
  82.                 if ((x + p) == (x_of_enemy + k))
  83.                 {
  84.                     for (int c = 0; c < 41; c++)
  85.                     {
  86.                         for (int w = 0; w < 41; w++)
  87.                         {
  88.                             if ((y + w) == (y_of_enemy + c))
  89.                             {
  90.                                 touched = true;
  91.                             }
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         fails_converted_to_a_string = to_string(fails);
  98.         some_other_text.setString(fails_converted_to_a_string);
  99.  
  100.         if (touched)
  101.         {
  102.             x_of_enemy = rand() % width;
  103.             y_of_enemy = 0;
  104.             touched = false;
  105.         }
  106.         else if (y_of_enemy > (height / 2) + 40)
  107.         {
  108.             x_of_enemy = rand() % (width - 40);
  109.             y_of_enemy = 0;
  110.             fails += 1;
  111.         }
  112.  
  113.         if (y_of_enemy < height) //Activate gravity for enemy
  114.             y_of_enemy += 5;
  115.  
  116.         if (direction == 1)
  117.             if (x < 0)
  118.             {
  119.                 ;
  120.             }
  121.             else
  122.                 x -= 10;
  123.         else if (direction == 2)
  124.             if ((x + 40) > width)
  125.             {
  126.                 ;
  127.             }
  128.             else
  129.                 x += 10;
  130.  
  131.         player.setPosition(x, y);
  132.         enemy.setPosition(x_of_enemy, y_of_enemy);
  133.  
  134.         window.draw(player);
  135.         window.draw(enemy);
  136.         window.draw(some_text);
  137.         window.draw(some_other_text);
  138.  
  139.         window.display();
  140.     }
  141.  
  142.     int pause; cin >> pause; //Pause the program
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement