raylib/examples/shapes/shapes_bouncing_ball.c

81 lines
2.9 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [shapes] example - bouncing ball
*
2022-07-20 02:28:37 +03:00
* Example originally created with raylib 2.5, last time updated with raylib 2.5
*
2022-07-20 02:28:37 +03:00
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
2023-01-01 18:00:56 +03:00
* Copyright (c) 2013-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2022-06-21 20:53:18 +03:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
int main(void)
{
// Initialization
//---------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
2019-05-20 17:36:42 +03:00
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
2019-05-20 17:36:42 +03:00
2021-10-25 11:21:16 +03:00
Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
2019-05-20 17:36:42 +03:00
Vector2 ballSpeed = { 5.0f, 4.0f };
int ballRadius = 20;
bool pause = 0;
int framesCounter = 0;
2019-05-20 17:36:42 +03:00
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//----------------------------------------------------------
2019-05-20 17:36:42 +03:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//-----------------------------------------------------
if (IsKeyPressed(KEY_SPACE)) pause = !pause;
2019-05-20 17:36:42 +03:00
if (!pause)
{
ballPosition.x += ballSpeed.x;
ballPosition.y += ballSpeed.y;
2019-05-20 17:36:42 +03:00
// 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++;
//-----------------------------------------------------
2019-05-20 17:36:42 +03:00
// Draw
//-----------------------------------------------------
BeginDrawing();
2019-05-20 17:36:42 +03:00
ClearBackground(RAYWHITE);
2019-05-20 17:36:42 +03:00
2021-10-25 11:21:16 +03:00
DrawCircleV(ballPosition, (float)ballRadius, MAROON);
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
2019-05-20 17:36:42 +03:00
// On pause, we draw a blinking message
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
DrawFPS(10, 10);
2019-05-20 17:36:42 +03:00
EndDrawing();
//-----------------------------------------------------
}
// De-Initialization
//---------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//----------------------------------------------------------
2019-05-20 17:36:42 +03:00
return 0;
}