Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. //main
  2. #include <SFML/Graphics.hpp>
  3. #include <iostream>
  4. #include <vector>
  5. #include "GameObjects.h"
  6.  
  7. using namespace sf;
  8.  
  9.  
  10. class GameFunctions {
  11. public:
  12. bool isMenu = 1;
  13. void chekBall(GameObjects &a, GameObjects &b)//a=ball,b=wood
  14. {
  15. if (b.life && (((((a.x >= b.x) && (a.x <= b.x + 80)) && ((b.y <= a.y) && (b.y + 30 >= a.y)))) || ((((a.x + 20 >= b.x) && (a.x + 20 <= b.x + 80)) && ((b.y <= a.y) && (b.y + 30 >= a.y)))) || ((((a.x + 20 >= b.x) && (a.x + 20 <= b.x + 80)) && ((b.y <= a.y + 20) && (b.y + 30 >= a.y + 20)))) || ((((a.x >= b.x) && (a.x <= b.x + 80)) && ((b.y <= a.y + 20) && (b.y + 30 >= a.y + 20))))))
  16. {
  17. b.life = false;
  18. a.dx *= -1;
  19. a.dy *= -1;
  20. };
  21. };
  22. void menu(RenderWindow & window)
  23. {
  24. Image menutab1;
  25. menutab1.loadFromFile("images/tab1.png");
  26. menutab1.createMaskFromColor(Color(255,255,255));
  27. Texture menuTexture1, menuBackground;
  28. menuTexture1.loadFromImage(menutab1);
  29. menuBackground.loadFromFile("images/menu.jpg");
  30. Sprite menu1(menuTexture1), menuBg(menuBackground);
  31.  
  32. int menuNum = 0;
  33. menu1.setPosition(100, 30);
  34. menuBg.setPosition(0, 0);
  35. while (isMenu)
  36. {
  37. Event event;
  38. menu1.setColor(Color::White);
  39. menuNum = 0;
  40. window.clear(Color(0, 0, 0));
  41. while (window.pollEvent(event))
  42. {
  43. if ((event.type == Event::Closed) || (event.key.code == Keyboard::Escape)) window.close();
  44. }
  45. if (IntRect(100, 30, 400, 100).contains(Mouse::getPosition(window))) { menu1.setColor(Color::Blue); menuNum = 1; }
  46. if (Mouse::isButtonPressed(Mouse::Left))
  47. {
  48. if (menuNum == 1)isMenu = false;
  49. }
  50. window.draw(menuBg);
  51. window.draw(menu1);
  52. window.display();
  53. }
  54. }
  55. };
  56.  
  57.  
  58. int main()
  59. {
  60. GameFunctions obj1;
  61. Clock gametime;
  62. sf::Clock clock1;
  63. bool lose = 0;
  64. bool replay = 0;
  65. String str ="board_and_tools.png";
  66. String *ptrstr = &str;
  67. Font font;//шрифт
  68. font.loadFromFile("1.ttf");
  69. Text text("", font, 100);
  70. Text text1("", font, 50);
  71. Text text2("", font, 50);
  72. text.setStyle(sf::Text::Bold);
  73. text1.setStyle(sf::Text::Bold);
  74. text2.setStyle(sf::Text::Bold);
  75.  
  76. RenderWindow window(VideoMode(1024, 768), "arkanoid");
  77. obj1.menu(window);
  78. Texture bg;
  79. bg.loadFromFile("images/bg.png");
  80. Sprite bgsprite;
  81. bgsprite.setTexture(bg);
  82.  
  83. bool start = false;
  84. std::vector <GameObjects> arrwoods;
  85. for (int i = 0; i <= 17; i++)
  86. {
  87. int b = 0;
  88. if (i > 8)b = 1;
  89. int a = i % 9;
  90. arrwoods.push_back(GameObjects(70 + (100 * a), 90 + (40 * b), 80, 30, 80, 70, *ptrstr));
  91. }
  92. GameObjects MyBoard(450, 650, 150, 20, 40, 40, *ptrstr);
  93. GameObjects Ball(450, 250, 21, 20, 40, 70, *ptrstr); Ball.dx = -0.2; Ball.dy = 0.2; Ball.boostx = 1;
  94. while (window.isOpen())
  95. {
  96. sf::Time elapsed1 = clock1.getElapsedTime();
  97. float sec = elapsed1.asSeconds();
  98.  
  99. float time = gametime.getElapsedTime().asMicroseconds();
  100. gametime.restart();
  101. time = time / 850;
  102.  
  103. Event event;
  104. while (window.pollEvent(event))
  105. {
  106. if ((event.type == Event::Closed) || (event.key.code == Keyboard::Escape)) window.close();
  107. }
  108. if ((Keyboard::isKeyPressed(Keyboard::R)) && (lose))
  109. {
  110. replay = true;
  111. std::cout << "R" << std::endl;
  112. }
  113. if ((Keyboard::isKeyPressed(Keyboard::Space)) && (sec > 0.2)) start = !start , clock1.restart();;
  114. //std::cout << elapsed1.asSeconds() << " sec=" << sec << std::endl;
  115.  
  116. if ((!lose)&&(start)) {
  117.  
  118. if (((Keyboard::isKeyPressed(Keyboard::Right)) || (Keyboard::isKeyPressed(Keyboard::D))) && (MyBoard.x < 810))
  119. {
  120. if (Ball.y >= 630)//boost speed ball for 3 sec
  121. {
  122. Ball.boostx = 1.8;
  123. Ball.boost = true;
  124. }
  125. MyBoard.dx = 0.3;
  126. MyBoard.x += MyBoard.dx*time;
  127. MyBoard.sprite.setPosition(MyBoard.x, MyBoard.y);
  128. std::cout << "->" << std::endl;
  129. }
  130.  
  131. if (((Keyboard::isKeyPressed(Keyboard::Left)) || (Keyboard::isKeyPressed(Keyboard::A))) && (MyBoard.x > 61))
  132. {
  133. if (Ball.y >= 630)//boost speed ball for 3 sec
  134. {
  135. Ball.boostx = 1.8;
  136. Ball.boost = true;
  137. }
  138. MyBoard.dx = -0.3;
  139. MyBoard.x += MyBoard.dx*time;
  140. MyBoard.sprite.setPosition(MyBoard.x, MyBoard.y);
  141. std::cout << "<-" << std::endl;
  142. }
  143. }
  144. MyBoard.sprite.setPosition(MyBoard.x, MyBoard.y);
  145. //keyboard
  146.  
  147. if ((Ball.boost) && (Ball.y <= 200))
  148. {
  149. Ball.boost = false;
  150. Ball.boostx = 1.0;
  151. }
  152.  
  153. if ((!lose)&&(start)) {
  154. if (Ball.x >= 940)Ball.dx = -0.3;
  155. if (Ball.x <= 60)Ball.dx = 0.3;
  156. if (Ball.y <= 80)Ball.dy = 0.3;
  157. if (Ball.y >= 635)
  158. {
  159. Ball.dy = -0.3;
  160.  
  161. if ((Ball.y >= 635) && ((MyBoard.x - 20 > (Ball.x)) || (MyBoard.x + 150) < (Ball.x)))lose = 1;
  162. }
  163. int slow = 1.2;
  164. Ball.x += Ball.dx*time*Ball.boostx / slow;
  165. Ball.y += Ball.dy*time*Ball.boosty / slow;
  166. }
  167. Ball.sprite.setPosition(Ball.x, Ball.y);
  168. //ball
  169.  
  170. for (int i = 0; i <= 17; i++)
  171. {
  172. obj1.chekBall(Ball, arrwoods[i]);
  173. }
  174.  
  175. window.clear();
  176. window.draw(bgsprite);
  177. if (!lose)
  178. {
  179. for (int i = 0; i <= 17; i++)
  180. {
  181. bool k1 = arrwoods[i].life;
  182. if (k1)window.draw(arrwoods[i].sprite);
  183. }
  184. window.draw(Ball.sprite);
  185. window.draw(MyBoard.sprite);
  186. }
  187. if (!start)
  188. {
  189. text1.setString("Game paused, press key 'space'");
  190. text1.setPosition(140, 299);
  191. window.draw(text1);
  192. }
  193. if (lose) {
  194. text2.setString("press key 'R' for new game");
  195. text2.setPosition(249, 499);
  196. window.draw(text2);
  197.  
  198. text.setString("game over");
  199. text.setPosition(299, 299);
  200. window.draw(text);
  201.  
  202. }
  203. if (replay)
  204. {
  205. for (int i = 0; i <= 17; i++)
  206. {
  207. arrwoods[i].life = 1;
  208. }
  209. MyBoard.x = 450;
  210. Ball.x = 450;
  211. Ball.y = 250;
  212. Ball.dx =-0.3;
  213. Ball.dy = 0.3;
  214. lose = 0;
  215. start = 0;
  216. replay = !replay;
  217. }
  218. window.display();
  219.  
  220. }
  221.  
  222. }
  223. //game obj.сpp
  224. #include "GameObjects.h"
  225.  
  226.  
  227.  
  228.  
  229. GameObjects::GameObjects(double X, double Y, double W, double H, double ix, double iy, const sf::String &f)
  230. {
  231. dx = 0; dy = 0; speed = 0, boostx = 1, boosty = 1;
  232. w = W; h = H;
  233. File = f;
  234. life = true;
  235. boost = false;
  236. image.loadFromFile("images/" + File);
  237. texture.loadFromImage(image);
  238. sprite.setTexture(texture);
  239. x = X; y = Y;
  240. sprite.setPosition(x, y);
  241. sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
  242. }
  243.  
  244. GameObjects::GameObjects()
  245. {
  246. x = 0, y = 0, dx = 0, dy = 0, speed = 0, ix = 0, iy = 0, w = 0, h = 0;
  247. life = true;
  248. boost = false;
  249. image.loadFromFile("images/bg.png");
  250. texture.loadFromImage(image);
  251. sprite.setTexture(texture);
  252. sprite.setPosition(x, y);
  253. sprite.setPosition(x, y);
  254. sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
  255. }
  256.  
  257. //game obj.cpp
  258. #include "GameObjects.h"
  259.  
  260.  
  261.  
  262.  
  263. GameObjects::GameObjects(double X, double Y, double W, double H, double ix, double iy, const sf::String &f)
  264. {
  265. dx = 0; dy = 0; speed = 0, boostx = 1, boosty = 1;
  266. w = W; h = H;
  267. File = f;
  268. life = true;
  269. boost = false;
  270. image.loadFromFile("images/" + File);
  271. texture.loadFromImage(image);
  272. sprite.setTexture(texture);
  273. x = X; y = Y;
  274. sprite.setPosition(x, y);
  275. sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
  276. }
  277.  
  278. GameObjects::GameObjects()
  279. {
  280. x = 0, y = 0, dx = 0, dy = 0, speed = 0, ix = 0, iy = 0, w = 0, h = 0;
  281. life = true;
  282. boost = false;
  283. image.loadFromFile("images/bg.png");
  284. texture.loadFromImage(image);
  285. sprite.setTexture(texture);
  286. sprite.setPosition(x, y);
  287. sprite.setPosition(x, y);
  288. sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement