Advertisement
Guest User

Untitled

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