This is a 2 part question
1.So when I move my block around it doesn't move the image just extends it for example:
this is the image when the program starts up
this is the image when you press the right keyCode:~
while it should beCode:~~
why does that happen.(I'll add attachments).Code:~
2. you have to keep on tapping a key for it to move in that direction, you can't just hold it down.
here is my code
Thank youCode:#include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <string> using namespace std; //block coordinates int x = 0; int y = 0; SDL_Surface *screen = NULL; SDL_Surface *block = NULL; SDL_Surface *wall = NULL; bool quit = false; SDL_Event event; void handle_events(){ if(SDL_PollEvent(&event)){ if(event.type == SDL_QUIT){ quit = true; } if(event.type = SDL_KEYDOWN){ switch(event.key.keysym.sym){ case SDLK_UP: y--; break; case SDLK_DOWN: y++;break; case SDLK_RIGHT: x++; break; case SDLK_LEFT: x--; break; case SDLK_ESCAPE: quit = true; break; default: break; } } } } void apply_surface(int X, int Y, SDL_Surface *source, SDL_Surface *destination){ SDL_Rect pos; pos.x = X; pos.y = Y; SDL_BlitSurface(source, NULL, destination, &pos); } SDL_Surface *load_image(string filename){ SDL_Surface *image; image = IMG_Load(filename.c_str()); return image; } bool init(){ if(SDL_Init(SDL_INIT_EVERYTHING) == -1){ return false; } screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); if(screen == NULL){ return false; } SDL_WM_SetCaption("Test", NULL); return true; } int main( int argc, char* args[] ) { if(init() == false){ return 1; } block = load_image("block.png"); wall = load_image("wall.png"); if(block == NULL){ return 1; } if(wall == NULL){ return 1; } //make screen color SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x12, 0x24, 0x48)); while(quit == false){ handle_events(); apply_surface(x, y, block, screen); apply_surface(320, 0, wall, screen); SDL_Flip(screen); } //Quit SDL SDL_Quit(); return 0; }



LinkBack URL
About LinkBacks


