Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Prerequisites:
- // install SDL2. On MacOS: brew install sdl2
- // g++ breakout.cpp -o breakout -lSDL2 -std=c++17
- #include <iostream>
- #include <vector>
- #include <SDL2/SDL.h>
- // Screen dimensions
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- // Structure for a brick
- struct Brick {
- SDL_Rect rect;
- bool destroyed;
- };
- int main(int argc, char* argv[]) {
- // Initialize SDL video subsystem
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- std::cerr << "SDL could not initialize! SDL_Error: "
- << SDL_GetError() << std::endl;
- return 1;
- }
- // Create the window
- SDL_Window* window = SDL_CreateWindow("Breakout",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- SDL_WINDOW_SHOWN);
- if (!window) {
- std::cerr << "Window could not be created! SDL_Error: "
- << SDL_GetError() << std::endl;
- SDL_Quit();
- return 1;
- }
- // Create renderer
- SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if (!renderer) {
- std::cerr << "Renderer could not be created! SDL_Error: "
- << SDL_GetError() << std::endl;
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 1;
- }
- // Define the paddle
- SDL_Rect paddle;
- paddle.w = 80;
- paddle.h = 20;
- paddle.x = (SCREEN_WIDTH - paddle.w) / 2;
- paddle.y = SCREEN_HEIGHT - paddle.h - 10;
- // Define the ball
- SDL_Rect ball;
- ball.w = 10;
- ball.h = 10;
- ball.x = SCREEN_WIDTH / 2 - ball.w / 2;
- ball.y = SCREEN_HEIGHT / 2 - ball.h / 2;
- float ballVelX = 4.0f;
- float ballVelY = -4.0f;
- // Create bricks: 5 rows x 10 columns
- std::vector<Brick> bricks;
- const int brickRows = 5;
- const int brickCols = 10;
- int brickWidth = SCREEN_WIDTH / brickCols;
- int brickHeight = 20;
- for (int row = 0; row < brickRows; row++) {
- for (int col = 0; col < brickCols; col++) {
- Brick brick;
- brick.rect.x = col * brickWidth + 1; // +1 for a small gap
- brick.rect.y = row * brickHeight + 50; // offset from top
- brick.rect.w = brickWidth - 2;
- brick.rect.h = brickHeight - 2;
- brick.destroyed = false;
- bricks.push_back(brick);
- }
- }
- bool quit = false;
- SDL_Event e;
- // Main game loop
- while (!quit) {
- // Handle events
- while (SDL_PollEvent(&e) != 0) {
- if (e.type == SDL_QUIT)
- quit = true;
- }
- // Handle paddle movement using keyboard state
- const Uint8* keystate = SDL_GetKeyboardState(NULL);
- if (keystate[SDL_SCANCODE_LEFT]) {
- paddle.x -= 6;
- if (paddle.x < 0)
- paddle.x = 0;
- }
- if (keystate[SDL_SCANCODE_RIGHT]) {
- paddle.x += 6;
- if (paddle.x + paddle.w > SCREEN_WIDTH)
- paddle.x = SCREEN_WIDTH - paddle.w;
- }
- // Update ball position
- ball.x += static_cast<int>(ballVelX);
- ball.y += static_cast<int>(ballVelY);
- // Bounce off left and right walls
- if (ball.x <= 0 || ball.x + ball.w >= SCREEN_WIDTH) {
- ballVelX = -ballVelX;
- }
- // Bounce off the top wall
- if (ball.y <= 0) {
- ballVelY = -ballVelY;
- }
- // If the ball goes below the paddle, reset it (loss condition)
- if (ball.y + ball.h >= SCREEN_HEIGHT) {
- ball.x = SCREEN_WIDTH / 2 - ball.w / 2;
- ball.y = SCREEN_HEIGHT / 2 - ball.h / 2;
- ballVelY = -4.0f;
- }
- // Check collision with paddle
- if (SDL_HasIntersection(&ball, &paddle)) {
- ballVelY = -ballVelY;
- // Change horizontal velocity based on where the ball hit the paddle
- int paddleCenter = paddle.x + paddle.w / 2;
- int ballCenter = ball.x + ball.w / 2;
- ballVelX = (ballCenter - paddleCenter) / 10.0f;
- }
- // Check collision with bricks
- for (auto &brick : bricks) {
- if (!brick.destroyed && SDL_HasIntersection(&ball, &brick.rect)) {
- brick.destroyed = true;
- ballVelY = -ballVelY;
- break;
- }
- }
- // Rendering section
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Black background
- SDL_RenderClear(renderer);
- // Draw paddle (white)
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDL_RenderFillRect(renderer, &paddle);
- // Draw ball (white)
- SDL_RenderFillRect(renderer, &ball);
- // Draw bricks (red)
- for (const auto &brick : bricks) {
- if (!brick.destroyed) {
- SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
- SDL_RenderFillRect(renderer, &brick.rect);
- }
- }
- SDL_RenderPresent(renderer);
- SDL_Delay(16); // Roughly 60 frames per second
- }
- // Clean up resources
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement