Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. #include <SFML/Audio/Sound.hpp>
  4. #include <SFML/Audio/SoundBuffer.hpp>
  5. #include <iostream>
  6. #include <windows.h>
  7. #include <SFML/Window/Mouse.hpp>
  8. #include <sstream>
  9.  
  10. using namespace sf;
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15. srand(time(0));
  16.  
  17. SoundBuffer buffer;
  18. buffer.loadFromFile("censor.wav");
  19. Sound censor;
  20. censor.setBuffer(buffer);
  21.  
  22. RenderWindow app(VideoMode(520, 450), "Arkanoid!");
  23. app.setFramerateLimit(60);
  24.  
  25. Texture t1,t2,t3,t4,t5;
  26. t1.loadFromFile("images/block01.png");
  27. t2.loadFromFile("images/poopole.jpg");
  28. t3.loadFromFile("images/ball.png");
  29. t4.loadFromFile("images/paddle.png");
  30.  
  31. Sprite sBackground(t2), sBall(t3), sPaddle(t4);
  32. sPaddle.setPosition(300,440);
  33.  
  34. Sprite block[100];
  35.  
  36. int n=0;
  37. for (int i=1;i<=10;i++)
  38. {
  39. for (int j=1;j<=10;j++)
  40. {
  41. block[n].setTexture(t1);
  42. block[n].setPosition(i*43,j*20);
  43. n++;
  44. }
  45. }
  46.  
  47. float dx=6, dy=5;
  48. float x=300, y=300;
  49.  
  50. int score = 0;
  51.  
  52. Font arial;
  53. arial.loadFromFile("arial.ttf");
  54.  
  55. stringstream ssScore;
  56. ssScore << "Score: " << score;
  57.  
  58. Text scoreDisplayed;
  59. scoreDisplayed.setCharacterSize(30);
  60. scoreDisplayed.setPosition({5,220});
  61. scoreDisplayed.setFont(arial);
  62. //ustawiamy napis i konwertujemy z inta na stringa
  63. scoreDisplayed.setString(ssScore.str());
  64. scoreDisplayed.setFillColor(sf::Color::Blue);
  65.  
  66. while (app.isOpen())
  67. {
  68. Event e;
  69. while (app.pollEvent(e))
  70. {
  71. if (e.type == Event::Closed)
  72. {
  73. Sleep(1500);
  74. app.close();
  75. }
  76. }
  77.  
  78. x+=dx;
  79.  
  80. for (int i=0;i<n;i++)
  81. {
  82. //upewniamy sie ze wspolrzedne pileczki nie beda jednoczesnie dotykac innego bloczka i ustawiamy jego rozmiar na 1/1
  83. //nastepnie po przejsciu kilku nastepnych ifow znow ustalamy koordynaty pileczki i automatycznie przywracamy do defaultowego rozmiaru
  84. if ( FloatRect(x+3,y+3,1,1).intersects(block[i].getGlobalBounds()) )
  85. {
  86. block[i].setPosition(-100,0);
  87. dx=-dx;
  88. score++;
  89. //upewniamy sie ze tu bedzie pusty score i wywalamy nowy
  90. ssScore.str("");
  91. ssScore << "Score: " << score;
  92. scoreDisplayed.setString(ssScore.str());
  93. }
  94. }
  95.  
  96.  
  97.  
  98. y+=dy;
  99.  
  100. for (int i=0;i<n;i++)
  101. {
  102. if ( FloatRect(x+3,y+3,1,1).intersects(block[i].getGlobalBounds()) )
  103. {
  104. block[i].setPosition(-100,0);
  105. dy=-dy;
  106. score++;
  107. ssScore.str("");
  108. ssScore << "Score: " << score;
  109. scoreDisplayed.setString(ssScore.str());
  110. }
  111. }
  112.  
  113. //odbijanie od scian
  114. if (x<0 || x>520)
  115. {
  116. dx=-dx;
  117. }
  118.  
  119. if (y<0 || y>450)
  120. {
  121. dy=-dy;
  122. }
  123.  
  124. //pileczka podaza za myszka, pobieramy relatywna pozycje myszki
  125. sPaddle.setPosition(Mouse::getPosition(app).x, sPaddle.getPosition().y);
  126.  
  127.  
  128. if ( FloatRect(x,y,12,12).intersects(sPaddle.getGlobalBounds()) )
  129. {
  130. dy=-(rand()%5+2);
  131. censor.play();
  132. }
  133.  
  134. sBall.setPosition(x,y);
  135.  
  136.  
  137. app.clear();
  138. app.draw(sBackground);
  139. app.draw(sBall);
  140. app.draw(sPaddle);
  141. app.draw(scoreDisplayed);
  142.  
  143. for (int i=0;i<n;i++)
  144. {
  145. app.draw(block[i]);
  146. }
  147.  
  148. app.display();
  149.  
  150. }
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement