From ad9d2ee712bd0bb1869edbbcbd93f1406921b3f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Thu, 9 Nov 2023 16:32:36 +0100 Subject: [PATCH] moving player --- assets/{4V_figur2.png => 4V_figur.png} | Bin src/Moving_object.h | 2 +- src/Object.h | 9 +++++-- src/Player.cc | 32 +++++++++++++++++++++++++ src/Player.h | 6 +++-- src/_main.cc | 16 ++++++------- src/constants.h | 9 +++++++ 7 files changed, 61 insertions(+), 13 deletions(-) rename assets/{4V_figur2.png => 4V_figur.png} (100%) create mode 100644 src/constants.h diff --git a/assets/4V_figur2.png b/assets/4V_figur.png similarity index 100% rename from assets/4V_figur2.png rename to assets/4V_figur.png diff --git a/src/Moving_object.h b/src/Moving_object.h index df01e7f..e20995e 100644 --- a/src/Moving_object.h +++ b/src/Moving_object.h @@ -3,7 +3,7 @@ #include "Object.h" -class Moving_object : Object +class Moving_object : public Object { public: protected: diff --git a/src/Object.h b/src/Object.h index dd5ed78..2fe6023 100644 --- a/src/Object.h +++ b/src/Object.h @@ -7,17 +7,22 @@ class Object { public: + Object() : x_pos(0), y_pos(0), sprite(), texture() {}; + virtual bool collides(Object& other) = 0; virtual void collision(Object& other) = 0; virtual void update() = 0; virtual void render(sf::RenderWindow& window) = 0; virtual ~Object() = default; -private: -protected: + double x_pos; double y_pos; sf::Sprite sprite; + sf::Texture texture; + +protected: +private: }; #endif \ No newline at end of file diff --git a/src/Player.cc b/src/Player.cc index e69de29..bee611c 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -0,0 +1,32 @@ +#include "Player.h" + +Player::Player() : Object(), collected(0) +{ + texture.loadFromFile("assets/4V_figur.png"); + sprite.setTexture(texture); + + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2); +} + +bool Player::collides(Object& other) +{ + return false; +} + +void Player::collision(Object& other) +{ + return; +} + +void Player::update() +{ + x_pos += 0.1; + sprite.setPosition((S_WIDTH / 2) + x_pos, S_HEIGHT / 2); + return; +} + +void Player::render(sf::RenderWindow& window) +{ + window.draw(sprite); +} \ No newline at end of file diff --git a/src/Player.h b/src/Player.h index de9d49b..b4d8c20 100644 --- a/src/Player.h +++ b/src/Player.h @@ -1,14 +1,16 @@ #include + #include "Moving_object.h" #include "Object.h" +#include "constants.h" -class Player : Moving_object +class Player : public Object { public: Player(); - ~Player(); + ~Player() = default; bool collides(Object& other); void collision(Object& other); void update(); diff --git a/src/_main.cc b/src/_main.cc index fff44bc..4c81ede 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -5,12 +5,9 @@ */ #include // includes most things in SFML -#include "Object.h" +#include "constants.h" -const unsigned int S_WIDTH {1280}; -const unsigned int S_HEIGHT {720}; - -const double S_SCALE_KOEFF {S_WIDTH / 1920.0}; +#include "Player.h" int main () { @@ -19,10 +16,12 @@ int main () "DespYrat" }; + Player test_player{}; sf::Texture t; t.loadFromFile("assets/hel_bakgrund.png"); - sf::Sprite s(t); + sf::Sprite s; + s.setTexture(t); s.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); @@ -39,10 +38,11 @@ int main () } } - - /* rita ut */ + /* rita ut */ window.clear(); // rensa skärmen window.draw(s); + test_player.render(window); + test_player.update(); window.display(); // visa ändringarna } } diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000..e1581fc --- /dev/null +++ b/src/constants.h @@ -0,0 +1,9 @@ +#ifndef CONSTANTS_H +#define CONSTANTS_H + +const unsigned int S_WIDTH {1280}; +const unsigned int S_HEIGHT {720}; + +const double S_SCALE_KOEFF {S_WIDTH / 1920.0}; + +#endif \ No newline at end of file -- 2.30.2