Certainly! Here's an example of how you can write a simple game of Pong in C++ using the SDL library:
#include <iostream>
#include <SDL.h>
// Window dimensions
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 400;
// Paddle dimensions
const int PADDLE_WIDTH = 10;
const int PADDLE_HEIGHT = 60;
const int PADDLE_SPEED = 5;
// Ball dimensions
const int BALL_SIZE = 10;
const int BALL_SPEED_X = 3;
const int BALL_SPEED_Y = 3;
int main(int argc, char** argv) {
// Initialize SDL
SDL_Init(SDL_INIT_VIDEO);
// Create a window
SDL_Window* window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
// Create a renderer for the window
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Set initial paddle positions
int playerY = WINDOW_HEIGHT / 2 - PADDLE_HEIGHT / 2;
int aiY = WINDOW_HEIGHT / 2 - PADDLE_HEIGHT / 2;
// Set initial ball position and velocity
int ballX = WINDOW_WIDTH / 2 - BALL_SIZE / 2;
int ballY = WINDOW_HEIGHT / 2 - BALL_SIZE / 2;
int ballVelocityX = BALL_SPEED_X;
int ballVelocityY = BALL_SPEED_Y;
// Game loop
bool isRunning = true;
while (isRunning) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
isRunning = false;
}
}
// Update player paddle position
const Uint8* keystates = SDL_GetKeyboardState(NULL);
if (keystates[SDL_SCANCODE_UP] && playerY > 0) {
playerY -= PADDLE_SPEED;
}
if (keystates[SDL_SCANCODE_DOWN] && playerY < WINDOW_HEIGHT - PADDLE_HEIGHT) {
playerY += PADDLE_SPEED;
}
// Update AI paddle position (simple AI)
if (aiY + PADDLE_HEIGHT / 2 < ballY + BALL_SIZE / 2) {
aiY += PADDLE_SPEED;
}
if (aiY + PADDLE_HEIGHT / 2 > ballY + BALL_SIZE / 2) {
aiY -= PADDLE_SPEED;
}
// Update ball position
ballX += ballVelocityX;
ballY += ballVelocityY;
// Reflect ball off paddles
if (ballX <= PADDLE_WIDTH && ballY + BALL_SIZE >= playerY && ballY <= playerY + PADDLE_HEIGHT) {
ballVelocityX = BALL_SPEED_X;
}
if (ballX >= WINDOW_WIDTH - PADDLE_WIDTH - BALL_SIZE && ballY + BALL_SIZE >= aiY && ballY <= aiY + PADDLE_HEIGHT) {
ballVelocityX = -BALL_SPEED_X;
}
// Reflect ball off top and bottom walls
if (ballY <= 0 || ballY >= WINDOW_HEIGHT - BALL_SIZE) {
ballVelocityY = -ballVelocityY;
}
// Clear the renderer
SDL_RenderClear(renderer);
// Draw paddles
SDL_Rect playerPaddle = { PADDLE_WIDTH, playerY, PADDLE_WIDTH, PADDLE_HEIGHT };
SDL_Rect aiPaddle = { WINDOW_WIDTH - PADDLE_WIDTH * 2, aiY, PADDLE_WIDTH, PADDLE_HEIGHT };
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &playerPaddle);
SDL_RenderFillRect(renderer, &aiPaddle);
// Draw ball
SDL_Rect ball = { ballX, ballY, BALL_SIZE, BALL_SIZE };
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &ball);
// Update the screen
SDL_RenderPresent(renderer);
}
// Clean up
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
To compile and run this code, you need to have the SDL library installed. Make sure to link the necessary SDL libraries while compiling.
This code sets up the initial dimensions, speeds, and positions of the game elements including the paddles and the ball. The game loop continuously updates the positions of the game elements based on user input and game logic. The SDL library is used to create the window, renderer, and to draw game elements on the screen.
Feel free to modify the code and add additional features or customizations to the game!