Jump to content
  • Advertisement
Sign in to follow this  
thugkilla

Moving my Alien around

This topic is 4688 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!