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 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 |
void CGame::CheckCollisions () { // Schussliste des Spielers holen list<CShot> *ShotList = m_pPlayer->GetShotList (); // Iteratoren für Asteroiden- und Schussliste list<CAsteroid>::iterator ItAsteroid = m_AsteroidList.begin (); list<CShot>::iterator ItShot; // Rects für Asteroiden und Schüsse SDL_Rect RectAsteroid; SDL_Rect RectShot; // Alle Asteroiden durchlaufen while (ItAsteroid != m_AsteroidList.end () ) { // Rect des Asteroiden holen RectAsteroid = ItAsteroid->GetRect (); // Alle Schüsse durchlaufen for (ItShot = ShotList->begin (); ItShot != ShotList->end (); ++ItShot) { // Rect des Schusses holen RectShot = ItShot->GetRect (); // Überschneiden sich die Rects? if (RectShot.y < RectAsteroid.y + RectAsteroid.h && RectShot.y + RectShot.h > RectAsteroid.y && RectShot.x < RectAsteroid.x + RectAsteroid.w && RectShot.x + RectShot.w > RectAsteroid.x && m_pPlayer->GetRect().y < RectAsteroid.y + RectAsteroid.h && m_pPlayer->GetRect().y + m_pPlayer->GetRect().h > RectAsteroid.y && m_pPlayer->GetRect().x < RectAsteroid.x + RectAsteroid.w && m_pPlayer->GetRect().x + m_pPlayer->GetRect().w > RectAsteroid.x) { // Ja, also gab es eine Kollision. Somit Schuss und // Asteroid deaktivieren ItAsteroid->SetAlive (false); ItShot->SetAlive (false); } else if (RectShot.y < RectAsteroid.y + RectAsteroid.h && RectShot.y + RectShot.h > RectAsteroid.y && RectShot.x < RectAsteroid.x + RectAsteroid.w && RectShot.x + RectShot.w > RectAsteroid.x) { ItAsteroid->SetAlive (false); ItShot->SetAlive (false); } else if (m_pPlayer->GetRect().y < RectAsteroid.y + RectAsteroid.h && m_pPlayer->GetRect().y + m_pPlayer->GetRect().h > RectAsteroid.y && m_pPlayer->GetRect().x < RectAsteroid.x + RectAsteroid.w && m_pPlayer->GetRect().x + m_pPlayer->GetRect().w > RectAsteroid.x) { ItAsteroid->SetAlive (false); } else {} } // Asteroid löschen, falls deaktiviert if (ItAsteroid->IsAlive () ) ItAsteroid++; else ItAsteroid = m_AsteroidList.erase (ItAsteroid); } } |
TigerClaw25
unregistriert
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 |
// CheckCollisions // // Aufgabe: Kollisionen zwischen Asteroiden und Schüssen prüfen // void CGame::CheckCollisions () { // Schussliste des Spielers holen list<CShot> *ShotList = m_pPlayer->GetShotList (); // Iteratoren für Asteroiden- und Schussliste list<CAsteroid>::iterator ItAsteroid = m_AsteroidList.begin (); list<CShot>::iterator ItShot; // Rects für Asteroiden und Schüsse sowie Spieler SDL_Rect RectAsteroid; SDL_Rect RectShot; SDL_Rect RectPlayer; // Alle Asteroiden durchlaufen while (ItAsteroid != m_AsteroidList.end () ) { // Rect des Asteroiden holen RectAsteroid = ItAsteroid->GetRect (); // Alle Schüsse durchlaufen for (ItShot = ShotList->begin (); ItShot != ShotList->end (); ++ItShot) { // Rect des Schusses holen RectShot = ItShot->GetRect (); // Überschneiden sich die Rects? if (RectShot.y < RectAsteroid.y + RectAsteroid.h && RectShot.y + RectShot.h > RectAsteroid.y && RectShot.x < RectAsteroid.x + RectAsteroid.w && RectShot.x + RectShot.w > RectAsteroid.x) { // Ja, also gab es eine Kollision. Somit Schuss und // Asteroid deaktivieren ItAsteroid->SetAlive (false); ItShot->SetAlive (false); m_AsteroidDestroy++; // Wieviele Asteroiden zerstört? // Asteriod explodiert -> Soundeffekt m_pSoundAsteroid->PlaySound(); } } // Kollision zwischen Asteroid und Spieler prüfen RectPlayer = m_pPlayer->GetRect(); if (RectPlayer.y < RectAsteroid.y + RectAsteroid.h && RectPlayer.y + RectPlayer.h > RectAsteroid.y && RectPlayer.x < RectAsteroid.x + RectAsteroid.w && RectPlayer.x + RectPlayer.w > RectAsteroid.x) { // Ja, also gab es eine Kollision. Somit Schuss und // Asteroid deaktivieren ItAsteroid->SetAlive (false); m_pPlayer->Reset(); // Asteriod explodiert -> Soundeffekt m_pSoundAsteroid->PlaySound(); } // Asteroid löschen, falls deaktiviert if (ItAsteroid->IsAlive () ) ItAsteroid++; else ItAsteroid = m_AsteroidList.erase (ItAsteroid); } } // CheckCollision |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Aufgabe: Spielerwerte auf Standard setzen // void CPlayer::Reset () { // Startposition des Spielers m_fXPos = 376.0f; m_fYPos = 520.0f; // Animationsphase m_fAnimPhase = 5.0f; // Es darf geschossen werden m_bShotLock = false; // Rect initialisieren (Erweiterung für Kollision zwischen Asteroid und Spieler) m_Rect.x = static_cast<int>(m_fXPos); m_Rect.y = static_cast<int>(m_fYPos); m_Rect.w = m_pSpritePlayer->GetRect().w; m_Rect.h = m_pSpritePlayer->GetRect().h; } // Reset |
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »TigerClaw25« (11.04.2013, 11:48)
Werbeanzeige