C++游戏编程:创建3D游戏第一章习题

记录C++游戏开发学习过程

练习题1:增加玩家2

  • 删除右面的墙壁
	//draw the right wall
	//wall.x = width - thickness; 
	//wall.y = 0;
	//wall.w = thickness;
	//wall.h = height;
	//SDL_RenderFillRect(mRenderer, &wall);
  • 新增2号玩家
	Vector2 mRightPaddlePos;
	int mRightPaddleDir;
  • 确保二号玩家不会移出屏幕
	else if (mPaddlePos.y > long int (height - thickness) - paddleH / 2.0 ||
			mRightPaddlePos.y > long int(height - thickness) - paddleH / 2.0) {
   
				mRightPaddlePos.y = height - thickness - paddleH / 2.0;
		}
  • 球是否与二号玩家碰撞
	//Did ball intersect with the right paddle?
	else if (dist_right <= paddleH / 2.0 && ball->BallPos.x > width - thickness * 1.5
		&& ball->BallPos.x < width - thickness && ball->BallVec.x > 0) {
   
		ball->BallVec.x *= -1;
		}
  • 球是否出界
	else if (ball->BallPos.x < 0 || ball->BallPos.x > width) {
   
			mIsRunning = false;
		}

练习题2. 多球

  • 修改结构体Vector2
struct Vector2 {
   
	float x;
	float y;

	Vector2(float _x, float _y) {
   
		x = _x;
		y = _y;
	}
	Vector2() {
   }
};
  • 新增结构体Ball
struct Ball {
   
	Vector2 BallPos;
	Vector2 BallVec;

	Ball(Vector2 pos, Vector2 vec) {
   
		BallPos = pos;
		BallVec = vec;
	}
};
  • 新增成员变量容器mBALLS
std::vector<Ball> mBalls;
  • 初始化双球
	Ball ball1(Vector2(width / 2.0f, height / 2.0f), Vector2(-200.0f, 235.0f));
	Ball ball2(Vector2(width / 2.0f, height / 2.0f), Vector2(200.0f, -235.0f));
	mBalls.push_back(ball1);
	mBalls.push_back(ball2);
  • 绘制双球
for (auto ball = mBalls.begin(); ball != mBalls.end(); ball++) {
   

		SDL_Rect b{
   
			static_cast<int>(ball->BallPos.x - thickness / 2), //static_cast<type>: forced type conversion
			static_cast<int>(ball->BallPos.y - thickness / 2),
			thickness,
			thickness
		};
		SDL_RenderFillRect(mRenderer, &b);
	}
  • 双球移动以及碰撞检测
for (auto ball = mBalls.begin(); ball != mBalls.end(); ball++) {
   

		ball->BallPos.x += ball->BallVec.x * deltaTime;
		ball->BallPos.y += ball->BallVec.y * deltaTime;

		float dist_left = fabs(double(ball->BallPos.y) - mPaddlePos.y);
		float dist_right = fabs(double(ball->BallPos.y) - mRightPaddlePos.y);
		//Did ball intersect with the paddle?
		if (dist_left <= paddleH / 2.0 && ball->BallPos.x >= thickness &&
			ball->BallPos.x <= thickness * 1.5 && ball->BallVec.x < 0) {
   
			ball->BallVec.x *= -1;
		}
		//Did ball intersect with the right paddle?
		else if (dist_right <= paddleH / 2.0 && ball->BallPos.x > width - thickness * 1.5
			&& ball->BallPos.x < width - thickness && ball->BallVec.x > 0) {
   
			ball->BallVec.x *= -1;
		}
		//Did the ball go off the screen?
		else if (ball->BallPos.x < 0 || ball->BallPos.x > width) {
   
			mIsRunning = false;
		}
		//reach the top wall
		if (ball->BallPos.y <= thickness * 1.5 && ball->BallPos.y < 0) {
    
			ball->BallVec.y *= -1;
		}
		/*//reach the right wall else if (mBallPos.x >= width - thickness*1.5 && mBallVec.x > 0) { mBallVec.x *= -1; }*/
		//reach the bottom wall
		else if (ball->BallPos.y >= height - thickness * 1.5 && ball->BallPos.y > 0) {
    
			ball->BallVec.y *= -1;
		}

3. 游戏运行截图

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务