gpt4 book ai didi

C++ SDL 二维跳跃(重力)

转载 作者:太空狗 更新时间:2023-10-29 20:05:16 27 4
gpt4 key购买 nike

我正在使用 C++ 自学 SDL、OpenGL 等。在线教程帮助我创建了一个在屏幕上移动的盒子和一个盒子无法通过的壁架...

我没能找到解释和展示如何让盒子跳跃或四处跳跃的教程。理想情况下,我希望我的盒子能像老式马里奥一样向前移动并跳上窗台。

我不确定这是否可以用 SDL 完成?我猜需要添加一些东西才能使盒子在按下向上后返回地面......我尝试了 up/down 键,并试图让盒子在按下后返回地面,但它没有用。我试过将盒子放在地上,但向上键不起作用。我也试过一旦按下向上并且盒子已经移动以使盒子下降相同的距离......我有点明白它需要如何实现(我认为),我只是不太了解 C++ 来编写它,或者它是否可以完成。

目前我可以向左或向右和向上移动,但他仍然漂浮在空中:(

这是我的代码:

    #include "SDL.h"
#include "SDL_image.h"
#include <string>

const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 20;
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;

SDL_Surface *square = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Rect wall;

class Square{
private:
SDL_Rect box;
int xVel, yVel;
public:
Square();
void handle_input();
void move();
void show();};

class Timer{
private:
int startTicks;
int pausedTicks;
bool paused;
bool started;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();};

SDL_Surface *load_image( std::string filename ){
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if( optimizedImage != NULL )
{
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 237, 145, 33 ) );//optimizedImage->format, 0, 0xFF, 0xFF
}
}
return optimizedImage;}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset );}

bool check_collision( SDL_Rect A, SDL_Rect B ){
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;

leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;

leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;

if( bottomA <= topB ){return false;}
if( topA >= bottomB ){return false;}
if( rightA <= leftB ){return false;}
if( leftA >= rightB ){return false;}
return true;}

bool init(){
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ){return false;}

screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL ){return false;}

SDL_WM_SetCaption( "Move the Square", NULL );
return true;}

bool load_files(){
square = load_image( "square.bmp" );
if( square == NULL ){return false;}
return true;}

void clean_up(){
SDL_FreeSurface( square );
SDL_Quit();}

Square::Square(){
box.x = 50;
box.y = 360;
box.w = SQUARE_WIDTH;
box.h = SQUARE_HEIGHT;
xVel = 0;
yVel = 0;}

void Square::handle_input(){
if( event.type == SDL_KEYDOWN ){
switch( event.key.keysym.sym ){
case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
//case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;}}
else if( event.type == SDL_KEYUP ){
switch( event.key.keysym.sym ){
case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
//case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;}}}

void Square::move(){
box.x += xVel;
if( ( box.x < 0 ) || ( box.x + SQUARE_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) ){
box.x -= xVel;}

box.y += yVel;

if( ( box.y < 0 ) || ( box.y + SQUARE_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) ){
box.y -= yVel;}}

void Square::show(){
apply_surface( box.x, box.y, square, screen );}

Timer::Timer(){
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;}

void Timer::start(){
started = true;
paused = false;
startTicks = SDL_GetTicks();}

void Timer::stop(){
started = false;
paused = false;}

void Timer::pause(){
if( ( started == true ) && ( paused == false ) ){
paused = true;
pausedTicks = SDL_GetTicks() - startTicks;}}

void Timer::unpause(){
if( paused == true ){
paused = false;
startTicks = SDL_GetTicks() - pausedTicks;
pausedTicks = 0;}}

int Timer::get_ticks(){
if( started == true ){
if( paused == true ){
return pausedTicks;}
else{return SDL_GetTicks() - startTicks;}
}return 0;}

bool Timer::is_started(){return started;}
bool Timer::is_paused(){return paused;}

int main( int argc, char* args[] ){
bool quit = false;
Square mySquare;
Timer fps;
if( init() == false ){return 1;}
if( load_files() == false ){return 1;}

wall.x = 130;
wall.y = 300;
wall.w = 220;
wall.h = 20;

while( quit == false ){
fps.start();
while( SDL_PollEvent( &event ) ){
mySquare.handle_input();
if( event.type == SDL_QUIT ){
quit = true;}}

mySquare.move();
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 1, 1, 1 ) );
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 237, 145, 33 ) );
mySquare.show();

if( SDL_Flip( screen ) == -1 ){return 1;}
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ){
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );}}

clean_up();
return 0;}

最佳答案

您需要应用一些非常基础的物理知识。有可用的库,但对于像这样的简单案例,只需要最少的代码。

您已经有了 xVel 和 yVel 变量。当按下 UP 键时,框“跳”起来,您将值添加到 yVel。重力是影响箱子速度的恒定力。箱子加速向下。实际上,您在 move() 函数的每一帧中将重力应用于速度。

yVel -= GRAVITY;    // Zero in space, small value on Moon, big value on Jupiter
box.y += yVel;

请注意,当盒子碰到地板时,您反转了 y 速度。这很好,但是在施加重力的情况下,重力会累积并且盒子最终会以荒谬的速度弹跳。盒子在弹跳时应该失去“能量”,例如,速度可能会减半。

您的代码中的问题是您使用的是整数值。重力是一种弱力,所以为了让它看起来真实,它应该是小值。对于整数值,可能无法找到该值。 (没有玩定点值,但这是另一个话题。)

因此考虑将 xVel 和 yVel 更改为 float 。当然,盒子的位置也应该使用 float 。

要获得更精确的物理学和考虑帧速率的真正精细的解决方案,请查看:https://gamedev.stackexchange.com/questions/15708/how-can-i-implement-gravity

关于C++ SDL 二维跳跃(重力),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14426870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com