Skip to content

Commit

Permalink
Add shapes bouncing ball example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 1fb3f1c commit 1ab8006
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Examples using raylib text functionality, including sprite fonts loading/generat

Examples using raylib shapes drawing functionality.

| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 31 | [shapes_basic_shapes](shapes/shapes_basic_shapes.rb) | <img src="shapes/shapes_basic_shapes.png" alt="shapes_basic_shapes" width="80"> | ⭐️☆☆☆ | 1.0 | **4.0** | [Ray](https://github.com/raysan5) |
| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|--------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 31 | [shapes_basic_shapes](shapes/shapes_basic_shapes.rb) | <img src="shapes/shapes_basic_shapes.png" alt="shapes_basic_shapes" width="80"> | ⭐️☆☆☆ | 1.0 | **4.0** | [Ray](https://github.com/raysan5) |
| 32 | [shapes_bouncing_ball](shapes/shapes_bouncing_ball.rb) | <img src="shapes/shapes_bouncing_ball.png" alt="shapes_bouncing_ball" width="80"> | ⭐️☆☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
Binary file added examples/shapes/shapes_bouncing_ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions examples/shapes/shapes_bouncing_ball.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ******************************************************************************************
#
# raylib [shapes] example - bouncing ball
#
# Example originally created with raylib 2.5, last time updated with raylib 2.5
#
# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.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-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

# Initialization
# --------------------------------------------------------------------------------------
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 450

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - bouncing ball")

ball_position = Raylib::Vector2.create(Raylib.get_screen_width / 2.0, Raylib.get_screen_height / 2.0)
ball_speed = Raylib::Vector2.create(5.0, 4.0)
ball_radius = 20

pause = false
frames_counter = 0

Raylib.set_target_fps(60)
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close
# Update
# ----------------------------------------------------------------------------------
pause = !pause if Raylib.is_key_pressed(Raylib::KEY_SPACE)

if pause
frames_counter += 1
else
ball_position.x += ball_speed.x
ball_position.y += ball_speed.y

# Check walls collision for bouncing
ball_speed.x *= -1.0 if (ball_position.x >= (Raylib.get_screen_width - ball_radius)) || (ball_position.x <= ball_radius)
ball_speed.y *= -1.0 if (ball_position.y >= (Raylib.get_screen_height - ball_radius)) || (ball_position.y <= ball_radius)
end
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)
Raylib.draw_circle_v(ball_position, ball_radius.to_f, Raylib::MAROON)
Raylib.draw_text("PRESS SPACE to PAUSE BALL MOVEMENT", 10, Raylib.get_screen_height - 25, 20, Raylib::LIGHTGRAY)

# On pause, we draw a blinking message
Raylib.draw_text("PAUSED", 350, 200, 30, Raylib::GRAY) if pause && (frames_counter / 30) % 2 == 1

Raylib.draw_fps(10, 10)
Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.close_window
# --------------------------------------------------------------------------------------

0 comments on commit 1ab8006

Please sign in to comment.