Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include "funkcje.hpp"
- class Kulka
- {
- public:
- sf::RectangleShape rect;
- float bottom, left, right, top;
- int x, score=0;
- Kulka(sf::Vector2f position, sf::Vector2f size, sf::Color color)
- {
- rect.setPosition(position);
- rect.setSize(size);
- rect.setFillColor(color);
- }
- ~Kulka()
- {
- std::cout << "wlaczyl sie destruktor !!!" << std::endl;
- }
- void update()
- {
- this->rect.move(0, -0.1f);
- bottom = rect.getPosition().y + rect.getSize().y;
- left = rect.getPosition().x;
- right = rect.getPosition().x + rect.getSize().x;
- top = rect.getPosition().y;
- if (bottom < 0)
- {
- //delete this; TUTAJ POWINNO BY USUWANIE OBIEKTU, ALE W TEN SPOSÓB ZASTOSOWANE NIE CHCE DZIALAC.
- }
- }
- int getx()
- {
- int x;
- x = rect.getPosition().x;
- return x;
- }
- int gety()
- {
- int y;
- y = rect.getPosition().y;
- return y;
- }
- };
- class Player
- {
- public:
- sf::RectangleShape rect;
- float bottom, left, right, top;
- int x, score=0;
- Player(sf::Vector2f position, sf::Vector2f size, sf::Color color)
- {
- rect.setPosition(position);
- rect.setSize(size);
- rect.setFillColor(color);
- }
- void update()
- {
- bottom = rect.getPosition().y + rect.getSize().y;
- left = rect.getPosition().x;
- right = rect.getPosition().x + rect.getSize().x;
- top = rect.getPosition().y;
- if (top > 480)
- rect.setPosition(sf::Vector2f(left,0));
- if (right < 0)
- rect.setPosition(sf::Vector2f(640,top));
- if (left > 640)
- rect.setPosition(sf::Vector2f(0,top));
- if (bottom < 0)
- rect.setPosition(sf::Vector2f(left,480));
- }
- void fall()
- {
- this->rect.move(0, 0.06f);
- top = rect.getPosition().y;
- if (top > 480)
- {
- x=rand() % 640;
- rect.setPosition(sf::Vector2f(x,0));
- }
- }
- int getx()
- {
- int x;
- x = rect.getPosition().x;
- return x;
- }
- int gety()
- {
- int y;
- y = rect.getPosition().y;
- return y;
- }
- };
- void Collision(Player &player1, Player &object, sf::Text &atext)
- {
- if(player1.getx() + 20 < object.getx() ||
- player1.getx() > object.getx() + 20 ||
- player1.gety() + 20 < object.gety() ||
- player1.gety() > object.gety() + 20)
- {
- }
- else
- {
- player1.score=player1.score+1;
- std::cout << "Wynik: " << player1.score << "" << std::endl;
- object.rect.setPosition(0,470);
- std::ostringstream ss;
- ss << player1.score;
- atext.setString(ss.str());
- }
- }
- int main()
- {
- //okno i gracze
- sf::RenderWindow Window( sf::VideoMode( 640, 480, 32 ), "Kurs" );
- Player p1(Player(sf::Vector2f(300,200), sf::Vector2f(20,20), sf::Color::Red));
- Player pp[10] = (Player(sf::Vector2f(639,479), sf::Vector2f(20,20), sf::Color::Blue));
- Kulka *a[100] ;
- int ilekulek=0;
- //napis z wynikiem
- sf::Font font;
- if (!font.loadFromFile("cour.ttf"))
- {
- std::cout << "czcionka sie nie wczytala" << std::endl;
- }
- sf::Text atext;
- atext.setFont(font);
- std::ostringstream ss;
- ss << p1.score;
- atext.setString(ss.str());
- atext.setCharacterSize(50);
- atext.setStyle(sf::Text::Bold);
- atext.setColor(sf::Color::White);
- atext.setPosition(530,20);
- // zmienne globalne
- int obj=0, x, czas;
- double time_reset=1;
- sf::Clock clock;
- while( Window.isOpen() )
- {
- sf::Event Event;
- while( Window.pollEvent( Event ) )
- {
- if( Event.type == sf::Event::Closed )
- Window.close();
- if( Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Escape )
- {
- a[ilekulek] = new Kulka(Kulka(sf::Vector2f(p1.rect.getPosition().x+5,p1.rect.getPosition().y-5), sf::Vector2f(10,10), sf::Color::White));
- ilekulek++;
- }
- }
- for(int i=0;i<10;i++)
- {
- pp[i].update();
- Collision(p1, pp[i], atext);
- }
- p1.update();
- sf::Time elapsed1 = clock.getElapsedTime();
- if(elapsed1.asSeconds() > 1)
- {
- clock.restart();
- if(obj < 10)
- {
- x=rand() % 640;
- obj++;
- }
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- p1.rect.move(0.3f, 0);
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- p1.rect.move(-0.3f, 0);
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- p1.rect.move(0, -0.3f);
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- p1.rect.move(0, 0.3f);
- Window.clear(sf::Color(20,130,55));
- Window.draw(atext);
- for(int i=0;i<ilekulek;i++)
- {
- a[i]->update();
- Window.draw(a[i]->rect);
- }
- Window.draw(p1.rect);
- for(int i=0; i<obj;i++)
- {
- Window.draw(pp[i].rect);
- pp[i].fall();
- }
- Window.display();
- sf::sleep(sf::milliseconds(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement