C++ SDL 键盘响应

发布于 2024-12-07 16:18:43 字数 1527 浏览 5 评论 0原文

我的游戏程序有问题。该程序响应我的键盘输入,但不会移动太远。它只会向上移动 1 位。

#include <SDL/SDL.h>
#include "Mrn.h"

int main (int argc,char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);//initialized everything.
SDL_Surface* screen;//allows drawing on screen.
screen=SDL_SetVideoMode(500,500,32,SDL_SWSURFACE);//sets resolution of screen.
bool running = true;
const int FPS = 30;//fps of the game.
Uint32 start;
while(running)//windows running loop.
{
    start = SDL_GetTicks();//timer.
    SDL_Event event;//allows for events to happen.
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_QUIT:
            running = false;//allows exiting.
            break;
        }
    }
    //game logic and rendering.
    player(screen);
    SDL_Flip(screen);
    if(1000/FPS > SDL_GetTicks() - start)
    {
        SDL_Delay(1000/FPS - (SDL_GetTicks() - start));
    }

}
SDL_Quit();
return 0;
}

Player.h 代码:

#include <SDL/SDL.h>
#ifndef MRN_H_INCLUDED
#define MRN_H_INCLUDED

int player(SDL_Surface* screen)
{
SDL_Surface* Marine;
SDL_Rect first;
first.x = 0;
first.y = 0;
first.w = 42;
first.h = 31;
SDL_Rect position;
position.x = 40;
position.y = 40;
Uint8 *key;
key = SDL_GetKeyState(NULL);

if(key[SDLK_w])
{
    position.y++;
}

Marine = SDL_LoadBMP("Images/Marine.bmp");
SDL_BlitSurface(Marine,&first,screen,&position);
SDL_FreeSurface(Marine);
return 0;
}

#endif // MRN_H_INCLUDED

除了移动之外,一切正常。它永远不会移动太远,释放后它会移回原来的位置。

I have a problem with my game program. The program responds to my keyboard input but won't move far. It will only move 1 bit up.

#include <SDL/SDL.h>
#include "Mrn.h"

int main (int argc,char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);//initialized everything.
SDL_Surface* screen;//allows drawing on screen.
screen=SDL_SetVideoMode(500,500,32,SDL_SWSURFACE);//sets resolution of screen.
bool running = true;
const int FPS = 30;//fps of the game.
Uint32 start;
while(running)//windows running loop.
{
    start = SDL_GetTicks();//timer.
    SDL_Event event;//allows for events to happen.
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_QUIT:
            running = false;//allows exiting.
            break;
        }
    }
    //game logic and rendering.
    player(screen);
    SDL_Flip(screen);
    if(1000/FPS > SDL_GetTicks() - start)
    {
        SDL_Delay(1000/FPS - (SDL_GetTicks() - start));
    }

}
SDL_Quit();
return 0;
}

Player.h code:

#include <SDL/SDL.h>
#ifndef MRN_H_INCLUDED
#define MRN_H_INCLUDED

int player(SDL_Surface* screen)
{
SDL_Surface* Marine;
SDL_Rect first;
first.x = 0;
first.y = 0;
first.w = 42;
first.h = 31;
SDL_Rect position;
position.x = 40;
position.y = 40;
Uint8 *key;
key = SDL_GetKeyState(NULL);

if(key[SDLK_w])
{
    position.y++;
}

Marine = SDL_LoadBMP("Images/Marine.bmp");
SDL_BlitSurface(Marine,&first,screen,&position);
SDL_FreeSurface(Marine);
return 0;
}

#endif // MRN_H_INCLUDED

everything works fine except for its movement. It will never move far and on release it will move back to its original position.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2024-12-14 16:18:43

每次调用 player() 时,您都会将位置设置为 (40,40)。您需要将玩家的初始化与玩家的处理分开。

Every time you call player(), you are setting position to (40,40). You need to separate player's initialization from player's processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文