Some checks failed
Android / build (arm64) (push) Has been cancelled
Android / build (x86_64) (push) Has been cancelled
Windows Examples / build (push) Has been cancelled
Windows / build (ARM64, ARM64, msvc16, winarm64) (push) Has been cancelled
Windows / build (i686, pe-i386, mingw-w64, win32) (push) Has been cancelled
Windows / build (x64, x64, msvc16, win64) (push) Has been cancelled
Windows / build (x86, Win32, msvc16, win32) (push) Has been cancelled
Windows / build (x86_64, pe-x86-64, mingw-w64, win64) (push) Has been cancelled
WebAssembly / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Linux Examples / build (push) Has been cancelled
Linux / build (i386, i386, /user/bin, 32) (push) Has been cancelled
Linux / build (x86_64, amd64, /user/bin, 64) (push) Has been cancelled
CMakeBuilds / Windows Build (push) Has been cancelled
CMakeBuilds / Linux Build (push) Has been cancelled
85 lines
3 KiB
C
85 lines
3 KiB
C
/*******************************************************************************************
|
|
*
|
|
* raylib [shapes] example - bouncing ball
|
|
*
|
|
* Example complexity rating: [★☆☆☆] 1/4
|
|
*
|
|
* Example originally created with raylib 2.5, last time updated with raylib 2.5
|
|
*
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
* BSD-like license that allows static linking with closed source software
|
|
*
|
|
* Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
|
|
*
|
|
********************************************************************************************/
|
|
|
|
#include "raylib.h"
|
|
|
|
//------------------------------------------------------------------------------------
|
|
// Program main entry point
|
|
//------------------------------------------------------------------------------------
|
|
int main(void)
|
|
{
|
|
// Initialization
|
|
//---------------------------------------------------------
|
|
const int screenWidth = 1920;
|
|
const int screenHeight = 1080;
|
|
|
|
SetTraceLogLevel(LOG_TRACE);
|
|
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
|
SetTargetFPS(120); // Set our game to run at 60 frames-per-second
|
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
|
|
|
|
Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
|
|
Vector2 ballSpeed = { 5.0f, 4.0f };
|
|
int ballRadius = 20;
|
|
|
|
bool pause = 0;
|
|
int framesCounter = 0;
|
|
|
|
//----------------------------------------------------------
|
|
|
|
// Main game loop
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
{
|
|
// Update
|
|
//-----------------------------------------------------
|
|
if (IsKeyPressed(KEY_SPACE)) pause = !pause;
|
|
|
|
if (!pause)
|
|
{
|
|
ballPosition.x += ballSpeed.x;
|
|
ballPosition.y += ballSpeed.y;
|
|
|
|
// Check walls collision for bouncing
|
|
if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
|
|
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
|
|
}
|
|
else framesCounter++;
|
|
//-----------------------------------------------------
|
|
|
|
// Draw
|
|
//-----------------------------------------------------
|
|
BeginDrawing();
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
DrawCircleV(ballPosition, (float)ballRadius, MAROON);
|
|
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
|
|
|
|
// On pause, we draw a blinking message
|
|
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
|
|
|
|
DrawFPS(10, 10);
|
|
|
|
EndDrawing();
|
|
//-----------------------------------------------------
|
|
}
|
|
|
|
// De-Initialization
|
|
//---------------------------------------------------------
|
|
CloseWindow(); // Close window and OpenGL context
|
|
//----------------------------------------------------------
|
|
|
|
return 0;
|
|
}
|