Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <SDL.h>
  3.  
  4. int main(int argc, char **argv){
  5.     SDL_Rect b0 = {10, 103, 10, 50};    //x, y, w, h
  6.     SDL_Rect b1 = {246, 103, -10, 50};
  7.     SDL_Rect ball = {123, 123, 10, 10};
  8.  
  9.     int xv = 2;
  10.     int yv = 2;
  11.     int p1 = 0;
  12.     int p2 = 0;
  13.  
  14.     SDL_Window *window;     // Declaring window
  15.     SDL_Renderer *renderer; // Declaring renderer
  16.  
  17.     SDL_Init(SDL_INIT_VIDEO); // Initialize SDL video
  18.  
  19.     window = SDL_CreateWindow("G A M E", //windown name               Initializing window
  20.         SDL_WINDOWPOS_UNDEFINED,         //window position
  21.         SDL_WINDOWPOS_UNDEFINED,         //window position
  22.         256,                             //window width
  23.         256,                             //window height
  24.         0);                              //flags
  25.  
  26.     renderer = SDL_CreateRenderer(window,                  // pointer to window
  27.     -1,                                                    // index
  28.     SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); // flags
  29.  
  30.     SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); // Set renderers draw colour to blue
  31.  
  32.     SDL_RenderClear(renderer); // Clear renderer to blue screen
  33.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Set renderers draw colour to white
  34.  
  35.     int done = 0;
  36.     SDL_Event event;
  37.     while(done == 0){
  38.         while(SDL_PollEvent(&event)){
  39.             switch(event.type){
  40.                 case SDL_WINDOWEVENT_CLOSE:
  41.                     if (window){
  42.                         SDL_DestroyWindow(window);
  43.                         window = NULL;
  44.                         done = 1;
  45.                     }
  46.                 break;
  47.                 case SDL_QUIT:
  48.                     done = 1;
  49.                 break;
  50.                 case SDL_KEYDOWN:
  51.                     switch(event.key.keysym.sym){
  52.                         case SDLK_ESCAPE:
  53.                             done = 1;
  54.                         break;
  55.                         case SDLK_DELETE:
  56.                             done = 1;
  57.                         break;
  58.                     }
  59.                 break;
  60.             }
  61.         }
  62.  
  63.         const Uint8 *keyboard = SDL_GetKeyboardState(NULL);
  64.         if (keyboard[SDL_SCANCODE_W]) b0.y -= (((unsigned) b0.y - 5) > 256) ? 0 : 5;
  65.         if (keyboard[SDL_SCANCODE_S]) b0.y += ((b0.y + 55) > 256) ? 0 : 5;
  66.  
  67.         if (keyboard[SDL_SCANCODE_UP]) b1.y -= (((unsigned) b1.y - 5) > 256) ? 0 : 5;
  68.         if (keyboard[SDL_SCANCODE_DOWN]) b1.y += ((b1.y + 55) > 256) ? 0 : 5;
  69.  
  70.         //code for ball
  71.         if((((unsigned) ball.y + yv) > 256) || (((unsigned) ball.y + 10 + yv) > 256)) yv *= -1; //bouncing off of top/bottom wall
  72.         else if(((unsigned) ball.x + xv) > 256) {p2++; ball.x = 123; ball.y = 123; xv *= -1;}
  73.         else if(((unsigned) ball.x + 10 + xv) > 256) {p1++; ball.x = 123; ball.y = 123; xv *= -1;}
  74.         else if(((ball.y > b0.y && ball.y < b0.y + b0.h) || (ball.y + ball.h > b0.y && ball.y + ball.h < b0.y + b0.h)) && ((ball.x > b0.x && ball.x + ball.w < b0.x + b0.w) || (ball.x > b0.x && ball.x < b0.x + b0.w))) {xv *= -1; puts("b0\n");} //b0 hit detection
  75.         else if((((ball.y > b1.y) && (ball.y < b1.y + b1.h)) || ((ball.y + ball.h > b1.y) && (ball.y + ball.h < b1.y + b1.h))) && (((ball.x > b1.x + b1.w) && (ball.x < b1.x)) || ((ball.x + ball.w > b1.x + b1.w) && (ball.x + ball.w < b1.x)))) {xv *= -1; puts("b1\n");} //b1 hit detection
  76.  
  77.         ball.x += xv;
  78.         ball.y += yv;
  79.  
  80.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set renderers draw colour to black
  81.  
  82.         SDL_RenderClear(renderer); // Clear renderer to black screen
  83.         SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Set renderers draw colour to white
  84.  
  85.         SDL_RenderFillRect(renderer, &b0);  // Fill rectangle with white
  86.         SDL_RenderFillRect(renderer, &b1);  // Fill rectangle with white
  87.         SDL_RenderFillRect(renderer, &ball);  // Fill rectangle with white
  88.  
  89.         SDL_RenderPresent(renderer); // Present
  90.         printf("P1: %d\nP2: %d\n", p1, p2);
  91.  
  92.  
  93.  
  94.         SDL_Delay(1000 / 60000); // Wait 1/60 seconds
  95.     }
  96.  
  97.     // Destroy window/renderer
  98.     SDL_DestroyRenderer(renderer);
  99.     SDL_DestroyWindow(window);
  100.  
  101.     // Clean up
  102.     SDL_Quit();
  103.  
  104.     // Return
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement