From b23957d2f0a972c3e2089da5ae5b8e8d5900c93a Mon Sep 17 00:00:00 2001 From: pranavganesh <104220606+pranavganesh@users.noreply.github.com> Date: Fri, 7 Jul 2023 21:36:11 +0530 Subject: [PATCH] Converting shapes_basic_shapes.c to python and adding it to examples (#104) * Converting shapes_basic_shapes.c to python and adding it to examples * Add shapes_bouncing_ball.py to examples in shapes --------- Co-authored-by: pranavganesh <50474789+Pranv11Ganesh@users.noreply.github.com> --- examples/shapes/shapes_basic_shapes.py | 71 +++++++++++++++++++++++++ examples/shapes/shapes_bouncing_ball.py | 52 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 examples/shapes/shapes_basic_shapes.py create mode 100644 examples/shapes/shapes_bouncing_ball.py diff --git a/examples/shapes/shapes_basic_shapes.py b/examples/shapes/shapes_basic_shapes.py new file mode 100644 index 0000000..864346b --- /dev/null +++ b/examples/shapes/shapes_basic_shapes.py @@ -0,0 +1,71 @@ +import pyray +from raylib.colors import ( + RAYWHITE, + DARKGRAY, + DARKBLUE, + SKYBLUE, + MAROON, + ORANGE, + RED, + VIOLET, + BEIGE, + BROWN, + BLACK, + GREEN, + GOLD +) + + +# Initialization +screenWidth = 800 +screenHeight = 450 +pyray.init_window(screenWidth, screenHeight, "raylib [shapes] example - basic shapes drawing") + +rotation = 0.0 + +pyray.set_target_fps(60) + +# Main game loop +while not pyray.window_should_close(): + # Update + rotation += 0.2 + + # Draw + pyray.begin_drawing() + + pyray.clear_background(RAYWHITE) + + pyray.draw_text("some basic shapes available on raylib", 20, 20, 20, DARKGRAY) + + # Circle shapes and lines + pyray.draw_circle(screenWidth // 5, 120, 35, DARKBLUE) + pyray.draw_circle_gradient(screenWidth // 5, 220, 60, GREEN, SKYBLUE) + pyray.draw_circle_lines(screenWidth // 5, 340, 80, DARKBLUE) + + # Rectangle shapes and lines + pyray.draw_rectangle(screenWidth // 4 * 2 - 60, 100, 120, 60, RED) + pyray.draw_rectangle_gradient_h(screenWidth // 4 * 2 - 90, 170, 180, 130, MAROON, GOLD) + pyray.draw_rectangle_lines(screenWidth // 4 * 2 - 40, 320, 80, 60, ORANGE) + + # Triangle shapes and lines + pyray.draw_triangle(pyray.Vector2(screenWidth / 4.0 * 3.0, 80.0), + pyray.Vector2(screenWidth / 4.0 * 3.0 - 60.0, 150.0), + pyray.Vector2(screenWidth / 4.0 * 3.0 + 60.0, 150.0), VIOLET) + + pyray.draw_triangle_lines(pyray.Vector2(screenWidth / 4.0 * 3.0, 160.0), + pyray.Vector2(screenWidth / 4.0 * 3.0 - 20.0, 230.0), + pyray.Vector2(screenWidth / 4.0 * 3.0 + 20.0, 230.0), DARKBLUE) + + # Polygon shapes and lines + pyray.draw_poly(pyray.Vector2(screenWidth / 4.0 * 3, 330), 6, 80, rotation, BROWN) + pyray.draw_poly_lines(pyray.Vector2(screenWidth / 4.0 * 3, 330), 6, 90, rotation, BROWN) + pyray.draw_poly_lines_ex(pyray.Vector2(screenWidth / 4.0 * 3, 330), 6, 85, rotation, 6, BEIGE) + + # NOTE: We draw all LINES based shapes together to optimize internal drawing, + # this way, all LINES are rendered in a single draw pass + pyray.draw_line(18, 42, screenWidth - 18, 42, BLACK) + + pyray.end_drawing() + +# De-Initialization +pyray.close_window() diff --git a/examples/shapes/shapes_bouncing_ball.py b/examples/shapes/shapes_bouncing_ball.py new file mode 100644 index 0000000..f0e60f2 --- /dev/null +++ b/examples/shapes/shapes_bouncing_ball.py @@ -0,0 +1,52 @@ +import pyray + +# Initialization +screenWidth = 800 +screenHeight = 450 +pyray.init_window(screenWidth, screenHeight, "pyray [shapes] example - bouncing ball") + +ballPosition = pyray.Vector2(pyray.get_screen_width() / 2.0, pyray.get_screen_height() / 2.0) +ballSpeed = pyray.Vector2(5.0, 4.0) +ballRadius = 20 + +pause = False +framesCounter = 0 + +pyray.set_target_fps(60) + +# Main game loop +while not pyray.window_should_close(): + # Update + if pyray.is_key_pressed(pyray.KEY_SPACE): + pause = not pause + + if not pause: + ballPosition.x += ballSpeed.x + ballPosition.y += ballSpeed.y + + # Check walls collision for bouncing + if (ballPosition.x >= (pyray.get_screen_width() - ballRadius)) or (ballPosition.x <= ballRadius): + ballSpeed.x *= -1.0 + if (ballPosition.y >= (pyray.get_screen_height() - ballRadius)) or (ballPosition.y <= ballRadius): + ballSpeed.y *= -1.0 + else: + framesCounter += 1 + + # Draw + pyray.begin_drawing() + + pyray.clear_background(pyray.RAYWHITE) + + pyray.draw_circle_v(ballPosition, ballRadius, pyray.MAROON) + pyray.draw_text("PRESS SPACE to PAUSE BALL MOVEMENT", 10, pyray.get_screen_height() - 25, 20, pyray.LIGHTGRAY) + + # On pause, we draw a blinking message + if pause and (framesCounter // 30) % 2: + pyray.draw_text("PAUSED", 350, 200, 30, pyray.GRAY) + + pyray.draw_fps(10, 10) + + pyray.end_drawing() + +# De-Initialization +pyray.close_window()