moving player
authorNils Forssén <nilfo359@student.liu.se>
Thu, 9 Nov 2023 15:32:36 +0000 (16:32 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Thu, 9 Nov 2023 15:32:36 +0000 (16:32 +0100)
assets/4V_figur.png [new file with mode: 0644]
assets/4V_figur2.png [deleted file]
src/Moving_object.h
src/Object.h
src/Player.cc
src/Player.h
src/_main.cc
src/constants.h [new file with mode: 0644]

diff --git a/assets/4V_figur.png b/assets/4V_figur.png
new file mode 100644 (file)
index 0000000..0fc5802
Binary files /dev/null and b/assets/4V_figur.png differ
diff --git a/assets/4V_figur2.png b/assets/4V_figur2.png
deleted file mode 100644 (file)
index 0fc5802..0000000
Binary files a/assets/4V_figur2.png and /dev/null differ
index df01e7ffd4003440f9462de60144caf964264c4d..e20995ec4277f6f6b72393d7d6925cc21f186975 100644 (file)
@@ -3,7 +3,7 @@
 
 #include "Object.h"
 
-class Moving_object : Object
+class Moving_object : public Object
 {
 public:
 protected:
index dd5ed78084f54a9ab2ca0e6f6b3b087124a994d5..2fe6023ddd787a78fdb61e5e37b0ac50ec4953c9 100644 (file)
@@ -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
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..bee611c0216fa96655f2ba6ce29eb42fe0a39a4d 100644 (file)
@@ -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
index de9d49ba32018f5a4507915322485cde9e18db6e..b4d8c20f701ec48f3b92ad5fbb2c096daaa919d4 100644 (file)
@@ -1,14 +1,16 @@
 
 #include <SFML/Graphics.hpp>
+
 #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();
index fff44bcef06e9abb1556db080cb3f978e8b95529..4c81edee1c82d6397513dfc4f640faf2f990e147 100644 (file)
@@ -5,12 +5,9 @@
 */
 
 #include <SFML/Graphics.hpp> // 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 (file)
index 0000000..e1581fc
--- /dev/null
@@ -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