我使用SDL2.0为图形编写了一个用C++编写的乒乓球游戏。关于这个代码,我有几个问题:
load_rendered_text()函数接受2种文本,并返回2种纹理,当它只需要1件文本来处理1种纹理时,会被调用两次。而且,我的Ball::move_ball()函数有超过10个if语句,检查各种条件。怎么解决这个问题呢?乒乓游戏代码
class Ball
{
public:
const int BALL_WIDTH = 15;
const int BALL_HEIGHT = 15;
const float BALL_VELOCITY = 2;
Ball();
void init_ball_direction();
void move_ball(float timeStep, SDL_Rect playerPaddle, SDL_Rect aiPaddle);
void render_ball();
private:
float ballPositionX, ballPositionY;
float ballVelocityX, ballVelocityY;
bool isMovingDown, isMovingUp;
bool isMovingLeft, isMovingRight;
SDL_Rect bCollider;
};
...
class AI
{
public:
const float AI_VELOCITY = 3;
void init_ai();
void move_ai();
void handle_ai_events();
SDL_Rect aiPaddle;
private:
float aiYPosition;
float aiVelocity;
};
...
void Ball::move_ball(float timeStep, SDL_Rect playerPaddle, SDL_Rect aiPaddle)
{
//move the ball, its collider, and the raycast. Check for collisions with the paddles
//by calling the check_collision() function.
if(ballPositionY + BALL_HEIGHT > SCREEN_HEIGHT){
isMovingDown = false;
isMovingUp = true;
}
if(ballPositionY < 0){
isMovingDown = true;
isMovingUp = false;
}
if(ballPositionX + BALL_WIDTH > SCREEN_WIDTH){
ballPositionX = SCREEN_WIDTH/2;
}
if(ballPositionX < 0){
ballPositionX = SCREEN_WIDTH/2;
}
if(check_collision(bCollider, playerPaddle)){
isMovingLeft = false;
isMovingRight = true;
}
if(check_collision(bCollider, aiPaddle)){
isMovingRight = false;
isMovingLeft = true;
}
if(isMovingUp){
ballPositionY -= ballVelocityY;
bCollider.y = ballPositionY;
bRaycastLine.y = ballPositionY;
}
if(isMovingDown){
ballPositionY += ballVelocityY;
bCollider.y = ballPositionY;
bRaycastLine.y = ballPositionY;
}
if(isMovingLeft){
ballPositionX -= ballVelocityX;
bCollider.x = ballPositionX;
bRaycastLine.x = ballPositionX;
}
if(isMovingRight){
ballPositionX += ballVelocityX;
bCollider.x = ballPositionX;
bRaycastLine.x = ballPositionX;
}
}
...
void AI::init_ai()
{
//initialize the AI, not much here becaue the AI is very basic.
aiPaddle.h = 40;
aiPaddle.w = 10;
aiPaddle.x = SCREEN_WIDTH - aiPaddle.w;
aiPaddle.y = SCREEN_HEIGHT/2 - aiPaddle.h/2;
aiYPosition = aiPaddle.y;
aiVelocity = 0;
}
void AI::handle_ai_events()
{
//check if the raycasted line is above/below the AI paddle...
if(bRaycastLine.y > aiPaddle.y + aiPaddle.h){
aiVelocity += AI_VELOCITY;
} else if(bRaycastLine.y < aiPaddle.y){
aiVelocity -= AI_VELOCITY;
}
}
void AI::move_ai()
{
//apply the velocities to the AI...
aiYPosition = aiVelocity;
aiPaddle.y = aiYPosition;
if(aiYPosition < 0){
aiYPosition -= aiVelocity;
aiPaddle.y = aiYPosition;
}
if(aiYPosition > aiPaddle.y + SCREEN_HEIGHT){
aiYPosition += aiVelocity;
aiPaddle.y = aiYPosition;
}
}bRaycastLine是一个跟踪球位置的SDL_Rect。
发布于 2014-12-28 21:49:44
一种让人工智能“蠢蛋”的方法是给它一个障碍。你可以限制它的移动速度,直到它不总是赢。
另一种选择是在球弹出后随机化球的轨迹,然后给出关于球轨迹的有限信息,而是将其表示为潜在状态的集合,当球离AI桨越来越近时,它就会变窄。
为了使用这样的方法,你需要首先使人工智能变得更聪明,这样它就可以进行轨迹预测,并且仍然能够以有限的信息做出现实的反应。
不用说,这就更难了。
发布于 2014-12-29 04:05:44
我不清楚你是否需要存储变量isMovingLeft..。假设你没有,你可以让函数move_ball简单一点。
void Ball::move_ball(float timeStep, SDL_Rect playerPaddle, SDL_Rect aiPaddle)
{
int upDown = 0;
int leftRight = 0;
//move the ball, its collider, and the raycast. Check for collisions with the paddles
//by calling the check_collision() function.
if(ballPositionY + BALL_HEIGHT > SCREEN_HEIGHT){
upDown = 1;
}
if(ballPositionY < 0){
upDown = -1;
}
if(ballPositionX + BALL_WIDTH > SCREEN_WIDTH){
ballPositionX = SCREEN_WIDTH/2;
}
if(ballPositionX < 0){
ballPositionX = SCREEN_WIDTH/2;
}
if(check_collision(bCollider, playerPaddle)){
leftRight = 1;
}
if(check_collision(bCollider, aiPaddle)){
leftRight = -1;
}
ballPositionY -= upDown*ballVelocityY;;
ballPositionX -= leftRight*ballVelocityX;
if ( upDown != 0 ) {
bCollider.y = ballPositionY;
bRaycastLine.y = ballPositionY;
}
if ( leftRight != 0 ) {
bCollider.x = ballPositionX;
bRaycastLine.x = ballPositionX;
}
}发布于 2015-02-12 23:46:33
有几件事在脑海中浮现:
https://codereview.stackexchange.com/questions/75082
复制相似问题