#include "Asprin.h" #include "GameBackground.h" AsprinGame::AsprinGame(SDL_Surface* pScreen, Difficulty eDifficulty) : m_pScreen(pScreen), m_Piece(pScreen), m_Object(pScreen), m_Lines(pScreen), m_eDifficulty(eDifficulty), m_nTargetsCaught(0), m_bGameOver(false) { m_pBackground = nSDL_LoadImage(image_background); m_pFont = nSDL_LoadFont(NSDL_FONT_THIN, 0, NSDL_FONTCFG_DEFAULT); int nStartingLines = eDifficulty == Easy ? STARTING_LINES_EASY : eDifficulty == Medium ? STARTING_LINES_MEDIUM : STARTING_LINES_HARD; for(int i=0; i nXPiece ? (nXTarget-nXPiece < PIECE_WIDTH) : true; bool bWithinRightEdge = nXTarget > nXPiece ? true : (nXPiece-nXTarget < OBJECT_WIDTH); bool bWithinTopEdge = nYTarget > nYPiece ? (nYTarget-nYPiece < PIECE_HEIGHT) : true; bool bWithinBottomEdge = nYTarget > nYPiece ? true : (nYPiece-nYTarget < OBJECT_HEIGHT); if( bWithinLeftEdge && bWithinRightEdge && bWithinTopEdge && bWithinBottomEdge )//Got target { m_nTargetsCaught++; m_Object.Setup();//Reposition target AddLine(); } if( m_Lines.CheckCollision(nXPiece, nYPiece) ) { //Game over m_bGameOver = true; } } void AsprinGame::AddLine() { bool bHorizontal = rand() % 2 == 0; m_Lines.AddLine(m_eDifficulty, bHorizontal, GetPosition(bHorizontal)); } int AsprinGame::GetPosition(bool bHorizontal) const { int nPos; int nAttempts = 0; do { nPos = GetNewPosition(bHorizontal); nAttempts++; if( nAttempts > 50 )//Continously getting a random isn't technically an algorithm by definition break;//So if cannot add a line after some number of attempts; keep going. } while(m_Lines.LineWithPosition(bHorizontal, nPos) || m_Piece.IsCloseToPiece(bHorizontal, nPos)); return nPos; } int AsprinGame::GetNewPosition(bool bHorizontal) const { return rand() % (bHorizontal ? (SCREEN_HEIGHT - BOARD_OFFSET_BOTTOM) : (SCREEN_WIDTH - BOARD_OFFSET_RIGHT)); }