Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL/SDL.h>
- #define SCREENWIDTH 640
- #define SCREENHEIGHT 480
- int Inbetween(int Min, int Value, int Max);
- int IsIntersecting (SDL_Rect Rect1, SDL_Rect Rect2);
- int main(int argc, char *argv[])
- {
- if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
- {
- fprintf(stderr, "Failed to initialise SDL: %s\n", SDL_GetError());
- return 1;
- }
- SDL_Surface *Screen;
- Screen = SDL_SetVideoMode(SCREENWIDTH, SCREENHEIGHT, 32, SDL_SWSURFACE);
- SDL_WM_SetCaption("Breakout", NULL);
- SDL_Rect PlayerRect;
- PlayerRect.x = SCREENWIDTH / 3;
- PlayerRect.y = SCREENHEIGHT - 10;
- PlayerRect.w = SCREENWIDTH / 16;
- PlayerRect.h = 3;
- SDL_Rect Ball;
- Ball.x = SCREENWIDTH / 4;
- Ball.y = SCREENHEIGHT / 2;
- Ball.w = SCREENHEIGHT / 48;
- Ball.h = Ball.w;
- SDL_Rect Block;
- Block.w = SCREENWIDTH / 20;
- Block.h = SCREENHEIGHT / 48;
- SDL_Rect ScreenRect;
- ScreenRect.x = 0;
- ScreenRect.y = 0;
- ScreenRect.h = SCREENHEIGHT;
- ScreenRect.w = SCREENWIDTH;
- short unsigned int Blocks[16][4];
- int i,j;
- for (i = 0; i < 16; i++)
- {
- for (j = 0; j < 4; j++)
- Blocks[i][j] = 1;
- }
- int BlocksLeft = 16 * 4;
- int Lives = 3;
- int BallXVel = 2, BallYVel = 2;
- int Time;
- Uint8 *Keyboard;
- while (Lives != -1 && BlocksLeft != 0)
- {
- Time = SDL_GetTicks();
- if (Ball.y + BallYVel < 106 + Block.h)
- {
- int X = (Ball.x + BallXVel + 4) / 40;
- int Y = (Ball.y + BallYVel + 10) / 32;
- if (Blocks[X][Y] == 1)
- {
- Blocks[X][Y] = 0;
- if (Ball.x + BallXVel + Ball.w - 4 - X * 40 > Ball.y + BallYVel + Ball.h - 10 - Y * 32) BallYVel *= -1;
- else BallXVel *= -1;
- }
- }
- Ball.x += BallXVel;
- Ball.y += BallYVel;
- if (!Inbetween(0, Ball.x, SCREENWIDTH - Ball.w)) BallXVel *= -1;
- if (Ball.y < 0 || IsIntersecting(Ball, PlayerRect)) BallYVel *= -1;
- else if (Ball.y > SCREENHEIGHT)
- {
- Ball.x = SCREENWIDTH / 4;
- Ball.y = SCREENHEIGHT / 2;
- BallXVel = 2;
- BallYVel = 2;
- PlayerRect.x = SCREENWIDTH / 3;
- Lives -= 1;
- }
- if (PlayerRect.x < 0) PlayerRect.x = 0;
- else if (PlayerRect.x + PlayerRect.w > SCREENWIDTH) PlayerRect.x = SCREENWIDTH - PlayerRect.w;
- Ball.h = SCREENHEIGHT / 48;
- SDL_FillRect(Screen, &ScreenRect, 0x000000);
- SDL_FillRect(Screen, &PlayerRect, 0xFFFFFF);
- SDL_FillRect(Screen, &Ball, 0xFFFFFF);
- for (i = 0; i < 16; i++)
- {
- for (j = 0; j < 4; j++)
- {
- if (Blocks[i][j] == 1)
- {
- Block.x = i * 40 + 4;
- Block.y = j * 32 + 10;
- SDL_FillRect(Screen, &Block, 0xFFFFFF);
- }
- }
- }
- SDL_PumpEvents();
- Keyboard = SDL_GetKeyState(NULL);
- if (Keyboard[SDLK_RIGHT]) PlayerRect.x += 3;
- else if (Keyboard[SDLK_LEFT]) PlayerRect.x -= 3;
- else if (Keyboard[SDLK_ESCAPE]) BlocksLeft = 0;
- SDL_Flip(Screen);
- Time = SDL_GetTicks() - Time;
- SDL_Delay(1000 / 60 - Time);
- }
- return 0;
- }
- int Inbetween(int Min, int Value, int Max)
- {
- return (Value > Min && Value < Max);
- }
- int IsIntersecting(SDL_Rect Rect1, SDL_Rect Rect2)
- {
- return !(Rect1.x > Rect2.x + Rect2.w || Rect1.x + Rect1.w < Rect2.x
- || Rect1.y > Rect2.y + Rect2.h || Rect1.y + Rect1.h < Rect2.y);
- }
Advertisement
Add Comment
Please, Sign In to add comment