Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Window.hpp>
- #include <SFML/Graphics.hpp>
- #include <conio.h>
- #include <string>
- using namespace std;
- int width = 500;
- int height = 500;
- int direction;
- int x = width / 2;
- int y = (height / 2);
- int x_of_enemy = 400;
- int y_of_enemy = 0;
- int random_value_x;
- bool touched = false;
- int fails = 0;
- int main()
- {
- srand(time(NULL));
- string fails_converted_to_a_string = to_string(fails);
- sf::Font arial_font;
- arial_font.loadFromFile("arial.ttf"); //This file has to be in project folder on the computer!
- sf::Text some_text;
- some_text.setFont(arial_font);
- some_text.setString("Fails: ");
- some_text.setFillColor(sf::Color::Black);
- some_text.setCharacterSize(16);
- //Points display
- sf::Text some_other_text;
- some_other_text.setFont(arial_font);
- some_other_text.setFillColor(some_text.getFillColor());
- some_other_text.setString("Hello");
- some_other_text.setCharacterSize(16);
- some_other_text.setPosition(40, 0);
- sf::RenderWindow window(sf::VideoMode(width, height), "Generic name for a window");
- window.setFramerateLimit(70);
- window.setVerticalSyncEnabled(true);
- sf::RectangleShape player(sf::Vector2f(40, 40));
- player.setFillColor(sf::Color::Cyan);
- sf::RectangleShape enemy(sf::Vector2f(40, 40));
- enemy.setFillColor(sf::Color::Red);
- enemy.setPosition(x_of_enemy, y_of_enemy);
- sf::Event event;
- while (window.isOpen())
- {
- window.clear(sf::Color::White);
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::KeyPressed)
- {
- if (event.key.code == sf::Keyboard::A)
- {
- direction = 1;
- }
- else if (event.key.code == sf::Keyboard::D)
- {
- direction = 2;
- }
- }
- }
- for (int k = 0; k < 41; k++)
- {
- for (int p = 0; p < 41; p++)
- {
- if ((x + p) == (x_of_enemy + k))
- {
- for (int c = 0; c < 41; c++)
- {
- for (int w = 0; w < 41; w++)
- {
- if ((y + w) == (y_of_enemy + c))
- {
- touched = true;
- }
- }
- }
- }
- }
- }
- fails_converted_to_a_string = to_string(fails);
- some_other_text.setString(fails_converted_to_a_string);
- if (touched)
- {
- x_of_enemy = rand() % width;
- y_of_enemy = 0;
- touched = false;
- }
- else if (y_of_enemy > (height / 2) + 40)
- {
- x_of_enemy = rand() % (width - 40);
- y_of_enemy = 0;
- fails += 1;
- }
- if (y_of_enemy < height) //Activate gravity for enemy
- y_of_enemy += 5;
- if (direction == 1)
- if (x < 0)
- {
- ;
- }
- else
- x -= 10;
- else if (direction == 2)
- if ((x + 40) > width)
- {
- ;
- }
- else
- x += 10;
- player.setPosition(x, y);
- enemy.setPosition(x_of_enemy, y_of_enemy);
- window.draw(player);
- window.draw(enemy);
- window.draw(some_text);
- window.draw(some_other_text);
- window.display();
- }
- int pause; cin >> pause; //Pause the program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement