我正在制作一个"突破"克隆,现在球移动得太快了。为了减慢它的速度,我尝试将变量转换为浮点数并使用小数,但之后球根本没有移动。
帮我弄清楚如何放慢速度。
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <ctime>
//The screen attributes
const int SCREEN_WIDTH = 1008;
const int SCREEN_HEIGHT = 720;
const int SCREEN_BPP = 32;
const int BALL_X = 500;
const int BALL_Y = 600;
Uint32 white;
//The surfaces
SDL_Surface *mainScreen = NULL;
SDL_Surface *difficultyScreen = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//Rects
SDL_Rect Paddle;
SDL_Rect Ball;
SDL_Rect Block1;
SDL_Rect Block2;
SDL_Rect Block3;
SDL_Rect Block4;
SDL_Rect Block5;
SDL_Rect Block6;
SDL_Rect Block7;
SDL_Rect Block8;
SDL_Rect Block9;
SDL_Rect Block10;
SDL_Rect Block11;
SDL_Rect Block12;
SDL_Rect Block13;
SDL_Rect Block14;
SDL_Rect Block15;
SDL_Rect Block16;
SDL_Rect Block17;
SDL_Rect Block18;
SDL_Rect Block19;
SDL_Rect Block20;
SDL_Rect Block21;
SDL_Rect Block22;
SDL_Rect Block23;
SDL_Rect Block24;
SDL_Rect Block25;
SDL_Rect Block26;
int xVel, yVel;
bool PointInRect(int x, int y, SDL_Rect rec)
{
if (x > rec.x && y > rec.y && x < rec.x + rec.w && y < rec.y + rec.h)
{
return true;
}
return false;
}
bool CheckCollision(SDL_Rect r1, SDL_Rect r2)
{
if (PointInRect(r1.x, r1.y, r2) == true ||
PointInRect(r1.x + r1.w, r1.y, r2) == true ||
PointInRect(r1.x, r1.y + r1.h, r2) == true ||
PointInRect(r1.x + r1.w, r1.y + r1.h, r2) == true)
{
return true;
}
return false;
}
void ResetBall()
{
Ball.x = BALL_X;
Ball.y = BALL_Y;
xVel = 1;
yVel = 1;
}
void LoadGame()
{
Paddle.x = 425;
Paddle.y = 650;
Paddle.h = 20;
Paddle.w = 150;
Ball.x = BALL_X;
Ball.y = BALL_Y;
Ball.h = 15;
Ball.w = 15;
Block1.x = 58;
Block1.y = 28;
Block1.h = 15;
Block1.w = 100;
Block2.x = 216;
Block2.y = 28;
Block2.h = 15;
Block2.w = 100;
Block3.x = 374;
Block3.y = 28;
Block3.h = 15;
Block3.w = 100;
Block4.x = 533;
Block4.y = 28;
Block4.h = 15;
Block4.w = 100;
Block5.x = 691;
Block5.y = 28;
Block5.h = 15;
Block5.w = 100;
Block6.x = 849;
Block6.y = 28;
Block6.h = 15;
Block6.w = 100;
Block7.x = 84;
Block7.y = 71;
Block7.h = 15;
Block7.w = 100;
Block8.x = 269;
Block8.y = 71;
Block8.h = 15;
Block8.w = 100;
Block9.x = 453;
Block9.y = 71;
Block9.h = 15;
Block9.w = 100;
Block10.x = 638;
Block10.y = 71;
Block10.h = 15;
Block10.w = 100;
Block11.x = 822;
Block11.y = 71;
Block11.h = 15;
Block11.w = 100;
Block12.x = 23;
Block12.y = 114;
Block12.h = 15;
Block12.w = 100;
Block13.x = 146;
Block13.y = 114;
Block13.h = 15;
Block13.w = 100;
Block14.x = 269;
Block14.y = 114;
Block14.h = 15;
Block14.w = 100;
Block15.x = 392;
Block15.y = 114;
Block15.h = 15;
Block15.w = 100;
Block16.x = 516;
Block16.y = 114;
Block16.h = 15;
Block16.w = 100;
Block17.x = 639;
Block17.y = 114;
Block17.h = 15;
Block17.w = 100;
Block18.x = 762;
Block18.y = 114;
Block18.h = 15;
Block18.w = 100;
Block19.x = 885;
Block19.y = 114;
Block19.h = 15;
Block19.w = 100;
Block20.x = 88;
Block20.y = 157;
Block20.h = 15;
Block20.w = 100;
Block21.x = 820;
Block21.y = 157;
Block21.h = 15;
Block21.w = 100;
Block22.x = 138;
Block22.y = 200;
Block22.h = 15;
Block22.w = 100;
Block23.x = 296;
Block23.y = 200;
Block23.h = 15;
Block23.w = 100;
Block24.x = 454;
Block24.y = 200;
Block24.h = 15;
Block24.w = 100;
Block25.x = 612;
Block25.y = 200;
Block25.h = 15;
Block25.w = 100;
Block26.x = 770;
Block26.y = 200;
Block26.h = 15;
Block26.w = 100;
white = SDL_MapRGB(screen->format, 255, 255, 255);
srand(time(NULL));
ResetBall();
}
void Logic()
{
SDL_Event occur;
SDL_PollEvent(&occur);
Uint8 *keystates = SDL_GetKeyState(NULL);
if (keystates[SDLK_LEFT])
{
Paddle.x -= 1;
}
if (keystates[SDLK_RIGHT])
{
Paddle.x += 1;
}
if (Paddle.x < 1)
{
Paddle.x = 1;
}
if (Paddle.x + Paddle.w > 1007)
{
Paddle.x = 1007 - Paddle.w;
}
//For ball movement
Ball.x += xVel;
Ball.y += yVel;
//Change the direction of the ball if ball hits the walls
if (Ball.y < 1)
{
yVel = -yVel;
}
if (Ball.y + Ball.h > 1007)
{
yVel = -yVel;
}
if (Ball.x < 1)
{
xVel = -xVel;
}
if (Ball.x + Ball.w > 1007)
{
xVel = -xVel;
}
//Change the direction of the ball if ball hits the paddle
if (CheckCollision(Ball, Paddle) == true)
{
yVel = -yVel;
}
//Change the direction of the ball if ball hits the blocks
if (CheckCollision(Ball, Block1) == true)
{
yVel = -yVel;
Block1.x = -200;
Block1.y = -200;
}
if (CheckCollision(Ball, Block2) == true)
{
yVel = -yVel;
Block2.x = -200;
Block2.y = -200;
}
if (CheckCollision(Ball, Block3) == true)
{
yVel = -yVel;
Block3.x = -200;
Block3.y = -200;
}
if (CheckCollision(Ball, Block4) == true)
{
yVel = -yVel;
Block4.x = -200;
Block4.y = -200;
}
if (CheckCollision(Ball, Block5) == true)
{
yVel = -yVel;
Block5.x = -200;
Block5.y = -200;
}
if (CheckCollision(Ball, Block6) == true)
{
yVel = -yVel;
Block6.x = -200;
Block6.y = -200;
}
if (CheckCollision(Ball, Block7) == true)
{
yVel = -yVel;
Block7.x = -200;
Block7.y = -200;
}
if (CheckCollision(Ball, Block8) == true)
{
yVel = -yVel;
Block8.x = -200;
Block8.y = -200;
}
if (CheckCollision(Ball, Block9) == true)
{
yVel = -yVel;
Block9.x = -200;
Block9.y = -200;
}
if (CheckCollision(Ball, Block10) == true)
{
yVel = -yVel;
Block10.x = -200;
Block10.y = -200;
}
if (CheckCollision(Ball, Block11) == true)
{
yVel = -yVel;
Block11.x = -200;
Block11.y = -200;
}
if (CheckCollision(Ball, Block12) == true)
{
yVel = -yVel;
Block12.x = -200;
Block12.y = -200;
}
if (CheckCollision(Ball, Block13) == true)
{
yVel = -yVel;
Block13.x = -200;
Block13.y = -200;
}
if (CheckCollision(Ball, Block14) == true)
{
yVel = -yVel;
Block14.x = -200;
Block14.y = -200;
}
if (CheckCollision(Ball, Block15) == true)
{
yVel = -yVel;
Block15.x = -200;
Block15.y = -200;
}
if (CheckCollision(Ball, Block16) == true)
{
yVel = -yVel;
Block16.x = -200;
Block16.y = -200;
}
if (CheckCollision(Ball, Block17) == true)
{
yVel = -yVel;
Block17.x = -200;
Block17.y = -200;
}
if (CheckCollision(Ball, Block18) == true)
{
yVel = -yVel;
Block18.x = -200;
Block18.y = -200;
}
if (CheckCollision(Ball, Block19) == true)
{
yVel = -yVel;
Block19.x = -200;
Block19.y = -200;
}
if (CheckCollision(Ball, Block20) == true)
{
yVel = -yVel;
Block20.x = -200;
Block20.y = -200;
}
if (CheckCollision(Ball, Block21) == true)
{
yVel = -yVel;
Block21.x = -200;
Block21.y = -200;
}
if (CheckCollision(Ball, Block22) == true)
{
yVel = -yVel;
Block22.x = -200;
Block22.y = -200;
}
if (CheckCollision(Ball, Block23) == true)
{
yVel = -yVel;
Block23.x = -200;
Block23.y = -200;
}
if (CheckCollision(Ball, Block24) == true)
{
yVel = -yVel;
Block24.x = -200;
Block24.y = -200;
}
if (CheckCollision(Ball, Block25) == true)
{
yVel = -yVel;
Block25.x = -200;
Block25.y = -200;
}
if (CheckCollision(Ball, Block26) == true)
{
yVel = -yVel;
Block26.x = -200;
Block26.y = -200;
}
}
//Function to load image
SDL_Surface *load_Image( std::string filename )
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
//If nothing went wrong in loading the image
if(loadedImage != NULL)
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat(loadedImage);
//Free the old surface
SDL_FreeSurface(loadedImage);
}
//Return the optimized image
return optimizedImage;
}
//Function to apply picture to screen
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface(source, clip, destination, &offset);
}
//Function to start SDL
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Name the window
SDL_WM_SetCaption( "Block Breaker", NULL );
//If everything initialized fine
return true;
}
//Function to load files
bool load_files()
{
//Load the image
mainScreen = load_Image( "Menu.bmp" );
difficultyScreen = load_Image( "Speed Menu.bmp" );
return true;
}
//function to stop SDL
void clean_up()
{
SDL_FreeSurface(mainScreen);
SDL_FreeSurface(difficultyScreen);
SDL_FreeSurface(screen);
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Variables
bool running = true;
bool menu = true;
bool difficulty = false;
bool mediumGame = false;
bool hardGame = false;
bool quit = false;
//Initialize
if ( init() == false )
{
return 1;
}
//Quit function
if (quit == true)
{
SDL_Quit();
}
//Load the files
if ( load_files() == false )
{
return 1;
}
LoadGame();
//while the user hasn't quit
while (quit == false)
{
//Menu screen
if (menu==true)
{
//Apply screen
apply_surface(0,0,mainScreen,screen);
//Loop to handle input by user
while(SDL_PollEvent(&event))
{
if (event.type==SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
//Button for difficulty screen
if ((x>370)&&(x<660)&&(y>265)&&(y<370))
{
menu = false;
difficulty = true;
SDL_Flip(screen);
}
if ((x>374)&&(x<660)&&(y>390)&&(y<500))
{
menu = false;
quit = true;
SDL_Flip(screen);
}
}
SDL_Flip(screen);
}
else if (event.type==SDL_QUIT)
{
quit = true;
}
}
SDL_Flip(screen);
}
//Difficulty screen
else if (difficulty==true)
{
//Apply surface
apply_surface(0,0,difficultyScreen,screen);
SDL_Flip(screen);
while (SDL_PollEvent(&event))
{
if (event.type==SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
//Button for medium game screen
if ((x>360)&&(x<660)&&(y>370)&&(y<465))
{
difficulty = false;
mediumGame = true;
SDL_Flip(screen);
}
//Button for hard game screen
if ((x>420)&&(x<610)&&(y>490)&&(y<575))
{
difficulty = false;
hardGame = true;
SDL_Flip(screen);
}
}
SDL_Flip(screen);
}
else if (event.type==SDL_QUIT)
{
quit = true;
}
}
}
//Medium game screen
else if (mediumGame == true)
{
//Fill color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x68, 0x68, 0x68 ));
//Logic for the game
Logic();
//Place Paddle
SDL_FillRect(screen, &Paddle, white);
//Place Ball
SDL_FillRect(screen, &Ball, white);
//Place Blocks
SDL_FillRect(screen, &Block1, white);
SDL_FillRect(screen, &Block2, white);
SDL_FillRect(screen, &Block3, white);
SDL_FillRect(screen, &Block4, white);
SDL_FillRect(screen, &Block5, white);
SDL_FillRect(screen, &Block6, white);
SDL_FillRect(screen, &Block7, white);
SDL_FillRect(screen, &Block8, white);
SDL_FillRect(screen, &Block9, white);
SDL_FillRect(screen, &Block10, white);
SDL_FillRect(screen, &Block11, white);
SDL_FillRect(screen, &Block12, white);
SDL_FillRect(screen, &Block13, white);
SDL_FillRect(screen, &Block14, white);
SDL_FillRect(screen, &Block15, white);
SDL_FillRect(screen, &Block16, white);
SDL_FillRect(screen, &Block17, white);
SDL_FillRect(screen, &Block18, white);
SDL_FillRect(screen, &Block19, white);
SDL_FillRect(screen, &Block20, white);
SDL_FillRect(screen, &Block21, white);
SDL_FillRect(screen, &Block22, white);
SDL_FillRect(screen, &Block23, white);
SDL_FillRect(screen, &Block24, white);
SDL_FillRect(screen, &Block25, white);
SDL_FillRect(screen, &Block26, white);
//Flip screen
SDL_Flip(screen);
}
//Hard game screen
else if (hardGame == true)
{
//Fill color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x68, 0x68, 0x68 ));
//Logic for the game
Logic();
//Place Paddle
SDL_FillRect(screen, &Paddle, white);
//Place Ball
SDL_FillRect(screen, &Ball, white);
//Place Blocks
SDL_FillRect(screen, &Block1, white);
SDL_FillRect(screen, &Block2, white);
SDL_FillRect(screen, &Block3, white);
SDL_FillRect(screen, &Block4, white);
SDL_FillRect(screen, &Block5, white);
SDL_FillRect(screen, &Block6, white);
SDL_FillRect(screen, &Block7, white);
SDL_FillRect(screen, &Block8, white);
SDL_FillRect(screen, &Block9, white);
SDL_FillRect(screen, &Block10, white);
SDL_FillRect(screen, &Block11, white);
SDL_FillRect(screen, &Block12, white);
SDL_FillRect(screen, &Block13, white);
SDL_FillRect(screen, &Block14, white);
SDL_FillRect(screen, &Block15, white);
SDL_FillRect(screen, &Block16, white);
SDL_FillRect(screen, &Block17, white);
SDL_FillRect(screen, &Block18, white);
SDL_FillRect(screen, &Block19, white);
SDL_FillRect(screen, &Block20, white);
SDL_FillRect(screen, &Block21, white);
SDL_FillRect(screen, &Block22, white);
SDL_FillRect(screen, &Block23, white);
SDL_FillRect(screen, &Block24, white);
SDL_FillRect(screen, &Block25, white);
SDL_FillRect(screen, &Block26, white);
//Flip screen
SDL_Flip(screen);
}
}
//Clean up
clean_up();
return 0;
}
答案 0 :(得分:1)
简短回答:使用浮球作为球的位置。
如果你认为你需要以比屏幕的整数像素分辨率更精确的速度调整速度,那么使用浮点数作为速度是正确的。使用浮点速度时球完全没有移动的原因是因为你没有使用浮点位置。
以下是一个例子:
int x = 4;
float velx = 0.05f;
x += velx;
x将具有什么价值?与此相同的值:
for(int i = 0; i < 1000; ++i)
x += velx;
x
没有改变,因为它只能是一个整数,并且当存储为整数时,十进制值会被截断。将x
更改为浮动,以使其正常工作。在您的情况下,请勿使用SDL_Rect(4个整数)来存储对象的位置。创建自己的使用浮点数的类/结构。