C++ SDL 键盘响应
我的游戏程序有问题。该程序响应我的键盘输入,但不会移动太远。它只会向上移动 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用
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.