1
删除右面的墙:
wall.x = 1024 - thickness;
wall.y = 0;
wall.w = thickness;
wall.h = 1024;
SDL_RenderFillRect(mRenderer, &wall);
增加判断失败条件:
if (mBallPos.x <= 0.0f||mBallPos.x>=1024.0f)
- 删除如下代码:
else if (mBallPos.x >= (1024.0f - thickness) && mBallVel.x > 0.0f)
{
mBallVel.x *= -1.0f;
}
对原有代码一些补充
- paddlePos是paddle的中心,所以绘制的时候应该是
static_cast(mPaddlePos.x-thickness/2),
绘制右paddle
int mRightPaddleDir;
Vector2 mRightPaddlePos;
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
,mTicksCount(0)
,mIsRunning(true)
,mPaddleDir(0)
,mRightPaddleDir(0)
{
}
mRightPaddlePos.x = 1230.0f;
mRightPaddlePos.y = 768.0f / 2.0f;
SDL_Rect rightPaddle{
static_cast<int>(mRightPaddlePos.x-thickness/2),
static_cast<int>(mRightPaddlePos.y-paddleH/2),
thickness,
static_cast<int>(paddleH)
};
SDL_RenderFillRect(mRenderer, &rightPaddle);
if (state[SDL_SCANCODE_I])
{
mRightPaddleDir -= 1;
}
if (state[SDL_SCANCODE_K])
{
mRightPaddleDir += 1;
}
if (mRightPaddleDir != 0)
{
mRightPaddlePos.y += mRightPaddleDir * 300.0f * deltaTime;
if (mRightPaddlePos.y < (paddleH / 2.0f + thickness))
{
mRightPaddlePos.y = paddleH / 2.0f + thickness;
}
else if (mRightPaddlePos.y > (768.0f - paddleH / 2.0f - thickness))
{
mRightPaddlePos.y = 768.0f - paddleH / 2.0f - thickness;
}
}
float rightDiff = mRightPaddlePos.y - mBallPos.y;
rightDiff = (rightDiff > 0.0f) ? rightDiff : -rightDiff ;
if (
rightDiff <= paddleH / 2.0f &&
mBallPos.x >=999.0f &&
mBallVel.x > 0.0f)
{
mBallVel.x *= -1.0f;
}
else if (mBallPos.x <= 0.0f||mBallPos.x>=1024.0f)
{
mIsRunning = false;
}
2 多球增加,面向对象的设计
typedef struct Ball {
Vector2 BallPos;
Vector2 BallVel;
Ball(Vector2 ballPos, Vector2 ballVel)
{
BallPos = ballPos;
BallVel = ballVel;
}
}Ball;
struct Vector2
{
float x;
float y;
Vector2(float _x, float _y)
{
x = _x;
y = _y;
}
Vector2()
{
}
};
vector<Ball> Balls;
Vector2 mBallPos;
Vector2 mBallVel;
Ball ball1(Vector2(1024.0f / 2.0f,768.0f / 2.0f),Vector2(-200.0f,235.0f));
Ball ball2(Vector2(1024.0f / 2.0f, 768.0f / 2.0f), Vector2(200.0f, -235.0f));
Balls.push_back(ball1);
Balls.push_back(ball2);
for (auto b = begin(Balls);b!=end(Balls);++b)
{
b->BallPos.x += b->BallVel.x*deltaTime;
b->BallPos.y += b->BallVel.y*deltaTime;
float diff = mPaddlePos.y - b->BallPos.y;
diff = (diff > 0.0f) ? diff : -diff;
if (
diff <= paddleH / 2.0f &&
b->BallPos.x <= 25.0f && b->BallPos.x >= 20.0f &&
b->BallVel.x < 0.0f)
{
b->BallVel.x *= -1.0f;
}
float rightDiff = mRightPaddlePos.y - b->BallPos.y;
rightDiff = (rightDiff > 0.0f) ? rightDiff : -rightDiff;
if (
rightDiff <= paddleH / 2.0f &&
b->BallPos.x >= 999.0f &&
b->BallVel.x > 0.0f)
{
b->BallVel.x *= -1.0f;
}
else if (b->BallPos.x <= 0.0f || b->BallPos.x >= (1024.0f-thickness))
{
mIsRunning = false;
}
if (b->BallPos.y <= thickness && b->BallVel.y < 0.0f)
{
b->BallVel.y *= -1;
}
else if (b->BallPos.y >= (768 - thickness) &&
b->BallVel.y > 0.0f)
{
b->BallVel.y *= -1;
}
}
for (auto b = begin(Balls); b != end(Balls); ++b)
{
SDL_Rect ball{
static_cast<int>(b->BallPos.x - thickness / 2),
static_cast<int>(b->BallPos.y - thickness / 2),
thickness,
thickness
};
SDL_RenderFillRect(mRenderer, &ball);
}