Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include "PlayGame_State.h"
  2. #include <cstdlib>
  3. #include <sstream>
  4.  
  5. PlayGame_State::PlayGame_State(sf::RectangleShape &rectShape, sf::RectangleShape &ballShape, sf::Font &f): ballSpeed(240.f, 240.f),
  6. playerSpeed(350), bricksPosition(5,5), amountOfBricks(40), player_score(0)
  7. {
  8. if(!f.loadFromFile("martell.otf"))
  9. std::cout << "break in playgame" << std::endl;
  10. font = f;
  11.  
  12. rectShape.setSize(sf::Vector2f(90,20));
  13. rectShape.setFillColor(sf::Color::Green);
  14. rectShape.setOutlineColor(sf::Color::Red);
  15. rectShape.setOutlineThickness(3);
  16. rectShape.setPosition(360, 540);
  17.  
  18. player = &rectShape;
  19.  
  20. ballShape.setSize(sf::Vector2f(23,23));
  21. ballShape.setFillColor(sf::Color(255,140,0));
  22. ballShape.setOutlineColor(sf::Color::Green);
  23. ballShape.setOutlineThickness(1);
  24. ballShape.setPosition(380, player->getPosition().y - player->getSize().y);
  25.  
  26. ball = &ballShape;
  27.  
  28. bricks[0].setFillColor(sf::Color(rand()%256, rand()%256, rand()%256));
  29. bricks[0].setPosition(bricksPosition.x, bricksPosition.y);
  30. bricks[0].setSize(sf::Vector2f(70,25));
  31. bricks[0].setOutlineColor(sf::Color::Green);
  32. bricks[0].setOutlineThickness(1);
  33. v_bricks.push_back(&bricks[0]);
  34.  
  35. for(int i=1;i<40;++i)
  36. {
  37. bricks[i].setSize(sf::Vector2f(70,25));
  38. bricksPosition.x+= bricks[i].getSize().x + 8;
  39. if(bricksPosition.x + bricks[i].getSize().x >= 798)
  40. {
  41. bricksPosition.y += 30;
  42. bricksPosition.x = 5;
  43. }
  44.  
  45. bricks[i].setFillColor(sf::Color(rand()%256, rand()%256, rand()%256));
  46. bricks[i].setPosition(bricksPosition.x, bricksPosition.y);
  47. bricks[i].setOutlineColor(sf::Color::Green);
  48. bricks[i].setOutlineThickness(1);
  49. v_bricks.push_back(&bricks[i]);
  50. }
  51. score.setString("0 Points");
  52. score.setColor(sf::Color(123, 231 ,123, 128));
  53. score.setCharacterSize(55);
  54. score.setPosition(270, 300);
  55. score.setFont(font);
  56. }
  57.  
  58.  
  59. bool PlayGame_State::collisionBallPlayer(const sf::RectangleShape &player_rect, const sf::RectangleShape &ball_rect) const
  60. {
  61. sf::FloatRect r1 = player_rect.getGlobalBounds();
  62. sf::FloatRect r2 = ball_rect.getGlobalBounds();
  63. return r1.intersects(r2);
  64. }
  65.  
  66. void PlayGame_State::movePlayer()
  67. {
  68. sf::Time delta = clock_for_ps.restart();
  69. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  70. player->move(-playerSpeed * delta.asSeconds(), 0);
  71. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  72. player->move(playerSpeed * delta.asSeconds(), 0);
  73. checkPosition();
  74. }
  75.  
  76. void PlayGame_State::moveBall()
  77. {
  78. sf::Time delta = clock_for_bs.restart();
  79. if(collisionBallPlayer(*player, *ball))
  80. ballSpeed.y = -ballSpeed.y;
  81. //left wall
  82. else if(ball->getPosition().x - 5 <= 0 ) ballSpeed.x = -ballSpeed.x;
  83. //top wall
  84. else if(ball->getPosition().y -5 <=0 ) ballSpeed.y = -ballSpeed.y;
  85. //right wall
  86. else if(ball->getPosition().x+ball->getSize().x >= 800) ballSpeed.x = -ballSpeed.x;
  87. //lost
  88. else if(ball->getPosition().y >= player->getPosition().y + player->getSize().y+15)
  89. ball->setPosition(player->getPosition().x + 20, player->getPosition().y);
  90.  
  91. ball->move(ballSpeed.x * delta.asSeconds(), ballSpeed.y * delta.asSeconds());
  92.  
  93. }
  94.  
  95. void PlayGame_State::checkPosition()
  96. {
  97. if(player->getPosition().x -5 <= 0)
  98. player->setPosition(7, player->getPosition().y);
  99. else if(player->getPosition().x + player->getSize().x >= 800)
  100. player->setPosition(player->getPosition().x - 5, player->getPosition().y);
  101. }
  102.  
  103. bool PlayGame_State::collisionBallBrick()
  104. {
  105. for(int i=0;i<amountOfBricks;++i)
  106. {
  107. if((ball->getPosition().x >= v_bricks[i]->getPosition().x && ball->getPosition().x <= v_bricks[i]->getPosition().x + v_bricks[i]->getSize().x) &&
  108. (ball->getPosition().y + ball->getSize().y > v_bricks[i]->getPosition().y && ball->getPosition().y < v_bricks[i]->getPosition().y + v_bricks[i]->getSize().y))
  109. {
  110. v_bricks.erase(v_bricks.begin() + i);
  111. amountOfBricks-=1;
  112. ballSpeed.y = -ballSpeed.y;
  113. updateScore();
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119.  
  120. void PlayGame_State::updateScore()
  121. {
  122. player_score+=100;
  123. std::stringstream str;
  124. str << player_score << " Points";
  125.  
  126. score.setString(str.str());
  127. }
  128.  
  129. void PlayGame_State::draw(sf::RenderWindow *window) const
  130. {
  131. window->draw(*player);
  132. window->draw(*ball);
  133. window->draw(score);
  134. for(int i=0;i<amountOfBricks;++i){
  135. window->draw(*v_bricks[i]);
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement