Facebook
From Morose Hog, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 265
  1. // SFMLgame.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <SFML\Graphics.hpp>
  6. #include <SFML\Window.hpp>
  7. #include "Ball.h"
  8.  
  9. using namespace sf;
  10. using namespace std;
  11. int main()
  12. {
  13.         RenderWindow window{ VideoMode{500,500} ,"Game SFML" };
  14.         window.setFramerateLimit(60);
  15.         Event event;
  16.         Ball ball(400,300);
  17.         while (true) //główna pętla gry
  18.         {
  19.                 window.clear(Color::Black);
  20.                 window.pollEvent(event);                 //czeka na zdarzenie i wykonuje wszystkie funkcjie wewnątrz
  21.                 if(event.type==Event::Closed)    //zamykanie okna
  22.                         {
  23.                         window.close();
  24.                         break;
  25.                         }
  26.                 window.draw(ball);
  27.                 window.display();
  28.         }
  29.     return 0;
  30. }
  31. -----------------------------------------------------------------
  32. #pragma once
  33. #include<SFML\Graphics.hpp>
  34.  
  35. using namespace sf;
  36.  
  37. class Ball : public sf::Drawable //mozliwosc rysowania
  38. {
  39. public:
  40.         Ball(float bX, float bY);
  41.         Ball()=delete;
  42.         ~Ball()=default;
  43. private:
  44.         CircleShape shape;
  45.         const float ballRadius{ 10.0f };
  46.         void draw(RenderTarget& target, RenderStates state) const override;
  47. };
  48. ---------------------------------------------------------------------
  49. #include "stdafx.h"
  50. #include "Ball.h"
  51.  
  52.  
  53. Ball::Ball(float bX,float bY)
  54. {
  55.         shape.setPosition(bX, bY);
  56.         shape.setRadius(this->ballRadius);
  57.         shape.setFillColor(Color::White);
  58.         shape.setOrigin(this->ballRadius, this->ballRadius);
  59. }
  60.  
  61. void Ball::draw(RenderTarget& target, RenderStates state)const
  62. {
  63.         target.draw(this->shape, state);
  64. }
  65.  
  66.