2016年1月13日 星期三

SDL - Step4 打乒乓球


螢幕截圖:
#include"SDL/SDL.h"
#include<stdio.h>
#include<stdlib.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;
   bool a[2]={0,0};
   bool b[2]={0,0};
 
   Uint32 start;
   SDL_Rect rect,board1,board2;
   srand(time(NULL));

/*ball*/
   rect.x=145;
   rect.y=145;
   rect.w=20;
   rect.h=20;

/*board1*/
   board1.x=290;
   board1.y=0;
   board1.w=10;
   board1.h=80;

/*board2*/
   board2.x=0;
   board2.y=0;
   board2.w=10;
   board2.h=80;

   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,255,0);   
   
   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_UP:
                                    a[0]=1;
                                    break;
                                case SDLK_DOWN:
                                    a[1]=1;
                                    break;
                                case SDLK_w:
                                    b[0]=1;
                                    break;
                                case SDLK_x:
                                    b[1]=1;
                                    break;
                         }
                         break;     
                   case SDL_KEYUP:
                        switch(event.key.keysym.sym)
                        {
                                case SDLK_UP:
                                    a[0]=0;
                                    break;
                                case SDLK_DOWN:
                                    a[1]=0;
                                    break;
                                case SDLK_w:
                                    b[0]=0;
                                    break;
                                case SDLK_x:
                                    b[1]=0;
                                    break;
                        }
                        break;           
            
             }                          
        }
/*red board move*/
        if(a[0])
          board1.y-=10;
        if(a[1])
          board1.y+=10;
        /*red board move 邊界*/
        if(board1.y<=0)
          board1.y=0;
        if(board1.y>=220)
          board1.y=220; 
        /*green board move*/
        if(b[0])
          board2.y-=10;
        if(b[1])
          board2.y+=10;
        /*green board move邊界*/
        if(board2.y<=0)
          board2.y=0;
        if(board2.y>=220)
          board2.y=220;

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

rect.x+=dx;
rect.y+=dy;
/*ball死亡條件*/
if(rect.x<0)
 {
   rect.x=145;
   rect.y=145;
   rect.w=20;
   rect.h=20;
 }
if(rect.x>=300)
 { 
   rect.x=145;
   rect.y=145;
   rect.w=20;
   rect.h=20;
 }
if(rect.y<=0)
dy=-dy;
if(rect.y>=280)
dy=-dy;

/*上下邊界*/
if(rect.y<=0)
rect.y=0;
if(rect.y>=280)
rect.y=280;

/*ball and green board 反彈*/
if(rect.x==10)
   if(rect.y>=board2.y-20 && rect.y<=board2.y+80)
      {
        dx=-dx;
        rect.x=10;
      }

/*ball and red board 反彈*/
if(rect.x==270)
   if(rect.y>=board1.y-20 && rect.y<=board1.y+80)
      {
        dx=-dx;
        rect.x=270;
      }
        /*render*/
         SDL_FillRect(screen,&screen->clip_rect,color);
         SDL_FillRect(screen,&rect,color2);
  SDL_FillRect(screen,&board1,color3);
         SDL_FillRect(screen,&board2,color4);
         SDL_Flip(screen);
    
        SDL_Delay(10);

    }
 SDL_Quit();
 return 0;

}

沒有留言:

張貼留言