Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "PlayGame_State.h"
- #include <cstdlib>
- #include <sstream>
- PlayGame_State::PlayGame_State(sf::RectangleShape &rectShape, sf::RectangleShape &ballShape, sf::Font &f): ballSpeed(240.f, 240.f),
- playerSpeed(350), bricksPosition(5,5), amountOfBricks(40), player_score(0)
- {
- if(!f.loadFromFile("martell.otf"))
- std::cout << "break in playgame" << std::endl;
- font = f;
- rectShape.setSize(sf::Vector2f(90,20));
- rectShape.setFillColor(sf::Color::Green);
- rectShape.setOutlineColor(sf::Color::Red);
- rectShape.setOutlineThickness(3);
- rectShape.setPosition(360, 540);
- player = &rectShape;
- ballShape.setSize(sf::Vector2f(23,23));
- ballShape.setFillColor(sf::Color(255,140,0));
- ballShape.setOutlineColor(sf::Color::Green);
- ballShape.setOutlineThickness(1);
- ballShape.setPosition(380, player->getPosition().y - player->getSize().y);
- ball = &ballShape;
- bricks[0].setFillColor(sf::Color(rand()%256, rand()%256, rand()%256));
- bricks[0].setPosition(bricksPosition.x, bricksPosition.y);
- bricks[0].setSize(sf::Vector2f(70,25));
- bricks[0].setOutlineColor(sf::Color::Green);
- bricks[0].setOutlineThickness(1);
- v_bricks.push_back(&bricks[0]);
- for(int i=1;i<40;++i)
- {
- bricks[i].setSize(sf::Vector2f(70,25));
- bricksPosition.x+= bricks[i].getSize().x + 8;
- if(bricksPosition.x + bricks[i].getSize().x >= 798)
- {
- bricksPosition.y += 30;
- bricksPosition.x = 5;
- }
- bricks[i].setFillColor(sf::Color(rand()%256, rand()%256, rand()%256));
- bricks[i].setPosition(bricksPosition.x, bricksPosition.y);
- bricks[i].setOutlineColor(sf::Color::Green);
- bricks[i].setOutlineThickness(1);
- v_bricks.push_back(&bricks[i]);
- }
- score.setString("0 Points");
- score.setColor(sf::Color(123, 231 ,123, 128));
- score.setCharacterSize(55);
- score.setPosition(270, 300);
- score.setFont(font);
- }
- bool PlayGame_State::collisionBallPlayer(const sf::RectangleShape &player_rect, const sf::RectangleShape &ball_rect) const
- {
- sf::FloatRect r1 = player_rect.getGlobalBounds();
- sf::FloatRect r2 = ball_rect.getGlobalBounds();
- return r1.intersects(r2);
- }
- void PlayGame_State::movePlayer()
- {
- sf::Time delta = clock_for_ps.restart();
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- player->move(-playerSpeed * delta.asSeconds(), 0);
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- player->move(playerSpeed * delta.asSeconds(), 0);
- checkPosition();
- }
- void PlayGame_State::moveBall()
- {
- sf::Time delta = clock_for_bs.restart();
- if(collisionBallPlayer(*player, *ball))
- ballSpeed.y = -ballSpeed.y;
- //left wall
- else if(ball->getPosition().x - 5 <= 0 ) ballSpeed.x = -ballSpeed.x;
- //top wall
- else if(ball->getPosition().y -5 <=0 ) ballSpeed.y = -ballSpeed.y;
- //right wall
- else if(ball->getPosition().x+ball->getSize().x >= 800) ballSpeed.x = -ballSpeed.x;
- //lost
- else if(ball->getPosition().y >= player->getPosition().y + player->getSize().y+15)
- ball->setPosition(player->getPosition().x + 20, player->getPosition().y);
- ball->move(ballSpeed.x * delta.asSeconds(), ballSpeed.y * delta.asSeconds());
- }
- void PlayGame_State::checkPosition()
- {
- if(player->getPosition().x -5 <= 0)
- player->setPosition(7, player->getPosition().y);
- else if(player->getPosition().x + player->getSize().x >= 800)
- player->setPosition(player->getPosition().x - 5, player->getPosition().y);
- }
- bool PlayGame_State::collisionBallBrick()
- {
- for(int i=0;i<amountOfBricks;++i)
- {
- if((ball->getPosition().x >= v_bricks[i]->getPosition().x && ball->getPosition().x <= v_bricks[i]->getPosition().x + v_bricks[i]->getSize().x) &&
- (ball->getPosition().y + ball->getSize().y > v_bricks[i]->getPosition().y && ball->getPosition().y < v_bricks[i]->getPosition().y + v_bricks[i]->getSize().y))
- {
- v_bricks.erase(v_bricks.begin() + i);
- amountOfBricks-=1;
- ballSpeed.y = -ballSpeed.y;
- updateScore();
- return true;
- }
- }
- return false;
- }
- void PlayGame_State::updateScore()
- {
- player_score+=100;
- std::stringstream str;
- str << player_score << " Points";
- score.setString(str.str());
- }
- void PlayGame_State::draw(sf::RenderWindow *window) const
- {
- window->draw(*player);
- window->draw(*ball);
- window->draw(score);
- for(int i=0;i<amountOfBricks;++i){
- window->draw(*v_bricks[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement