Sign in to follow this  

Moving my Alien around

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There are no errors ,but I cant figure out how to move my alien around here is my source #include "SDL/SDL.h"; #include <cstdlib>; const int screenwidth=640; const int screenheight=480; SDL_Surface* Displaysurface=NULL; SDL_Surface* aliensurface=NULL; SDL_Rect srect,drect; SDL_Event move,quit; int main(int argc,char*argv[]) { SDL_Init(SDL_INIT_VIDEO); Displaysurface=SDL_SetVideoMode(screenwidth,screenheight,0,SDL_ANYFORMAT); aliensurface=SDL_LoadBMP("alien.bmp"); srect.w=drect.w=aliensurface->w; srect.h=drect.h=Displaysurface->h; drect.y=100; drect.x=100; Uint32 color; color=SDL_MapRGB(aliensurface->format,255,0,255); SDL_SetColorKey(aliensurface,SDL_SRCCOLORKEY,color); for(;;) { SDL_BlitSurface(aliensurface,&srect,Displaysurface,&drect); SDL_UpdateRect(Displaysurface,0,0,0,0); if(SDL_PollEvent(&move)==SDLK_DOWN) { --drect.y; } } return 0; }

Share this post


Link to post
Share on other sites
Note: I don't know sdl so this could be completely mistaken

but i think what you might want is rather than


if(SDL_PollEvent(&move)==SDLK_DOWN)
{
--drect.y;
}


is


SDL_PollEvent(&move);

if(move == SDLK_DOWN)
{
--drect.y;
}


Plus you'll probably want to check that the SDL_PollEvent call doesn't return an error.

Share this post


Link to post
Share on other sites
First thing, to move the sprite DOWN you should INCREASE the y value because the y value of zero is at the top of your screen.

Next, here are two ways to deal with keys in SDL:

First, (and best for games), is to simply see if the key is down:


Uint8* keys;

keys = SDL_GetKeyState(NULL);

if(keys[SDLK_UP] ){
MovePlayer(MOVE_UP); // or whatever non-object thing you are doing.
}//end if




This works well because if the player holds down the key, it will move the whole time.

The second thing is to see if a key was pressed down as an event. This isnt good because it only works if it was pressed down:


SDL_Event event;

while ( SDL_PollEvent(&event) ){
if ( event.type == SDL_QUIT ) { return 1; }
if ( event.type == SDL_KEYDOWN ){
if ( event.key.keysym.sym == SDLK_ESCAPE ) { return 1; }
if ( event.key.keysym.sym == SDLK_RETURN ) { done = 1; }
if ( event.key.keysym.sym == TALKING_KEY ) { done = 1; }
}
}//end while (SDL_PollEvent)


Share this post


Link to post
Share on other sites
PollEvent returns 1 if there are any pending events, or 0 if there are none available. The event is then stored at the address you specify as the first/only parimeter. You then take the event->type, which contains the information your looking for.

Share this post


Link to post
Share on other sites

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this