Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //SDL tile collision
- #include <iostream>
- #include <vector>
- #include <SDL/SDL.h>
- #include <cmath>
- #include <fstream>
- const int WIDTH=640;
- const int HEIGHT=480;
- int map[15][20]={
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
- };
- bool collision(SDL_Rect* player,int direction)
- {
- if(direction == 1 && (map[(player->y/32)+1][player->x/32] == 1 || map[(player->y/32)+1][(int)ceil(player->x/32.0)] == 1)) return true;
- if(direction == 2 && (map[(int)ceil(player->y/32.)-1][player->x/32] == 1 || map[(int)ceil(player->y/32.)-1][(int)ceil(player->x/32.)] == 1)) return true;
- if(direction == 3 && (map[player->y/32][(int)ceil(player->x/32.)-1] == 1 || map[(int)ceil(player->y/32.)][(int)ceil(player->x/32.)-1] == 1)) return true;
- if(direction == 4 && (map[player->y/32][(player->x/32)+1] == 1 || map[(int)ceil(player->y/32.)][(player->x/32)+1] == 1)) return true;
- return false;
- }
- int main(int argc,char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
- bool running=true;
- SDL_Event event;
- Uint32 start;
- SDL_Rect player={32,32,32,32};
- bool b[4]={0,0,0,0};
- int direction=0;
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- }
- break;
- case SDL_MOUSEMOTION:
- break;
- }
- }
- for(int i=0;i<15;i++)
- for(int j=0;j<20;j++)
- {
- SDL_Rect rec={j*32,i*32,32,32};
- SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,0,0,map[i][j]*255));
- }
- SDL_FillRect(screen,&player,SDL_MapRGB(screen->format,255,0,0));
- direction=0;
- Uint8* keys=SDL_GetKeyState(0);
- if(keys[SDLK_w])
- {
- direction=2;
- if(!collision(&player,direction))
- player.y-=2;
- }
- if(keys[SDLK_s])
- {
- direction=1;
- if(!collision(&player,direction))
- player.y+=2;
- }
- if(keys[SDLK_a])
- {
- direction=3;
- if(!collision(&player,direction))
- player.x-=2;
- }
- if(keys[SDLK_d])
- {
- direction=4;
- if(!collision(&player,direction))
- player.x+=2;
- }
- SDL_Flip(screen);
- if(1000.0/30>(SDL_GetTicks()-start))
- SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement