collision detect--SDL lib

来源:互联网 发布:学校网络舆情监控制度 编辑:程序博客网 时间:2024/04/19 13:45
#include <SDL>SDL_Surface*screen=NULL;SDL_Surface*background=NULL;SDL_Surface*sprite=NULL;int SCREEN_WIDTH=640;int SCREEN_HEIGHT=480;int width=32;int height=32;int dstX=0;int dstY=0;bool gameRunning = true;SDL_Event event;void apply_surface(int x, int y, SDL_Surface*source,SDL_Surface*destination){  SDL_Rect rect;rect.x=x;rect.y=y;SDL_BlitSurface(source, NULL, destination,&rect);}void draw_sprite(int srX, int srcY, int detX, int dstY, int width, int height,SDL_Surface*source, SDL_Surface*destination){SDL_Rect src;src.x=srcX;src.y=srcY;src.w=width;src.h=height;SDL.Rect dst;dst.x=dstX;dst.y=dstY;dst.w=width;dst.h=height;SDL_BlitSurface(source, &src,destination,&dst);}int main(int argc, char*argv[]){ SDL_Init(SDL_INIT_VIDEO);SDL_WM_SetCaption("Apply image to screen",NULL);sreen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEEN_HEIGHT,32,SDL_SWSURFACE);background=SDL_LoadBMP("background.bmp");sprite=SDL_LoadBMP("sprite.bmp");SDL_SetColorKey(sprite,SDL_SRCCOLOKEY,SDL_MapRGB(sprite->format,200,0,200));bool keys[323]={false};while(GameRunning){  if(SDL_PollEvent(&event))    {                 if(event.type==SDL_QUIT)             {               gameRunning=false;             }           if(event.type==SDL_KEYDOWN)             {             key[event,key,keysym,sym]=true;             }           if(event,key,keysym,sym]=false;             {                key[event,key,keysym,sym]=false;             }              if(key[SDLK_RIGHT])             {              dstX+=1;              }               if(key[SDLK_LEFT])              {               dstX-=1;              }             if(keys[SDLK_DOWN])             { dstY+=1;             }              if(keys[SDL_UP)              {                 dstY-=1;              }             if(dstX<0)                     {                dstX=0;              }              if(dstY<0)               {                dstY=0;              }              if(dstY+height>SCREEN_HEIGHT)              {               dstY=480-width;              }                if(dstX+width>SCREEN_WIDTH)             {                dstX=SCREEN_WIDTH-width;             }             apply_surface(0,0,background,screen);              draw_sprite(srcX,srcY, dstX,width,height,sprite,screen);             SDL_Flip(screen);             }}              

原创粉丝点击