2016年1月13日 星期三

SDL - Step3 方塊碰撞邊界會反彈並且變色


螢幕截圖:
#include<stdio.h>
#include<stdlib.h>
#include<SDL/SDL.h>
#include<time.h>
int main(int argc,char** argv)
{
   SDL_Init(SDL_INIT_EVERYTHING);
   SDL_Surface *screen;
   screen=SDL_SetVideoMode(300,300,32,SDL_SWSURFACE);
   bool running=true;
   int dx=1,dy=1,R=255,G=255,B=255; 
   Uint32 start;
   SDL_Rect rect;
   srand(time(NULL));
   rect.x=145;
   rect.y=145;
   rect.w=20;
   rect.h=20;
   Uint32 color=SDL_MapRGB(screen->format,0,0,0);
   Uint32 color2;
  
   
   while(running)
   {  
        start = SDL_GetTicks();
 SDL_Event event;
        Uint32 color2=SDL_MapRGB(screen->format,R,G,B);
        while(SDL_PollEvent(&event))
        {
switch(event.type) 
             {
                    case SDL_QUIT:
                         running=false;
                         break;
                    case SDL_KEYDOWN:
                         switch(event.key.keysym.sym) 
                         {
                                case SDLK_UP:
                                    dy=-1;
                                    break;
                                case SDLK_LEFT:
                                    dx=-1;
                                    break;
                                case SDLK_DOWN:
                                    dy=1;
                                    break;
                                case SDLK_RIGHT:
                                    dx=1;
                                    break;


                                case SDLK_a:
                                     if(dx>=0 && dy>=0)
                                      {  
                                        dx++;
                                        dy++;
                                      } 
                if(dx>=0 && dy<=0)
                                      { 
                                        dx++;
                                        dy--;
                                      }
                if(dx<=0 && dy>=0)
                                      { 
                                        dx--;
                                        dy++;
                                      }
                       if(dx<=0 && dy<=0)
                                      { 
                                        dx--;
                                        dy--;
                                      } 
                                    break;
                                case SDLK_d:
                                      if(dx>=0 && dy>=0)
                                      {
                                        dx--;
                                        dy--;
                                      }
                                     if(dx>=0 && dy<=0)
                                      {
                                        dx--;
                                        dy++;
                                      }
                                     if(dx<=0 && dy>=0)
                                      {
                                        dx++;
                                        dy--;
                                      }
                                     if(dx<=0 && dy<=0)
                                      {
                                        dx++;
                                        dy++;
                                      } 
                                    break;
                          }
                          break;                       
             }                          
        }
if(rect.x==0 || rect.x==280 || rect.y==0 || rect.y==280)
 {  
   R=rand()%256;
   G=rand()%256;
   B=rand()%256;
 }
if(R==0 && G==0 && B==0)
      { 
        R=rand()%256;
        G=rand()%256; 
        B=rand()%256;
      }
rect.x+=dx;
rect.y+=dy;
if(rect.x<=0)
dx=-dx;
if(rect.x>=280)
dx=-dx;
if(rect.y<=0)
dy=-dy;
if(rect.y>=280)
dy=-dy;


if(rect.x<=0)
rect.x=0;
if(rect.x>=280)
rect.x=280;
if(rect.y<=0)
rect.y=0;
if(rect.y>=280)
rect.y=280;

    //render
    SDL_FillRect(screen,&screen->clip_rect,color);
    SDL_FillRect(screen,&rect,color2);
    SDL_Flip(screen);
    
    SDL_Delay(10);

    }
 SDL_Quit();
 return 0;

}

沒有留言:

張貼留言