Moving my Alien around
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;
}
Note: I don't know sdl so this could be completely mistaken
but i think what you might want is rather than
is
Plus you'll probably want to check that the SDL_PollEvent call doesn't return an error.
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.
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:
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:
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)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement