/* Crafter®.Graphics Copyright (C) 2025 Catcrafts® Catcrafts.net This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include import Crafter.Event; import Crafter.Graphics; using namespace Crafter; // Constants const uint32_t SCREEN_WIDTH = 800; const uint32_t SCREEN_HEIGHT = 600; const uint32_t PADDLE_WIDTH = 10; const uint32_t PADDLE_HEIGHT = 100; const float PADDLE_SPEED = 5.0f; const uint32_t BALL_SIZE = 10; const float BALL_SPEED = 5.0f; struct Paddle { float x, y; }; struct Ball { float x, y; float dx, dy; }; Paddle leftPaddle = { 50, SCREEN_HEIGHT / 2 - PADDLE_HEIGHT / 2 }; Paddle rightPaddle = { SCREEN_WIDTH - 50 - PADDLE_WIDTH, SCREEN_HEIGHT / 2 - PADDLE_HEIGHT / 2 }; Ball ball = { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, BALL_SPEED, BALL_SPEED }; uint32_t scoreLeft = 0; uint32_t scoreRight = 0; void resetBall() { ball.x = SCREEN_WIDTH / 2; ball.y = SCREEN_HEIGHT / 2; // Reverse direction on reset ball.dx = -ball.dx; ball.dy = (rand() % 2 == 0) ? BALL_SPEED : -BALL_SPEED; } void updateBall() { ball.x += ball.dx; ball.y += ball.dy; // Top and bottom collision if (ball.y <= 0 || ball.y + BALL_SIZE >= SCREEN_HEIGHT) { ball.dy = -ball.dy; } // Left paddle collision if (ball.x <= leftPaddle.x + PADDLE_WIDTH && ball.y + BALL_SIZE >= leftPaddle.y && ball.y <= leftPaddle.y + PADDLE_HEIGHT) { ball.dx = std::abs(ball.dx); // Make sure it moves right } // Right paddle collision if (ball.x + BALL_SIZE >= rightPaddle.x && ball.y + BALL_SIZE >= rightPaddle.y && ball.y <= rightPaddle.y + PADDLE_HEIGHT) { ball.dx = -std::abs(ball.dx); // Make sure it moves left } // Scoring if (ball.x < 0) { scoreRight++; resetBall(); } else if (ball.x > SCREEN_WIDTH) { scoreLeft++; resetBall(); } } void moveLeftPaddleUp() { if (leftPaddle.y > 0) leftPaddle.y -= PADDLE_SPEED; } void moveLeftPaddleDown() { if (leftPaddle.y + PADDLE_HEIGHT < SCREEN_HEIGHT) leftPaddle.y += PADDLE_SPEED; } void moveRightPaddleUp() { if (rightPaddle.y > 0) rightPaddle.y -= PADDLE_SPEED; } void moveRightPaddleDown() { if (rightPaddle.y + PADDLE_HEIGHT < SCREEN_HEIGHT) rightPaddle.y += PADDLE_SPEED; } int main() { WindowWaylandWayland window("HelloWindow", 1280, 720); window.StartSync(); }