raylib/ports/shapes/shapes_bouncing_ball
Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license.
Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.
3. This notice may not be removed or altered from any source distribution.
Code
shapes_bouncing_ball.odin ¶86 linesSource
1package raylib_examples
2
3/*******************************************************************************************
4*
5* raylib [shapes] example - bouncing ball
6*
7* This example was originally created in C with raylib 2.5 before being translated to Odin.
8*
9* The original example in C can be found on raylib.com at:
10*
11* https://www.raylib.com/examples/shapes/loader.html?name=shapes_bouncing_ball
12*
13* raylib is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
14* BSD-like license that allows static linking with closed source software. The license
15* can be found in its entirety as part of the standard Odin distribution at:
16*
17* /vendor/raylib/LICENSE
18*
19* or online at:
20*
21* https://www.raylib.com/license.html
22*
23* This example is licensed under an unmodified zlib/libpng license, which is an
24* OSI-certified, BSD-like license that allows static linking with closed source software.
25*
26* Copyright (c) 2013-2023 Ramon Santamaria (@raysan5)
27* Copyright (c) 2023 Benjamin G. Thompson (@bg-thompson)
28*
29********************************************************************************************/
30
31import rl "vendor:raylib"
32
33SCREEN_WIDTH :: 800
34SCREEN_HEIGHT :: 450
35
36main :: proc() {
37 rl.SetConfigFlags(rl.ConfigFlags{.MSAA_4X_HINT}) // Try to enable MSAA 4X.
38 rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - bouncing ball")
39 defer rl.CloseWindow() // Close window and OpenGL context when leaving main.
40
41 // Ball position, velocity, radius, and color.
42 ball_pos := rl.Vector2{f32(rl.GetScreenWidth() / 2), f32(rl.GetScreenHeight() / 2)}
43 ball_vel := rl.Vector2{5,4}
44 ball_rad :: 20
45 ball_color :: rl.MAROON
46
47 pause := true
48 framesCounter := 0
49 rl.SetTargetFPS(60) // Set frames-per-second.
50
51 // The primary loop.
52 for !rl.WindowShouldClose() { // Detect window close button or ESC key press.
53 if rl.IsKeyPressed(rl.KeyboardKey.SPACE) {
54 pause = ! pause
55 }
56 if ! pause {
57 ball_pos.x += ball_vel.x
58 ball_pos.y += ball_vel.y
59
60 // Check walls collision for bouncing.
61 if ball_pos.x >= f32(rl.GetScreenWidth() - ball_rad) || ball_pos.x <= ball_rad {
62 ball_vel.x *= -1
63 }
64 if ball_pos.y >= f32(rl.GetScreenHeight() - ball_rad) || ball_pos.y <= ball_rad {
65 ball_vel.y *= -1
66 }
67 } else {
68 framesCounter += 1
69 }
70
71 // Start drawing.
72 rl.BeginDrawing()
73
74 rl.ClearBackground(rl.RAYWHITE)
75 rl.DrawCircleV(ball_pos, ball_rad, ball_color)
76 rl.DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, rl.GetScreenHeight() - 25, 20, rl.LIGHTGRAY)
77 rl.DrawFPS(10, 10)
78
79 // On pause, we draw a blinking message
80 if pause && (framesCounter / 30) % 2 != 0 {
81 rl.DrawText("PAUSED", 350, 200, 30, rl.GRAY)
82 }
83
84 rl.EndDrawing()
85 }
86}
Declarations Used 21
- vendor:raylib BeginDrawing · ClearBackground · CloseWindow · ConfigFlags · DrawCircleV · DrawFPS · DrawText · EndDrawing · GetScreenHeight · GetScreenWidth · GRAY · InitWindow · IsKeyPressed · KeyboardKey · LIGHTGRAY · MAROON · RAYWHITE · SetConfigFlags · SetTargetFPS · Vector2 · WindowShouldClose