cpp
exercises
exercises.cpp⚙️cpp
/**
* Graphics - Exercises
*
* Install: sudo apt install libsfml-dev
* Compile: g++ -std=c++17 -Wall exercises.cpp -lsfml-graphics -lsfml-window -lsfml-system -o exercises
*/
#include <iostream>
#include <string>
using namespace std;
// ============================================================
// Exercise 1: Bouncing Ball ⭐⭐
// ============================================================
// TODO: Create a ball that bounces off window edges
// - Ball moves with velocity
// - Reverses direction on edge collision
void exercise1() {
cout << R"(
// Create window 800x600
// CircleShape ball with radius 20
// velocity = (200, 150) pixels/sec
// Each frame:
// - Move ball by velocity * dt
// - If hitting edge, reverse velocity component
// - Draw ball
)" << endl;
}
// ============================================================
// Exercise 2: Mouse Follower ⭐
// ============================================================
// TODO: Shape follows mouse cursor smoothly
// - Lerp between current position and mouse position
void exercise2() {
cout << R"(
// lerp: position += (target - position) * speed * dt
// Get mouse position each frame
// Move shape toward mouse
)" << endl;
}
// ============================================================
// Exercise 3: Keyboard-Controlled Player ⭐⭐
// ============================================================
// TODO: WASD movement with boundaries
// - Don't let player leave screen
// - Smooth movement with delta time
void exercise3() {
cout << R"(
// Player rectangle
// Check WASD keys
// Move with speed * dt
// Clamp position to screen bounds
)" << endl;
}
// ============================================================
// Exercise 4: Simple Shooter ⭐⭐⭐
// ============================================================
// TODO: Player shoots bullets on spacebar
// - Bullets move upward
// - Remove when off-screen
void exercise4() {
cout << R"(
// Player at bottom center
// Press Space -> create bullet at player position
// Bullets move up at 400 pixels/sec
// Remove bullets when y < 0
)" << endl;
}
// ============================================================
// Exercise 5: Sprite Animation ⭐⭐
// ============================================================
// TODO: Animate a sprite sheet
// - 8 frames, 32x32 each
// - Loop through frames
void exercise5() {
cout << R"(
// Load sprite sheet texture
// setTextureRect to show one frame
// Change frame every 0.1 seconds
// Loop back to frame 0 after frame 7
)" << endl;
}
// ============================================================
// Exercise 6: Particle System ⭐⭐⭐
// ============================================================
// TODO: Click to spawn particles
// - Particles have random velocity and lifetime
// - Fade out over time
void exercise6() {
cout << R"(
struct Particle {
sf::CircleShape shape;
sf::Vector2f velocity;
float lifetime;
float age = 0;
};
// On click: spawn 20 particles with random velocities
// Update: move particles, increase age
// Set alpha based on remaining lifetime
// Remove when age >= lifetime
)" << endl;
}
// ============================================================
// Exercise 7: Pong Game ⭐⭐⭐
// ============================================================
// TODO: Simple pong with two paddles
// - Ball bounces off walls and paddles
// - Score when ball passes paddle
void exercise7() {
cout << R"(
// Two paddles (left: W/S, right: Up/Down)
// Ball with velocity
// Bounce off top/bottom walls
// Bounce off paddles
// Score point when ball exits left/right
// Reset ball to center
)" << endl;
}
// ============================================================
// Exercise 8: Tilemap Renderer ⭐⭐⭐
// ============================================================
// TODO: Render a simple tilemap from 2D array
// - Different tile types (floor, wall, etc.)
void exercise8() {
cout << R"(
int map[10][10] = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
...
};
// 0 = floor (gray), 1 = wall (brown)
// Loop through array
// Draw rectangle for each tile at (x*tileSize, y*tileSize)
)" << endl;
}
// ============================================================
// MAIN
// ============================================================
int main() {
cout << "=== Graphics Exercises ===" << endl;
cout << "Note: Implement these using SFML library.\n" << endl;
cout << "Ex1: Bouncing Ball" << endl;
exercise1();
cout << "Ex2: Mouse Follower" << endl;
exercise2();
cout << "Ex3: Keyboard Player" << endl;
exercise3();
cout << "Ex4: Simple Shooter" << endl;
exercise4();
cout << "Ex5: Sprite Animation" << endl;
exercise5();
cout << "Ex6: Particle System" << endl;
exercise6();
cout << "Ex7: Pong Game" << endl;
exercise7();
cout << "Ex8: Tilemap Renderer" << endl;
exercise8();
return 0;
}
// ============================================================
// ANSWERS (Skeleton)
// ============================================================
/*
Ex1 - Bouncing Ball:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Bouncing Ball");
window.setFramerateLimit(60);
sf::CircleShape ball(20);
ball.setFillColor(sf::Color::Red);
ball.setPosition(400, 300);
sf::Vector2f velocity(200, 150);
sf::Clock clock;
while (window.isOpen()) {
float dt = clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
// Move
ball.move(velocity * dt);
sf::Vector2f pos = ball.getPosition();
// Bounce
if (pos.x <= 0 || pos.x >= 760) velocity.x *= -1;
if (pos.y <= 0 || pos.y >= 560) velocity.y *= -1;
window.clear();
window.draw(ball);
window.display();
}
return 0;
}
Ex3 - Keyboard Player:
// In game loop:
sf::Vector2f pos = player.getPosition();
float speed = 300.0f;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
player.move(0, -speed * dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
player.move(0, speed * dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
player.move(-speed * dt, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
player.move(speed * dt, 0);
// Clamp to bounds
pos = player.getPosition();
if (pos.x < 0) player.setPosition(0, pos.y);
if (pos.y < 0) player.setPosition(pos.x, 0);
if (pos.x > 750) player.setPosition(750, pos.y);
if (pos.y > 550) player.setPosition(pos.x, 550);
Ex7 - Pong (Structure):
struct Paddle {
sf::RectangleShape shape;
float speed = 400.0f;
sf::Keyboard::Key upKey, downKey;
void update(float dt) {
if (sf::Keyboard::isKeyPressed(upKey))
shape.move(0, -speed * dt);
if (sf::Keyboard::isKeyPressed(downKey))
shape.move(0, speed * dt);
}
};
struct Ball {
sf::CircleShape shape;
sf::Vector2f velocity;
void update(float dt) {
shape.move(velocity * dt);
// Bounce off top/bottom
if (shape.getPosition().y < 0 || shape.getPosition().y > 580)
velocity.y *= -1;
}
bool hitsPaddle(const Paddle& p) {
return shape.getGlobalBounds().intersects(
p.shape.getGlobalBounds());
}
};
*/