#include "stdafx.h" #include #include #include "Ball.h" #include "player.h" using namespace sf; using namespace std; int main() { RenderWindow window{ VideoMode{ 500,700 } ,"Game SFML" };//tworzy okno (width,height) nazwa window.setFramerateLimit(60); //ustala klatki na sec Event event; Ball ball(0,0); player Player(200, 650); while (window.isOpen()) //główna pętla gry { window.clear(Color::Black); window.pollEvent(event); //czeka na zdarzenie i wykonuje wszystkie funkcjie wewnątrz if(event.type==Event::Closed) //zamykanie okna { window.close(); break; } ball.update(); Player.update(); window.draw(ball); //rysowanie ?? window.draw(Player); window.display(); } return 0; } #pragma once #include using namespace std; using namespace sf; class player :public sf::Drawable { public: player(float pX,float pY); player() = delete; ~player()=default; void update(); float top(); float bottom(); float left(); float right(); private: RectangleShape shape; const float Pwidth {80.0f}; const float Pheight{20.0f}; const float Pvelocity{ 6.0f }; void draw(RenderTarget& target, RenderStates state) const override; Vector2f velocity{ Pvelocity,0.0f }; }; /////////////////////// #include "stdafx.h" #include "Ball.h" Ball::Ball(float bX,float bY) { shape.setPosition(bX, bY); shape.setRadius(this->ballRadius); shape.setFillColor(Color::White); shape.setOrigin(this->ballRadius, this->ballRadius); } void Ball::draw(RenderTarget& target, RenderStates state)const { target.draw(this->shape, state); } //updateuje void Ball::update() { shape.move(this->velocity); if (this->left() < 0) { velocity.x = ballVelocity1; } else if (this->right() >500) { velocity.x = -ballVelocity1; } else if (this->top() < 0) { velocity.y = ballVelocity2; } else if (this->bottom() > 700) { velocity.y = -ballVelocity2; } } //krawedzie float Ball::top() { return this->shape.getPosition().y - shape.getRadius(); } float Ball::bottom() { return this->shape.getPosition().y + shape.getRadius(); } float Ball::left() { return this->shape.getPosition().x - shape.getRadius(); } float Ball::right() { return this->shape.getPosition().x + shape.getRadius(); } // #pragma once #include using namespace sf; class Ball : public sf::Drawable //mozliwosc rysowania { public: Ball(float bX, float bY); Ball()=delete; ~Ball()=default; void update(); float top(); float bottom(); float left(); float right(); private: CircleShape shape; const float ballRadius{ 10.0f }; const float ballVelocity1{ 3.0f }; const float ballVelocity2{ 3.0f }; Vector2f velocity{ ballVelocity1,ballVelocity2 }; //wektor z dwoma zmiennymi void draw(RenderTarget& target, RenderStates state) const override; }; /////////// #include "stdafx.h" #include "player.h" #include using namespace std; using namespace sf; player::player(float pX,float pY) { shape.setPosition(pX, pY); shape.setSize({ this->Pwidth,this->Pheight }); shape.setFillColor(Color::Green); shape.setOrigin(Pwidth / 2.f, Pheight / 2.f); //centrum paletki } void player::draw(RenderTarget& target, RenderStates state)const { target.draw(this->shape, state); } void player::update() { this->shape.move(this->velocity); if (Keyboard::isKeyPressed(Keyboard::Key::Left) && this->left() > 0) { velocity.x = -Pvelocity; } else if (Keyboard::isKeyPressed(Keyboard::Key::Right) && this->right() < 500) { velocity.x = Pvelocity; } else { velocity.x = 0; } } float player::left() { return this->shape.getPosition().x - shape.getSize().x / 2.f; } float player::right() { return this->shape.getPosition().x + shape.getSize().x / 2.f; } float player::top() { return this->shape.getPosition().y - shape.getSize().y/2.f; } float player::bottom() { return this->shape.getPosition().y + shape.getSize().y / 2.f; } // #pragma once #include using namespace std; using namespace sf; class player :public sf::Drawable { public: player(float pX,float pY); player() = delete; ~player()=default; void update(); float top(); float bottom(); float left(); float right(); private: RectangleShape shape; const float Pwidth {80.0f}; const float Pheight{20.0f}; const float Pvelocity{ 6.0f }; void draw(RenderTarget& target, RenderStates state) const override; Vector2f velocity{ Pvelocity,0.0f }; };