2016年1月13日 星期三

SDL - Step5 打磚塊


螢幕截圖:

#include<stdio.h>
#include<stdlib.h>
#include<SDL/SDL.h>
#include<time.h>
#include<stdbool.h>
#define m 4
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;
   bool a[2]={0,0};
   bool b[m]={0}; 
   Uint32 start;
   SDL_Rect rect,board,brick[m];
   srand(time(NULL));

/*ball*/
   rect.x=180;
   rect.y=175;
   rect.w=15;
   rect.h=15;

/*board*/
   board.x=110;
   board.y=295;
   board.w=60;
   board.h=5;

/*brick*/
   int i;
   for(i=0;i<m;i++)
 {
    brick[i].x=75*i;
    brick[i].y=40;
    brick[i].w=70;
    brick[i].h=20;      
 }

   Uint32 color=SDL_MapRGB(screen->format,0,0,0);
   Uint32 color2;
   Uint32 color3=SDL_MapRGB(screen->format,255,0,0);
   Uint32 color4=SDL_MapRGB(screen->format,0,0,255);   
   
   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_ESCAPE:
  running=false;
        break;
    case SDLK_LEFT:
                                    a[0]=1;
                                    break;
                                case SDLK_RIGHT:
                                    a[1]=1;
                                    break;
                         }
                         break;     
                   case SDL_KEYUP:
                        switch(event.key.keysym.sym)
                        {
                                case SDLK_LEFT:
                                    a[0]=0;
                                    break;
                                case SDLK_RIGHT:
                                    a[1]=0;
                                    break;                                
                        }
                        break;           
            
             }                          
        }
        /*red board move*/
        if(a[0])
          board.x-=3;
        if(a[1])
          board.x+=3;
        /*red board move 邊界*/
        if(board.x<=0)
          board.x=0;
        if(board.x>=240)
          board.x=240; 
  

/*變色*/  
if(rect.x==0 || rect.x==285)
 {  
   R=rand()%256;
   G=rand()%256;
   B=rand()%256;
 }

/*ball移動*/
rect.x+=dx;
rect.y+=dy;

/*ball死亡後,復活條件*/
if(rect.y>=300)
 {
   rect.x=125;
   rect.y=180;
   rect.w=15;
   rect.h=15;
 }

/*上,左,右邊界*/
if(rect.y<=0)
 dy=-dy;
if(rect.x<=0)
 dx=-dx;
if(rect.x>=285)
  dx=-dx;

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

/*brick 消失 and ball 反彈*/
for(i=0;i<m;i++)
{
   if(b[i]==0)
   { 
 if(rect.x+15>=brick[i].x && rect.x<=brick[i].x+70)
 {
  if(rect.y+15==40 || rect.y==60)
  {
   dy=-dy;
   b[i]=1;
  }
  
 }

 if(rect.y<=brick[i].y+20 && rect.y+15>=brick[i].y)
 {
  if(rect.x+15== brick[i].x || rect.x==brick[i].x+70)
  {
   dy=-dy;
   b[i]=1;
  }
 }
   }
}
 
  
/*ball and  board 反彈*/
if(rect.y==280)
   if(rect.x>=board.x-15 && rect.x<=board.x+60)
      {
 dy=-dy;
 rect.y=280;
      }

        /*render*/
         SDL_FillRect(screen,&screen->clip_rect,color);
         SDL_FillRect(screen,&rect,color2);
  SDL_FillRect(screen,&board,color3);
 for(i=0;i<4;i++)
   { 
    if(b[i]==0)   
             SDL_FillRect(screen,&brick[i],color4);
   }
   
         SDL_Flip(screen);
        SDL_Delay(10);

    }
   SDL_Quit();
  return 0;
}

沒有留言:

張貼留言