使用 C++ 和 SDL 库的二维运动

问题描述 投票:0回答:0

我正在尝试使用 SDL 在 C++ 中制作 pacman 游戏。 所以我得到了 pacman png 并显示了它,但它移动不流畅也不正确。 基本上它不会移动,但是当它移动之前它只会向上和向左移动。我测试了它,是的,它检测到键盘输入。我希望它向上、向右、向左和向下移动,而不是一点一点地移动,而是平稳地移动。 这是我的代码:

#include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<SDL2/SDL_image.h>

using std::cout, std::endl;

int height = 600;
int width = 800;
const float PACSPEED = 0.1;
bool running = true;

bool left = true;
bool right = false;
bool up = false;
bool down = false;

int main(int argc, char *argv[]){
    SDL_Init(SDL_INIT_EVERYTHING);
    
    SDL_Window *window = SDL_CreateWindow("PacMan", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);

    if(NULL==window){
        cout<<"Could not create window: " << SDL_GetError()<<endl;
        return 1;
    }
    
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

    if(NULL == renderer){
        cout<<"Could not creante renderer: "<<SDL_GetError()<<endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    SDL_Surface *imageSurface = IMG_Load("Images/pacman.png");
    if(NULL==imageSurface){
        cout<<"Could not load image: "<<IMG_GetError()<<endl;
        SDL_DestroyWindow(window);
        SDL_DestroyRenderer(renderer);
        SDL_Quit();
        return 1;
    }

    SDL_Texture *imageTexture = SDL_CreateTextureFromSurface(renderer, imageSurface);
    SDL_FreeSurface(imageSurface);

    if(NULL==imageSurface){
        cout<<"Could not texture:"<<SDL_GetError()<<endl;
        SDL_DestroyWindow(window);
        SDL_DestroyRenderer(renderer);
        SDL_Quit();
        return 1;
    }

    int originalWidth = imageSurface->w;
    int originalHeight = imageSurface->h;

    SDL_Rect destRect;
    destRect.x = 500;
    destRect.y = 500;
    destRect.w = originalWidth/25;
    destRect.h = originalHeight/25;

    SDL_Event event;
    Uint64 lastFrameTime = SDL_GetTicks();

    while(running){

        Uint64 currentFrameTime = SDL_GetTicks();
        float deltaTime = (currentFrameTime - lastFrameTime) / 1000.0f;
        lastFrameTime = currentFrameTime;

        if(SDL_PollEvent(&event)){
            if(SDL_QUIT == event.type){
                running = false;
            }
            if(SDL_KEYDOWN == event.type){
                if(event.key.keysym.sym == SDLK_a){
                    right = false;
                    up = false;
                    down = false;
                    left = true;
                }
                if(event.key.keysym.sym == SDLK_d){
                    left = false;
                    up = false;
                    down = false;
                    right = true;
                }
                if(event.key.keysym.sym == SDLK_w){
                    right = false;
                    left = false;
                    down = false;
                    up = true;
                }
                if(event.key.keysym.sym == SDLK_s){
                    right = false;
                    up = false;
                    left = false;
                    down = true;
                }
            }
            else{
                right = false;
                up = false;
                left = false;
                down = false;
            }
        }

        
        if(left){destRect.x -= PACSPEED*deltaTime;}
        if(right){destRect.x += PACSPEED*deltaTime;}
        if(up){destRect.y -= PACSPEED*deltaTime;}
        if(down){destRect.y += PACSPEED*deltaTime;}

        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, imageTexture, NULL, &destRect);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyTexture(imageTexture);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
}
c++ image sdl-2 keyboard-events pacman
© www.soinside.com 2019 - 2024. All rights reserved.