Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.39 KB | None | 0 0
  1. #ifndef CONSTANTS_H_INCLUDED
  2. #define CONSTANTS_H_INCLUDED
  3.  
  4. #define WIN_WIDTH 800
  5. #define WIN_HEIGHT 600
  6.  
  7. #define BALL_SPEED_X 4
  8. #define BALL_SPEED_MAX_Y 4
  9.  
  10. #define BAR_FROM_BORDERS 40
  11. #define BAR_SPEED 7
  12.  
  13. #define MAX_FPS 60
  14.  
  15.  
  16.  
  17. #endif // CONSTANTS_H_INCLUDED
  18.  
  19. #ifndef PSPRITE_H
  20. #define PSPRITE_H
  21.  
  22. /**
  23. *This class inherits the class
  24. *Sprite, and defines some
  25. *attributes to keep track of
  26. *a Sprite's geometry.
  27. */
  28.  
  29. #include <SFML/Graphics.hpp>
  30. #include "CollisionManager.h"
  31. #include <string>
  32. #include <vector>
  33.  
  34. class PSprite : public sf::Sprite
  35. {
  36. public:
  37. PSprite(std::string&, const float& x, const float& y);
  38.  
  39. const float& getDirectionX();
  40. const float& getDirectionY();
  41. const sf::Vector2f& getLowerRight(); //Getters
  42. const float& getWidth();
  43. const float& getHeight();
  44.  
  45. void setPosition(float,float);
  46. void setDirectionX(const float&);
  47. void setDirectionY(const float&); //Setters
  48. void setLowerRight();
  49.  
  50. bool strictlyHigherThan(PSprite&);
  51. bool strictlyLowerThan(PSprite&);
  52. bool strictlyLeftOf(PSprite&); //Relative position verification
  53. bool strictlyRightOf(PSprite&);
  54.  
  55.  
  56. PSprite* nextMove(); //Returns a ptr on the sprite after its next movement
  57.  
  58. protected:
  59. sf::Texture texture; //The sprite's texture
  60. CollisionManager collision; //The Collision manager
  61. float directionX; //The direction on X axis
  62. float directionY; //The direction on Y axis
  63. float width; //The sprite's width
  64. float height; //The Sprite's height
  65. sf::Vector2f lowerRight; //Lower right corner's coordinates
  66.  
  67. };
  68.  
  69. #endif // PSPRITE_H
  70.  
  71. #include "PSprite.h"
  72. #include <string>
  73.  
  74. using namespace sf;
  75. using namespace std;
  76.  
  77. PSprite::PSprite(std::string& filename,const float& x, const float& y)
  78. {
  79. texture.loadFromFile(filename); //Loading the texture
  80.  
  81. setTexture(texture); //Setting the sprite
  82. setPosition(x,y);
  83.  
  84. width=texture.getSize().x; //Setting width and height
  85. height=texture.getSize().y;
  86.  
  87. //setLowerRight(); //Setting the lower right corner
  88.  
  89. directionX=0; //Setting the directions to default values
  90. directionY=0;
  91. }
  92.  
  93. const float& PSprite::getDirectionX()
  94. {
  95. return directionX;
  96. }
  97.  
  98. const float& PSprite::getDirectionY()
  99. {
  100. return directionY;
  101. }
  102.  
  103. const Vector2f& PSprite::getLowerRight()
  104. {
  105. return lowerRight;
  106. }
  107.  
  108. const float& PSprite::getWidth()
  109. {
  110. return width;
  111. }
  112.  
  113. const float& PSprite::getHeight()
  114. {
  115. return height;
  116. }
  117.  
  118.  
  119. void PSprite::setPosition(float x,float y)
  120. {
  121. Sprite::setPosition(x,y);
  122. setLowerRight(); //We set the lower right corner each time we change the position
  123. }
  124. void PSprite::setDirectionX(const float& dirX)
  125. {
  126. directionX=dirX;
  127. }
  128.  
  129. void PSprite::setDirectionY(const float& dirY)
  130. {
  131. directionY=dirY;
  132. }
  133.  
  134. void PSprite::setLowerRight()
  135. {
  136. lowerRight.x=getPosition().x+width;
  137. lowerRight.y=getPosition().y+height;
  138. }
  139.  
  140.  
  141. bool PSprite::strictlyHigherThan(PSprite& spr)
  142. {
  143. return lowerRight.y<spr.getPosition().y; //If the lowest point of this is higher than spr's highest
  144. }
  145.  
  146. bool PSprite::strictlyLowerThan(PSprite& spr)
  147. {
  148. return spr.strictlyHigherThan(*this); //If spr is strictly higher than this
  149. }
  150.  
  151. bool PSprite::strictlyLeftOf(PSprite& spr)
  152. {
  153. return lowerRight.x<spr.getPosition().x; //If the rightest point of this is on spr's leftest left
  154. }
  155.  
  156. bool PSprite::strictlyRightOf(PSprite& spr)
  157. {
  158. return spr.strictlyLeftOf(*this); //If spr is strictly on the left of this
  159. }
  160.  
  161.  
  162. PSprite* PSprite::nextMove() //Returns a pointer on this' position after its next move
  163. {
  164. PSprite *next=new PSprite(*this);
  165.  
  166. next->setPosition(getPosition().x+directionX,getPosition().y+directionY);
  167.  
  168. return next;
  169. }
  170.  
  171. #ifndef COLLISIONMANAGER_H
  172. #define COLLISIONMANAGER_H
  173.  
  174.  
  175. /**
  176. *Class that manages collisions (!)
  177. *between 2 PSprites or a PSprite
  178. *and the screen borders
  179. */
  180.  
  181. #include <SFML/Graphics.hpp>
  182. #include "Constants.h"
  183. class PSprite;
  184.  
  185. class CollisionManager
  186. {
  187. public:
  188. CollisionManager();
  189.  
  190. bool CollBorderV(PSprite&); //Managing collision between a PSprite and the vertical borders of the screen
  191. bool CollBorderH(PSprite&); //Managing collision between a PSprite and the horizontal borders of the screen
  192.  
  193. bool Coll2PSprite(PSprite&,PSprite&); //Managing the collision between two PSprites
  194.  
  195. };
  196.  
  197. #endif // COLLISIONMANAGER_H
  198.  
  199. #include "CollisionManager.h"
  200. #include "Ball.h"
  201. #include "PSprite.h"
  202. #include <iostream>
  203.  
  204. using namespace sf;
  205.  
  206. CollisionManager::CollisionManager()
  207. {
  208.  
  209. }
  210.  
  211.  
  212. bool CollisionManager::Coll2PSprite(PSprite& spr1, PSprite& spr2) //Managing the collision between two PSprites
  213. {
  214. PSprite *nxt=spr1.nextMove();
  215.  
  216. if(nxt->strictlyHigherThan(spr2) || nxt->strictlyLowerThan(spr2) || nxt->strictlyLeftOf(spr2) || nxt->strictlyRightOf(spr2)) //If they don't collide after spr1's next move
  217. {
  218. delete nxt;
  219. return 0;
  220. }
  221.  
  222. if(spr1.strictlyHigherThan(spr2))
  223. {
  224. spr1.setPosition(spr1.getPosition().x,spr2.getPosition().y-spr1.getHeight());
  225. //spr1.setLowerRight();
  226. }
  227.  
  228. else if(spr1.strictlyLowerThan(spr2))
  229. {
  230. spr1.setPosition(spr1.getPosition().x,spr2.getLowerRight().y);
  231. //spr1.setLowerRight();
  232. }
  233.  
  234. if(spr1.strictlyLeftOf(spr2))
  235. {
  236. spr1.setPosition(spr2.getPosition().x-spr1.getWidth(),spr1.getPosition().y);
  237. //spr1.setLowerRight();
  238. }
  239.  
  240. else if(spr1.strictlyRightOf(spr2))
  241. {
  242. spr1.setPosition(spr2.getLowerRight().x,spr1.getPosition().y);
  243. //spr1.setLowerRight();
  244. }
  245.  
  246. delete nxt;
  247. return 1;
  248. }
  249.  
  250. bool CollisionManager::CollBorderH(PSprite& spr)
  251. {
  252. PSprite *nxt=spr.nextMove();
  253.  
  254. if(nxt->getPosition().y>0 && nxt->getLowerRight().y<WIN_HEIGHT) //If there is no collision
  255. {
  256. delete nxt;
  257. return 0;
  258. }
  259.  
  260. if(nxt->getPosition().y<0) //If there is a collision with the upper border
  261. {
  262. spr.setPosition(spr.getPosition().x,0);
  263. //spr.setLowerRight();
  264. }
  265.  
  266. else if(nxt->getLowerRight().y>WIN_HEIGHT) //If there is a collision with the lower border
  267. {
  268. spr.setPosition(spr.getPosition().x,WIN_HEIGHT-spr.getHeight());
  269. //spr.setLowerRight();
  270. }
  271.  
  272. delete nxt;
  273. return 1;
  274. }
  275.  
  276. bool CollisionManager::CollBorderV(PSprite& spr) //Manages collisions between a PSPrite and vertical borders
  277. {
  278. PSprite *nxt=spr.nextMove();
  279.  
  280. if(nxt->getPosition().x>0 && nxt->getLowerRight().x<WIN_WIDTH)
  281. {
  282. delete nxt;
  283. return 0;
  284. }
  285.  
  286. if(nxt->getPosition().x<0)
  287. {
  288. spr.setPosition(0,spr.getPosition().y);
  289. }
  290.  
  291. else if(nxt->getLowerRight().x>WIN_WIDTH)
  292. {
  293. spr.setPosition(WIN_WIDTH-spr.getWidth(),spr.getPosition().y);
  294. }
  295.  
  296. delete nxt;
  297. return 1;
  298. }
  299.  
  300. #ifndef BALL_H_INCLUDED
  301. #define BALL_H_INCLUDED
  302.  
  303.  
  304. /**
  305. *This class inherits from the class
  306. *PSprite and defines the method motion
  307. *that indicates how the Ball moves
  308. */
  309.  
  310. #include <SFML/Graphics.hpp>
  311. #include <string>
  312. #include "Bar.h"
  313. #include "CollisionManager.h"
  314. #include "PSprite.h"
  315.  
  316. class Ball:public PSprite
  317. {
  318. public:
  319. Ball(std::string,float,float);
  320.  
  321. void motion(std::vector<PSprite*>,bool&);
  322. };
  323.  
  324. #endif // BALL_H_INCLUDED
  325.  
  326. #include "Ball.h"
  327. #include <string>
  328. #include <iostream>
  329. #include <vector>
  330. #include <cstdlib>
  331. #include "Constants.h"
  332.  
  333. using namespace sf;
  334. using namespace std;
  335.  
  336. Ball::Ball(string filename,float x, float y):PSprite(filename,x,y)
  337. {
  338.  
  339. }
  340.  
  341. void Ball::motion(vector<PSprite*> sprites,bool& paused)
  342. {
  343. for(int i=0;i<sprites.size();i++) //sprites contains pointers on all the sprites on screen, in this case the 2 bars
  344. {
  345. if(collision.Coll2PSprite(*this,*sprites[i])) //If a ball collides with a PSprite it changes directionX
  346. {
  347. directionX*=-1;
  348. directionY=rand()%BALL_SPEED_MAX_Y;
  349. }
  350. }
  351.  
  352. if(collision.CollBorderH(*this)) //If a ball collides with a horizontal border it changes directionY
  353. {
  354. directionY=directionY*-1;
  355. }
  356.  
  357. move(directionX,directionY); //Making the next movement
  358.  
  359.  
  360. if(collision.CollBorderV(*this)) //If the ball collides with a vertical border we reset the game
  361. {
  362. setPosition(WIN_WIDTH/2,WIN_HEIGHT/2);
  363. directionY=0;
  364.  
  365. sprites[0]->setPosition(WIN_WIDTH-BAR_FROM_BORDERS,WIN_HEIGHT/2);
  366. sprites[0]->setLowerRight();
  367.  
  368. sprites[1]->setPosition(BAR_FROM_BORDERS,WIN_HEIGHT/2);
  369. sprites[1]->setLowerRight();
  370.  
  371. paused=1;
  372. }
  373.  
  374. setLowerRight();
  375. }
  376.  
  377. #ifndef BAR_H
  378. #define BAR_H
  379.  
  380. /**
  381. * This class Bar inherits from the class
  382. * PSprite and defines the method motion
  383. * which indicates how the bars move.
  384. */
  385.  
  386. #include <string>
  387. #include <SFML/Graphics.hpp>
  388. #include "CollisionManager.h"
  389. #include "PSprite.h"
  390.  
  391. class Bar:public PSprite
  392. {
  393. public:
  394. Bar(std::string,float,float);
  395. void motion();
  396. };
  397.  
  398. #endif // BAR_H
  399.  
  400. #include "Bar.h"
  401. #include "PSprite.h"
  402.  
  403. using namespace sf;
  404. using namespace std;
  405.  
  406. Bar::Bar(string filename,float x,float y):PSprite(filename,x,y)
  407. {
  408.  
  409.  
  410. }
  411.  
  412. void Bar::motion()
  413. {
  414. if(!collision.CollBorderH(*this)) //If a bar collides with a horizontal border it can't move in its direction
  415. {
  416. move(0,directionY);
  417. setLowerRight();
  418. directionY=0;
  419. }
  420. }
  421.  
  422. #include <SFML/Graphics.hpp>
  423. #include "Ball.h"
  424. #include "Bar.h"
  425. #include <vector>
  426. #include "Constants.h"
  427.  
  428. using namespace sf;
  429. using namespace std;
  430.  
  431. int main()
  432. {
  433.  
  434. RenderWindow app(sf::VideoMode(WIN_WIDTH, WIN_HEIGHT), "Pong XI: Tokyo Drift"); // Create the main window
  435.  
  436. Ball ball("Sprites/ball.png",WIN_WIDTH/2,WIN_HEIGHT/2); //Setting the ball
  437. ball.setDirectionX(BALL_SPEED_X);
  438.  
  439. Bar blu("Sprites/bluBar.png",WIN_WIDTH-BAR_FROM_BORDERS,WIN_HEIGHT/2); //Setting the bars
  440. Bar red("Sprites/redBar.png",BAR_FROM_BORDERS,WIN_HEIGHT/2);
  441.  
  442. vector<PSprite*> bars; //Creating the vector and adding the two bars to it
  443. bars.push_back(&blu);
  444. bars.push_back(&red);
  445.  
  446. bool paused=1;
  447.  
  448. bool z=0;
  449. bool s=0;
  450. bool up=0;
  451. bool down=0;
  452.  
  453. Texture background;
  454. background.loadFromFile("Sprites/background.png"); //Setting the background
  455. Sprite sp_bg;
  456. sp_bg.setTexture(background);
  457.  
  458. app.setFramerateLimit(MAX_FPS);
  459. app.setVerticalSyncEnabled(1);
  460.  
  461. while (app.isOpen())
  462. {
  463. Event event; // Process events
  464. while (app.pollEvent(event))
  465. {
  466. switch (event.type) // Event's type
  467. {
  468. case Event::Closed : // Close button on the window
  469. app.close();
  470. break;
  471.  
  472. case Event::KeyPressed : // Event key pressed
  473. {
  474. switch (event.key.code) // The pressed key
  475. {
  476. case Keyboard::Escape : // Escape
  477. app.close();
  478. break;
  479.  
  480. case Keyboard::Up:
  481. if(!paused)
  482. {
  483. up=1;
  484. }
  485. break;
  486.  
  487. case Keyboard::Down: //To manage simultaneous key pressing we set booleans
  488. if(!paused) //linked to the pressed buttons and later make movements
  489. { //based on their values
  490. down=1;
  491. }
  492. break;
  493.  
  494. case Keyboard::Z:
  495. if(!paused)
  496. {
  497. z=1;
  498. }
  499. break;
  500.  
  501. case Keyboard::S:
  502. if(!paused)
  503. {
  504. s=1;
  505. }
  506. break;
  507.  
  508. case Keyboard::Space:
  509. paused=!paused;
  510. break;
  511.  
  512. default:
  513. break;
  514. }
  515. }
  516. break;
  517.  
  518. case Event::KeyReleased:
  519. {
  520. switch (event.key.code) // The released key
  521. {
  522. case Keyboard::Up:
  523. if(!paused) //Stopping the movement when the keys are released
  524. {
  525. up=0;
  526. }
  527. break;
  528.  
  529. case Keyboard::Down:
  530. if(!paused)
  531. {
  532. down=0;
  533. }
  534. break;
  535.  
  536. case Keyboard::Z:
  537. if(!paused)
  538. {
  539. z=0;
  540. }
  541. break;
  542.  
  543. case Keyboard::S:
  544. if(!paused)
  545. {
  546. s=0;
  547. }
  548. break;
  549.  
  550. default:
  551. break;
  552. }
  553. }
  554. break;
  555.  
  556. default :
  557. break;
  558. }
  559. }
  560.  
  561. if(paused)
  562. {
  563. z=0; //Stopping the movements if the game is paused
  564. s=0;
  565. up=0;
  566. down=0;
  567. }
  568.  
  569. if(z) //Verifying the booleans to make movements
  570. {
  571. red.setDirectionY(-BAR_SPEED);
  572. red.motion();
  573. }
  574.  
  575. if(s)
  576. {
  577. red.setDirectionY(BAR_SPEED);
  578. red.motion();
  579. }
  580.  
  581. if(up)
  582. {
  583. blu.setDirectionY(-BAR_SPEED);
  584. blu.motion();
  585. }
  586.  
  587. if(down)
  588. {
  589. blu.setDirectionY(BAR_SPEED);
  590. blu.motion();
  591. }
  592.  
  593. if(!paused)
  594. {
  595. ball.motion(bars,paused);
  596. }
  597.  
  598. app.clear(); // Clear screen
  599.  
  600.  
  601. app.draw(sp_bg);
  602. app.draw(ball); // Draw the sprite
  603. app.draw(blu);
  604. app.draw(red);
  605.  
  606.  
  607. app.display(); // Update the window
  608.  
  609. }
  610.  
  611. return EXIT_SUCCESS;
  612. }
  613.  
  614. PSprite::PSprite(std::string& filename, const float& x, const float& y)
  615. : directionX(0), directionY(0)
  616. {
  617. texture.loadFromFile(filename); //Loading the texture
  618.  
  619. setTexture(texture); //Setting the sprite
  620. setPosition(x, y);
  621.  
  622. width = texture.getSize().x; //Setting width and height
  623. height = texture.getSize().y;
  624. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement