collision detect--SDL lib

#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);
             }
}
              

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值