Abdulg

Breakout [SDL]

Sep 10th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL/SDL.h>
  4.  
  5. #define SCREENWIDTH 640
  6. #define SCREENHEIGHT 480
  7.  
  8. int Inbetween(int Min, int Value, int Max);
  9. int IsIntersecting (SDL_Rect Rect1, SDL_Rect Rect2);
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
  14.     {
  15.         fprintf(stderr, "Failed to initialise SDL: %s\n", SDL_GetError());
  16.         return 1;
  17.     }
  18.    
  19.     SDL_Surface *Screen;
  20.     Screen = SDL_SetVideoMode(SCREENWIDTH, SCREENHEIGHT, 32, SDL_SWSURFACE);
  21.     SDL_WM_SetCaption("Breakout", NULL);
  22.  
  23.     SDL_Rect PlayerRect;
  24.     PlayerRect.x = SCREENWIDTH / 3;
  25.     PlayerRect.y = SCREENHEIGHT - 10;
  26.     PlayerRect.w = SCREENWIDTH / 16;
  27.     PlayerRect.h = 3;
  28.  
  29.     SDL_Rect Ball;
  30.     Ball.x = SCREENWIDTH / 4;
  31.     Ball.y = SCREENHEIGHT / 2;
  32.     Ball.w = SCREENHEIGHT / 48;
  33.     Ball.h = Ball.w;
  34.  
  35.     SDL_Rect Block;
  36.     Block.w = SCREENWIDTH / 20;
  37.     Block.h = SCREENHEIGHT / 48;
  38.  
  39.     SDL_Rect ScreenRect;
  40.     ScreenRect.x = 0;
  41.     ScreenRect.y = 0;
  42.     ScreenRect.h = SCREENHEIGHT;
  43.     ScreenRect.w = SCREENWIDTH;
  44.  
  45.     short unsigned int Blocks[16][4];
  46.     int i,j;
  47.     for (i = 0; i < 16; i++)
  48.     {
  49.         for (j = 0; j < 4; j++)
  50.             Blocks[i][j] = 1;
  51.     }
  52.  
  53.     int BlocksLeft = 16 * 4;
  54.     int Lives = 3;
  55.     int BallXVel = 2, BallYVel = 2;
  56.     int Time;
  57.    
  58.     Uint8 *Keyboard;
  59.  
  60.     while (Lives != -1 && BlocksLeft != 0)
  61.     {
  62.         Time = SDL_GetTicks();
  63.  
  64.         if (Ball.y + BallYVel < 106 + Block.h)
  65.         {
  66.             int X = (Ball.x + BallXVel + 4) / 40;
  67.             int Y = (Ball.y + BallYVel + 10) / 32;
  68.            
  69.             if (Blocks[X][Y] == 1)
  70.             {
  71.                 Blocks[X][Y] = 0;
  72.                
  73.                 if (Ball.x + BallXVel + Ball.w - 4 - X * 40 > Ball.y + BallYVel + Ball.h - 10 - Y * 32) BallYVel *= -1;
  74.                 else BallXVel *= -1;
  75.             }
  76.         }
  77.        
  78.         Ball.x += BallXVel;
  79.         Ball.y += BallYVel;
  80.  
  81.         if (!Inbetween(0, Ball.x, SCREENWIDTH - Ball.w)) BallXVel *= -1;
  82.         if (Ball.y < 0 || IsIntersecting(Ball, PlayerRect)) BallYVel *= -1;
  83.         else if (Ball.y > SCREENHEIGHT)
  84.         {
  85.             Ball.x = SCREENWIDTH / 4;
  86.             Ball.y = SCREENHEIGHT / 2;
  87.             BallXVel = 2;
  88.             BallYVel = 2;
  89.             PlayerRect.x = SCREENWIDTH / 3;
  90.             Lives -= 1;
  91.         }
  92.        
  93.         if (PlayerRect.x < 0) PlayerRect.x = 0;
  94.         else if (PlayerRect.x + PlayerRect.w > SCREENWIDTH) PlayerRect.x = SCREENWIDTH - PlayerRect.w;
  95.         Ball.h = SCREENHEIGHT / 48;
  96.        
  97.         SDL_FillRect(Screen, &ScreenRect, 0x000000);
  98.  
  99.         SDL_FillRect(Screen, &PlayerRect, 0xFFFFFF);
  100.         SDL_FillRect(Screen, &Ball, 0xFFFFFF);
  101.        
  102.         for (i = 0; i < 16; i++)
  103.         {
  104.             for (j = 0; j < 4; j++)
  105.             {
  106.                 if (Blocks[i][j] == 1)
  107.                 {
  108.                     Block.x = i * 40 + 4;
  109.                     Block.y = j * 32 + 10;
  110.                     SDL_FillRect(Screen, &Block, 0xFFFFFF);
  111.                 }
  112.             }
  113.         }
  114.        
  115.         SDL_PumpEvents();
  116.         Keyboard = SDL_GetKeyState(NULL);
  117.         if (Keyboard[SDLK_RIGHT]) PlayerRect.x += 3;
  118.         else if (Keyboard[SDLK_LEFT]) PlayerRect.x -= 3;
  119.         else if (Keyboard[SDLK_ESCAPE]) BlocksLeft = 0;
  120.         SDL_Flip(Screen);
  121.        
  122.         Time = SDL_GetTicks() - Time;
  123.         SDL_Delay(1000 / 60 - Time);
  124.     }
  125.  
  126.     return 0;
  127. }
  128.  
  129. int Inbetween(int Min, int Value, int Max)
  130. {
  131.     return (Value > Min && Value < Max);
  132. }
  133.  
  134. int IsIntersecting(SDL_Rect Rect1, SDL_Rect Rect2)
  135. {
  136.     return !(Rect1.x > Rect2.x + Rect2.w || Rect1.x + Rect1.w < Rect2.x
  137.            || Rect1.y > Rect2.y + Rect2.h || Rect1.y + Rect1.h < Rect2.y);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment