mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-07-02 19:13:43 -04:00
Update/merge examples back into main repo (#192)
This commit is contained in:
96
Examples/Shapes/BouncingBall.cs
Normal file
96
Examples/Shapes/BouncingBall.cs
Normal file
@ -0,0 +1,96 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [shapes] example - bouncing ball
|
||||
*
|
||||
* This example has been created using raylib 1.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2013 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Shapes;
|
||||
|
||||
public class BouncingBall
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//---------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
|
||||
|
||||
Vector2 ballPosition = new(GetScreenWidth() / 2, GetScreenHeight() / 2);
|
||||
Vector2 ballSpeed = new(5.0f, 4.0f);
|
||||
int ballRadius = 20;
|
||||
|
||||
bool pause = false;
|
||||
int framesCounter = 0;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//----------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//-----------------------------------------------------
|
||||
if (IsKeyPressed(KeyboardKey.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 += 1;
|
||||
}
|
||||
//-----------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//-----------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RAYWHITE);
|
||||
|
||||
DrawCircleV(ballPosition, ballRadius, Color.MAROON);
|
||||
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, Color.LIGHTGRAY);
|
||||
|
||||
// On pause, we draw a blinking message
|
||||
if (pause && ((framesCounter / 30) % 2) == 0)
|
||||
{
|
||||
DrawText("PAUSED", 350, 200, 30, Color.GRAY);
|
||||
}
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//-----------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//---------------------------------------------------------
|
||||
CloseWindow();
|
||||
//----------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user