Advertisement
Guest User

Untitled

a guest
Jan 1st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include "funkcje.hpp"
  6.  
  7. class Kulka
  8. {
  9. public:
  10. sf::RectangleShape rect;
  11. float bottom, left, right, top;
  12. int x, score=0;
  13. Kulka(sf::Vector2f position, sf::Vector2f size, sf::Color color)
  14. {
  15. rect.setPosition(position);
  16. rect.setSize(size);
  17. rect.setFillColor(color);
  18. }
  19. ~Kulka()
  20. {
  21. std::cout << "wlaczyl sie destruktor !!!" << std::endl;
  22. }
  23.  
  24. void update()
  25. {
  26. this->rect.move(0, -0.1f);
  27. bottom = rect.getPosition().y + rect.getSize().y;
  28. left = rect.getPosition().x;
  29. right = rect.getPosition().x + rect.getSize().x;
  30. top = rect.getPosition().y;
  31. if (bottom < 0)
  32. {
  33. //delete this; TUTAJ POWINNO BY USUWANIE OBIEKTU, ALE W TEN SPOSÓB ZASTOSOWANE NIE CHCE DZIALAC.
  34. }
  35.  
  36. }
  37.  
  38. int getx()
  39. {
  40. int x;
  41. x = rect.getPosition().x;
  42. return x;
  43. }
  44. int gety()
  45. {
  46. int y;
  47. y = rect.getPosition().y;
  48. return y;
  49. }
  50. };
  51.  
  52.  
  53.  
  54.  
  55. class Player
  56. {
  57. public:
  58. sf::RectangleShape rect;
  59. float bottom, left, right, top;
  60. int x, score=0;
  61. Player(sf::Vector2f position, sf::Vector2f size, sf::Color color)
  62. {
  63. rect.setPosition(position);
  64. rect.setSize(size);
  65. rect.setFillColor(color);
  66. }
  67.  
  68. void update()
  69. {
  70. bottom = rect.getPosition().y + rect.getSize().y;
  71. left = rect.getPosition().x;
  72. right = rect.getPosition().x + rect.getSize().x;
  73. top = rect.getPosition().y;
  74.  
  75. if (top > 480)
  76. rect.setPosition(sf::Vector2f(left,0));
  77. if (right < 0)
  78. rect.setPosition(sf::Vector2f(640,top));
  79. if (left > 640)
  80. rect.setPosition(sf::Vector2f(0,top));
  81. if (bottom < 0)
  82. rect.setPosition(sf::Vector2f(left,480));
  83.  
  84. }
  85.  
  86. void fall()
  87. {
  88. this->rect.move(0, 0.06f);
  89. top = rect.getPosition().y;
  90. if (top > 480)
  91. {
  92. x=rand() % 640;
  93. rect.setPosition(sf::Vector2f(x,0));
  94. }
  95.  
  96. }
  97.  
  98. int getx()
  99. {
  100. int x;
  101. x = rect.getPosition().x;
  102. return x;
  103. }
  104. int gety()
  105. {
  106. int y;
  107. y = rect.getPosition().y;
  108. return y;
  109. }
  110. };
  111.  
  112.  
  113.  
  114. void Collision(Player &player1, Player &object, sf::Text &atext)
  115. {
  116. if(player1.getx() + 20 < object.getx() ||
  117. player1.getx() > object.getx() + 20 ||
  118. player1.gety() + 20 < object.gety() ||
  119. player1.gety() > object.gety() + 20)
  120. {
  121. }
  122. else
  123. {
  124. player1.score=player1.score+1;
  125. std::cout << "Wynik: " << player1.score << "" << std::endl;
  126. object.rect.setPosition(0,470);
  127. std::ostringstream ss;
  128. ss << player1.score;
  129. atext.setString(ss.str());
  130. }
  131. }
  132.  
  133. int main()
  134. {
  135. //okno i gracze
  136. sf::RenderWindow Window( sf::VideoMode( 640, 480, 32 ), "Kurs" );
  137. Player p1(Player(sf::Vector2f(300,200), sf::Vector2f(20,20), sf::Color::Red));
  138. Player pp[10] = (Player(sf::Vector2f(639,479), sf::Vector2f(20,20), sf::Color::Blue));
  139. Kulka *a[100] ;
  140. int ilekulek=0;
  141.  
  142.  
  143.  
  144. //napis z wynikiem
  145.  
  146. sf::Font font;
  147. if (!font.loadFromFile("cour.ttf"))
  148. {
  149. std::cout << "czcionka sie nie wczytala" << std::endl;
  150. }
  151. sf::Text atext;
  152. atext.setFont(font);
  153. std::ostringstream ss;
  154. ss << p1.score;
  155. atext.setString(ss.str());
  156. atext.setCharacterSize(50);
  157. atext.setStyle(sf::Text::Bold);
  158. atext.setColor(sf::Color::White);
  159. atext.setPosition(530,20);
  160.  
  161.  
  162. // zmienne globalne
  163. int obj=0, x, czas;
  164. double time_reset=1;
  165. sf::Clock clock;
  166.  
  167. while( Window.isOpen() )
  168. {
  169. sf::Event Event;
  170. while( Window.pollEvent( Event ) )
  171. {
  172. if( Event.type == sf::Event::Closed )
  173. Window.close();
  174. if( Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Escape )
  175. {
  176. a[ilekulek] = new Kulka(Kulka(sf::Vector2f(p1.rect.getPosition().x+5,p1.rect.getPosition().y-5), sf::Vector2f(10,10), sf::Color::White));
  177. ilekulek++;
  178. }
  179.  
  180. }
  181. for(int i=0;i<10;i++)
  182. {
  183. pp[i].update();
  184. Collision(p1, pp[i], atext);
  185. }
  186. p1.update();
  187.  
  188. sf::Time elapsed1 = clock.getElapsedTime();
  189. if(elapsed1.asSeconds() > 1)
  190. {
  191. clock.restart();
  192. if(obj < 10)
  193. {
  194. x=rand() % 640;
  195. obj++;
  196. }
  197. }
  198.  
  199. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  200. p1.rect.move(0.3f, 0);
  201. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  202. p1.rect.move(-0.3f, 0);
  203. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  204. p1.rect.move(0, -0.3f);
  205. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  206. p1.rect.move(0, 0.3f);
  207.  
  208. Window.clear(sf::Color(20,130,55));
  209. Window.draw(atext);
  210. for(int i=0;i<ilekulek;i++)
  211. {
  212.  
  213. a[i]->update();
  214. Window.draw(a[i]->rect);
  215. }
  216.  
  217. Window.draw(p1.rect);
  218. for(int i=0; i<obj;i++)
  219. {
  220. Window.draw(pp[i].rect);
  221. pp[i].fall();
  222. }
  223.  
  224. Window.display();
  225. sf::sleep(sf::milliseconds(1));
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement