Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

What just happened?

What are we doing here? At the beginning, the sky's left border is the same as the view's left border, both at point (0, 0). At the end, when Benjamin has walked to the maximum right, the sky's right border should be the same as the view's right border. So, at this position, the shift of the sky will be equal to the sky's width (m_sky->boundingRect().width()) minus the width of the view (width()). The shift of the sky depends on the position of the camera and, consequently, the value of the m_worldShift variable; if it is far to the left, the sky isn't shifted, and if the camera is far to the right, the sky is maximally shifted. Thus, we have to multiply the sky's maximum shift value with a factor based on the current position of the camera. The relation to the camera's position is the reason this is handled in the movePlayer() function. The factor we have to calculate has to be between 0 and 1. So we get the minimum shift (0 * shift, which equals 0) and the maximum shift (1 * shift, which equals shift). We name this factor as ratio.

How far the world was shifted is saved in m_worldShift, so by dividing m_worldShift by maxWorldShift, we get the needed factor. It is 0 when the player is to the far left and 1 if they are to the far right. Then, we have to simply multiply ratio with the maximum shift of the sky.

The same calculation is used for the other background items, so it is moved to a separate function. The calculation also explains why a smaller image is moving slower. It's because the overlap of the smaller image is less than that of the larger one, and since the backgrounds are moved in the same time period, the larger one has to move faster.