1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shapes] example - bouncing ball |
4 | * |
5 | * This example has been created using raylib 1.0 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2013 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | int main(void) |
15 | { |
16 | // Initialization |
17 | //--------------------------------------------------------- |
18 | const int screenWidth = 800; |
19 | const int screenHeight = 450; |
20 | |
21 | InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball" ); |
22 | |
23 | Vector2 ballPosition = { GetScreenWidth()/2, GetScreenHeight()/2 }; |
24 | Vector2 ballSpeed = { 5.0f, 4.0f }; |
25 | int ballRadius = 20; |
26 | |
27 | bool pause = 0; |
28 | int framesCounter = 0; |
29 | |
30 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
31 | //---------------------------------------------------------- |
32 | |
33 | // Main game loop |
34 | while (!WindowShouldClose()) // Detect window close button or ESC key |
35 | { |
36 | // Update |
37 | //----------------------------------------------------- |
38 | if (IsKeyPressed(KEY_SPACE)) pause = !pause; |
39 | |
40 | if (!pause) |
41 | { |
42 | ballPosition.x += ballSpeed.x; |
43 | ballPosition.y += ballSpeed.y; |
44 | |
45 | // Check walls collision for bouncing |
46 | if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f; |
47 | if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f; |
48 | } |
49 | else framesCounter++; |
50 | //----------------------------------------------------- |
51 | |
52 | // Draw |
53 | //----------------------------------------------------- |
54 | BeginDrawing(); |
55 | |
56 | ClearBackground(RAYWHITE); |
57 | |
58 | DrawCircleV(ballPosition, ballRadius, MAROON); |
59 | DrawText("PRESS SPACE to PAUSE BALL MOVEMENT" , 10, GetScreenHeight() - 25, 20, LIGHTGRAY); |
60 | |
61 | // On pause, we draw a blinking message |
62 | if (pause && ((framesCounter/30)%2)) DrawText("PAUSED" , 350, 200, 30, GRAY); |
63 | |
64 | DrawFPS(10, 10); |
65 | |
66 | EndDrawing(); |
67 | //----------------------------------------------------- |
68 | } |
69 | |
70 | // De-Initialization |
71 | //--------------------------------------------------------- |
72 | CloseWindow(); // Close window and OpenGL context |
73 | //---------------------------------------------------------- |
74 | |
75 | return 0; |
76 | } |