Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#pragma once #include <SFML\Graphics.hpp> #include <iostream> #include "Player.hpp" #include "Enemy.hpp" #include "Ball.hpp" using namespace sf; class CFramework { friend class CEnemy; public: CFramework (); ~CFramework (); void Run (); private: void Update (float fElapsedTime); void Render (); void HandleEvents (); void Quit (); float GetElapsedTime (); RenderWindow *m_pWindow; Event *m_pEvent; Clock *m_pClock; CPlayer *m_pPlayer; CEnemy *m_pEnemy; CBall *m_pBall; float m_fElapsedTime; bool m_bRunning; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include "Framework.hpp" CFramework::CFramework() { m_bRunning = true; m_pWindow = new RenderWindow(VideoMode(800, 600), "Pong", Style::Titlebar | Style::Close); m_pEvent = new Event; m_pClock = new Clock; m_pPlayer = new CPlayer(String("Assets/Pong.png"), Vector2f(30, 300)); m_pEnemy = new CEnemy(String("Assets/Pong.png"), Vector2f(770, 300)); m_pBall = new CBall(String("Assets/Ball.png"), Vector2f(400, 300)); } CFramework::~CFramework() { delete m_pWindow; delete m_pEvent; delete m_pClock; delete m_pPlayer; delete m_pEnemy; delete m_pBall; m_pWindow = nullptr; m_pEvent = nullptr; m_pClock = nullptr; m_pPlayer = nullptr; m_pEnemy = nullptr; m_pBall = nullptr; } void CFramework::Run() { while (m_bRunning) { Update(m_fElapsedTime); Render(); HandleEvents(); GetElapsedTime(); Quit(); } } void CFramework::Update(float fElapsedTime) { m_pPlayer->Update(fElapsedTime); m_pEnemy->Update(fElapsedTime); m_pBall->Update(fElapsedTime); } void CFramework::Render() { m_pWindow->clear(); m_pPlayer->Render(m_pWindow); m_pEnemy->Render(m_pWindow); m_pBall->Render(m_pWindow); m_pWindow->display(); } void CFramework::HandleEvents() { while (m_pWindow->pollEvent(*m_pEvent)) { if (m_pEvent->type == m_pEvent->Closed) { m_bRunning = false; } } } void CFramework::Quit() { if (m_bRunning == false) { m_pWindow->close(); } } float CFramework::GetElapsedTime() { m_fElapsedTime = m_pClock->getElapsedTime().asSeconds(); m_pClock->restart(); return m_fElapsedTime; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include <SFML\Graphics.hpp> using namespace sf; using namespace std; class CPlayer { public: CPlayer (string sTexture, Vector2f vPosition); ~CPlayer (); void Update (float fElapsedTime); void Render (RenderWindow *pWindow); private: Texture *m_pTexture; Sprite *m_pSprite; float m_fSpeed; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include "Player.hpp" CPlayer::CPlayer(string sTexture, Vector2f vPosition) { m_pTexture = new Texture; m_pSprite = new Sprite; m_pTexture->loadFromFile(sTexture.c_str()); m_pSprite->setTexture(*m_pTexture); m_pSprite->setOrigin(static_cast<float>(m_pTexture->getSize().x / 2), static_cast<float>(m_pTexture->getSize().y / 2)); m_pSprite->setPosition(vPosition); m_fSpeed = 300.0f; } CPlayer::~CPlayer() { delete m_pTexture; delete m_pSprite; m_pTexture = nullptr; m_pSprite = nullptr; } void CPlayer::Update(float fElapsedTime) { if (Keyboard::isKeyPressed(Keyboard::Key::W) && m_pSprite->getPosition().y - m_pTexture->getSize().y / 2 >= 0) { m_pSprite->move(0, -m_fSpeed * fElapsedTime); } if (Keyboard::isKeyPressed(Keyboard::Key::S) && m_pSprite->getPosition().y + m_pTexture->getSize().y / 2 <= 600) { m_pSprite->move(0, m_fSpeed * fElapsedTime); } } void CPlayer::Render(RenderWindow *pWindow) { pWindow->draw(*m_pSprite); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include <SFML\Graphics.hpp> #include "Framework.hpp" using namespace sf; using namespace std; class CEnemy { public: CEnemy (string sTexture, Vector2f vPosition); ~CEnemy (); void Update (float fElapsedTime); void Render (RenderWindow *pWindow); private: Texture *m_pTexture; Sprite *m_pSprite; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include "Enemy.hpp" CEnemy::CEnemy(string sTexture, Vector2f vPosition) { m_pTexture = new Texture; m_pSprite = new Sprite; m_pTexture->loadFromFile(sTexture.c_str()); m_pSprite->setTexture(*m_pTexture); m_pSprite->setOrigin(static_cast<float>(m_pTexture->getSize().x / 2), static_cast<float>(m_pTexture->getSize().y / 2)); m_pSprite->setPosition(vPosition); } CEnemy::~CEnemy() { delete m_pTexture; delete m_pSprite; m_pTexture = nullptr; m_pSprite = nullptr; } void CEnemy::Update(float fElapsedTime) { } void CEnemy::Render(RenderWindow *pWindow) { pWindow->draw(*m_pSprite); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#pragma once #include <SFML\Graphics.hpp> using namespace sf; using namespace std; class CBall { public: CBall (string sTexture, Vector2f vPosition); ~CBall (); void Update (float fElapsedTime); void Render (RenderWindow *pWindow); private: Texture *m_pTexture; Sprite *m_pSprite; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include "Ball.hpp" CBall::CBall(string sTexture, Vector2f vPosition) { m_pTexture = new Texture; m_pSprite = new Sprite; m_pTexture->loadFromFile(sTexture.c_str()); m_pSprite->setTexture(*m_pTexture); m_pSprite->setOrigin(static_cast<float>(m_pTexture->getSize().x / 2), static_cast<float>(m_pTexture->getSize().y / 2)); m_pSprite->setPosition(vPosition); } CBall::~CBall() { delete m_pTexture; delete m_pSprite; m_pTexture = nullptr; m_pSprite = nullptr; } void CBall::Update(float fElapsedTime) { if (Keyboard::isKeyPressed(Keyboard::Key::Up)) { m_pSprite->move(0, -300.0f * fElapsedTime); } if (Keyboard::isKeyPressed(Keyboard::Key::Down)) { m_pSprite->move(0, 300.0f * fElapsedTime); } if (Keyboard::isKeyPressed(Keyboard::Key::Left)) { m_pSprite->move(-300.0f * fElapsedTime, 0); } if (Keyboard::isKeyPressed(Keyboard::Key::Right)) { m_pSprite->move(300.0f * fElapsedTime, 0); } } void CBall::Render(RenderWindow *pWindow) { pWindow->draw(*m_pSprite); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#pragma once #include <SFML\Graphics.hpp> #include "Enemy.hpp" using namespace sf; using namespace std; class CBall { friend class CEnemy; public: CBall (string sTexture, Vector2f vPosition); ~CBall (); void Update (float fElapsedTime); void Render (RenderWindow *pWindow); private: Texture *m_pTexture; Sprite *m_pSprite; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include <SFML\Graphics.hpp> #include "Ball.hpp" using namespace sf; using namespace std; class CEnemy { public: CEnemy (string sTexture, Vector2f vPosition); ~CEnemy (); void Update (float fElapsedTime, CBall *pBall); void Render (RenderWindow *pWindow); private: Texture *m_pTexture; Sprite *m_pSprite; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include "Enemy.hpp" CEnemy::CEnemy(string sTexture, Vector2f vPosition) { m_pTexture = new Texture; m_pSprite = new Sprite; m_pTexture->loadFromFile(sTexture.c_str()); m_pSprite->setTexture(*m_pTexture); m_pSprite->setOrigin(static_cast<float>(m_pTexture->getSize().x / 2), static_cast<float>(m_pTexture->getSize().y / 2)); m_pSprite->setPosition(vPosition); } CEnemy::~CEnemy() { delete m_pTexture; delete m_pSprite; m_pTexture = nullptr; m_pSprite = nullptr; } void CEnemy::Update(float fElapsedTime, CBall *pBall) { m_pSprite->setPosition(pBall->m_pSprite->getPosition().x, 0); } void CEnemy::Render(RenderWindow *pWindow) { pWindow->draw(*m_pSprite); } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »N4SONIC« (26.12.2015, 00:14)
Damit ich die Collision abfragen kann :o
Also soll ich anstatt std::unique_ptr benutzen ? Und im Destruktor die pointer nicht mehr auf null setzten ?
Zitat
Na gib dem Ball doch einfach eine checkCollision() Methode!?
Zitat
Naja, was genau hast du denn für einen Grund, diese Objekte alle per new anzulegen? Sollen diese Objekte dynamische Lebensdauer haben? Was genau ist der Sinn, den du im Null-setzen der Pointer siehst?
Ok. Aber ich muss doch dann in der Parameterliste die CEnemy Klasse übergebn oder nicht ? :o
Von Tutorials habe ich das
Wie sollte ich das den am besten machen könntest du mir ein beispiel schreiben ?
Werbeanzeige