c++游戏编程:创建3D游戏第一章练习

1

删除右面的墙:
	// Draw right wall
	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)
- 删除如下代码:
	// Did the ball collide with the right wall?
	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;
  • 绘制右paddle
	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;
	}
  • 根据输入更新右Paddle的位置
	// Update rightpaddle position based on direction
	if (mRightPaddleDir != 0)
	{
		mRightPaddlePos.y += mRightPaddleDir * 300.0f * deltaTime;
		// Make sure paddle doesn't move off screen!
		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;
		}
	}
  • 更新ball碰到右paddle的行为
	float rightDiff = mRightPaddlePos.y - mBallPos.y;
	// Take absolute value of difference
	rightDiff = (rightDiff > 0.0f) ? rightDiff : -rightDiff ;
	if (
		// Our y-difference is small enough
		rightDiff <= paddleH / 2.0f &&
		// We are in the correct x-position
		mBallPos.x >=999.0f &&
		// The ball is moving to the left
		mBallVel.x > 0.0f)
	{
		mBallVel.x *= -1.0f;
	}

	// Did the ball go off the screen? (if so, end game)
	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;
  • 删除原来的
	// Position of ball
	Vector2 mBallPos;
	// Velocity of ball
	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);
  • 球的移动和碰撞检测
	// Update balls position based on ball velocity
	for (auto b = begin(Balls);b!=end(Balls);++b)
	{
		b->BallPos.x += b->BallVel.x*deltaTime;
		b->BallPos.y += b->BallVel.y*deltaTime;

		// Bounce if needed
// Did we intersect with the paddle?
		float diff = mPaddlePos.y - b->BallPos.y;
		// Take absolute value of difference
		diff = (diff > 0.0f) ? diff : -diff;
		if (
			// Our y-difference is small enough
			diff <= paddleH / 2.0f &&
			// We are in the correct x-position
			b->BallPos.x <= 25.0f && b->BallPos.x >= 20.0f &&
			// The ball is moving to the left
			b->BallVel.x < 0.0f)
		{
			b->BallVel.x *= -1.0f;
		}

		float rightDiff = mRightPaddlePos.y - b->BallPos.y;
		// Take absolute value of difference
		rightDiff = (rightDiff > 0.0f) ? rightDiff : -rightDiff;
		if (
			// Our y-difference is small enough
			rightDiff <= paddleH / 2.0f &&
			// We are in the correct x-position
			b->BallPos.x >= 999.0f &&
			// The ball is moving to the left
			b->BallVel.x > 0.0f)
		{
			b->BallVel.x *= -1.0f;
		}


		// Did the ball go off the screen? (if so, end game)
		else if (b->BallPos.x <= 0.0f || b->BallPos.x >= (1024.0f-thickness))
		{
			mIsRunning = false;
		}
		// Did the ball collide with the top wall?
		if (b->BallPos.y <= thickness && b->BallVel.y < 0.0f)
		{
			b->BallVel.y *= -1;
		}
		// Did the ball collide with the bottofm wall?
		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)
	{
		// Draw ball
		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);
	}

你可能感兴趣的:(C++)