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<int>(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);
	}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JustEasyCode

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值