void DemoBObject::DoUpdate(int iCurrentTime) { // Change speed if player presses a key if ( GetEngine()->IsKeyPressed( SDLK_UP ) ) m_iCurrentScreenY -=2; if ( GetEngine()->IsKeyPressed( SDLK_DOWN ) ) m_iCurrentScreenY +=2; if ( GetEngine()->IsKeyPressed( SDLK_LEFT ) ) m_iCurrentScreenX -=2; if ( GetEngine()->IsKeyPressed( SDLK_RIGHT ) ) m_iCurrentScreenX +=2; if ( m_iCurrentScreenX < 0 ) m_iCurrentScreenX = 0; if ( m_iCurrentScreenX >= GetEngine()->GetScreenWidth() - m_iDrawWidth ) m_iCurrentScreenX = GetEngine()->GetScreenWidth() - m_iDrawWidth; if ( m_iCurrentScreenY < 0 ) m_iCurrentScreenY = 0; if ( m_iCurrentScreenY >= GetEngine()->GetScreenHeight() - m_iDrawHeight) m_iCurrentScreenY = GetEngine()->GetScreenHeight() - m_iDrawHeight; // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
/** Handle the update action, moving the object and/or handling any game logic */ void BouncingBall1::DoUpdate( int iCurrentTime ) { // Work out current position m_oMovement.Calculate(iCurrentTime); m_iCurrentScreenX = m_oMovement.GetX(); m_iCurrentScreenY = m_oMovement.GetY(); // If movement has finished then request instructions if ( m_oMovement.HasMovementFinished( iCurrentTime ) ) { m_oMovement.Reverse(); m_oMovement.Calculate(iCurrentTime); m_iCurrentScreenX = m_oMovement.GetX(); m_iCurrentScreenY = m_oMovement.GetY(); } if ( m_pTileManager->IsValidTilePosition( m_iCurrentScreenX, m_iCurrentScreenY ) ) { int iTileX = m_pTileManager->GetTileXForPositionOnScreen(m_iCurrentScreenX); int iTileY = m_pTileManager->GetTileYForPositionOnScreen(m_iCurrentScreenY); int iCurrentTile = m_pTileManager->GetValue( iTileX, iTileY ); m_pTileManager->UpdateTile( GetEngine(), iTileX, iTileY, iCurrentTile+1 ); } // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
// Called frequently, this should move the item // In this case we also accept cursor key presses to change the speed // Space will set the speed to zero void DoUpdate( int iCurrentTime ) { // Change speed if player presses a key if ( GetEngine()->IsKeyPressed( SDLK_UP ) ) m_dSY -= 0.001; if ( GetEngine()->IsKeyPressed( SDLK_DOWN ) ) m_dSY += 0.001; if ( GetEngine()->IsKeyPressed( SDLK_LEFT ) ) m_dSX -= 0.001; if ( GetEngine()->IsKeyPressed( SDLK_RIGHT ) ) m_dSX += 0.001; if ( GetEngine()->IsKeyPressed( SDLK_SPACE ) ) m_dSX = m_dSY = 0; // Alter position for speed m_dX += m_dSX; m_dY += m_dSY; // Check for bounce off the edge if ( (m_dX+m_iStartDrawPosX) < 0 ) { m_dX = - m_iStartDrawPosX; if ( m_dSX < 0 ) m_dSX = -m_dSX; } if ( (m_dX+m_iStartDrawPosX+m_iDrawWidth) > (GetEngine()->GetScreenWidth()-1) ) { m_dX = GetEngine()->GetScreenWidth() -1 - m_iStartDrawPosX - m_iDrawWidth; if ( m_dSX > 0 ) m_dSX = -m_dSX; } if ( (m_dY+m_iStartDrawPosY) < 0 ) { m_dY = -m_iStartDrawPosY; if ( m_dSY < 0 ) m_dSY = -m_dSY; } if ( (m_dY+m_iStartDrawPosY+m_iDrawHeight) > (GetEngine()->GetScreenHeight()-1) ) { m_dY = GetEngine()->GetScreenHeight() -1 - m_iStartDrawPosY - m_iDrawHeight; if ( m_dSY > 0 ) m_dSY = -m_dSY; } // Set current position - you NEED to set the current positions m_iCurrentScreenX = (int)(m_dX+0.5); m_iCurrentScreenY = (int)(m_dY+0.5); printf("Position %f, %f\n", m_dX, m_dY ); // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void PacmanObject::DoUpdate(int iCurrentTime) { m_iPreviousScreenX = m_iCurrentScreenX; m_iPreviousScreenY = m_iCurrentScreenY; if (m_oMover.HasMovementFinished(iCurrentTime)) HandleMovementFinished(iCurrentTime); else HandleMovementNotFinished(iCurrentTime); RedrawObjects(); }
/** Handle the update action, moving the object and/or handling any game logic */ void BouncingBall2::DoUpdate( int iCurrentTime ) { if ( GetEngine()->IsKeyPressed( SDLK_UP ) ) m_dSY -= 0.01; if ( GetEngine()->IsKeyPressed( SDLK_DOWN ) ) m_dSY += 0.01; if ( GetEngine()->IsKeyPressed( SDLK_LEFT ) ) m_dSX -= 0.01; if ( GetEngine()->IsKeyPressed( SDLK_RIGHT ) ) m_dSX += 0.01; if ( GetEngine()->IsKeyPressed( SDLK_SPACE ) ) m_dSX = m_dSY = 0; m_dX += m_dSX; m_dY += m_dSY; if ( (m_dX+m_iStartDrawPosX) < 0 ) { m_dX = - m_iStartDrawPosX; if ( m_dSX < 0 ) m_dSX = -m_dSX; } if ( (m_dX+m_iStartDrawPosX+m_iDrawWidth) > (GetEngine()->GetScreenWidth()-1) ) { m_dX = GetEngine()->GetScreenWidth() -1 - m_iStartDrawPosX - m_iDrawWidth; if ( m_dSX > 0 ) m_dSX = -m_dSX; } if ( (m_dY+m_iStartDrawPosY) < 0 ) { m_dY = -m_iStartDrawPosY; if ( m_dSY < 0 ) m_dSY = -m_dSY; } if ( (m_dY+m_iStartDrawPosY+m_iDrawHeight) > (GetEngine()->GetScreenHeight()-1) ) { m_dY = GetEngine()->GetScreenHeight() -1 - m_iStartDrawPosY - m_iDrawHeight; if ( m_dSY > 0 ) m_dSY = -m_dSY; } // Work out current position m_iCurrentScreenX = (int)(m_dX+0.5); m_iCurrentScreenY = (int)(m_dY+0.5); // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void DemoBObject::DoUpdate(int iCurrentTime) { // Change position if player presses a key if (GetEngine()->IsKeyPressed(SDLK_UP)) m_iCurrentScreenY -= 2; if (GetEngine()->IsKeyPressed(SDLK_DOWN)) m_iCurrentScreenY += 2; if (GetEngine()->IsKeyPressed(SDLK_LEFT)) m_iCurrentScreenX -= 2; if (GetEngine()->IsKeyPressed(SDLK_RIGHT)) m_iCurrentScreenX += 2; // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Demo2Object::DoUpdate( int iCurrentTime ) { int testX = 565; int testY = 580; int time =0; m_iPreviousScreenX = m_iCurrentScreenX; m_iPreviousScreenY = m_iCurrentScreenY; int targetx =0; int targety = 0; targetx = testX-m_iPreviousScreenX ; targety = testY-m_iPreviousScreenY; if(m_iCurrentScreenX >GetEngine()->GetScreenWidth()-10 ) m_iCurrentScreenX = m_iPreviousScreenX - 5; m_iCurrentScreenY = m_iPreviousScreenY - 5; if(m_iCurrentScreenX < 300 && m_iCurrentScreenY >=550) m_iCurrentScreenX = m_iPreviousScreenX + 5; m_iCurrentScreenY = m_iPreviousScreenY + 5; //m_iCurrentScreenX = m_iCurrentScreenX; + targetx * 100*time; //m_iCurrentScreenY = m_iCurrentScreenY; + targety * 100*time; //m_iCurrentScreenX = m_iPreviousScreenX + (rand()%121 - 60)/30; //m_iCurrentScreenY = m_iPreviousScreenY + (rand()%121 - 60)/30; // Ensure not off the screen if ( m_iCurrentScreenX < 50 ) m_iCurrentScreenX = 100; if ( m_iCurrentScreenX >= 750 ) m_iCurrentScreenX = 400; if ( m_iCurrentScreenY >= 550 ) m_iCurrentScreenY = 10; /*if ( m_iCurrentScreenY < 50 ) m_iCurrentScreenY = 10;*/ // Ensure that the object gets redrawn on the display, if something changed time +=1; RedrawObjects(); }
void Psyml5Object1::DoUpdate(int iCurrentTime) { // Change position if player presses a key if (GetEngine()->IsKeyPressed(SDLK_UP) || GetEngine()->IsKeyPressed(SDLK_w)) m_iCurrentScreenY -= 5; if (GetEngine()->IsKeyPressed(SDLK_DOWN) || GetEngine()->IsKeyPressed(SDLK_s)) m_iCurrentScreenY += 5; if (GetEngine()->IsKeyPressed(SDLK_LEFT) || GetEngine()->IsKeyPressed(SDLK_a)) m_iCurrentScreenX -= 5; if (GetEngine()->IsKeyPressed(SDLK_RIGHT) || GetEngine()->IsKeyPressed(SDLK_d)) m_iCurrentScreenX += 5; if (GetEngine()->IsKeyPressed(SDLK_q)){ m_iCurrentScreenX -= 5; m_iCurrentScreenY -= 5; } if (GetEngine()->IsKeyPressed(SDLK_z)){ m_iCurrentScreenX -= 5; m_iCurrentScreenY += 5; } if (GetEngine()->IsKeyPressed(SDLK_e)){ m_iCurrentScreenX += 5; m_iCurrentScreenY -= 5; } if (GetEngine()->IsKeyPressed(SDLK_c)){ m_iCurrentScreenX += 5; m_iCurrentScreenY += 5; } if (m_iCurrentScreenX < 0) m_iCurrentScreenX = rand() % (ava_x); if (m_iCurrentScreenX >= ava_x) m_iCurrentScreenX = rand() % (ava_x); if (m_iCurrentScreenY >= ava_y) m_iCurrentScreenY = rand() % (ava_y); if (m_iCurrentScreenY < 0) m_iCurrentScreenY = rand() % (ava_y); if (GetEngine()->IsKeyPressed(SDLK_p)) if (m_iCurrentScreenX >0 && m_iCurrentScreenX <ava_x && m_iCurrentScreenY >0 && m_iCurrentScreenY <ava_y) Draw(); // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Jxt03uEnemy::DoUpdate(int iCurrentTime) { YVelocity += gravity; ChasePlayer(); if (m_iCurrentScreenX < 0) m_iCurrentScreenX = 0; if (m_iCurrentScreenX >= GetEngine()->GetScreenWidth() - m_iDrawWidth) m_iCurrentScreenX = GetEngine()->GetScreenWidth() - m_iDrawWidth; if (m_iCurrentScreenY < 0) m_iCurrentScreenY = 0; if (m_iCurrentScreenY >= GetEngine()->GetScreenHeight() - m_iDrawHeight) m_iCurrentScreenY = GetEngine()->GetScreenHeight() - m_iDrawHeight; RedrawObjects(); }
void Nxs13uPlayerTwoObject::DoUpdate(int currentTime) { // Change position if player presses a key if (GetEngine()->IsKeyPressed(SDLK_UP)) //player 1 up { m_iCurrentScreenY -= 2; } if (GetEngine()->IsKeyPressed(SDLK_DOWN)) //player 1 down { m_iCurrentScreenY += 2; } if (GetEngine()->IsKeyPressed(SDLK_LEFT)) //player 1 left { m_iCurrentScreenX -= 2; } if (GetEngine()->IsKeyPressed(SDLK_RIGHT)) //player 1 right { m_iCurrentScreenX += 2; } //keep the objects in the playing field //so the dimensions of the player field is (0,50), (600,50), (0,550), (600,550) int fieldXmin = 0; int fieldXmax = 600; int fieldYmin = 50; int fieldYmax = 550; if ((m_iCurrentScreenX) < fieldXmin + radius) m_iCurrentScreenX = fieldXmin + radius; if (m_iCurrentScreenX > fieldXmax - radius) m_iCurrentScreenX = fieldXmax - radius; if (m_iCurrentScreenY < fieldYmin + radius) m_iCurrentScreenY = fieldYmin + radius; if (m_iCurrentScreenY > fieldYmax - radius) m_iCurrentScreenY = fieldYmax - radius; // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
/** Handle the update action, moving the object and/or handling any game logic */ void BouncingBall1::DoUpdate( int iCurrentTime ) { // Work out current position m_oMovement.Calculate(iCurrentTime); m_iCurrentScreenX = m_oMovement.GetX(); m_iCurrentScreenY = m_oMovement.GetY(); // If movement has finished then request instructions if ( m_oMovement.HasMovementFinished( iCurrentTime ) ) { printf("%d \n",changeX); /*SetVisible(false); m_oMovement.Reverse();*/ SetVisible(true); changeX = (0+ (rand() % (int)(GetEngine()->GetScreenWidth() - 0 + 1))); printf("%d,%d \n",changeX,GetEngine()->GetScreenWidth()); m_oMovement.Setup(changeX,10,560,GetEngine()->GetScreenHeight()-50,iCurrentTime,iCurrentTime + 1000); //m_oMovement.GetX = 10; //m_oMovement.Calculate(iCurrentTime); // ///*m_iCurrentScreenX = 100; //m_iCurrentScreenY = 10; //m_oMovement.Calculate(iCurrentTime);*/ //m_iCurrentScreenX = m_oMovement.GetX(); //m_iCurrentScreenY = m_oMovement.GetY(); } if ( m_pTileManager->IsValidTilePosition( m_iCurrentScreenX, m_iCurrentScreenY ) ) { int iTileX = m_pTileManager->GetTileXForPositionOnScreen(m_iCurrentScreenX); int iTileY = m_pTileManager->GetTileYForPositionOnScreen(m_iCurrentScreenY); int iCurrentTile = m_pTileManager->GetValue( iTileX, iTileY ); m_pTileManager->UpdateTile( GetEngine(), iTileX, iTileY, iCurrentTile+1 ); } // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Ball::DoUpdate(int iCurrentTime) { // Move with racket if not served yet if (m_dVelocity[0] == 0 && m_dVelocity[1] == 0) m_iCurrentScreenX = m_pRacket->GetXCentre(); m_iCurrentScreenX += m_dVelocity[0]; m_iCurrentScreenY += m_dVelocity[1]; // Bounce the ball off the top screen border if (m_iCurrentScreenY < 0) { // Change movement to opposite direction m_dVelocity[1] *= -1; m_iCurrentScreenY = 1; } // Bounce the ball off the left or right screen border if (m_iCurrentScreenX < 0) { m_dVelocity[0] *= -1; m_iCurrentScreenX = 1; } else if (m_iCurrentScreenX >= GetEngine()->GetScreenWidth() - m_iDrawWidth) { m_dVelocity[0] *= -1; m_iCurrentScreenX = GetEngine()->GetScreenWidth() - m_iDrawWidth; } // Detect collision with racket if (IsCollideRect(this, m_pRacket)) { // Bounce ball off racket Bounce(); // Pass some racket velocity to the ball m_dVelocity[0] += 0.5 * m_pRacket->GetXVelocity(); } // Brick collision detection is in Breakout.cpp RedrawObjects(); }
/** Handle the update action, moving the object and/or handling any game logic */ void BouncingBall::DoUpdate( int iCurrentTime ) { // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Ball::DoUpdate( int iCurrentTime ) { DisplayableObject* pObject; for(int iObjectId = 0; (pObject = GetEngine()->GetDisplayableObject( iObjectId )) != NULL ; iObjectId++ ){ if ( pObject == this ) // This is us, skip it continue; PlayerWall* pWall = (PlayerWall*) pObject; // We have no other objects so no point checking if it's not a player wall, hence no dynamic cast int xx = pWall->GetXCentre() - (pWall->GetDrawWidth()/2); int xx1 = pWall->GetXCentre() + (pWall->GetDrawWidth()/2); int yy = pWall->GetYCentre() - (pWall->GetDrawHeight()/2); int yy1 = pWall->GetYCentre() + (pWall->GetDrawHeight()/2); int x = m_iCurrentScreenX + m_iStartDrawPosX + m_iDrawWidth/2; int x1 = m_iCurrentScreenX + m_iStartDrawPosX + m_iDrawWidth/2; int y = m_iCurrentScreenY + m_iStartDrawPosY + m_iDrawHeight/2; int y1 = m_iCurrentScreenY + m_iStartDrawPosY + m_iDrawHeight/2; if(collide(x, x1, y, y1, xx, xx1, yy, yy1)){ pWall->IncreaseScore(10); m_dSY = -m_dSY; float fDiff = (pObject->GetXCentre() - m_iCurrentScreenX)/400.0; // Bounce angle relative to hit position, lower denomenator means higher angle // Calculating the angle as above also allows a bounce to have a shallower angle than the angle it hit at depending where it hits the player. //m_dSY -= 0.04; // Increase speed m_dSY += pWall->GetPlayer() ? -0.04 : 0.04; m_dSX += -fDiff; // Change horizontal speed } } m_dX += m_dSX; m_dY += m_dSY; if ( (m_dX+m_iStartDrawPosX) < 0 ) { m_dX = - m_iStartDrawPosX; if ( m_dSX < 0 ) m_dSX = -m_dSX; } if ( (m_dX+m_iStartDrawPosX+m_iDrawWidth) > (GetEngine()->GetScreenWidth()-1) ) { m_dX = GetEngine()->GetScreenWidth() -1 - m_iStartDrawPosX - m_iDrawWidth; if ( m_dSX > 0 ) m_dSX = -m_dSX; } if ( (m_dY+m_iStartDrawPosY) < 0 ) { m_dY = -m_iStartDrawPosY; if ( m_dSY < 0 ) m_dSX = m_dSY = 0; // Game over, bot player wins m_pEngine->GameOver(true/*bot*/); } if ( (m_dY+m_iStartDrawPosY+m_iDrawHeight) > (GetEngine()->GetScreenHeight()-1) ) { m_dY = GetEngine()->GetScreenHeight() -1 - m_iStartDrawPosY - m_iDrawHeight; if ( m_dSY > 0 ){ m_dSX = m_dSY = 0; // Game over, top player wins m_pEngine->GameOver(false/*top*/); } } // Work out current position m_iCurrentScreenX = (int)(m_dX+0.5); m_iCurrentScreenY = (int)(m_dY+0.5); // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Demo4Object::DoUpdate( int iCurrentTime ) { m_iPreviousScreenX = m_iCurrentScreenX; m_iPreviousScreenY = m_iCurrentScreenY; /********** EXTRA ********/ // Iterate through the objects // We are looking for one which is too close to us DisplayableObject* pObject; for ( int iObjectId = 0 ; (pObject = m_pMainEngine->GetDisplayableObject( iObjectId ) ) != NULL ; iObjectId++ ) { if ( pObject == this ) // This is us, skip it continue; // If you need to cast to the sub-class type, you must use dynamic_cast, see lecture 19 // We are just using base class parts int iXDiff = pObject->GetXCentre() - m_iCurrentScreenX; int iYDiff = pObject->GetYCentre() - m_iCurrentScreenY; // Estimate the size - by re-calculating it int iTick = iCurrentTime/20; // 1 per 20ms int iFrame = iTick % 30; int iSize = 10 + iFrame; if ( iFrame > 15 ) iSize = 10 + (30-iFrame); int iSizeOther = iSize; // Assume both the same size // Pythagorus' theorum: if ( ((iXDiff*iXDiff)+(iYDiff*iYDiff)) < ((iSizeOther+iSize)*(iSizeOther+iSize)) ) { // Move us to 1,1 and set direction right m_iMapX = 1+rand()%13; m_iMapY = 1+(rand()%2)*8; m_iDir = 1; // Face right m_oMover.Setup( m_iMapX *50 + 25 + 25, //m_iCurrentScreenX, m_iMapY *50 + 25 + 40, //m_iCurrentScreenY, m_iMapX *50 + 25 + 25, m_iMapY *50 + 25 + 40, iCurrentTime, iCurrentTime+400+rand()%200 ); // Ask the mover where the object should be m_oMover.Calculate( iCurrentTime ); m_iCurrentScreenX = m_oMover.GetX(); m_iCurrentScreenY = m_oMover.GetY(); // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); return; } } /********** END EXTRA ********/ // If movement has finished if ( m_oMover.HasMovementFinished(iCurrentTime) ) { Demo4TileManager& tm = m_pMainEngine->GetTileManager(); // Handle any tile that we just moved onto switch ( tm.GetValue( m_iMapX, m_iMapY ) ) { case 2: case 3: case 4: case 5: case 6: case 7: tm.UpdateTile( m_pMainEngine, m_iMapX, m_iMapY, tm.GetValue( m_iMapX, m_iMapY ) + 1 ); break; case 8: tm.UpdateTile( m_pMainEngine, m_iMapX, m_iMapY, 0 ); break; } // Set off a new movement switch( rand() % 10 ) { case 0: // Increase dir by 1 m_iDir = ( m_iDir + 1 )%4; break; case 1: // Reduce dir by 1 m_iDir = ( m_iDir + 3 )%4; break; } // Allow some control over the object by the player if ( m_pMainEngine->IsKeyPressed( SDLK_UP ) ) m_iDir = 0; if ( m_pMainEngine->IsKeyPressed( SDLK_RIGHT ) ) m_iDir = 1; if ( m_pMainEngine->IsKeyPressed( SDLK_DOWN ) ) m_iDir = 2; if ( m_pMainEngine->IsKeyPressed( SDLK_LEFT ) ) m_iDir = 3; switch ( tm.GetValue( m_iMapX + GetXDiffForDirection(m_iDir), m_iMapY + GetYDiffForDirection(m_iDir) ) ) { case 0: // Passageway case 2: // Pellet case 3: // Pellet case 4: // Pellet case 5: // Pellet case 6: // Pellet case 7: // Pellet case 8: // Pellet // Allow move - set up new movement now m_iMapX += GetXDiffForDirection(m_iDir); m_iMapY += GetYDiffForDirection(m_iDir); m_oMover.Setup( m_iCurrentScreenX, m_iCurrentScreenY, m_iMapX *50 + 25 + 25, m_iMapY *50 + 25 + 40, iCurrentTime, iCurrentTime+400+rand()%200 ); break; case 1: // Wall m_iDir = rand()%4; // Rotate randomly break; } } // If making a move then do the move if ( !m_oMover.HasMovementFinished(iCurrentTime) ) { // Ask the mover where the object should be m_oMover.Calculate( iCurrentTime ); m_iCurrentScreenX = m_oMover.GetX(); m_iCurrentScreenY = m_oMover.GetY(); } // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Demo3ObjectFinished::DoUpdate( int iCurrentTime ) { m_iPreviousScreenX = m_iCurrentScreenX; m_iPreviousScreenY = m_iCurrentScreenY; // If movement has finished if ( m_oMover.HasMovementFinished(iCurrentTime) ) { Demo3TileManager& tm = m_pMainEngine->GetTileManager(); // Handle any tile that we just moved onto switch ( tm.GetValue( m_iMapX, m_iMapY ) ) { case 2: case 3: case 4: case 5: case 6: case 7: tm.UpdateTile( m_pMainEngine, m_iMapX, m_iMapY, tm.GetValue( m_iMapX, m_iMapY ) + 1 ); break; case 8: tm.UpdateTile( m_pMainEngine, m_iMapX, m_iMapY, 0 ); break; } // Set off a new movement switch( rand() % 10 ) { case 0: // Increase dir by 1 m_iDir = ( m_iDir + 1 )%4; break; case 1: // Reduce dir by 1 m_iDir = ( m_iDir + 3 )%4; break; } if ( m_pMainEngine->IsKeyPressed( SDLK_UP ) ) m_iDir = 0; if ( m_pMainEngine->IsKeyPressed( SDLK_RIGHT ) ) m_iDir = 1; if ( m_pMainEngine->IsKeyPressed( SDLK_DOWN ) ) m_iDir = 2; if ( m_pMainEngine->IsKeyPressed( SDLK_LEFT ) ) m_iDir = 3; switch ( tm.GetValue( m_iMapX + GetXDiffForDirection(m_iDir), m_iMapY + GetYDiffForDirection(m_iDir) ) ) { case 0: // Passageway case 2: // Pellet case 3: // Pellet case 4: // Pellet case 5: // Pellet case 6: // Pellet case 7: // Pellet case 8: // Pellet // Allow move - set up new movement now m_iMapX += GetXDiffForDirection(m_iDir); m_iMapY += GetYDiffForDirection(m_iDir); m_oMover.Setup( m_iCurrentScreenX, m_iCurrentScreenY, m_iMapX *50 + 25 + 25, m_iMapY *50 + 25 + 40, iCurrentTime, iCurrentTime+400+rand()%200 ); break; case 1: // Wall m_iDir = rand()%4; // Rotate randomly break; } } // If making a move then do the move if ( !m_oMover.HasMovementFinished(iCurrentTime) ) { // Ask the mover where the object should be m_oMover.Calculate( iCurrentTime ); m_iCurrentScreenX = m_oMover.GetX(); m_iCurrentScreenY = m_oMover.GetY(); } // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void Demo3Object::DoUpdate( int iCurrentTime ) { m_iPreviousScreenX = m_iCurrentScreenX; m_iPreviousScreenY = m_iCurrentScreenY; // If movement has finished if ( m_oMover.HasMovementFinished(iCurrentTime) ) { // INSERT POSITION A // Set off a new movement switch( rand() % 10 ) { case 0: // Increase dir by 1 m_iDir = ( m_iDir + 1 )%4; break; case 1: // Reduce dir by 1 m_iDir = ( m_iDir + 3 )%4; break; } // INSERT POSITION C // REPLACE THE FOLLOWING CODE FOR CHANGE B if ( m_iMapX + GetXDiffForDirection(m_iDir) > 14 ) m_iDir = 3; if ( m_iMapX + GetXDiffForDirection(m_iDir) < 0 ) m_iDir = 1; if ( m_iMapY + GetYDiffForDirection(m_iDir) > 10 ) m_iDir = 0; if ( m_iMapY + GetYDiffForDirection(m_iDir) < 0 ) m_iDir = 2; // Allow move - set up new movement now m_iMapX += GetXDiffForDirection(m_iDir); m_iMapY += GetYDiffForDirection(m_iDir); m_oMover.Setup( m_iCurrentScreenX, m_iCurrentScreenY, m_iMapX *50 + 25 + 25, m_iMapY *50 + 25 + 40, iCurrentTime, iCurrentTime+400+rand()%200 ); // END OF REPLACE THE FOLLOWING CODE FOR CHANGE B } // If making a move then do the move if ( !m_oMover.HasMovementFinished(iCurrentTime) ) { // Ask the mover where the object should be m_oMover.Calculate( iCurrentTime ); m_iCurrentScreenX = m_oMover.GetX(); m_iCurrentScreenY = m_oMover.GetY(); } // Ensure that the object gets redrawn on the display, if something changed RedrawObjects(); }
void GameMainObject::DoUpdate(int iCurrentTime) { m_iPreviousScreenX = m_iCurrentScreenX; m_iPreviousScreenY = m_iCurrentScreenY; counterTime++; if(counterTime > 7) { counterTime = 0; if (m_iPlayerSpriteX == 0) { m_iPlayerSpriteX +=121; } else if(m_iPlayerSpriteX == 121) { m_iPlayerSpriteX +=121; } else if(m_iPlayerSpriteX == 242) { m_iPlayerSpriteX +=121; } else if(m_iPlayerSpriteX == 363) { m_iPlayerSpriteX +=121; } else if(m_iPlayerSpriteX == 484) { m_iPlayerSpriteX +=121; } else { m_iPlayerSpriteX = 0; } } if(isOnGround()) { velocityX*=0.9; if(GetEngine()->IsKeyPressed(SDLK_SPACE)) { if(jumpAgain == true){ velocityY=-10; jumpAgain = false; } } else { jumpAgain = true; } } if(!isOnGround()) { velocityY+=0.5; } m_iCurrentScreenY += velocityY+(0.5); m_iCurrentScreenYObject = m_iCurrentScreenY; if((m_iCurrentScreenY+m_iDrawHeight)>=521) { m_iCurrentScreenY=521-m_iDrawHeight; m_iCurrentScreenYObject = m_iCurrentScreenY; } RedrawObjects(); }
void Nxs13uEnemyObject::DoUpdate(int currentTime) { //keep the objects in the playing field //so the dimensions of the player field is (0,50), (600,50), (0,550), (600,550) int fieldXmin = 0; int fieldXmax = 600; int fieldYmin = 50; int fieldYmax = 550; if (id == 3){ //for start page, enemy must be able to travel anywhere fieldXmin = 0; fieldXmax = GetEngine()->GetScreenWidth(); fieldYmin = 0; fieldYmax = GetEngine()->GetScreenHeight(); } // Alter position for speed - this makes the ball appear to vibrate // remember that speed is basically pixels travelled per tick m_iCurrentScreenX += (speedX * rand() % (difficulty + 1)); m_iCurrentScreenY += (speedY * rand() % (difficulty + 1)); // Check for bounce off the edge if (m_iCurrentScreenX < fieldXmin + radius) { m_iCurrentScreenX = fieldXmin + radius; if (speedX < 0) speedX = -speedX; } if (m_iCurrentScreenX > fieldXmax - radius) { m_iCurrentScreenX = fieldXmax - radius; if (speedX > 0) speedX = -speedX; } if (m_iCurrentScreenY < fieldYmin + radius) { m_iCurrentScreenY = fieldYmin + radius; if (speedY < 0) speedY = -speedY; } if (m_iCurrentScreenY > fieldYmax - radius) { m_iCurrentScreenY = fieldYmax - radius; if (speedY > 0) speedY = -speedY; } // Set current position m_iCurrentScreenX = (int)(m_iCurrentScreenX + speedX); m_iCurrentScreenY = (int)(m_iCurrentScreenY + speedY); //dynamic cast DisplayableObject* playerObject; //COLLISIONS if (id == 2){ //FOR GAME MODE ENEMY (not start-up page enemy) for (int objectId = 0; (playerObject = m_pMainEngine->GetDisplayableObject(objectId)) != NULL; objectId++) { if (playerObject == this) // This is us, skip it continue; //get the difference between the player and the enemy int playerSize = 40; int xDifference = playerObject->GetXCentre() - m_iCurrentScreenX; int yDifference = playerObject->GetYCentre() - m_iCurrentScreenY; // Pythagorus' theorum, if the distance is smaller than the size (radius size of both objects), then it is a collission if (((xDifference*xDifference) + (yDifference*yDifference)) < ((playerSize + radius)*(playerSize + radius))) { //move 70 pixels away from user and go in opposite direction (prevents re-collision) speedX = -speedX; speedY = -speedY; if (speedX < 0 || m_iCurrentScreenX > 530) m_iCurrentScreenX = m_iCurrentScreenX - 70; else m_iCurrentScreenX = m_iCurrentScreenX + 70; if (speedY < 0 || m_iCurrentScreenY > 480) m_iCurrentScreenY = m_iCurrentScreenY - 70; else m_iCurrentScreenY = m_iCurrentScreenY + 70; collision = true; //check which player it collided with - lives are passed back to Nxs13uMain if (objectId == 0) player1lives--; else if (objectId == 1) player2lives--; } // end of collissions } } RedrawObjects(); }