Facebook
From Flying Gibbon, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 226
  1. #include "stdafx.h"
  2. #include <SFML\Graphics.hpp>
  3. #include <SFML\Window.hpp>
  4. #include "Ball.h"
  5. #include "player.h"
  6.  
  7. using namespace sf;
  8. using namespace std;
  9. int main()
  10. {
  11.         RenderWindow window{ VideoMode{ 500,700 } ,"Game SFML" };//tworzy okno (width,height) nazwa
  12.         window.setFramerateLimit(60);                          //ustala klatki na sec
  13.         Event event;
  14.         Ball ball(0,0);
  15.         player Player(200, 650);
  16.         while (window.isOpen()) //główna pętla gry
  17.         {
  18.                 window.clear(Color::Black);
  19.                 window.pollEvent(event);                 //czeka na zdarzenie i wykonuje wszystkie funkcjie wewnątrz
  20.                 if(event.type==Event::Closed)    //zamykanie okna
  21.                         {
  22.                         window.close();
  23.                         break;
  24.                         }
  25.                 ball.update();
  26.                 Player.update();
  27.                 window.draw(ball); //rysowanie ??
  28.                 window.draw(Player);
  29.                 window.display();
  30.         }
  31.     return 0;
  32. }
  33. #pragma once
  34. #include <SFML\Graphics.hpp>
  35. using namespace std;
  36. using namespace sf;
  37. class player :public sf::Drawable
  38. {
  39. public:
  40.         player(float pX,float pY);
  41.         player() = delete;
  42.         ~player()=default;
  43.  
  44.         void update();
  45.         float top();
  46.         float bottom();
  47.         float left();
  48.         float right();
  49.  
  50. private:
  51.         RectangleShape shape;
  52.         const float Pwidth {80.0f};
  53.         const float Pheight{20.0f};
  54.         const float Pvelocity{ 6.0f };
  55.         void draw(RenderTarget& target, RenderStates state) const override;
  56.         Vector2f velocity{ Pvelocity,0.0f };
  57. };
  58. ///////////////////////
  59. #include "stdafx.h"
  60. #include "Ball.h"
  61.  
  62.  
  63. Ball::Ball(float bX,float bY)
  64. {
  65.         shape.setPosition(bX, bY);
  66.         shape.setRadius(this->ballRadius);
  67.         shape.setFillColor(Color::White);
  68.         shape.setOrigin(this->ballRadius, this->ballRadius);
  69. }
  70.  
  71. void Ball::draw(RenderTarget& target, RenderStates state)const
  72. {
  73.         target.draw(this->shape, state);
  74. }
  75. //updateuje
  76. void Ball::update()
  77. {
  78.         shape.move(this->velocity);
  79.  
  80.         if (this->left() < 0)
  81.         {
  82.                 velocity.x = ballVelocity1;
  83.         }
  84.         else if (this->right() >500)
  85.         {
  86.                 velocity.x = -ballVelocity1;
  87.         }
  88.         else if (this->top() < 0)
  89.         {
  90.                 velocity.y = ballVelocity2;
  91.         }
  92.         else if (this->bottom() > 700)
  93.         {
  94.                 velocity.y = -ballVelocity2;
  95.         }
  96. }
  97. //krawedzie
  98. float Ball::top()
  99. {
  100.         return this->shape.getPosition().y - shape.getRadius();
  101. }
  102. float Ball::bottom()
  103. {
  104.         return this->shape.getPosition().y + shape.getRadius();
  105. }
  106. float Ball::left()
  107. {
  108.         return this->shape.getPosition().x - shape.getRadius();
  109. }
  110. float Ball::right()
  111. {
  112.         return this->shape.getPosition().x + shape.getRadius();
  113. }
  114. //
  115. #pragma once
  116. #include<SFML\Graphics.hpp>
  117.  
  118. using namespace sf;
  119.  
  120. class Ball : public sf::Drawable //mozliwosc rysowania
  121. {
  122. public:
  123.         Ball(float bX, float bY);
  124.         Ball()=delete;
  125.         ~Ball()=default;
  126.  
  127.         void update();
  128.         float top();
  129.         float bottom();
  130.         float left();
  131.         float right();
  132.  
  133.  
  134. private:
  135.         CircleShape shape;
  136.         const float ballRadius{ 10.0f };
  137.         const float ballVelocity1{ 3.0f };
  138.         const float ballVelocity2{ 3.0f };
  139.         Vector2f velocity{ ballVelocity1,ballVelocity2 }; //wektor z dwoma zmiennymi
  140.         void draw(RenderTarget& target, RenderStates state) const override;
  141. };
  142. ///////////
  143. #include "stdafx.h"
  144. #include "player.h"
  145. #include <SFML\Graphics.hpp>
  146.  
  147. using namespace std;
  148. using namespace sf;
  149.  
  150. player::player(float pX,float pY)
  151. {
  152.         shape.setPosition(pX, pY);
  153.         shape.setSize({ this->Pwidth,this->Pheight });
  154.         shape.setFillColor(Color::Green);
  155.         shape.setOrigin(Pwidth / 2.f, Pheight / 2.f); //centrum paletki
  156. }
  157. void player::draw(RenderTarget& target, RenderStates state)const
  158. {
  159.         target.draw(this->shape, state);
  160. }
  161. void player::update()
  162. {
  163.         this->shape.move(this->velocity);
  164.         if (Keyboard::isKeyPressed(Keyboard::Key::Left) && this->left() > 0)
  165.         {
  166.                 velocity.x = -Pvelocity;
  167.         }
  168.         else if (Keyboard::isKeyPressed(Keyboard::Key::Right) && this->right() < 500)
  169.         {
  170.                 velocity.x = Pvelocity;
  171.         }
  172.         else
  173.         {
  174.                 velocity.x = 0;
  175.         }
  176. }
  177. float player::left()
  178. {
  179.         return this->shape.getPosition().x - shape.getSize().x / 2.f;
  180. }
  181. float player::right()
  182. {
  183.         return this->shape.getPosition().x + shape.getSize().x / 2.f;
  184. }
  185. float player::top()
  186. {
  187.         return this->shape.getPosition().y - shape.getSize().y/2.f;
  188. }
  189. float player::bottom()
  190. {
  191.         return this->shape.getPosition().y + shape.getSize().y / 2.f;
  192. }
  193. //
  194. #pragma once
  195. #include <SFML\Graphics.hpp>
  196. using namespace std;
  197. using namespace sf;
  198. class player :public sf::Drawable
  199. {
  200. public:
  201.         player(float pX,float pY);
  202.         player() = delete;
  203.         ~player()=default;
  204.  
  205.         void update();
  206.         float top();
  207.         float bottom();
  208.         float left();
  209.         float right();
  210.  
  211. private:
  212.         RectangleShape shape;
  213.         const float Pwidth {80.0f};
  214.         const float Pheight{20.0f};
  215.         const float Pvelocity{ 6.0f };
  216.         void draw(RenderTarget& target, RenderStates state) const override;
  217.         Vector2f velocity{ Pvelocity,0.0f };
  218. };
  219.